Placing a check next to a menu item
This lesson demonstrates how to place a checkmark to a menu item.
The menuPick message
The menuPick message is sent when the user chooses a menu item from the menu. Remember that in LiveCode, menus are usually implemented as buttons: the button's style and menuMode properties control how it is displayed, and the button's text property is used as the list of menu items. When the menuPick message is sent to the button, it triggers this handler.
The menuHistory property
The menuHistory property of a button menu is the number of the last-chosen menu item. Each line of the button contents becomes a separate menu item when the menu is displayed, so the menuHistory is also the line number of the menu item. The menuPick handler gets the text of the chosen menu item as a parameter. However, in this example, we use chunk expressions to change the contents of the button, so it's easier to deal with the line number instead.
Adding a checkmark to a menu item
When a menu item is checkmarked, the characters "!c" are placed at the start of that menu item in the button contents. For example, if the second menu item has a checkmark, the second line of the button contents starts with "!c". You checkmark a menu item by adding "!c" at the start of the corresponding line, and remove the checkmark by deleting these characters. The next time the menu is displayed, the checkmarks are shown as specified by the button contents.
This example handler tests whether the chosen menu item is already checkmarked, that is, whether its line starts with "!c". If it is, the first two characters are deleted to remove the checkmark. Otherwise, the "!c" marker is added to the start of the line to add a checkmark.
Note: In a cascading menu, the process of checkmarking a menu item works the same way: the line corresponding to the checkmarked menu item starts with "!c". The characters "!c" must precede the tab character (that indicates the menu item is part of a cascading menu).
The menuPick handler code
on menuPick pItem local tItemNumber put the menuHistory of me into tItemNumber if char 1 to 2 of line tItemNumber of me is "!c" then delete char 1 to 2 of line tItemNumber of me else put "!c" before line tItemNumber of me ## This handler just handles the checkmarks. If you ## want to do other things in response to a menu choice, ## you'll do them here, after the checkmark code. Here's ## a simple example: answer "You chose" && quote & pItem & quote & "." end if end menuPick
Mutually exclusive line items
This example allows multiple menu items to be checkmarked. Choosing an already checkmarked item unchecks it again. But suppose the menu items are mutually exclusive, and we want only one menu item to be checkmarked at a time? In this case, we would remove any existing checkmark before checkmarking the new menu item. The simplest way to do this is to uncheck every menu item before checkmarking the chosen one:
repeat with x = 1 to the number of lines in me if char 1 to 2 of line x of me is "!c" then delete char 1 to 2 of line x of me end if end repeat ## checkmark the item put "!c" before line tItemNumber of me
The method above is simple but inefficient, since it requires the handler to loop through every menu item to find the one menu item that's checkmarked. For most menus, the difference in speed will not be noticeable, but for very complex cascading menus that may have hundreds of menu items, it may make sense to use a faster method. One alternative is to store the line number of the checkmarked menu item in a custom property of the button, and then retrieve the custom property and uncheck just that menu item:
put the cCurrentCheckedLine of me into tOldLine delete char 1 to 2 of line tOldLine of me put "!c" before line tItemNumber of me set the cCurrentCheckedLine of me to tItemNumber