In this module we shall fit in the last piece that makes our application into a fully functional program. It will record, save and restore up to 50 items of secret information. There will be further bells and whistles to add, but it will achieve its purpose for those who wish to start to actually use it. In Module 11 we saved our data automatically within the application directory. All we need do now is work out the routines for restoring that data.
To load existing data, we need to activate the load data button in the main window, and create a new window in which we can enter the password and select auto load or specify a file name. Again, autoload is easier, and we shall concentrate of that first. A procedure will be required to load the data, very similar to PROCautosave.

The decode window looks familiar
The style of this window will be identical to the check code window, and you may wonder whether we can use the same window for both tasks. It would be possible, but the wording in the icons would have to be changed, and an essential feature of the Wimp is responding to clicks over a specific window that lead to specific forms of action. So, although it is similar, we will take the easier route and define a new window as follows.
REM load data window
DIM autoin% 9
$autoin%="auto load"
loadcode%=FNcreate_window(550,340,468,216,0,0,&87000012,1,12,&19,"Decode",3,0)
a%=FNcreate_icon(loadcode%,0,-160,468,208,&1700013D,"",nul%,R5%,2)
a%=FNcreate_icon(loadcode%,16,-72,436,48,&7000119,"",sctext%,0,30)
a%=FNcreate_icon(loadcode%,144,-136,192,48,&700F13D,"",code%,hideR2%,9)
a%=FNcreate_icon(loadcode%,0,-216,234,56,&1700313D,"",autoin%,R5%,10)
a%=FNcreate_icon(loadcode%,234,-216,234,56,&1700313D,"",manual%,R5%,16)
This code should be added at the end of PROCinit after the check code window definitions. Notice how similar these windows are, and how we actually use the same string in both of them for the last icon using the text already defined and found at manual%. We have also used the string "Enter code up to 8 characters" that was set up in the save data window definition and stored at sctext%. It is helpful to note that we can use the same variables in different icons and in different windows.
Now to make sure this window appears at the proper time we must add another option to the main% loop in PROCclick. The load window should be opened by a click on icon number 12, so after the line 'WHEN 9:PROCnext' we need to add
WHEN 12:REM Load
!block%=loadcode%
SYS"Wimp_GetWindowState",,block%
block%!28=-1
SYS"Wimp_OpenWindow",,block%
PROCcaret(loadcode%,2)
This section simply opens our new window, and puts the caret ready in the icon for you to enter the password. When you have done so, you will click on 'Auto load' to proceed. The next piece of coding must therefore respond to this further mouse click.
Still in PROCclick, but now at the end of the procedure after the loop beginning 'WHEN savecheck% ...' we add 'WHEN loadcode% ...' as follows.
WHEN loadcode%
CASE block%!16 OF
WHEN 3:
code$=FNstring(code%)
$code%=STRING$(8,CHR$0)
!block%=loadcode%
SYS"Wimp_CloseWindow",,block%
PROCautoload
ENDCASE
and this must be complemented by a new procedure PROCautoload. I suggest that this procedure is inserted between PROCautosave and PROCencode because they are two sides of the same process, but you will appreciate that it could be placed anywhere in the program. In order to keep coding tidily arranged, it should be kept within the 'Encryption, Saving and Loading routines' section that we have already established.
DEFPROCautoload
PROCencode
path$="<Secrets$Dir>.Data0"
$path%=path$
PROCload
FORX%=1TO50
secret$(0,X%)=""
desc$(0,X%)=""
NEXT
ENDPROC
PROCencode is an interesting one. It is here to set a couple of variables rather than to encode, and acts as a bit of a decoy! Having set up the path name of the data file, the procedure goes on to call PROCload, which is always relatively easy to write because it must mirror the way in which the data was saved, so PROCload will have a close similarity with PROCsave. Add PROCload after PROCsave:
DEFPROCload
I=OPENIN(path$)
FORX%=1TO50
INPUT#I,desc$(0,X%),secret$(0,X%)
secret$(1,X%)=FNencrypt(secret$(0,X%))
desc$(1,X%)=FNencrypt(desc$(0,X%))
NEXT
CLOSE#I
!block%=main%
SYS"Wimp_CloseWindow",,block%
n%=1
$text1%=desc$(1,n%)
$text2%=secret$(1,n%)
!block%=main%
SYS"Wimp_GetWindowState",,block%
block%!28=-1
SYS"Wimp_OpenWindow",,block%
ENDPROC
In terms of Basic, you will see the correspondence between O=OPENOUT in PROCsave, and I=OPENIN in the load routine. You will also notice the read function from a file is INPUT# as opposed to PRINT#. Note that the saved data is loaded into secret$(0, ...) etc, and is, of course, still encrypted, so the next two lines pass the loaded data through the function FNencrypt to use the reversing property of the EOR function, and puts the results where they belong in secret$(1, ...).
PROCload then has one more task to do compared to PROCsave, which is to update the main window to show the new data. To do this we close the window first, set the count n% back to the first record, updated the strings held at text1% and text2%, and then reopened the window. There are other ways of updating windows, but this method involves very little additional explanation and works well enough. Where it falls short is where frequent changes take place and the flashing off and on of the window becomes irritating to the eye, but the way to avoid that is another story.
Our application is now up and running, so what remains to make it complete?
There are two buttons we have not yet activated in the main window. We suggested that you try to write the code for this in Module 9. If you have not done so yet, we will complete this in the next Module.
We have not provided the code to save a file other than automatically. This requires more knowledge of the Wimp than it does of Basic, but we will give an outline of this with the necessary code in Module 13.
A main window should have a menu as part of what is expected in a desktop program. The freeware version only has a couple of items in the menu, relying on the more popular button approach. All the buttons could be replicated in a menu, and we will take a look at this in Module 14.
Another useful function that is not included in the freeware version is a search facility. The argument is that it is not particularly arduous to flip through 50 records to fins the one you need, but if you prefer a search method, this could be added. Could is the operative word. This is more a project for those who want to enhance the program for themselves.