Popup Menu BASIC
Volume Number: 5
Issue Number: 4
Column Tag: BASIC School
Related Info: Menu Manager
Pop Up Menus in True BASIC
By Dave Kelly, MacTutor Editorial Board
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
New users of the Macintosh are sometimes fascinated with the use of pull-down
menus. Even experienced users head straight for the menu bar when they get a new
program to see just what it does. Usually the menus get checked out before the manual
does. Hierarchical menus added additional capabilities for the user. A little over a
year ago, we began seeing the appearance of pop-up menus as another interface for the
user. The various versions of BASIC cover the regular pull-down menus pretty
easily. Hierarchical menus and pop-up menus are now being used in most of the
software which is now being released. This column will attempt to explain how to set
up pop-up menus using True Basic. Similar methods for implementing may be used in
ZBasic or any other language for that matter. The method is about the same in
whatever language is used and since many of the commands come from the Macintosh
Toolbox. It should be pointed out that you must be using System 4.1 or greater for
Hierarchical or pop-up menus to work. You should upgrade to the latest version
(6.02).
It is advisable to know when it is appropriate to use pop-up menus. Apple
recommends that pop-up menus be used for setting values or choosing from lists of
related items. A pop-up menu could “pop up” anywhere (its location is global), but
usually used in a dialog. The indication that there is a pop-up menu is that there is a
box with a one-pixel thick drop shadow which is drawn around the current value.
When the user presses the mouse button which pointing within the box, the pop-up
menu appears with the current value checked and highlighted. Other than that, the
pop-up menu acts just like any other menu.
The Macintosh ROM takes care of the pop-up menu action, but you must take care
of drawing the shadowed box which indicates that there is a pop-up menu. You must
take care of inverting the title when the menu is showing. The current value should
appear in the pop-up menu when it is selected. Apple also recommends that you NOT
use Hierarchical pop-up menus. You should consider very carefully if you really need
to use a pop-up menu. Some features could better be implemented with icons or with
the normal menus in the menu bar. Don’t use commands in your pop-up menus unless
you have to. If you do use commands, you should duplicate the item in the menu bar
menus. “All commands that are in general use throughout the application should
appear in the menu bar. This assures that the most important commands are always
visible and available to the user”. (refer to “Commands in pop-up Menu” from HI
Update #13).
How it works....
Inside Macintosh Volume. V menu manager routines added one new ROM function
for doing pop-up menus. The function PopUpMenuSelect allows pop-up menus to be
handled and created anywhere on the screen. Since the pop-up menu resources are
handled the same as for other menus, they may contain color and submenus. The True
Basic demo program has a basic event loop structure which can be used as a basic
skeleton for other applications if you want. The libraries which are used come from
the True Basic Macintosh Programmer’s Kit which is essential for any serious
Macintosh program development.
To implement pop-up menus, the program uses an array to set up the menu
items. These items are declared in the PopItem$ array which is dimensioned near the
beginning of the program. SysEnvirons is called to see if the system has the pop-up
menu routines. If it doesn’t, the program quits. It would be better to display a dialog
to warn the user why the program quit. In addition to setting up the menu bar, the
menu used in the pop-up is created and stored in a resource where MyMenus$(4) is
the handle to the menu resource. After the menu resource is created, the only thing
left to do is draw the pop-up title and shadowed box then wait for an event.
The top and left corner of the pop-up are assigned at the beginning of the
Drawpopup routine so that you may place the pop-up wherever you like. Be sure to
leave room for the title. The routine figures out the width of the Title and longest menu
item and draws them at the appropriate locations. When the pop-up menu comes up,
the shadowed box should always line up with the pop-up menu. This is simple, but you
can get confused unless you realize that the shadowed box and title are drawn with local
window coordinates, but the pop-up menu location is determined by global screen
coordinates. It is necessary to convert this location from local to global to tell the
pop-up select routine where the menu should go. If the window gets moved (by
someone dragging it), remember that the global coordinate is now different, but the
local coordinate stayed the same.
You determine that a pop-up menu needs to be handled by checking to see if the
mouse was clicked within the shadowed box. The GetNextEvent function returns the
point where the click occurred. This point is compared with the PtInRect routine to
determine if the point is within the shadowed rectangle where the pop-up will appear.
If so then the program handles the pop-up event by calling the PopUpMenuSelect
routine. PopUpMenuSelect takes the menu handle, the left and top coordinates where
the menu will appear, and the currently selected menu item and handles the menu by