Events in ZBASIC
Volume Number: 3
Issue Number: 9
Column Tag: BASIC School
Mixing Event Manager Calls with ZBASIC 
By Dave Kelly, MacTutor Editorial Board
Toolbox Event Manager from ZBASIC
Let’s explore ZBASIC from the perspective of the toolbox. At this point it should
be observed that although ZBasic (version 4.0) works, it does not answer a few of our
needs. Perhaps we can encourage Zedcor to add a few missing features which I will
explain here.
As most of you know, programming the Macintosh in most languages involves
heavy use of the toolbox routines. You might say that programming in Basic makes you
soft. That’s why so many programmers move on to other languages like Pascal or C. It
is easy to be lazy when the implementation of the language does most of the complex
programming for you. Actually, once you know what to do, the complex stuff doesn’t
seem so bad. However, because ZBasic provides statements which do a set of toolbox
functions for you, your time is free to work on the organization problems of your
program.
DOS versus Mac OS
In a recent discussion on the merits ( or demerits) of MS/DOS with a co-worker,
my friend remarked that he thought that MS/DOS had more flexibility than the Mac
operating system because the programmer could manipulate files with “powerful”
statements (that he had to remember long enough to type into the computer in the
correct syntax, I might add). Of course, we (as Mac programmers) all know that the
power of the Mac goes beyond “powerful” statements to copy files. The Mac user
interface allows “the rest of us” to cut through the mush and get to the heart of the
machine. I wonder how much time has been wasted by MS/DOS users as they wade
through their manual to figure out how to do something as simple as copying a file.
Their time could have been used more usefully. After all, computers are supposed to
make life easier, right?
Hard to Program?
The only reason I mention all this is that I feel that the Mac has taken a lot of flak
about begin hard to program. Well, some have found it very difficult to program,
e specially when they don’t follow standard guidelines and try to write with the “look
and feel” of MS/DOS on the Mac! ZBasic is different. If you want to program with
toolbox calls exclusively, you can still do it. Or, if using ZBasic statements (like
MENU or WINDOW) would make it easier, then use them. Maybe now that ZBasic is
here the rest of the world out there might notice that it isn’t so hard to program the
Mac after all.
Fig. 1 IM Sample Program in ZBasic
Some Things Require Direct Intervention
There are a few things that ZBasic does not yet support. A disk insertion event is
one of them. ZBasic does mount and unmount volumes properly, but to sense the
insertion of a disk requires us to create our own GETNEXTEVENT loop. The problem
with this comes with the fact that if you use your own GETNEXTEVENT loop you will not
be able to do any ON DIALOG or ON MENU etc. type event trapping. You will be trapping
events yourself the same way that Pascal or C programmers do it. ZBasic inserts a
GETNEXTEVENT statement at the beginning of every line between the ON and OFF
statements (such as MENU ON or DIALOG ON). If you insert your own GETNEXTEVENT
you will lose some events to the ZBasic GETNEXTEVENT statements. There may be
times when you need to have precise control over where the GETNEXTEVENT is placed.
Fortunately, you can do this (yes, it can and may be done), but it requires you to
abandon some of the built-in capability and rely on the toolbox directly; i.e., become a
“real” Mac programmer! In some ways this is better anyway. And you may still mix
ZBasic statements (any that don’t trap events) along with the toolbox statements.
This month I have adapted the Sample program found at the beginning of Inside
Macintosh vol 1 into a working ZBasic toolbox program. This program has revealed
some interesting things about ZBasic which should be noted.
Event Manager and ZBasic
The Macintosh is event-driven. That is, the application decides what to do from
moment to moment by requesting information from the Event Manager portion of the
toolbox ROM through the GETNEXTEVENT statement and then responding to each event
one by one in an appropriate way.
Most events are held in a queue called the event queue. The event queue normally
has a capacity of 20 events. The event queue is a FIFO (first-in-first-out) buffer
which holds events until they can be read from the queue.
There are several different types of events which the Event Manager will keep
track of. The most important of these are
• Mouse-down, Mouse-up events;
• Key-down, Key-up events including Auto-key events which occur when the user
holds down a repeating key;
• Disk inserted events;
Other important events include update and activate events which involve events
which concern the Window Manager. The Event Manager always returns the
highest-priority event available of the requested types.
First thing to mention is the method for setting up records of various kinds found
in Inside Macintosh. The procedure is fairly painless. For example, the event record
must be defined. In Pascal the event record is defined as:
TYPE EventRecord = RECORD
what: INTEGER; {event code}
message: LONGINT; {event message}
when: LONGINT; {ticks since startup}
where: Point; {mouse location}
modifiers: INTEGER; {modifier flags}
END;
Remember that Pascal integer types are 2 bytes, and longint types are 4 bytes.
By declaring ZBasic’s static variables, the location of the record will be preserved
where we will always know where to find it. Therefore we define the event record in
ZBasic as follows:
myEvent%=0 ‘event code
message&=0 ‘event message
when&=0 ‘ticks since startup
where&=0 ‘mouse location
modifiers=0 ‘modifier flags
The ‘what’ variable is the same location as myEvent% as shown by the Pascal
record. The other variables of the record follow sequentially in memory. That is, the
next variable declared is statically defined at the next memory location. The only thing
left to do is use the GETNEXTEVENT statement to see what events happen. Of course it
may be harder to decide what you want to happen when the event occurs. This is where
the ZBasic built-in automatic statements would have been nice to use. When
GETNEXTEVENT is called, the event record will then contain the desired information
regarding what, when, where and other pertinent information. Unwanted events may
be screened out if desired. Maybe you only want to know when a key is pressed and
don’t care about any other events.
This quick overview will never be a substitute for your own experience, but I
will attempt to explain some of the ‘features’ of the Sample program.
Mixing Event Manager Calls with ZBasic
The typical ZBasic program for the Macintosh should start out with the WINDOW
OFF, COORDINATE WINDOW statements and if the mouse is used any place you will
probably want to use the DEF MOUSE=-1 statement. These statements turn off the
default window, set the coordinate system to pixel coordinates and set up the mouse to
read in a more Macintosh like mode.
The charCodeMask and keyCodeMask variables are masks we may use to mask the