Previous | Table of Contents | Next |
6.2.7.5. Image Files
Image files can be read into a window. Similarly, a rectangular portion of a window can be saved as an image file. Icon supports GIF, the CompuServe Graphics Interchange Format (Murray & vanRyper, 1994). Additional image file formats are supported on some platforms.
An image can be loaded into a window when it is opened by using the image attribute with the name of an image file. The size of the window is set automatically to the size of the image. An example is
WOpen(image=fire_engine.gif)
which opens a window using the GIF file fire_engine.gif. The result might look like this:
6.2.7.6. User Interaction
A user can interact with a program through a window at a low level using the keyboard and mouse or at a high level using dialogs and interface tools such as menus and buttons.
Low-Level Interaction
Pressing a mouse button or typing a character with the mouse cursor in the window produces an event that the program can detect. The program can determine what the user action was and where in the window it occurred. User events are queued until the program requests them.
The function Event() produces the next event in the queue. For example,
repeat { case Event() of { o : draw_outline() f : draw_fill() q : break } }
is an event loop that performs different operations depending on the characters a user types: o causes draw_outline() to be called, f causes draw_fill() to be called, and a q terminates the loop. All other events are ignored.
A three-button mouse is standard, with left, middle, and right buttons that can be pressed and released, each of which produces an event. If the mouse is moved while a button is pressed, a drag event is produced. These events are represented by keywords:
&rpress | right button press |
&mpress | middle button press |
&lpress | left button press |
&rdrag | right button drag |
&mdrag | middle button drag |
&ldrag | left button drag |
&rrelease | right button release |
&mrelease | middle button release |
&lrelease | left button release |
When an event is processed, the position in the window where the event occurred is automatically assigned to the keywords &x and &y.
An example of using the mouse coordinates when a mouse event occurs is the following event loop, which draws lines between points at which the left mouse button is pressed:
repeat case Event() of { &lpress: { DrawLine(\x, \y, &x, &y) x := &x y := &y } e: EraseArea() q: break }
If e is entered, the window is erased. If q is entered, the loop terminates. The non-null test prevents an attempt to draw a line the first time the left mouse button is pressed. An example produced by the event loop is shown here:
Dialogs
A dialog is a temporary window that a program can produce to provide information to the user or get information from the user. The notice dialog informs the user of a situation such as an error and requires the user to acknowledge the notice. The function Notice(s) produces a dialog with the message s. For example,
Notice(Launch aborted!)
produces the following dialog window:
The dialog window remains open until the user dismisses it by clicking on the Okay button. Other kinds of dialogs are provided for common situations, such as selecting one of several alternatives, as in
SelectDialog(Pick a color:, [red, blue, green])
which produces the following dialog:
Other forms of dialogs allow the user to enter text in fields, turn switches on or off, and select colors interactively.
Visual Interfaces
A visual interface provides for user interaction using tools such as menus, buttons, and sliders.
Icon provides several kinds of interface tools:
In addition, text and lines can be used to decorate an interface.
This is the visual interface for an application that performs symmetric drawing:
The user draws in the shaded area, called the generating region. What is drawn is reflected by mirrors, indicated by the straight lines, to produce the sunflower symmetry, whose technical designation is p4m.
The construction of programs with visual interfaces is described in section 6.3.4.
Previous | Table of Contents | Next |