Having touched on dialogs in the last session, let's go into more depth with this extremely handy OPL feature. One function often found in professional software is a "Preferences" screen where the user can choose how the program should behave. For instance, whether the sound volume should be "Off", "Low" or "High". In this session, we're going to give our MiLo program the ability to make sounds and then give the user a chance to choose whether these sounds are made or not.
First things first. Find the line of code "generate:" and add a line after
it which simply reads:
BEEP 10,300
Don't worry about what the numbers mean, just take my word
for it that this magic sequence gives a nicely pitched beeping noise. Translate
and run the program and you'll see what I mean.
In a quiet office, of course, the user might not want to have the program beeping at him or her. So let's give the user the chance to turn the sound off if needed. One thing dialogs are very good at is asking questions of the user. Sometimes it's the value of a number or character string and sometimes (as here) it's to ask them to make a choice between a set of possible selections.
Go up to the top of the program and enter a new GLOBAL variable:
GLOBAL sound% rem 1=On, 2=Off
sound%=1 rem default ON
Note that it's important to give variables a default number right at the
start of each program. If no other value is specified, the Psion will assume a
value of zero. In the case of our sound% variable, zero isn't a valid value
and thus we take the step of setting it up as 1 or 2 (On or Off):
Before we can write out code for a preferences dialog, we
have to provide some way for the user to call it up. As with the Help and About
screens, let's make up a menu option. Find the menu card section and add in
some text to the second mCARD:
mCARD "Info","Help",%h,"About",%a,"Preferences",%p
and add the following after the new BEEP line:
ELSEIF j%=%p
prefs:
Which should leave your screen looking something like this:
The next step is to make a prefs: procedure to match up with
the line of code above. Go to the very bottom of the program code and add the
following:
PROC prefs:
dINIT "Preferences"
dCHOICE sound%,"Sound","On,Off"
dBUTTONS "OK",13
DIALOG
RETURN
ENDP
Translate and run the program and try out the new Preferences
menu option. Pressing TAB on the "Sound" line gives the possible choices as
you'd expect, with OPL's dCHOICE feature doing all the hard work for us. The
global sound% is simply set to 1 if the first choice from the list is made
("On"), 2 if the second ("Off"). And so on, if we had more than one item in the
comma-separated list.
Of course, there's one more thing we need to do. We've set up
a variable and allowed the user to change it but the program doesn't actually
use the variable at all.... yet.
Go back to the BEEP line and add a line above and below it as shown here:
IF sound%=1
BEEP 10,300
ENDIF
In other words, if the program finds sound% set to 1 it does
the beep. And if not, it doesn't. Translate and run MiLo and verify that it now
works OK. Turn the sound off within the program and then generate a set of
numbers.
That wasn't too painful, was it?! See you next week, when we'll look at saving our new preference so that the user doesn't have to set things up each time he or she runs the program...
Go to next lesson | Programming index