Smart CODE
Your on-line guide to the generated code

Using Getters and Setters. A worked example


Introduction

This example shows that you can program interface controls quite effectively without needing to write toolkit dependent code yourself. The callback you write will work as well on a Windows PC running with MFC, as it would using X/Motif.

Things you must do/know BEFORE running the example

Environment Variables etc

As always, you should make sure that $XDROOT/bin is in your path.

Here is a summary of the variables you will need to set up before working through the example

XDROOT
LM_LICENSE_FILE

Preparing the Example

Step 1 Create a new directory called, for example, getters
Step 2 Change directory to getters
Step 3 Run xdesigner using the xdreplay script $XDROOT/src/examples/sc/getset.vcr, ie

$ xdreplay -f $XDROOT/src/examples/sc/getset.vcr xdesigner


This will create the example application, generate the code, copy standard versions of the callbacks you would have written into the appropriate place, and invoke make to build the program.

Running the Example

First, check that you have set up the environment variables. If there is a problem, check that they are set correctly, eg

$ ls $XDROOT

You run the example application by typing

$ ./untitled

The example (see the code below) endeavours to keep the toggle and the option-menu values in sync with each other, and reports whether it needed to update anything, via the text field.

Some words of explanation

Creating the Interface

The application is designed to illustrate how easy it can be to move a callback out of an application, and over to a server - or wherever is appropriate. The interface is trivial, a button to hang the callback on, and a group comprising a toggle and a text field.

Step 1 Create an Application Shell, a form, and underneath it, a button, toggle, an Option Menu and a and text field
Step 2 Populate the Option Menu with two buttons, and give the first the label NO and the second, the label YES.
Step 3 Give the toggle the label PRESSME
Step 4 Click on the toggle, then Shift-click on the text field and the Option Menu so they are all selected
Step 5 Select add to new group from the toolbar. Close the group editor when it appears. It only comes up to allow you to change the group name if you want to.
Step 6 Select the button, bring up the callbacks dialog, name the callback doit_callback, select Smart/Code- Interface Get/Set. Then press the group button, and in the Group Editor make sure the group is selected, and press Apply.
Step 7 Use Generate/generate... from the menu bar to get to the code generation page. Make sure that you are generating C, that you are generating to the right directory, and that the default toggles are set including the Stubs generation toggle

Press the generate button.

Step 9 Back in the shell, use make to build the application
The application does nothing at all at the moment. We have not written any code in our stub file. Here is the example code, that is used in the replay script.

void
doit_callback_user (sc_data_t * d, mygroup_t* group, void* client_data)
{
	int option_ix  = SC_GET(SelectionByIndex,group->optionMenu1);
	int toggle_val = SC_GET(State,group->toggle1);

	if (option_ix != toggle_val) {
		SC_SET(Value,group->text1, "Changing Settings...");
		SC_SET(SelectionByIndex,group->optionMenu1,toggle_val);
	} else
		SC_SET(Value,group->text1, "Settings ok");

}

The example is forced. But it keeps the toggle in sync with the option menu and says what it is doing in the text field.

Notes