|
ObjectivesIn the last Chapter we looked at how the CASLide could be used to run, debug, compile, make, and install an existing application on the Windows and PDA targets. In this Chapter we'll introduce you to CASL programming. Specifically, we'll show you how to:
You'll need to have read the last chapter before reading this one, or you should have a basic familiarity with the CASLide. As we'll be introducing CASL language components (variables, objects, and functions) without much background, you may be more comfortable reading this chapter after giving the CASL Language Reference Manual a cover to cover first read, in order to familiarize yourself with the CASL language, especially if you have no prior experience with similar languages, such as Visual Basic, Java Script, or Pascal. The CASL Language Manual comes in a Word version which can be printed, or a Windows Help version which can be used while using the CASLide.
Program SpecificationBefore starting a program we should know what were trying to accomplish and we should weigh our design options and make some decisions regarding these options. Normally this might involve discussion with our targeted user group, but in this case the user group will be ourselves. We're going to use the Wind Chill and Heat Index formulas used by the WeatherCalc sample application. We'll see how to provide a simple user interface, and with our success, we'll progressively try to provide a more sophisticated user interface. As this is our first application we're experimenting with our design options.
So our final program specification is one where:
Not quite the user interface sophistication of Hal from the movie 2001. Although our PDA has a speaker which CASL can control, we'll find it is a bit limited for carrying on a pleasant conversation. Most Palm devices don't have a microphone for listening, ruling out a sophisticated 2-way conversation. So although we'll be using labels, textboxes, and buttons for "talking" and "listening", just the same, as a tribute to artificial intelligence, we'll call our program "Hal2001". It sounds a lot better than "Hello World". New Project (CPJ)Start the CASLide. You create a new project by using the menu command File | New Project, or using the New Project toolbar button. When using the menu command be sure to keep the New/Open/Close operations for Projects separate from those same operations for Files. A Project is a collection of Files. We'll look at File commands in detail later. You can tell what toolbar buttons do by hovering the mouse cursor over a button and a tip note will display. If you don't see the File Toolbar, you can toggle its display by using the menu command View | File Toolbar. You'll first see an Open Dialog box. You'll want to navigate to a folder where you'll want to keep your source code. You can create a new folder at any time by using the new folder button at the top of the dialog box. (I'm assuming we all know our way around Windows. If you don't, I suggest consulting a Windows help book). Once you're in a folder where you'd like to keep your project files, type a program name. According to our program spec, we're using the name, Hal2001. Click Open. Click Yes to confirm.
The CASLide creates a new project (CPJ), and by default provides an empty an empty form window (CFF), and a main CASL source code window (CSL). These windows correspond to files that are now in your project. Both files have the same name as your project.
The programming processWe can now begin creating our first application! How shall we do this? Creating a CASL program consists of the following tasks:
This is the same process used with any program that will run on a GUI (graphical user interface) environment. For these familiar with writing programs for the Windows environment with such languages as Visual Basic, Java Script, or Delphi, it's a simple transition to the PDA environment and CASL. For those of you familiar with old fashion Fortran, BASIC, or other procedural languages, then user interface objects like buttons and event functions will be new. We'll try to cover the basics of GUI programming in this chapter. For those totally new to programming of any kind, or if you're otherwise left wanting for a bit more background information, you may find you'll want to reference a beginner's book on GUI programming (otherwise known as object oriented, event-driven programming). I'd suggest one of the many Visual Basic or Delphi books. The CASL, Visual Basic, and Pascal syntax are very similar, so you could work out coding examples in CASL with minor translation. Designing FormsOk so what's a form? A form is a collection of display objects that will appear to the user when the program is running. On Windows this will appear in a window. On the PDA this will be the full screen display. Display objects are things like a frame, a button, a textbox, a menu item, or a selector list. We'll look at display objects in more detail in a minute It's objects that makes CASL an object oriented language. There is a formal specification for object programming languages. For those familiar with formal object oriented programming languages, CASL provides pre-defined, encapsulated objects, however you cannot create new instances during run time. It follows then inheritance is also not supported. Objects you need are defined during compile time. You can define object arrays, and these in many ways give you "collection" type functionality. Objects can be passed by reference to functions. You can combine objects to simulate the behavior of a new object class, but there is no formal encapsulation or inheritance. Attaching CASL CodeWhen the user interacts with a display object, things are presumably supposed to happen. You as the programmer are in charge of making these things happen. You do this by attaching CASL code to the event function that is invoked (run) when the user interacts with a display object. This interaction could be a tap, or entering text with the keyboard or graffiti. CASL can also respond to system events like a period of time elapsing. It's because CASL code is executed only when invoked by the user or system event, that we call CASL an event-drive language. Testing and DebuggingIt's always best to start any application very simply, and add features one by one, testing each before proceeding with the next. Therefore the overall process is not 3 discrete steps, but rather the iterative application of these same 3 steps over and over until the full application has be realized. We'll use this approach here. Displaying Information with Frame, and LabelThe simplest application is one that will only display information. The classic example is a "Hello World" application. Since it's Hal2001 we're programming, perhaps our program should display, "Hello pilgrim, how's it going?" (Hal can be a bit condescending). I mentioned we'd be doing a lot of debugging as we go along. Well we already have an empty form and code window. What will happen if we run our "program" now? Make sure the selected target platform is "Windows Release" or "Windows Debug". Click the Run toolbar button (or menu command Execute | Run).
Our program runs. It displays an empty form, and does nothing. That is just what we should have expected. Close the program by clicking the "x" button in upper right hand corner (or using the control menu in upper left hand corner). The frame is the container object for all display objects. The frame is the parent, and the other objects are children. Every CASL program needs at least one frame (assuming it will be displaying anything). In the Project Window, double click on the form file. The form file will now be active in the Form Builder Window. To display the whole form, you can close the build window, and double clicking on the title bar of form window will make full size.
Double click on the Form. A frame properties dialog box will come up. The CASLide created a frame for your new project by default. At this point I suggest giving your frame a name, and the Display property contents will define the title bar of this form. Let's call the frame, frMain, and type this in the Name textbox. Let's type, Hal2001, in the Display textbox. Note, you don't put quotes around the text you enter into property textboxes. This is true when the Expression checkbox is left unchecked. You can enter a legal CASL string expression and click the Expression checkbox, if you'd like the content to be evaluated before assigning to the Display property. Click OK. Click Run. Your program should be running and "Hal2001" should be displayed on the title bar. Pretty good, eh? Ok, so we've got a bit more in mind for our application. Let's use a label object to display a bit more text. Select the label button on the Form Controls toolbar. Use the hover tips to find the button. It's the "Aa" button (not the "ab|" text object button). Then click the left mouse button at the top left hand corner of our form. While holding the left button down, drag the display object to take the shape of a single line across the width of the form.
Tip: An existing display object can be selected, copied and then pasted to the same form or a different form. Delete any or all selected objects with the Del key. Double click on the label to bring up the property window. We can change the default object name to lbWelcome. Fill in the display property with, Hello pilgrim, how's it going?
Ok, now we've defined a frame (actually we only set it's properties, one was created by default when the project was created. We defined a label on this frame. Together these define our form. What will happen when we run this program? Click the Run toolbar button (or Execute | Run) and let's find out. If you've typed everything correctly, you should see the form below.
If you have some compile errors, or your display is different, check your property values. Our program offers us a greeting, and the user doesn't even need to interact with our program. This is maximizing the object definition part of GUI programming. The form builder in effect has written our program for us. So far we've had to write no event code. The Startup EventWhen a CASL program starts, this trigger a pre-defined system event. When this event occurs a function called startup will be called, if it exists. Attaching code to this function will allow us to do things we'd like to do to initialize our program. This might be initializing variables, initializing a display screen, or it might be reading a database. Whatever it is we need to do at program startup, will be done by putting the code in the startup function. To illustrate a simple activity for our startup function to undertake, let's have our label object display something different that our message that's been set by the form builder. ### add better explanatoin of obejctname.propname as variable type holder. can query or assign values. Use the Project Window to make the code window for Hal2001.csl active. Type the following into this file. # the Hal2001 sample app # code to execute at program startup function startup; lbWelcome.display="Program has started"; end; Statements starting with the # character are comments, which are ignored by the CASL compiler. Notice all CASL statements end with a semicolon. As a CASL programmer, you'll need to get used to this. Forgetting to end each statement with a semicolon is the most common compiler error. Did you notice when you typed the period character, a popup list of objects displayed? When you hit Enter or double click on an object, a context sensitive list of properties pops up. Hit Enter or double click on a property, and the property is automatically inserted into your code statement.
Tip: Object property detection is intended as a typing convenience. If there's a situation where you don't want to use this feature, the escape key will make the list disappear. You can permanently turn off this feature by using Tools | IDE Settings, and unchecking, Allow object property detection. We've written our first CASL code! Let's see if it will run. Click the Run toolbar button. The build window informs you about the compile status. If you have an error, double clicking on an error will take you to the offending line of code in the code window. You'll normally want to start by examining the first compile error, as often later compile errors are dependent on the first error. Make sure you've entered the code exactly as given in the example. Tip: Notice that the CASLide code window color codes all CASL key words as blue. This is helpful for readability, but also for picking up spelling errors. Comments are in dark green. String literals are in red. Normal CASL code is in black. The color coding assists in picking up syntax errors. You can change the default colors by using the Tools | IDE Settings, Syntax coloring menu option. If your program's running correctly we should now see that our new display text replaces the text we initially set with the form builder. Displaying Information with Message_box FunctionOne of CASL's predefined functions is the message_box function. Using this function we can easily display information, or query the user with a 3 button input means. Tip: If you need to see a function's definition, you can type the name of the function in your current code window, and hit the F1 key. This will start the CASL Language Manual, and place the cursor at the function's definition. You can also use the Help | Language Reference menu option, and then use the WinHelp engine as you normally would. Index | Find is a common way to find things for which you're looking. Let's use this function to display information. Looking in the the CASL Language Manual, we see that the message_box function is defined as follows: Numeric Message_box(Numeric class, String caption, String description, String B0, String B1, String B2)
Presents a message box on the screen, waits for a button press, and returns the value 0, 1 or 2, indicating which button was pressed. The message_box function returns a numeric data type for the button pressed. We're planning to provide only an OK button, so we're not interested in the return value, but we need a variable holder for this value none the less. Tip: Numeric and string are the main data types in CASL. Numerics are an 8 byte IEEE format native to Palm OS. They support 16 digits of accuracy, and the number range of 10E+38 to 10E-38. Numerics are used for all integer and floating number needs of a CASL program. Strings are for holding text. Strings accept all 8 bit, 1-255 ASCII characters. The ASCII 0 character cannot be part of a string, since it's used by the system as the string terminating character. We'll plan to display the "info" icon, and provide literal strings for the title and description parameters. We can do this by modifying our startup function as follows: # the Hal2001 sample app # code to execute at program startup function startup; variables; numeric mb; end; lbWelcome.display="Program has started"; mb=message_box(0,"Information","Whining is futile","","OK",""); end; We're declaring a local numeric variable, mb. We do this with the variable declaration block. Tip: To learn more about the variables statement, highlight the key word in your code window and hit F1. We're asking the message_box function to display the "info" icon by passing the 0 parameter. We asking that "Information" be the title string. We're asking that "Whining is futile" be the message string. We're asking that only the middle button be shown with the string, "OK". We're assigning the message_box return value to the variable mb. Let's run this program and see what we get.
As expected, as soon as the program starts, the message_box function in invoked and displays it's information. It then waits for the user to tap the OK button. The message_box function is another handy way to display information, especially if we don't have the screen space on our main form for another label object. The button object and tap eventSo far we've looked at objects that allow the program to display information ("talk"). Let's look at an object where the program accepts a command from the user ("listen"). The button object allows you to define a display object that the user can tap. The button is the most common way to allow your user to interact with your program. We define a button very similar to how we defined the label; with the form builder. The only difference is we need to select the button tool. Go ahead and try to add a button to our form. First you'll need to select the form in the project window. You may need to close the build windows and double click on the form's title bar to get a full screen view. You select the button tool. You can hover the mouse cursor over each button on the form controls toolbar, to get a tool tip. With the button tool selected, draw the button is the lower right hand corner of the form. Double click on the button and the properties dialog box will come up. Name the button, btDo, and define the display string as, Do This. Click OK. Your form should now look like this:
Similar to how we defined code to the system startup event, we can now attach code to the button's tap event. The name of the button's event function which is invoked is by default the name of the object. You can also use the object property dialog window to specify another name for the event function that will be invoked. This is done by specifying a name in the invokes property. Tip: Since CASL objects with events normally only have one event type, it will be the practice in this tutorial to always use the default invokes function name. The object prefix that is used with the object name, as it identifies the object type, also identifies the event type. This is a matter of style. Using this button as an example, one could define an invokes property of btDo_tap, should a programmer find this more helpful. Let's now modify our program so that the message box is not displayed until the user taps the Do This button. We do this by adding a new function, btDo. We'll move the message_box code from the startup function to this new function. # the Hal2001 sample app # code to execute when btDo button tapped function btDo; variables; numeric mb; end; mb=message_box(0,"Information","Whining is futile","","OK",""); end; # code to execute at program startup function startup; # variables; # numeric mb; # end; # lbWelcome.display="Program has started"; # mb=message_box(0,"Information","Whining is futile","","OK",""); end; Notice we've modified the startup function to do nothing, by putting a comment character in front of each statement. We could have also just deleted the whole function. Tip: There are 2 ways to comment out a block of CASL code. Put a comment character in front of each statement, or enclose the block of code in a compile_if false/compile_end_if block. The later method is good for long code blocks, and also it's easy to turn the code back "on" by changing false to true. By the running the program now, you should see at startup the same form we defined with form builder. It's waiting for user action. When you tap the "Do This" button, the message box is displayed. Tapping the OK button will return the display to the first form, and the program to the waiting state.
### either add weather calc compute code, and then text object, or do other way around |
[ Top ] © 2001, Feras Information Technology. All rights reserved. |