Logowbtitlelogo

The Wimp Development System for Risc OS



Tutorial 3 - The Archive Screensaver/Screenlock



Part 2 - Adding the code



Download the resource
files and compiled
application (12k).

download
Introduction


In last month's article, we designed the windows that will be needed for the SaverSetup application. This time we will complete the program, with all the necessary code and variables being set up.

The program is, in reality, a very simple database. The information reads and writes to a specified datafile, and doesn't need to use wimp drag and drop for load/save. The data saving is, in effect, totally transparent to the user.

This program, in terms of wimp programming is also very simple, it doesn't deal with events or anything else complicated, (although Wimpbasic (WB) makes handling of these complex areas fairly straightforward) unlike the screensaver itself, which will make use of the wimp's more interesting features. The only difficult bit comes with the setting up of the password, but more of that later.

As you will have guessed from the windows created last month the program is a full wimp program, however, it does not have an iconbar icon, an info window, application sprite or even a menu. The program does have two windows that are related, and opened with the use of a button. The program is exited by clicking on either the cancel or save buttons. This forces the user to make a choice and is fairly important if password protection is initiated.

On with the show.

To kick off go to the variables editor and create a group global. Within this group create the following variables, lock%,pass$,static%. These should all be in lower case. Although WB is case insensitive, it makes debugging easier.

Variables window
Figure 2 the variables window


Note the main and newpass groups are your actual windows, created in the first part.

Close the variable editor and move to the code editor, and create a new procedure defprocdefault(file$). Add the following lines ;

global:lock%=0
global:static%=0
global:pass$=chr$(88)+chr$(88)+chr$(88)
procwrite(file$)
endproc
Remember you can enter variables into the program by using the option enter ->variable ->variables from the menu. It makes input less prone to typing errors.

The first line sets a flag indicating the password is unset, the second whether the graphic is moving or not. The third line sets a default password of XXX. You can choose your own. You may wonder why the password is stored as ASCII , well it has been done to stop the default password being written to the messages file on compilation. All you have to do is remember the default.

The final line simply writes the default data to file. This routine will only ever be called if the setup program has never been run before. The other times the program will look for its settings file. You could simply have the defaults set up, and not saved but I prefer to have the default settings written as early as possible.

Save the procedure and close the code editor.

In WBs main window click on the sunken icon next to startup procedure.

The main window

Figure 1

This should create a blank procedure (procstartupprocedure) ready for you to populate. Enter the following lines

local x%,file$
open main
hide main
file$="<choices$dir>.scrsavers.archive"
x%=openin(file$)
if x%=0 then
close#x%
procdefault(file$)
else
close#x%
procreadfile(file$)
endif

Lines 2-3 open and hide the main window so that the internal variables used by WB to store icon data are created, so that when you read in the variables in from the file or the defaults the relevant buttons can be set to the correct values.

Line 4 sets up the path to where the program expects to find the settings file. The following line uses Basic's openin command to open the file. If the file exists a 'channel' is assigned to the variable x%, if the file is non existent then the value returned will be 0. The rest of the code handles either situation, and will either call procdefault or open the existing file. Note that the channel x% is closed both times. You should not leave channels open for any longer than necessary. Remember in a single tasking situation it does not matter, but you cannot guarantee that the wimp will not need the assigned channel. There are only a finite number of channels, which can be used up very quickly in the window enviroment. So open and close files as quickly as possible.

Create a new procedure readfile(file$)

local x%
x%=openin(file$)
input#x%,global:lock%
input#x%global:pass$
input#x%global:static%                  
close#x%
endproc

This routine needs little explanation. The file is opened and the values read in assigned to the variables already created.

Create a new file defprocwritefile(file$)

local x%
x%=openout(file$)
print#x%,global:lock%
print#x%,global:pass$
print#x%,global:static%
close#x%
endproc
You can probably guess what this routine does. The use of print# is probably the simplest way of writing data to a file. It also has the advantage of making the data harder to read for someone not in the know due to the way the data is stored. After all it will possibly hold a password later on.

We now need to set up our main window when it is opened. To do this we need to go back to defprocstartup. After the endif add the following lines

if global:lock%=1 then
setselect main:lock$,true
setfade main:lock$,true
setfade main:change$,false
else
setfade main:lock$,false
setfade main:change$,true
endif
if global:static%=0 then
setselect main:r1$,true
else
setselect main:r11$,true
endif

The first part of the code checks to see whether the password is set and, if so, sets the password check icon, and then greys it out. The change button is then ungreyed, or vice versa. The check icon is set and greyed to show a visual reminder that the password is set. The variable is also used as an internal flag by the program.

The second statement then checks which of the graphics options has been set and then sets either of the radio buttons. Note that we explicitly set only one button, because both are in the same ESG group the wimp will ensure that the other button is always unset. The window is set up ready for opening. So add the following

opencentred main

The above command needs little explanation. It simply opens the window called main in a central position on screen. No complex calculations needed. Always remember WB windows only open at the end of a procedure, so opening a window at the start of a procedure is not of much use unless you need the variables set up, as we do in this case. If you want the window open and on screen at the start of a routine, put the open window into a separate procedure.

Create a new procedure defprocmovement1 with the following

global:static%=0
endproc

Create a second procedure defprocmovement2

global:static%=1
endproc

These two routines will be connected to the radio buttons at a later date, and merely set a variable, they do not alter the screen display in any way.

Create another procedure depfprocpprotect with the following lines

local x%
x%=isselected(main:lock$)
if x% then
setfade main:change$,false
else
setfade main:change$,true
endif
endproc

This checks the status of the of the password protect button at the top of the window when the user clicks on it and resets it accordingly. Setfade can used to either fade or fade out an icon. When set to false it is unfaded.

Create a further procedure called defprocopenchange

hide main
opencentred newpass
newpass:old$=""
newpass:input1$=""
newpass:input2$=""
endproc

This routine opens the change password window andclears all of the variables ready for the users input. The main window is hidden, so that the user cannot start to make changes and then abort the change password in an unwanted fashion. Note that the window is opened at the start so that we can clear all of the variables. This is not strictly necessary, but wise to ensure the variables are clear. The window however does not open until endproc is reached

We need to create a further procedure defproccancelchange

close newpass
opencentred main
endproc

The next piece of code is quite complex, as it deals with the user input, and makes comparisons call the routine defprocmakechange

local w%
w%=0
if newpass:old$<>global:pass$ then
w%=ask(true,false,"Invalid old password. Re-enter")
newpass$:old$=""
careticon newpass:old$,-1,-1
endproc
endif

This is the first of the checks. Has the user entered the correct old password? A comparison is made. If it is incorrect an error is displayed, via the ask function. This displays a standard error window. The first two parameters determine whether the window has an Ok and cancel button. In our case we only want an Ok button, so the first parameter is set to true. The third parameter is the message to be displayed. The field is then wiped, and the caret put into the icon. The two -1s simply ask the wimp to set the caret to the correct width and height for the screen mode.

The next bit of code checks the new password in the later fields to ensure that it has been typed in correctly by the user. If the passwords do not match then both fields are wiped and the caret placed in the first new password icon.

if newpass:input1$<>newpass:input2$ then
w%=ask(true,false,"New passwords do not match.")
newpass:input1$=""
newpass:input2$=""
careticon newpass:input1$,-1,-1
endproc
endif
if newpass:input1$="" and newpass:input2$="" then
w%=ask(true,true,"Remove password protection?")
if w%=1 then
global:lock%=0
newpass:input1$=chr$(88)+chr$(88)+chr$(88)
endif
if w%=0 then
global:lock%=1
global:pass$=newpass:input1$
if global:lock%=0 then
setfade main:lock$,false
setselect main:lock$,true
setfade main:change$,true
endif
close newpass
opencentred main
endproc

The above code simply checks status of global:lock% and if unset unfades the tick icon, unticks it and greys out the change password button. In effect it sets the top part of the main window to its default state.

Two final procedures and the code is complete so create

defproccancel
quit
endproc

and

defprocwf

local file$

file$="<choices$Dir>.scrsavers.archive"

procwritefile(file$)

endproc

That's all the code needed. However, we now need to link the code to the various buttons for the program to work. So if you haven't already, close the code editor and open the window editor and open the main window. From the top select each icon in turn and from the menu select amend icon and link as follows

Button/icon name Procedure to use
password protect procpprotect
change procopenchange
stationary procmovement1
moving procmovement2
cancel proccancel
save choices procwf

Save and close the window and open the password window, and link the following procedure to :

Button/icon name Procedure to use
cancel proccancelchange
change procmakechange

Save and close the window. You are now ready to compile the program, and run it.

You should have a program that now saves the choices file in choices$dir. To check that previous choices have taken effect, just run the program a second time. Remember if setting the password it is case sensitive and that the default is XXX (uppercase X)

If you have any problems please email me direct or via the Archive office. All the files are on this month's disc (I hope) but if not you can get them from my website the address of which is below.

In the final part we will look at the actual screensaver, and see how WB can be used to access OS commands and wimp events.

jpeachey@argonet.co.uk

Happy programming!!

previoustutorial homeNext
Previous ArticleTutorial HomeNext Article


redline.gif - 1307 bytes


The contents of these pages represent my own views and not necessarily those of my ISP