How to Create Pop-Up Menus in Macintosh QuickBASIC (47782)



The information in this article applies to:

  • Microsoft QuickBASIC Compiler for the Apple Macintosh 1.0

This article was previously published under Q47782

SUMMARY

This article describes how to call ToolBox and ToolBoxNEWTOOL routines to define pop-up/pull-down menus anywhere on the screen (unlike normal menus, which appear only in the menu bar at the top of the screen).

One limitation to QuickBASIC is that the menu items defined with this technique can be selected only with the mouse; COMMAND+key equivalents (such as those defined with the CmdKey MBLC routine) do not function.

This information applies to Microsoft QuickBASIC Version 1.00 for the Apple Macintosh.

MORE INFORMATION

The easiest way to define and use menus in the QuickBASIC language is through the MENU statement, MENU() function, MENU ON/OFF/STOP statements, and ON MENU GOSUB statement.

However, advanced programmers can also define pop-up menus through the use of ToolBox calls within Microsoft QuickBASIC Version 1.00 for the Apple Macintosh. The menu definitions can either be created within the program or be stored as a resource.

The sample code below illustrates the creation of a pop-up menu. The program requires the use of various ToolBox calls along with some ToolBoxNEWTOOL calls. The ToolBoxNEWTOOL MBLC routine is a modified version of the ToolBox MBLC routine in QuickBASIC and must be created as described in a separate article. To search for that article, query on the following word:

ToolBoxNEWTOOL

The ToolBoxNEWTOOL MBLC should be added as a resource to Microsoft QuickBASIC (b) or (d) so it is available for the programs running in the environment. ToolBoxNEWTOOL can then be added to a compiled application by selecting the "Include MBPCs & MBLCs" option when compiling. This option pastes into the compiled application all MBPC and MBLC resources stored within the source program and QuickBASIC itself. The "Extra MBLC Remover" sample program can then be used to reduce the size of the file by removing unnecessary MBLC resources from the application.

Either ResEdit or the "Statement Mover" sample program can be used to add the ToolBoxNEWTOOL MBLC resource to the source program, to the compiled application, or to QuickBASIC itself.

Detecting which menu option was selected must be done by polling the mouse position with a QuickBASIC statement, such as GetMouse, instead of invoking a system event-trapping routine such as GetNextEvent. QuickBASIC does not allow programs to use the Macintosh ROM routine GetNextEvent because QuickBASIC traps all events and empties out the event queue before it allows a ToolBox GetNextEvent call to act upon them. GetNextEvent always returns a value of FALSE (zero) because the event queue is cleared by QuickBASIC.

The code example below makes use of the following ToolBox routines: InsertMenu, PopUpMenuSelect, NewMenu, and AppendMenu. The PopUpMenu SUBprogram handles drawing the user-created menu at specified (x,y) coordinates on the screen and also returns the number of the option the user selected. Then, that value can be acted upon with a simple SELECT CASE structure. A MENUID of -1 tells InsertMenu that the subsequent menu is either a pop-up menu or a hierarchical menu.

(By calling ToolBoxNEWTOOL, you may also be able to define "hierarchical menus," where selecting a menu item pops up a submenu that branches off to the side. Microsoft has not tested or developed any QuickBASIC program examples that perform hierarchical menus, but you can try using the techniques in the program in this article as a base for attempting this.)

Code Example

' ******************************
' POP-UP MENUS FOR THE MACINTOSH
' WRITTEN IN QUICKBASIC VERSION 1.00
' AUGUST, 1989
' ******************************
DIM SHARED MenuRect%(3)              ' Target box coordinates
DIM SHARED Border%(3)                ' Screen border coordinates
DIM SHARED Pt%(1)                    ' Screen coordinates for menu
DIM SHARED Mpt%(1)
DIM SHARED MH&, MenuID%, X%, Y%      ' Misc. variables
' ******************************
MenuID% = 200                        ' Menu ID number, cannot be
                                     ' negative
MH& = 0                              ' Initialize menu handle
True = -1                            ' Initialize Boolean variable
ToolBox "I"                          ' ToolBox initialization routines
ToolBoxNEWTOOL "I"
X% = 200                        ' Set initial pop-up menu coordinates
Y% = 100
' ******************************
BuildTargetBox
BuildUserMenus                   ' Build the user's menus
WHILE True                       ' Loop while True
  GetMousePosition Bool%         ' Determine mouse position
  IF Bool% THEN                  ' If in target box, continue
    PopUpMenu Result&            ' Pop up user's menu if mouse is down
    Choice% =Result& AND &HFF&   ' Determine which choice they
                                 ' selected
    SELECT CASE Choice%          ' Select the appropriate routine
      CASE 1                     ' Print Hello 10 times
        Times% = 10
        Col = 30
        PrintHello Times%,Col
      CASE 2                         ' Print Hello 20 times
        Times% = 20
        Col = 26
        PrintHello Times%,Col
      CASE 3                         ' Print Hello 30 times
        Times%=30
        Col = 23
        PrintHello Times%,Col
      CASE 4                         ' Print Hello 40 times
        Times%= 40
        Col = 20
        PrintHello Times%,Col
      CASE 6                         ' Change target box location
        CLS
        ChangeCoordinates            ' Get new coordinates
      CASE 8                         ' Quit
        DoEnd
      CASE ELSE           ' CASE ELSE executes if mouse didn't select
    END SELECT            ' an item.
  END IF
WEND                                 ' End While Loop
' ******************************
SUB PopUpMenu (Result&) STATIC
  ' *** Draw the pop up menu and determine the selected choice
  PopUpTrap& = &HA80B
  InsertMenuTrap& = &HA935
  Result& = 0
  Item% = 0
  BeforeID% = -1                    ' Signifies pop-up menu to
  Pt%(0) = MenuRect%(0)             ' InsertMenu routine
  Pt%(1) = MenuRect%(1)
  LocalToGlobal Pt%(0)
  ToolBoxNEWTOOL "PQ",InsertMenuTrap&,(MH&),(BeforeID%)
  ToolBoxNEWTOOL "LQ",PopUpTrap&,(MH&),(Pt%(0)),(Pt%(1)),(Item%),Result&
END SUB
' ******************************
SUB ChangeCoordinates STATIC
  ' *** Let the user determine screen locations for pop-up menu
  MaxX% = SYSTEM(5) - 110
  MaxY% = SYSTEM(6) - 100
  LOCATE 8,17: PRINT "Enter X Value ( 0 < X <"; MaxX%;") "; : INPUT X%
  LOCATE 10,17:PRINT "Enter Y Value ( 0 < Y <"; MaxY%;") "; : INPUT Y%
  BuildTargetBox
END SUB
' ******************************
SUB PrintHello (Times%, Col) STATIC
  ' *** Print Hello x many times
  CLS
  Row = 3
  FOR i = 1 TO Times%
    IF Row > 12 THEN
      Row = 3
      Col = Col + 7
    END IF
    LOCATE Row,Col : PRINT "Hello"
    Row = Row + 1
  NEXT i
  LOCATE 18,20 : PRINT "Hit the Mouse Button To Continue";
  WHILE MOUSE(0) <> 1 : WEND
  BuildTargetBox
END SUB
' ******************************
SUB BuildTargetBox STATIC
  ' *** Put Up Target Box
  MenuRect% (0) = Y%
  MenuRect% (1) = X%
  MenuRect% (2) = Y% + 20
  MenuRect% (3) = X% + 78
  CLS
  CALL FRAMERECT(VARPTR(MenuRect%(0)))
  TEXTFACE 1
  MOVETO X%+2,Y% +15
  PRINT "Click Here";
  TEXTFACE 0
END SUB
' ******************************
SUB BuildUserMenus STATIC
  ' *** Build user's menu
  AddResMenuTrap& = &HA94D
  NewMenuTrap&  = &HA931
  AppendMenuTrap& = &HA933
  ClearMenuTrap& = &HA934
  DrawMenuTrap& = &HA937
  ToolBox "PQ",ClearMenuTrap&          ' Clear top menu bar
  ToolBox "PQ",DrawMenuTrap&           ' Draw empty menu bar
  ToolBox "LQ",NewMenuTrap&, (MenuID%),("Hello"),MH&
  ' You may change the face of menu items as follows:
  ' <I means Italic item.    <B means Bold item.
  ' <O means Outlined item.  <S means Outlined and Bold
  temp$="Hello 10 Times;Hello 20 Times<I"
  ToolBox "PQ",AppendMenuTrap&,(MH&),(temp$)
  temp2$="Hello 30 Times< U;Hello 40 Times<B"
  ToolBox "PQ",AppendMenuTrap&,(MH&),(temp2$)
  ToolBox "PQ",AppendMenuTrap&,(MH&),("(--------------;Change X,Y <O")
  ToolBox "PQ",AppendMenuTrap&,(MH&),("(--------------;Quit<S")
  ' *** This line can be added to include the Apple menu options:
  ' ToolBoxNEWTOOL "PQ",AddResMenuTrap&,(MH&),(&H44525652&)
  ' *** This line is a translation of the previous line:
  ' ToolBoxNEWTOOL "PQ",AddResMenuTrap&,(MH&),("DRVR")
END SUB
' ******************************
SUB GetMousePosition (Bool%) STATIC
  ' *** Determine mouse position, then check to see if it
  ' *** is in the target box
  Bool%  = 0
  GetMouse mpt%(0)
  PtInRect mpt%(0),MenuRect%(0),Bool%
END SUB
' ******************************
SUB DoEnd STATIC
  ' Clear our menu handle and reset menus, then end
  IF MH& <> 0 THEN DisposeHandle MH&
  MENU RESET
  END
END SUB
' ******************************
				

Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB47782