JanetTerra
Sep 24, 2006
==Menus: The Liberty BASIC Popup Menu==
[[toc]]
||[[image:PopLB.png]]||Unlike the **[[menus01|Menu Bar]]** which is appended to the window caption, the popup menu coordinates are dependent upon the cursor coordinates. The x, y coordinates of the mouse cursor become the x, y coordinates of the upper left corner of the popup menu. Another name for the popup menu is the **[[http://en.wikipedia.org/wiki/Context_menu|context menu.]]**||
===The POPUPMENU Command===
The command is **{{POPUPMENU}}**. **{{POPUPMENU}}** is //case``-``insensitive//, meaning you can write **{{POPUPMENU}}** or **{{popupmenu}}** or **{{Popupmenu}}** or any other combination of upper and lower case letters. The code for popup windows comes after the window is opened. In fact, a popup menu is not dependent upon a window being opened at all. The **{{POPUPMENU}}** command is followed by the listed item and place of execution.
[[code format="vb"]]
INPUT "See a popup menu? ";yn$
IF INSTR(UPPER$(yn$), "Y") > 0 THEN
POPUPMENU "Selection 1", [sel1], "Selection 2", [sel2]
END IF
END
[sel1]
NOTICE "Selection 1";Chr$(13);"You chose the first item."
END
[sel2]
NOTICE "Selection 2";Chr$(13);"You chose the second item."
END
[[code]]
**{{POPUPMENU}}**, the command
**{{"Selection 1"}}**, the first listing of the popup menu
**{{[sel1]}}**, the block of code to be executed if Selection 1 is clicked
**{{"Selection 2"}}**, the second listing of the popup menu
**{{[sel2]}}**, the block of code to be executed if Selection 2 is clicked
The advantage of a context menu is that the menu is brought to the mouse cursor, eliminating the need for the mouse to travel to the stationary menu bar. This is especially helpful in a program that traps mouseclicks. (For more information on trapping mouse events, see **[[http://lbpe.wikispaces.com/Tutorial+WhenMouse|Trapping Mouse Actions and the When Commands.]]**)
[[code format="vb"]]
WindowWidth = 400
WindowHeight = 300
GRAPHICBOX #main.gb, 0, 0, 400, 300
OPEN "SpiroDot" for Window as #main
PRINT #main.gb, "Trapclose [EndProgram]"
PRINT #main.gb, "Down; Fill Lightgray; Flush"
PRINT #main.gb, "When leftButtonUp [SelColor]"
WAIT
[SelColor]
xVar = MouseX
yVar = MouseY
POPUPMENU "Red", [col1], "Blue", [col2], "Yellow", [col3]
WAIT
[col1]
PRINT #main.gb, "Color Red; Backcolor Red"
PRINT #main.gb, "Place ";xVar;" ";yVar
PRINT #main.gb, "Circlefilled 10"
PRINT #main.gb, "Flush"
WAIT
[col2]
PRINT #main.gb, "Color Blue; Backcolor Blue"
PRINT #main.gb, "Place ";xVar;" ";yVar
PRINT #main.gb, "Circlefilled 10"
PRINT #main.gb, "Flush"
WAIT
[col3]
PRINT #main.gb, "Color Yellow; Backcolor Yellow"
PRINT #main.gb, "Place ";xVar;" ";yVar
PRINT #main.gb, "Circlefilled 10"
PRINT #main.gb, "Flush"
WAIT
[EndProgram]
CLOSE #main
END
[[code]]
||Popup menus can be used to confirm user action. Again, the advantage is the proximity of the popup menu to the mouse cursor.||[[image:PopQuit.png]]||
===Menu Listings===
[[code format="vb"]]
POPUPMENU "1st Listed Item", [BranchTo1], "2nd Listed Item", [BranchTo2], "3rd Listed Item", _
[BranchTo3], "4th Listed Item", [BranchTo4], "5th Listed Item", [Branchto5], "6th Listed Item", _
[BranchTo6], "7th Listed Item", [BranchTo7], "8th Listed Item", [BranchTo8], "9th Listed Item", _
[BranchTo9], "10th Listed Item", [BranchTo10]
[[code]]
||There is really no limit to the number of listed items a popup menu can contain. If there are many listings, it may be advisable to use Liberty BASIC's //line continuation character//, which is the underscore, to break up the listing onto multiple lines.||[[image:PopLong.png]]||
===Menu Separators===
[[code format="vb"]]
POPUPMENU "New", [newFile], "Load", [loadFile], "Save", [saveFile],|, "Exit", [EndProgram]
[[code]]
||Similar menu items can be grouped by inserting a menu separator (horizontal) line. Instead of a listing and its associated branch label, insert a pipe {{**``|``**}}.||[[image:PopSep.png]]||
===Accelerator or 'Hot Key' Selection===
||[[image:PopHot.png]]||The ampersand ({{**&**}}) key is a special key when added to any menu item. This allows menu selection by keypress rather than mouse click. The associated alphanumeric key is case``-``insensitive. Any alphanumeric key can be combined with the {{**&**}} key for menu selection. The {{**&**}} character doesn't appear on the popupmenu. Often the {{**&**}} precedes the first character in the menu title or item, but that isn't always the case. {{**E&xit**}}, or {{**X**}}, is frequently used as a menu item to close a window and quit an application. Without the addition of the ampersand ({{**&**}}) key, only the first letter acts as the 'Hot Key.'||
===Branch Events and Subs===
Menu selection directs the code execution to the area specified by the branch label. The SpiroDot program uses branch labels to direct code execution. The **{{POPUPMENU}}** statement must be followed by a **{{WAIT}}** statement to prevent the code from //falling through//.
[[code format="vb"]]
WindowWidth = 400
WindowHeight = 300
GRAPHICBOX #main.gb, 0, 0, 400, 300
OPEN "SpiroDot" for Window as #main
PRINT #main.gb, "Trapclose [EndProgram]"
PRINT #main.gb, "Down; Fill Lightgray; Flush"
PRINT #main.gb, "When leftButtonUp [SelColor]"
WAIT
[SelColor]
xVar = MouseX
yVar = MouseY
POPUPMENU "Red", [col1], "Blue", [col2], "Yellow", [col3]
WAIT
[col1]
PRINT #main.gb, "Color Red; Backcolor Red"
PRINT #main.gb, "Place ";xVar;" ";yVar
PRINT #main.gb, "Circlefilled 10"
PRINT #main.gb, "Flush"
WAIT
[col2]
PRINT #main.gb, "Color Blue; Backcolor Blue"
PRINT #main.gb, "Place ";xVar;" ";yVar
PRINT #main.gb, "Circlefilled 10"
PRINT #main.gb, "Flush"
WAIT
[col3]
PRINT #main.gb, "Color Yellow; Backcolor Yellow"
PRINT #main.gb, "Place ";xVar;" ";yVar
PRINT #main.gb, "Circlefilled 10"
PRINT #main.gb, "Flush"
WAIT
[EndProgram]
CLOSE #main
END
[[code]]
When code is directed to a branch label, that complete block of code must also end with a {{**WAIT**}} statement. If there is no {{**WAIT**}} statement, code execution will continue through the lines in succession until a {{**WAIT**}} statement is reached, the {{**END**}} statement is reached, or the code encounters an error.
A **{{POPUPMENU}}** can not be directed to or call a **{{SUB}}**.
See also **[[http://lbpe.wikispaces.com/Menus01|Menu Bar.]]**
If you have a favorite Popupmenu or other custom menu you'd like to share, please consider **[[http://lbpe.wikispaces.com/Submit|submitting an article.]]**
----