John.Wiley.and.Sons.Rapid.Mobile.Enterprise.Development.for.Symbian.OS.An.Introduction.to.OPL.Application.Design.and.Programming.May.2005.eBook-LinG (779881), страница 15
Текст из файла (страница 15)
Both ways are correct, andneither is more correct than the other. From here we can see the threeelements that make up dTEXT, the prompt (p$) the body text (body$),and the formatting (the value of t%).Where you have a variable in a command explanation, this is forguidance and reference only.
You can enclose a string in quotationmarks, and this will work just as well as a reference to a variable. SodTEXT "Name","Symbian" is valid, as is:Prompt$="Name"Body$="Symbian"...dTEXT Prompt$,Body$You are not limited to the variable names in the command explanation,you can use whatever you need to use in your program.72A CONVERSION PROGRAM: EVENT CORE IN PRACTICEThe text notes that the formatting will default to left-aligned, but thesecond way of writing the command (dTEXT p$,body$,t%) allows youto specify your own formatting.
The first option is where to align the text(left, center, or right) and the second allows you to add other formattingfeatures with which you can experiment to get the correct look. There aremore details on the formatting options in the Appendix.Values Returned from a DialogSo now we’ve initialized our dialog, and created the text to display (usingdTEXT for each line of text), we can display it by using the DIALOGcommand. Here’s the entry for Dialog from the command index:**************************************************************************DIALOGPresents a dialogUsage:n%=DIALOGPresents the dialog prepared by dINIT and commands such as dTEXT anddCHOICE.
If you complete the dialog by pressing Enter, your settings arestored in the variables specified in dLONG, dCHOICE, etc., although youcan prevent this with dBUTTONS. If you use dBUTTONS when preparing thedialog, the keycode that ended the dialog is returned. Otherwise, DIALOGreturns the line number of the item that was current when Enter waspressed. The top item (or the title line, if present) has line number 1.If you cancel the dialog by pressing Esc, the variables are not changedand KDlgCancel% is returned:KDlgCancel%0Return value: dialog was cancelledSee also dINIT.**************************************************************************What we have here is this entire section in three paragraphs – explainingexactly how dialogs work, how we start them, construct them and finallydisplay (present) them on the screen.
It also explains the value that theDIALOG command will pass to the integer variable that is used within theline of code that presents the DIALOG.Getting Input from a Dialog (Prefs)While a dialog can be used to present information, they are even moreuseful when used to gather information. Whenever you need to have anumber, or a string of text, entered by a user, your first thought should beto use a dialog box.There are four main dialog commands used to get values from the user.These are:FIRST STEPS WITH EVENT CORE73• dCHOICE• dEDIT• dLONG• dFLOATFor a full description of these commands (and subsequent commands),look in the Appendix for the command list.dCHOICEdCHOICE provides you with a drop down list of text strings to choosefrom.
Let’s imagine we want to present the user with a choice of twoitems. For example, in our temperature conversion, we need to know if itis Celsius to Fahrenheit, or vice versa.So our two options in the drop down box in the dialog are "Celsiusto Fahrenheit" and "Fahrenheit to Celsius". We need to store the resultof this choice, and we use an integer number. If we choose the firstoption then the integer will be 1, and if we choose the second it will be2. Let’s store this value as TemperatureDirection%. Remember thatwe’ll need to have initialized this variable with either LOCAL TemperatureDirection% or GLOBAL TemperatureDirection% at somepoint previously in the code:dINIT "Choose Conversion"dCHOICE TemperatureDirection%,"Celsius to Fahrenheit,Fahrenheit toCelsius"LOCK ON :Return%=DIALOG :LOCK OFFdEDITdEDIT allows the user to type in a string and have it stored in a stringvariable when the dialog is successfully closed (i.e.
the end user doesn’tpress Esc or a cancel button). dEDIT isn’t used in Convert, but it is oneof the more important dialog commands. Because the dialog commandsare built in to the runtime, you don’t need to worry about reading inkeystrokes, or T9 input, or character recognition: this will always be donefor you. dEDIT is the easiest way to get the user to enter text strings.dFLOAT and dLONGIn a similar way to dEDIT, these commands allow you to type a numberin. dFLOAT is for very large numbers or numbers with decimal points (e.g.LargeNumber, where there is no sign at the end of the variable name).dLONG is used for long integers (e.g. MyNumber&).
Both commandsrequire a lower and upper limit on the number, which you can specify,and a prompt (which can simply be an empty string represented by twoquotation marks "", if you really want no prompt):74A CONVERSION PROGRAM: EVENT CORE IN PRACTICEdFLOAT FloatPoint,Prompt$,MinValue,MaxValuedLONG Long&,Prompt$,MinValue&,MaxValue&Again, see the command list for a full explanation.4.1.9Putting it All TogetherSo, let’s take the Event Core, what we now know about menus anddialogs, and put together the Convert program.The user initiates a conversion dialog with either the menu entry, orwith a hot key press.
For both cases, we need to add some lines into ourPROC ActionHotKey:PROC ActionHotKey:IF Key&=ASC("e")-96 : Exit:ELSEIF Key&=ASC("k")-96 : SetPreferences:ELSEIF Key&=ASC("A")-96 : About:ELSEIF Key&=ASC("t")-96 : DialogTemperatureConvert:ELSEIF Key&=ASC("l")-96 : DialogDistanceConvert:ELSEIF Key&=ASC("d")-96 : DialogShortDistanceConvert:ELSEIF Key&=ASC("w")-96 : DialogWeightConvert:ENDIFENDPSo once a conversion is chosen, we’re passed over to a procedure to dothat conversion.The Convert VariablesSo we’ve worked out which conversion we’re going to do, and we’vebeen sent to the correct procedure to work out two values.
The first iswhat direction the conversion will go in (e.g. miles to kilometers, orkilometers to miles). The second is the value we are converting (e.g. 500miles). We’ll use a dialog box to gather these, and then pass the twovariables to a procedure that will calculate the result, and then display it.Now, we could set up two GLOBAL variables to hold both ConversionDirection% and ConversionValue%, but remember thatGLOBAL variables are always present and available to a program, usingup memory.
There is no need to store these values after the calculation isdone, so we will create two LOCAL variables with the appropriate names,and then pass these to the procedure that does all the calculations:PROC DialogDistanceConvert:LOCAL ConversionDirection%,ConversionValue,ConversionReturn, Foo%rem get the values from the userdINITdCHOICE ConversionDirection%,"Direction","Miles to Km,Km to Miles"FIRST STEPS WITH EVENT CORE75dFLOAT ConversionValue,“Value”,0,1000000LOCK ON :Foo%=DIALOG :LOCK OFFIF Foo%=0RETURNENDIFrem Go and do the calculation in another procedureIF ConversionDirection%=1ConversionReturn=DoConversion:(1.60934,ConversionValue)ELSEConversionReturn=DoConversion:(0.62137,ConversionValue)ENDIFrem Display result hereENDPSo once we’ve taken the values from the dialog, we pass them to aprocedure that will do the conversion of the values passed to it.
‘Passing’variables is a great way to send a fixed number to another procedurewhen you don’t want to use a global variable. To pass a variable, youenclose it in brackets after calling a procedure. You can pass more thanone by separating them with a comma.Here are some examples. Note we can pass string variables, actualstrings, and all types of number variable. Both of these are valid ways topass information to a procedure:CallCustomer:(Name$,IDNumber%,Number$)CallCustomer:("Geoff",67,"+441234567890")Doing the MathsSo at the other end, we need to make sure the procedure is expecting toreceive the information passed to it. We do this in a similar way to declaring LOCAL variables, except we name the passed information by usingnames in brackets after the procedure name.
So our DoConversion:procedure will look something like this:PROC DoConversion:(Multiplier,FirstValue)It’s important to remember that these values are like temporary constants.You cannot make any changes to the information contained in thesenamed values – they’re for reference only.We’ll need somewhere to store the result:LOCAL StoreResultNow we can do the maths itself:StoreResult=Multiplier*FirstValue76A CONVERSION PROGRAM: EVENT CORE IN PRACTICEFinally we must return the result to the procedure that called the DoConversion: routine, and end this procedure:RETURN StoreResultENDPNow, let’s look back at the line that called this procedure:ConversionReturn=DoConversion:(1.60934,ConversionValue)The RETURNed value of StoreResult is now going to be assigned toConversionReturn.
You’ll notice that we call the same DoConversion: procedure for all the conversions; we just alter the values that wepass to it. This is an example of code re-use – we don’t need to re-inventthe wheel and duplicate the actual calculation code for every conversion.Showing the ResultsWe’ve now done the conversion, and we want to display the result. We’lladd this to the end of the PROC DialogDistanceConversion:.