|
197 |
||||||||||||||
|
Chapter 14: |
||||||||||||||
|
GUI Toolbox Modules |
||||||||||||||
|
The medium is the metaphor. |
||||||||||||||
|
- Neil Postman |
||||||||||||||
|
Possibly the biggest reason people use Mac OS is for its renowned ease of |
||||||||||||||
|
The Mac OS, due in no small part to the tremendous work Apple has done in |
||||||||||||||
|
Thanks to several of MacPerl's toolbox modules, this aspect of Mac OS pro- |
||||||||||||||
|
There are volumes of information in the Inside Macintosh |
||||||||||||||
|
|
||||||||||||||
|
1 |
||||||||||||||
|
|
|||||||||||||||||
|
concepts that can be used directly, reworked into other programs, or built |
|||||||||||||||||
|
The standard toolbox module disclaimers apply: these operations should |
|||||||||||||||||
|
We recommend that you read Macintosh Human Interface Guidelines (a |
|||||||||||||||||
|
GUI programming in MacPerl often requires multiple modules for simple |
|||||||||||||||||
|
The basic building block of the window, and many other GUI elements, is |
|||||||||||||||||
|
The first coordinate set defines the top left corner of the rectangle (e.g., 50, |
|||||||||||||||||
|
50,50 |
300, 50 |
||||||||||||||||
|
|
|||||||||||||||||
|
50, 100 |
300, 100 |
||||||||||||||||
|
|
|||||||||||||||||
|
2 |
|||||||||||||||||
|
|
||||||||||||
|
The MacWindow |
||||||||||||
|
The script below will stay open until the window's close box is clicked. |
||||||||||||
|
If the window is still open when the script ends, the END |
||||||||||||
|
#!perl |
||||||||||||
|
$style = floatProc(); |
||||||||||||
|
while ($win->window()) { |
||||||||||||
|
END { |
||||||||||||
|
sub draw_it {} |
||||||||||||
|
|
||||||||||||
|
5 |
||||||||||||
|
|
||||||||||
|
Of course, this window is not very interesting. It should display items, res- |
||||||||||
|
Note that, instead of calling draw_it() |
||||||||||
|
There are many different styles of windows,8 |
||||||||||
|
Some windows have the title bar on the side (or no title bar at all), some |
||||||||||
|
There are three basic types of windows: dialog windows, standard windows, |
||||||||||
|
Also given in the tables are five characteristics of the window. A bullet (*) |
||||||||||
|
|
||||||||||
|
7 |
||||||||||
|
|
||||||||||||||||||||||||||||||||
|
If the window has a zoom box (Z), then the zoom box's behavior is noted. It |
||||||||||||||||||||||||||||||||
|
All of the windows have borders (B), using one of four types: the normal Mac |
||||||||||||||||||||||||||||||||
|
Dialog Windows |
||||||||||||||||||||||||||||||||
|
Dialog windows have no zoom, grow, or close box, and only the movable |
||||||||||||||||||||||||||||||||
|
Because these windows do not have close boxes, they must be closed in some |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
Subroutine Name |
# |
T |
D |
C |
Z |
G |
B |
|||||||||||||||||||||||||
|
dBoxProc |
1 |
- |
- |
- |
C |
|||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
|
Standard Windows |
|||||||||||||||||||||||||||
|
These windows are the standard ones that are used by most non-dialog Mac |
|||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||
|
Subroutine Name |
# |
T |
D |
C |
Z |
G |
B |
||||||||||||||||||||
documentProc 0 * * * - * N noGrowDocProc 4 * * * - - N zoomDocProc 8 * * * A * N zoomNoGrow 12 * * * A - P rBoxProc 16 * * * - - P |
|||||||||||||||||||||||||||
|
Floating Windows |
|||||||||||||||||||||||||||
|
Floating windows are used primarily for special functions (e.g., palettes, |
|||||||||||||||||||||||||||
|
|
|||||||||||||
|
dows in an application. The "side" floating windows don't have a title bar, |
|||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
Subroutine Name # T D C Z G B floatProc 1985 * * * - - N floatGrowProc 1987 * * * - * N floatZoomProc 1987 * * * B - N floatZoomGrowProc 1989 * * * B * N floatSideProc 1991 - * * - - N floatSideGrowProc 1993 - * * - * N floatSideZoomProc 1995 - * * B - N floatSideZoomGrowProc 1997 - * * B * N |
|||||||||||||
|
|
||||||||||||
|
The Mac::QuickDraw |
||||||||||||
|
Go Ahead, Draw Something |
||||||||||||
|
To draw a shape, we only need to add functionality to the draw_it() |
||||||||||||
|
sub draw_it { |
||||||||||||
|
||||||||||||
|
Whenever the redraw |
||||||||||||
|
PaintOval() |
||||||||||||
|
Not all drawing requires rectangles, though. Lines just require a start and |
||||||||||||
|
|
||||||||||||
|
can also be used to make a starting point for drawing text,11 |
||||||||||||
|
sub draw_it { |
||||||||||||
|
|
||||||||||||
|
||||||||||||
|
A Dash of Color |
||||||||||||
|
Let's put the oval back into the draw_it() |
||||||||||||
|
$win = MacWindow->new( |
||||||||||||
|
There are two color characteristics of the window that we work with: the |
||||||||||||
|
|
||||||||||||
|
11 |
||||||||||||
|
|
|||||||||||
|
Color specifications are composed of three colors - red, green, and blue - |
|||||||||||
|
sub new_color { |
|||||||||||
|
Calls to SetForeColor() |
|||||||||||
|
sub draw_it { |
|||||||||||
|
RGBForeColor( |
|||||||||||
|
|
|||||||||||
|
|||||||||||
|
|
|||||||||||
|
12 |
|||||||||||
|
|
||||||||||||||
|
Now For An Image ... |
||||||||||||||
|
Using GetPicture(), the program can import a PICT |
||||||||||||||
|
DrawPicture() |
||||||||||||||
|
Note: |
||||||||||||||
|
sub draw_it { |
||||||||||||||
|
RGBForeColor(new_color(16384, 16384, 16384)); |
||||||||||||||
|
RGBForeColor(new_color(0, 0, 0)); |
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
|
13 |
||||||||||||||
|
|
||||||||||
|
||||||||||
|
If a window always stays "in front", it will never need to be redrawn. Users |
||||||||||
|
The previous example used events |
||||||||||
|
An event handler |
||||||||||
|
Event handlers are passed two parameters; the event's object (such as our |
||||||||||
|
The event handler should return true |
||||||||||
|
sub handle_keys { |
||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
|
Modifier keys (command, option, control, shift, and caps lock) do not trigger |
||||||||||||||
|
And as you might guess, the event handler would be set beneath the similar |
||||||||||||||
|
$win->sethook('key' => \&handle_keys); |
||||||||||||||
|
Mac::Controls |
||||||||||||||
|
There are three steps to using a control. First, it must be created. Second, it |
||||||||||||||
|
There are two event handlers that can be set for a control; the primary one |
||||||||||||||
|
Push Button |
||||||||||||||
|
A push button needs no value, so those three parameters can be 0. The title |
||||||||||||||
|
$con = $win->new_control( |
||||||||||||||
|
|
||||||||||||||
|
14 |
||||||||||||||
|
|
|||||||||||
|
|
|||||||||||
|
|
|||||||||||
|
The value that is passed to hit_me() |
|||||||||||
|
sub hit_me { |
|||||||||||
|
Radio Button / Check Box |
|||||||||||
|
Radio buttons and check boxes are created in the same way, differing only |
|||||||||||
|
|
|||||||||||
|
Button and box values can be 0 (off), 1 (on), or 2 (mixed). Mixed is almost |
|||||||||||
|
$con0 = $win->new_control( |
|||||||||||
|
|
|||||||||||||
|
$con0->sethook('hit' => \&hit_off); |
|||||||||||||
|
This create a vertically-arranged pair of radio buttons. $con0 |
|||||||||||||
|
sub hit_off { |
|||||||||||||
|
Because we saved the value of which button was on in $val, we can print |
|||||||||||||
|
print "You selected: $val\n"; |
|||||||||||||
|
Scroll Bar |
|||||||||||||
|
A scroll bar has no title. It has two "direction" buttons, a position indicator, |
|||||||||||||
|
kControlUpButtonPart |
|||||||||||||
|
The value of the control is the indicator's position on the bar, ranging from |
|||||||||||||
|
The narrow dimension of the scroll bar's rectangle should be 16 pixels. The |
|||||||||||||
|
|
|||||||||||
|
$con = $win->new_control( |
|||||||||||
|
|
|||||||||||
|
By examinig the return values, we can determine whether the user hit one |
|||||||||||
|
sub hit_me { |
|||||||||||
|
Menus, supported by |
|||||||||||
|
It is important to use an ID that is not already taken by the MacPerl appli- |
|||||||||||
|
Each menu item can have several characteristics. Titles are required, as are |
|||||||||||
|
|
|||||||||||
|
$m1 = MacMenu->new(2048, 'Test Menu', ( |
|||||||||||
|
END { |
|||||||||||
|
Hierarchical Menus |
|||||||||||
|
A submenu, or hierarchical menu, can be added into a menu. A hierarchical |
|||||||||||
|
Instead of the menu object, the (sub-)menu event handler is passed the menu |
|||||||||||
|
sub menu_hit { |
|||||||||||
|
|
|||||||||||
|
15 |
|||||||||||
|
|
||||||||||
|
|
||||||||||
|
Dialog boxes are similar to windows, but normally serve different functions, |
||||||||||
|
Window items are drawn, then controls are attached via calls to new_con |
||||||||||
|
In this example,17 |
||||||||||
|
$dlg = MacDialog->new( |
||||||||||
|
|
||||||||||
|
16 |
||||||||||
|
|
||||||||||
|
|
||||||||||
|
Using some standard calls, the OK |
||||||||||
|
SetDialogCancelItem ($dlg->window(), 1); |
||||||||||
|
Hint: |
||||||||||
|
||||||||||
|
Then, of course, event handlers must be set. This dialog box needs three: one |
||||||||||
|
$dlg->item_hit(1 => \&d1); |
||||||||||
|
Because d4() |
||||||||||
|
|
||||||||||||
|
sub d4 { |
||||||||||||
|
Or, more tersely: |
||||||||||||
|
sub d4 { |
||||||||||||
|
d1() |
||||||||||||
|
sub d1 { |
||||||||||||
|
d2() |
||||||||||||
|
sub d2 { |
||||||||||||
|
If the script ends prematurely, the dialog box should be disposed of. |
||||||||||||
|
END { $dlg->dispose() if (defined($dlg)) } |
||||||||||||
|
|
|||||||||||||
|
Now all that's left is to determine the behavior of the dialog box while it |
|||||||||||||
|
There are two major types of dialog boxes, modal |
|||||||||||||
|
The while |
|||||||||||||
|
Modal: |
|||||||||||||
|
while ($dlg->window()) { |
|||||||||||||
|
Nonmodal: |
|||||||||||||
|
while ($dlg->window()) { |
|||||||||||||
|
There is much about the GUI toolbox modules that we did not cover. By now, |
|||||||||||||
|
Put together your own examples and play around. The point of providing all |
|||||||||||||
|
|
|||||||||||||
|
19 |
|||||||||||||
Copyright © 1997-1998 by Prime Time Freeware. All Rights Reserved.