Chapter 5 Example ADL Programs

5.2 A Simple Image Viewer Class

In the second example we explore the creation of a simple image viewer. This class has the ability to display a still image from a file. It is "reloadable" in the sense that the same viewer can be used to display different images, one at a time. We implement the viewer so that the construction of the viewer is separate from the loading of an image into the viewer, allowing us to create viewers at the start of an application and load images when needed.

5.2.1 ADL Implementation of the Viewer Class
Simple Image Viewer Class
class Viewer : XFtop

{

handle pimage;

XFvisual screen {x=0; y=0;borderWidth=0; height=640; width=480;};

string currentImage = "";

/* standard constructor */

upon Construct

{

visible = FALSE;

}

/* special constructor that also loads image */

upon ConstructAndLoad: string iname

{

visible = FALSE;

{'LoadImage, iname} => self;

}

/* load a new image */

on LoadImage: string imageName

{

IOfile imageFile;

XFmessageDlg {'Create, self} => openFailDialog

{title="File Open Failed";

message="Attempt to open file failed:"; dialogIcon="warning";

buttonSet="ok";};

/* check if image is already displayed */

if(currentImage == imageName) {

visible = TRUE;

return;

}

/* delete old image */

if(?pimage) {

delete pimage;

}

/* check if new image file can be opened */

{'OpenNative, imageName, 'ReadOnly} => imageFile;

if('Fail=>imageFile) {

openFailDialog.message = openFailDialog.message & imageName;

'PostModal => openFailDialog;

return;

}

'Close => imageFile;

/* create new image object */

pimage = new {'Construct, {'MEimage, {'MAfile, imageName}}}

=>MMimage;

screen.height = pimage->height;

screen.width = pimage->width;

/* display the image */

{'PresentOn, &screen} => pimage;

height = pimage->height;

width = pimage->width;

currentImage = imageName;

title = imageName;

visible = TRUE;

}

on Destroy

{

if(pimage != UNSET) {

delete pimage;

}

}

}; /* end of class Viewer */

Line 1 defines the beginning of the Viewer class. This class inherits from the XFtop class.

Line 3 defines the handle pimage. This stores the handle to the image being displayed.

Line 4 defines an instance of the XFvisual class named screen. This is the display surface for the image.

Line 5 defines a string named currentImage. This holds the name of the file containing the image to be displayed.

Lines 7 - 10 define the Construct method. This method is the default constructor for the class. It simply sets the visibility of the object to FALSE so that it does not appear on the user's display until it is loaded with an image.

Line 12 - 16 provide an alternative special constructor called ConstructAndLoad. This is used when the image is to be loaded at the same time a viewer is constructed. The constructor sets the visible attribute to FALSE and invokes the method LoadImage.

Line 18 begins the LoadImage method. This has a single argument containing a string with the name of the file where the image to be loaded is stored.

Line 20 defines an instance of the IOfile wrapped class. This is used to open and read the file where the image is stored.

Lines 21-24 defines an instance of the XFmessageDlg wrapped class. This is used for simple dialog boxes. In this case, we use it to display warning and error messages associated with loading the image file.

Lines 26-29 test if the file is already loaded into the viewer. If so, then the viewer is made visible and the method returns.

Lines 31-33 test whether the value of pimage (the handle to the image being displayed) is set. If it is already set, then the image the handle points to is deleted.

Line 35 attempts to open the image file by sending the OpenNative message to imageFile.

Lines 36-40 test whether the image file was successfully opened. If it was not opened, then the message in the dialog box is set and the dialog box is posted by sending it the PostModal message. The method then returns if the image file could not be opened.

Line 41 closes the image file.

Lines 36-40 test whether the image file was successfully opened. If it was not opened, then the message in the dialog box is set and the dialog box is posted by sending it the PostModal message. The method then returns if the image file could not be opened.

Lines 43-44 use the new operator to construct an image object on the heap by sending the Construct message to the MMimage class.The type of file and the name of the file are sent as a list argument to the MMimage class.

Lines 45-46 set the width and height of the XFvisual on which the image is displayed.

Line 48 uses the PresentOn method to cause the image to be displayed on the visual.

Lines 49-50 set the height and width of the shell widget containing the viewer.

Lines 51-53 set the value of the currentImage, the title of the shell widget and set the visibility of the entire viewer to TRUE, making it appear on the display.

Lines 51-53 set the value of the currentImage, the title of the shell widget and set the visibility of the entire viewer to TRUE, making it appear on the display.

Lines 55-60 implement a destructor method named Destroy for the class. This method deletes the image pointed to by pimage if that value is set.

Line 61 ends the definition of the Viewer class.

5.2.2 Example Use of the Viewer Class

The table below shows a simple ADL application that makes use of the Viewer class. This application provides a text field for the user to input the name of the file to be displayed, a button labelled "Load" to load that file into a viewer, and an exit button to end the application.
Image Viewer Class Example
anonymous: XFtop

{

XFtextField fileName {x=5; y=5; height=50; width=300;};

XFbutton loadButton {x=5; y=60; height=50; width=100;

label="Load";};

XFbutton exitButton {x=115; y=60; height=50; width=100;

label="Exit";};

Viewer myViewer;

upon Construct

{

loadButton.Pressed = {'LoadViewer, self};

exitButton.Pressed = {'Exit, theApp};

}

on LoadViewer

{

{'LoadImage, fileName.text} => myViewer;

}

} myTop {height=200; width=400;};

Line 1 declares this to be an anonymous instance inheriting from the XFtop class.

Lines 2-7 declare an instance of an XFtextField and two buttons, one labelled Load and the other labelled Exit.

Line 8 declares the variable myViewer to be an instance of the Viewer class. This is used to display images.

Lines 10 -14 provide the default constructor for the application. The Construct method sets the Pressed attributes of the load and exit buttons. When the load button is pressed, the method LoadViewer is messaged.

Lines 16-19 define the method named LoadViewer. This method messages the LoadImage method of the Viewer and provides the name of the file given in the text widget as an argument.

Line 20 closes the anonymous class's definition and uses an initialization (izor) block to set the height and width of the shell that displays when the application starts.

5.2.1 - ADL Implementation of the Viewer Class
5.2.2 - Example Use of the Viewer Class

AM2 Documentation - 19 NOV 1996

Generated with Harlequin WebMaker