We are almost ready to save our data away secretly, but before we dash into the code we need to establish a clear procedure we expect to follow. In Secrets, it goes like this
On clicking the save button, open a password window
When the password is entered open a second password window to check the first entry
Now give the user a choice: Automatic save, or a 'manual' save
For the automatic save, nothing further is required on screen
For a 'manual' save a traditional save window is required
Finally the encrypted file is saved.
For this process we shall need three new windows, several extra elements in DEFPROCclick to respond to these steps, and the save process itself, so there is a sizeable amount of coding to be done.
In the full program, a mouse click on the save data button opens the first password window. The code to create this window is as follows:
REM save data window
DIM code% 8,sctext% 30
$code%=STRING$(8,CHR$0):$sctext%="Enter code up to 8 characters"
savecode%=FNcreate_window(550,340,468,144,0,0,&87000012,1,12,&19,"Encode",3,0)
a%=FNcreate_icon(savecode%,0,-160,468,208,&1700013D,"",nul%,R5%,2)
a%=FNcreate_icon(savecode%,16,-72,436,48,&7000119,"",sctext%,0,30)
a%=FNcreate_icon(savecode%,144,-136,192,48,&700F13D,"",code%,hideR2%,9)
This code is very similar to the code already written to create the main window. The first line after the REM DIMensions two new locations, the first to hold the password of up to 8 letters, and the second to hold the on-screen instructions to the user, to enter code up to 8 characters.
The next line introduces a new Basic keyword STRING$. This enables us to create a string of specific length composed of all the same characters. In this case, STRING$(8,CHR$0) creates a string 8 characters long composed of the null element - CHR$0. This is a way of ensuring that the writable icon will be blank when it first appears on screen.
The last four lines create the window, identified as savecode%, and three icons. Note the use of hideR2% in the last definition. This is the writable icon, and this ensures that no one can read the code as it is entered. hideR2% is defined at the start of PROCinit as described in Module 8.

The first password window
One of the great pluses of Basic is that, because it is not a compiled language, you can run your code straightaway without any delay to recompile, and thus ensure that it is still doing what you want before you move on. In this case, we need some more code, but it makes sense to write this so that we can bring this new window up on screen and confirm that all is well.
The window in question is opened when we click on the 'Save data' button in the main window. This button is icon number 15, and so we need to add
WHEN 15: REM Save data button
!block%=savecode%
SYS"Wimp_GetWindowState",,block%
block%!28=-1
SYS"Wimp_OpenWindow",,block%
PROCcaret(savecode%,2)
immediately after the line 'WHEN 13:PROCadd' in the main% loop of PROCclick. The four lines beginning '!block%= ... ' represent the standard method of opening a window, and can be used in most cases. In some programs I make this a procedure in itself, so that an alternative way of writing this would be
WHEN 15:REM Save data button
PROCopen(savecode%)
and the corresponding procedure would then be
DEFPROCopen(w%)
!block%=w%
SYS"Wimp_GetWindowState",,block%
block%!28=-1
SYS"Wimp_OpenWindow",,block%
ENDPROC
The final line of the original code calls PROCcaret which tells the Wimp to put the caret in icon 2, so the writable icon will already have the caret in place for you to type in your password without further ado. PROCcaret is contained within the Library.
Having typed in this code, you might like to test the program to make sure this all happens as predicted. Assuming all is well, compare the window flags with what actually appears, and try to identify the three icons, and why they appear as they do. Notice that icon 1 containing the instruction has no border and appears simply as text on the background of icon 0, which is itself just a grey slab giving a pleasant 3-dimensional appearance to the window. Building one icon on top of another is an easy way of creating more pleasing windows.
We now need to introduce another fundamental procedure that is used in virtually all programs -- a procedure to respond to a key press which we will call PROCkey. In the final version of Secrets, the only way to register that you have entered your code is to press Return. You may prefer to have another button, but most RISC users press Return automatically to enter data, and it is easier than returning to the mouse to click over a button.
So far our wimp poll routine does not recognise key presses, so we must add a line to change this. In the wimp poll loop at the beginning of the program, add the following line after the line that reads 'WHEN 6:PROCclick(block%!12)
WHEN 8: PROCkey(block%!24)
We now need to add the procedure just before DEFPROCmenuselect as follows.
DEFPROCkey(k%)
CASE k% OF
WHEN 13:
CASE !block% OF
WHEN savecode%
code$=FNstring(code%)
$code%=STRING$(8,CHR$0)
!block%=savecode%
SYS"Wimp_CloseWindow",,block%
!block%=savecheck%
SYS"Wimp_GetWindowState",,block%
block%!28=-1
SYS"Wimp_OpenWindow",,block%
PROCcaret(savecheck%,2)
ENDCASE
OTHERWISE
SYS"Wimp_ProcessKey",k%
ENDCASE
ENDPROC
The first point to make is that block%!24 identifies the key that has been pressed. For ordinary keys it will return the ASCII value, but for special keys such as F1 to F9 there are special values allocated so that they too can be recognised.
13 is the ASCII code for Return, so the one case so far corresponds to this key, but the second CASE statement makes sure that we only respond if the window is the correct one. The window handle is stored at block%, and needs to be savecode% for the password window.
The subsequent code uses FNstring to extract the password from the icon and replace it with a null string ready for next time. Just two lines are needed to close the window, and then the standard lines for opening the new window that checks the first entry.
Note the use of the keyword OTHERWISE in this procedure. In a CASE statement, if you want to take action when all other listed options have failed, then you can use the keyword OTHERWISE, so the structure is
CASE <variable> OF
WHEN ...
WHEN ...
...
OTHERWISE
...
ENDCASE
In PROCkey the OTHERWISE statement followed by the SYS call SYS"Wimp_ProcessKey",k% is essential. This ensures that the key press is made accessible to all other applications running at that time.
We cannot check this last piece of coding until we have defined the second window. The code for this is as follows, and should be inserted immediately after the code for the save data window entered at the start of this module.
REM check code window
DIM checktext% 30,auto% 9,manual% 15
$checktext%="Enter code again as a check"
$auto%="auto save":$manual%="enter filename"
savecheck%=FNcreate_window(550,340,468,216,0,0,&87000012,1,12,&19,"Check Code!",3,0)
a%=FNcreate_icon(savecheck%,0,-160,468,208,&1700013D,"",nul%,R5%,2)
a%=FNcreate_icon(savecheck%,16,-72,436,48,&7000119,"",checktext%,0,30)
a%=FNcreate_icon(savecheck%,144,-136,192,48,&700F13D,"",code%,hideR2%,9)
a%=FNcreate_icon(savecheck%,0,-216,234,56,&1700313D,"",auto%,R5%,10)
a%=FNcreate_icon(savecheck%,234,-216,234,56,&1700313D,"",manual%,R5%,16)
This code should cause no difficulty, and you can now run the program again to make sure all works so far. You should be able to get to this second window.

Check the password before you continue
I always like to end each module with some kind of exercise, but this time we are in the middle of the save process which we hope to complete in Module 11. You might however like to explore the full program and see if you can create the code yourself to produce a standard looking save window opened by clicking on the last button of the check code window?