This chapter demonstrate basic RapidApp use by describing how to create a simple calculator application that adds two integers. Each step that you perform is described in detail in Chapter 3, Chapter 4, and Chapter 5.
Figure 1-1 shows how the Calculator application looks when finished. Refer to Appendix D, “Source Code for the Calculator Application,” for the source code for this example.
To create the calculator application:
Start RapidApp.
RapidApp's main window opens as shown in Figure 1-2. The areas of this window are described in “The RapidApp Interface”.
Create a top-level window.
From the Window palette, click the Simple Window icon.
Position the pointer somewhere on the screen and click the left mouse button to place the window.
In the Instance Name text field in the instance header area of the RapidApp's main window, type “calcWindow” and press <Return>.
In the resource editor, find the resource named title, type “Calculator”, and press <Return>.
Add a Bulletin Board container to the window
From the Containers palette, click the Bulletin Board icon.
Position the pointer over the window and click again.
Add an Text Field to the Bulletin Board.
From the Controls palette, click the Text Field icon and then click over the Bulletin Board element.
Adjust the size and position of the widget, if necessary, to match the appearance shown in Figure 1-3.
Add a second Text Field below the first and place a label to the left of the second Text Field. Figure 1-4 shows the resulting layout.
Complete the layout by adding a separator below the second Text Field, and a PushButton and third Text Field below the separator. Figure 1-5 shows the layout after all widgets have been placed.
Rename the top text field element “value1,” the second “value2,” and the third “result.”
Click each text field element in turn
In the Instance Name field in the header area, enter the new name.
Change the label on the Label element to read “+”.
Click the label widget, and find the resource field named labelString.
Replace the text in that field with a “+” character.
Reposition the label if necessary.
Change the label on the button element to “=”.
Click the button widget, find the labelString resource field, and change the value to “=”.
Reposition the button if necessary.
Add a callback named “add” to the PushButton widget.
With the button widget still selected, find the resource named activateCallback and type “add”.
Press <Return>.
Notice that RapidApp automatically adds “( )” after the function name. At this point, the interface should look like the window in Figure 1-1.
Test the interface.
From the View menu, choose “Play Mode.”
Type into the text fields, press the button, and so on.
Notice as you press the button, an information window appears at the bottom of the screen (see Figure 1-6), reporting that the add() callback is being called.
Set code generation options.
From the View Menu, choose “Build Mode” to go back to build mode.
From the File menu, choose “Preferences.”
In the Preferences dialog, go to the Project card.
Change the directory path to the directory where you want RapidApp to save the files for the application. If this directory doesn't exist, RapidApp asks if it should create the directory.
Change the name field to “calculator,” as shown in Figure 1-7. Make sure that the rest of the options are set as shown in Figure 1-7.
Click the Ok button.
Save the interface
From the File menu, choose “Save.”
RapidApp displays a dialog prompting you for a filename for the interface you have just created.
Save the file as calc.uil.
(The “uil” suffix stands for user interface language, and is a file format used by IRIS IM, as well as by many user interface tools. You should name your files with a “.uil” suffix.)
Generate code by choosing “Generate C++” from the Project menu.
RapidApp displays a status window to report the files that it creates.
When the dialog displays indicating that the window was not declared as a class, click the Continue, Don't Ask Again button.
Build and run the application by choosing “Run Application” from the Project menu.
The Developer Magic Build Manager (see Figure 1-8) appears and compiles the application. Once compiled, the application runs automatically.
Add functionality.
From the Project menu, choose “Edit File.”
In the file selection dialog, choose the file BulletinBoard.C.
When the text editor appears, scroll down until you locate the section of code shown in Example 1-1, which is the callback invoked when the user clicks the “=” pushbutton:
void BulletinBoard::add ( Widget w, XtPointer callData )
{
//---- Start editable code block: BulletinBoard add
XmPushButtonCallbackStruct *cbs = (XmPushButtonCallbackStruct*) callData;
//--- Comment out the following line when BulletinBoard::add is implemented:
::VkUnimplemented ( w, "BulletinBoard::add" );
//---- End editable code block: BulletinBoard add
} // End BulletinBoard::add()
|
Edit this function so that it appears as shown in Example 1-2 (your additions are shown in bold).
void BulletinBoard::add ( Widget w, XtPointer callData )
{
//---- Start editable code block: BulletinBoard add
XmPushButtonCallbackStruct *cbs = (XmPushButtonCallbackStruct*) callData;
//--- Comment out the following line when BulletinBoard::add is implemented:
//::VkUnimplemented ( w, "BulletinBoard::add" );
int a = atoi(XmTextFieldGetString(_value1));
int b = atoi(XmTextFieldGetString(_value2));
XmTextFieldSetString(_result, (char *) VkFormat("%d", a + b));
//---- End editable code block: BulletinBoard add
} // End BulletinBoard::add()
|
The first two added lines call XmTextFieldGetString() to retrieve the contents of the top two text field widgets. Because this function retrieves a string, you must use atoi() to convert the string to an integer. Then XmTextFieldSetString() sets the resulting value in the result text field. XmTextFieldSetString() expects a string; this example uses the IRIS ViewKit convenience function VkFormat(), which works like printf() but returns a character string suitable for displaying in a text field widget. Notice that the widgets in this example are accessible in the BulletinBoard class as data members whose names are the names given in RapidApp but with a leading “_” added.
Now scroll to near the top of the file and find the line:
//---- Begin editable code block: headers and declarations |
After that line, add the header file for the VkFormat() function and <stdlib.h> for the atoi() function:
#include <Vk/VkFormat.h> #include <stdlib.h> |
Save the file and close the editor.
Test the completed application.
From the Project menu, choose “Run Application.”
The Build Manager appears again and builds the application. If you made any errors in typing in the changes, you can browse the errors using the Build Manager. Once compiled, the application runs automatically. Figure 1-9 shows the completed application as it appears on the screen.
Try typing integer values into the text fields and pressing the “=” button.
Package the product so that other users can install the calculator application using the Software Manager software installation tool (swmgr). Note that you must have inst_dev installed.
Go to the Calc directory and enter:
% make image |
This creates a complete installable image in a subdirectory named images.