TweetFollow Us on Twitter

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 displaying the menu then returns the item number of the menu item that was selected. That item is then redrawn into the shadowed box and becomes the newly selected item. The program should then handle whatever action or flag the menu item should do.

This process is simple enough after seeing a few examples. The main problem with pop-up menus as with other controls that you may want to use is that you must be careful not to clutter up the window too much. The graphic design of the interface is just as important as the code that is written. That’s why there will always be a demand for good programs even though HyperCard and other tools make developing programs easier.

By the way, some of the programming in True Basic can be simplified by using the TrueWindows Library. The problem comes when you want to create an application using the Runtime package. The Binder utility asks for the compiled libraries and then the compiled program and outputs them to an application that contains the True Basic interpreter. If you use libraries that have calls or functions with the same name, they may work fine until you bind them together into an application with Binder. Binder gives an error when putting the libraries together which says that you have more than one call with the same name. This could happen if you are using TrueWindows and the other Macintosh Programmer’s Kit routines (like the ones used in this program). You may be able to get around this by going to the source code files and removing the duplicate name and recompiling the library. Be sure to keep it separate from the original so that they don’t get mixed up or someday you may need a routine that you deleted and wonder why things don’t work.

I set up my copy of the True Basic runtime application with my own icon and when I create an application, the information is already set up provided that I want to use the same one each time I make a new application. Usually you will want them to be different. I use the Prof. Mac icon for any applications that I make for MacTutor.

! PopUp Menu Demo
! True Basic version 2.01
! Requires True Basic Macintosh Developer’s ToolKit Libraries
! by Dave Kelly
! ©1989 MacTutor

REM Open up libraries
LIBRARY “MenuLib*”                ! Menu Manager
LIBRARY “WindowLib*”              ! Window Manager
LIBRARY “DeskLib*”                ! Desk Manager
LIBRARY “EventLib*”               ! Event Manager
LIBRARY “QuickLib*”               ! Quickdraw
LIBRARY “DataLib*”                ! Desk Acc and system calls
LIBRARY “MacLib*”                 ! True Basic event control
LIBRARY “System*”                 ! System Calls

REM following variables are used globally throughout program
DECLARE DEF NIL$, POINTER$,screenBits$,bounds$,top,left,bottom,right,TopLeft$,H,V
DECLARE DEF OpenDeskAcc,NewWindow$,SystemEdit
DECLARE DEF MenuSelect,PtInRect,TrackGoAway

DIM MyMenus$(1:4)
DIM PopItem$(1:3)
CALL SysEnvirons(sysEnvRec$,status)    ! Get current system revision
CALL UnpackEnvirons(sysEnvRec$,envversion, machine,sysversion,processor, 
hasFPU,hasColorQD,keyboardtype,atversion, sysvrefnum)
IF sysversion=0 then              ! Do we have the right ROM?
   STOP
END IF
CALL TakeMac   ! turn off True Basic and let the program do its own thing
LET everyevent=-1                 ! event mask for all events
LET doneFlag=0                    ! this flag is set when program ending 
has been selected.
LET z$=bounds$(screenBits$)       ! Get the size of the screen.
CALL setrect(r$,left(z$)+4,top(z$)+44,right(z$)-4,bottom(z$)-4)
CALL setrect(dragrect$,4,24,right(z$)-4,bottom(z$)-4)
LET myWindow$=NewWindow$(NIL$,r$,”Sample”,1,0,POINTER$(-1),1,0)      

! Create a window
CALL SetPort(myWindow$)           ! Access the new window
CALL SetUpMenus                   ! Turn on menus
CALL Drawwindow                   ! Set up window info

! Main Event Loop

DO
   CALL SystemTask                ! Handle System tasks/DAs
   CALL GetNextEvent(everyevent,theEvent$,eResult)    
! check for events
   IF eResult<>0 then    ! if no event error occurred then...
      CALL UnpackEvent(theEvent$,what,mess,when,where$,mod)
      SELECT CASE what 
! what represents the kind of event that occurred.
      CASE 1                      ! mouse down event occurred
           CALL FindWindow(where$,whichWindow$,wResult)
           SELECT CASE wResult
           CASE 1         ! Event was in the menu bar
                LET mResult=MenuSelect(where$)
                CALL DoMenu(mResult)
           CASE 2         ! Event was in a system window
                CALL SystemClick(theEvent$,whichWindow$)   
 ! Pass the event to the system
           CASE 3   ! Event was in content region of a window
                CALL GlobalToLocal(where$)  
 ! convert coordinates for the window
                IF PtInRect(where$,PopRect$)=1 THEN   
 ! see if popup was selected
                   CALL PopUpEvent     ! if so, handle the popup event
                END IF
           CASE 4   ! Event in the window’s drag region
                CALL DragWindow(whichWindow$,where$,dragrect$)
           CASE 6   ! Event in go-away region of active window
                LET doneFlag=TrackGoAway(whichWindow$,where$)
           CASE else
           END SELECT
      CASE 6                      ! update event occurred
           CALL Packb(w$,1,32,mess)
           CALL BeginUpdate(w$)
           CALL Drawwindow
           CALL DrawPopUp
           CALL EndUpdate(w$)
      CASE else                   ! anything else?
      END SELECT
   END IF
LOOP until doneFlag<>0

CALL DisposeWindow(myWindow$)     ! Throw away window handle
CALL ClearMenuBar                 ! Clear Menus
FOR i=Lbound(MyMenus$) to Ubound(MyMenus$)
    CALL DisposeMenu(MyMenus$(i))
NEXT i
CALL GiveMac                      ! Return control back to True Basic
STOP                              ! End the program

SUB DrawWindow                    ! Draw message in window
    CALL textfont(2)              ! Set font to New York font
    CALL textsize(12)             ! Set size to 12 point
    CALL textface(1)              ! Set text to bold
    CALL textmode(0)              ! Set to copy mode
    CALL moveto(10,20)
    CALL DrawString(“True BASIC Version 2.0 PopUp Menu demo”)
    CALL textface(0)              ! Set text to plain
END SUB

SUB DoMenu(code)                  ! handle Menu events
    CALL Packb(s$,1,32,code)
    LET MenuNumber=Unpackb(s$,1,-16)
    LET Menuitem = Unpackb(s$,17,-16)
    SELECT CASE MenuNumber
    CASE 1                        ! Apple Menu
         CALL GetItem(MyMenus$(1),MenuItem,name$)
         LET mrefNum=OpenDeskAcc(name$)
         CALL SetPort(mywindow$)
    CASE 2                        ! File Menu
         LET doneFlag=-1
    CASE 3                        ! Edit Menu
         LET z=SystemEdit(Menuitem+1)
    CASE else
    END SELECT
    CALL HiliteMenu(0)
END SUB

SUB SetUpMenus
    DECLARE DEF NewMenu$,StringWidth ! Declare variables used
    DECLARE DEF GetFontInfo$      ! in toolbox functions

    LET MyMenus$(1)=NewMenu$(1,chr$(20))  ! The first menu is 
    CALL AddResMenu(MyMenus$(1),”DRVR”)   ! Apple menu.
    LET MyMenus$(2)=NewMenu$(2,”File”)   ! File menu is second
    CALL AppendMenu(MyMenus$(2),”Quit”)
    LET MyMenus$(3)=NewMenu$(3,”Edit”)   ! Next the Edit menu
    CALL AppendMenu(MyMenus$(3),”Cut”)
    CALL AppendMenu(MyMenus$(3),”Copy”)
    CALL AppendMenu(MyMenus$(3),”Paste”)
    LET PopTitle$=”PopUp Menu Title:  “   ! Save pop up title
    LET MyMenus$(4)=NewMenu$(4,PopTitle$) ! Create pop up menu
    LET Popitem$(1)=”Item 1"
    LET Popitem$(2)=”Item 2"
    LET Popitem$(3)=”Item 3"
    LET NoOfPopItems=3
    LET PopItem=1
    FOR i=1 to 3
        CALL AppendMenu(MyMenus$(4),Popitem$(i))  ! Add popup items
    NEXT i
    FOR i=lbound(MyMenus$) to Ubound(MyMenus$)-1  ! put the menus into
        CALL insertMenu(MyMenus$(i),0)      ! the menu bar
    NEXT i
    CALL InsertMenu(MyMenus$(4),-1)    ! Add pop up menu
    CALL CheckItem(MyMenus$(4),PopItem,1)   ! check default item

    REM Get maximum length of PopUp items
    CALL TextFont(0)              ! Set to system font
    CALL TextSize(12)             ! Set to 12 point size
    CALL GetFontInfo(FontInfo$)
    LET ascent = Unpackb(fontinfo$,1,-16)
    LET descent = Unpackb(fontinfo$,17,-16)
    LET widMax = Unpackb(fontinfo$,33,-16)
    LET leading = Unpackb(fontinfo$,49,-16)
    LET MaxItemLength=0

    FOR i=1 to NoOfPopItems
        LET strwidth=StringWidth(Popitem$(i))
        IF StrWidth>MaxItemLength then LET MaxItemLength=StrWidth
    NEXT i

    CALL DrawPopUp
    CALL DrawMenuBar
END SUB

SUB DrawPopUp
    CALL TextFont(0)       ! Set Font to Chicago (System)
    CALL TextSize(12)             ! Set Size to 12 point
    LET Popuptop=100              ! Top of Popup menu
    LET Popupleft=200             ! Left of Popup menu
    CALL SetRect(PopRect$, Popupleft,Popuptop, Popupleft+5+MaxItemLength+13, 
Popuptop+ascent+ descent+ leading+1)
    CALL FrameRect(PopRect$)   ! Draw currently selected item
    CALL MoveTo(Right(PopRect$),Top(PopRect$)+1)
    CALL LineTo(Right(PopRect$),Bottom(PopRect$))
    CALL MoveTo(Left(PopRect$)+1,Bottom(PopRect$))
    CALL LineTo(Right(PopRect$),Bottom(PopRect$))
    LET StrWidth=StringWidth(PopTitle$)
    LET xlocation=Left(PopRect$)-StrWidth
    LET ylocation=(Top(PopRect$)+Bottom(PopRect$))/2+(ascent-descent)/2
    CALL MoveTo(xlocation,ylocation)
    CALL Drawstring(PopTitle$)    ! Draw the Popup menu title
    CALL SetRect(InvertTitleRect$,xlocation-8,Top(PopRect$)+1,Left(PopRect$),Bottom(PopRect$))
    LET xlocation=Left(PopRect$)+13
    CALL MoveTo(xlocation,ylocation)
    CALL Drawstring(PopItem$(PopItem))      
 ! Draw the currently selected item
END SUB

SUB PopUpEvent
    DECLARE DEF PopUpMenuSelect   ! Declare function
    CALL InvertRect(InvertTitleRect$)  ! invert popup title
    LET TempPoint$=TopLeft$(PopRect$)
    CALL LocalToGlobal(TempPoint$)   ! Change to global coords
    LET PopTop=V(TempPoint$)+1
    LET PopLeft=H(TempPoint$)+1
    LET Result=PopUpMenuSelect(MyMenus$(4),PopTop,PopLeft,PopItem)
   ! Do the Popup
    CALL Packb(s$,1,32,Result)    ! Get the menu result
    LET MenuNumber=Unpackb(s$,1,-16) 
  ! Ignore Menunumber, we know which menu this is
    LET Menuitem = Unpackb(s$,17,-16)  ! Get the menu item

    IF MenuItem=PopItem THEN
       CALL InvertRect(InvertTitleRect$) 
    ! Invert the title to normal if old item selected
    ELSE
       CALL CheckItem(MyMenus$(4),PopItem,0) ! uncheck last item
       CALL CheckItem(MyMenus$(4),MenuItem,1) ! check new item
       CALL EraseRect(PopRect$)   ! Draw the current menu item
       CALL FrameRect(PopRect$)
       CALL MoveTo(xlocation,ylocation)
       CALL Drawstring(PopItem$(PopItem))
       CALL InvertRect(InvertTitleRect$)
       SELECT CASE MenuItem       ! Handle menu event
       CASE 1
            REM Do Item 1
            LET PopItem=MenuItem
       CASE 2
            REM Do Item 2
            LET PopItem=MenuItem
       CASE 3
            REM Do Item 3
            LET PopItem=MenuItem
       CASE ELSE
       END SELECT
       CALL MoveTo(xlocation,ylocation)
       CALL TextFont(0)    ! Set font to Chicago (System)
       CALL Drawstring(PopItem$(PopItem))   ! draw selected popup item
    END IF
END SUB
END

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.