Chapter 4 Using Activities in ADL

4.1 Using the Pressed Attribute

One of the most common uses of activities is the use of button objects. For example, one often places buttons in a multimedia application that, when pressed by the user, trigger some computation or presentation. This use of buttons is so common that AM2 provides a shortcut to simplify the general activity mechanism in such cases.

The wrapped class XFbutton creates a simple button on the screen. (see Section 6.2.9, "XFmessageDlg" page 136 for detailed documentation on the class.) The XFbutton class provides typical attributes such as width, height, (x,y) location on the screen, foreground and background colors, and a text label. For example, the following code is a very simple ADL program that puts a button at coordinates (50,50) inside an instance of an object that is a subclass of the XFtop shell class
A Simple ADL Application with a Button
anonymous:XFtop

{

XFbutton aButton {x=50; y=50; height=100; width=200;

label="Push Me";};

} myApplication {height=300; width=400;};

In this example, there is a single instance of a class that inherits from the XFtop wrapped class. The anonymous keyword in line 1 indicates that this class has no name; only this instance of the class is named. (In this case, this instance is named myApplication.) The anonymous class has a single member, a button object name aButton defined in lines 3 and 4.

This ADL program draws the button on the screen. Clearly, we need a way of assigning the trigger event when the user presses the button. The XFbutton class supports a special member called Pressed. You can assign this special member a list that instructs the program to take some action when the user clicks the mouse on that button. In its simplest form the Pressed attribute is a list that has two values: a string giving the name of the method invoked, and a handle to the object that receives a message when the user presses the button. For example, the assignment

aButton.Pressed = {'Exit, theApp};

registers the fact that the message Exit goes to the built-in handle theApp. This is a predefined message that terminates the program when sent to the application.

The example in Figure 4.2 displays some text information. Suppose we want to have a help button in an application that displays some text information when pressed. The ADL program below shows this using an instance of the XFtext wrapped class which is initially not visible (done by setting the visible attribute to FALSE), and reversing that attribute when the user presses the button.

An Example of a Help Button
anonymous:XFtop

{

XFbutton helpButton {x=50; y=50; height=50; width=150;

recomputeSize=FALSE; label="Help";};

XFbutton exitButton {x=250; y=50; height=50; width=100;

recomputeSize=FALSE; label="Exit";};

XFtext helpText {x=50;y=100;height=100;width=200;wordWrap=TRUE;

visible=FALSE; editable=FALSE;

text="This is an example of a help message.";};

upon Construct

{

exitButton.Pressed = {'Exit, theApp};

helpButton.Pressed = {'BPress, self};

}

on BPress

{

if (helpText.visible) {

helpButton.label = "Help";

}

else {

helpButton.label = "Remove Help";

}

helpText.visible = ! helpText.visible;

}

} myApplication {height=300; width=400;};

In this example there are two buttons, one to exit the application and one to trigger the visibility of the help text. The special constructor method named Construct automatically receives a message when the application starts, and sets the Pressed attributes for these buttons. The assignment statement in line 14 sets the Pressed member so that the message BPress goes to the variable self. (The variable self is an automatically-generated handle that references the object itself.) Thus when the user presses the button, the BPress method that starts on line 17 executes. This method changes the text of the label on the help button and, on line 25, reverses the visibility of the text object.

You can also use the Pressed attribute to send a message to a method that requires one or more arguments. For example, suppose we wanted to create two buttons which, when pressed, move an instance of a label ten pixels to the right or left respectively. To do this, let's create a method that changes the x attribute of the label by n pixels, where n is an argument to the method. For example, if the name of the label object is myLabel, then the following method would move it:


	on MoveLabel: integer n
	{
		myLabel.x = myLabel.x + n;
	}

If we name the two buttons leftButton and rightButton, we set their Pressed members as follows:

	leftButton.Pressed = {{'MoveLabel, -10}, self};
	rightButton.Pressed = {{'MoveLabel, 10}, self};

AM2 Documentation - 19 NOV 1996

Generated with Harlequin WebMaker