From 11r@ornl.gov Sun Jul 6 06:14:42 1997 From: "Thomas L. Ferrell" <11r@ornl.gov> Subject: Chipmunk Tutorial Ch 4 CHAPTER 4 Chipmunk Basic's Graphical User Interface 4.1 The Message Box In older forms of the Basic language the user responds to questions only in the console window. That is, in the code for the program, there is a simple Input statement such as 200 Input "What color do you prefer? (1=red, 2=blue): "; color and the user would type 1 or 2, followed by . The value of the variable named "color" would then be used in the program in some way. There is nothing wrong in doing things in this fashion, but it is old-fashioned and not in keeping with the intent of the modern graphical user interface (GUI). Modern users expect extra windows for messages and for input and output of certain things. They also allow users to make choices based upon menus. Chipmunk Basic offers several elements that may be used as part of the GUI you may wish to have in your program. A simple example is the message box. To display a message that demands the attention of the user of a program the message box is a simple piece of the GUI that is very easy to implement. For instance, suppose that you want the user to know that he can try another run of your program after any given run. Then, following the last line of your executing code you ould have a line such as 6000 msgbox "Try Another Run!" That's all there is to it. The user would see a message box flash up with your message inside it, and would click on it to dismiss it. This is certainly the easiest GUI element you could imagine, but use it sparingly since an unnecessary and repetitioius message can be annoying. Often, you'd like a message box to appear only under certain conditions and those conditions should not be so common as to be bothersome. Thus, you might consider using a snippet that calls a message box following an "if then" condition. 4.2 The Choice Box Now, for something slightly more sophisticated and useful, let's combine two ideas. One is the idea that a program which needs user input should be able to store previous input in order to avoid unnecessary re-entering of data. The second is our main idea, which is use of a Choice (button) window. The code snippet below illustrates how this is done as it creates a text file (Simple Text or Teach Text) of two pieces of data separated by a comma, and allows the user to just recall the text file the next time the program is run, or to enter new data into the text file that overwrites the old data. This is usually termed a "Preferences" file, although it need not be in the Preferences Folder in the System Folder on a Mac. 210 call "wintitle", "Click a Choice":rem Specifies the Choice window of title Click a Choice. 215 rem The window will have 2 clickable buttons in it. 220 graphics 0 : rem Graphics on 230 graphics color 100,0,0 : rem sets the color (red %, green %, blue %) 240 graphics button "Update Data Inputs",20,30,240,32, "mysub(420)" 250 graphics button "Use Previous Inputs",20,100,260,32,"mysub(300)" 255 rem The above two lines specify the buttons and actions(see text) 260 sub mysub(x) : rem This subroutine, when called, defines action of the button press 270 goto x : rem The subroutine is trivial--it's just a goto line x as specified by the call 280 end sub 290 end 300 Open "balldata.st" for input as #2 : rem Opens the old text file to read its data 400 input #2,initialheight, initialspeed :rem inputs the old data to the program 410 goto 540 420 doevents() :rem keeps GUI input while program is active 430 graphics -1: rem Graphics off for Choice 1 500 input "Initially the ball's height is? ";initialheight 510 input "Initially the ball's speed downward is ? ";initial speed 520 open "balldata.st" for output as #3 :rem opens text data file for new data 530 print #3, initialheight,",",initialspeed :rem Overwrites old data with new data 540 print: graphics -1 : rem Graphics off for Choice 2 Whatever the remainder of your program might do with the values of the variables initialheight and initialspeed, the values are ready if you use this snippet. Moreover, the user doesn't have to enter them everytime the program is used if the values used previously are satisfactory. 4.3 Making a Menu One way of getting user input is by offering choices in a menu. Chipmunk has some menus that always appear. These are File, Edit, and Control. Under each are some choices to be made when the user holds down the mouse button on the menu, moves down to the preferred choice, and releases the mouse button. This differs from the way menus operate on Wintel computers, although their "sticky" menus are offered on the Mac by certain add-on utilities and by Mac OS 8. It's just a matter of likes and dislikes. To add a menu, you need to decide what choices will be under the menu and what title the menu should have. For example, you may want to have a menu named "Options". Suppose you want the user to select one of two choices (as the simplest possible example of making a menu). Let these choices be named "No Air Resistance" and "Standard Air Resistance", as in making a program to calculate the distance a ball is thrown---the case with no air resistance might be interesting to compare with the case in which the effects of air resistance are included in the calculations done by your program. To simplify matters, suppose you use a variable named "marker" which signifies to the program by its value (0 or 1) the choice made. Then, the code snippet below would be one way of doing this. 100 items$ = "No Air Resistance;Standard Air Resistance;" :rem These are the items. 105 actions$ = "goto 140;goto 150": rem Program's actions resulting from the choices. 110 menu 4,"Options",items$,actions$: rem Calls menu with specified items and actions. 120 doevents() : rem Keeps GUI input active while program is active 130 goto 120 140 marker=0: Called in 105 by choice of first item in menu 145 goto 160 150 marker=1 : Called in 105 by choice of second item in menu Another example for a general program would be to have the items in the menu be "Use Old Preferences" and "Update Your Preferences". These choices could then call a Choice window in which Choices can be clicked to specify preferences---i.e., you could combine the ideas in the snippet of the previous section with the menu as described. This is a very Mac-like way of doing things. By the way, if you want to open a prefences file you have created in this way and write the output to it with a particular number of decimal places (See Chap 3 also), you can use a snippet like this: 1000 open "Prefs_Today" for output as #0 5000 print #0, using "##.#"; x1; ",";x2 5005 close #0 This puts the data into the file with the two values x1 and x2 separated by a comma. Chipmunk reads comma-delimited data and not data separarted by spaces or by tabs. In this case the data would be written with a single decimal place.