LiveCode LessonsHow To - LiveCode Sample Scripts MenusResponding to a choice in a Window menu

Responding to a choice in a Window menu

This lesson demonstrates how to respond to a choice in a Window menu.

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 menuPick handler

In the mouseDown handler described in "Updating a menu with a list of open Windows", we placed the title of each open stack in the menu, and set a custom property called cCurrentStacksList containing the corresponding stack names. If that mouseDown handler is in the menu button's script (or the script of the menu bar group that contains the button), the property has already been set when the user clicked the menu, so it's ready when our menuPick handler is executed. This property is needed because the menu displays titles, not the actual stack names; the cCurrentStacksList property lets us figure out the name corresponding to the stack label the user chose.

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 also gets the text of the chosen menu item as a parameter. However, in this example, we don't really care what the text of the chosen menu item is: it's a stack title, and you can't specify a stack by its title in order to bring it to the front. Instead, we need the stack name stored in the button's cCurrentStacksList custom property.

The names were stored in the cCurrentStacksList property in the same order as the labels in the menu itself, so if we know the menu item's number, and get that line number of the cCurrentStacksList property, we have the name of the stack. The menuHistory gives us this number.

Once we've retrieved the stack's name from the cCurrentStacksList property, we use the go command to bring the stack window to the front. If there is some problem and the go command can't find the stack, it sets the result function to empty. The last thing we do is check the result, and beep if it's not empty.

The menuPick handler code

on menuPick

local tChosenStackName

## goes in the menu button script

##This handler assumes the button has a custom property

## named "cStackNamesList" that contains the names of

## the stacks listed in the menu


put line (the menuHistory of me) of the cStackNamesList of me into tChosenStackName

go stack tChosenStackName

if the result is not empty then beep ## couldn't find stack

end menuPick

An alternative

The cCurrentStacksList custom property is needed because the Window menu displays a list of the titles of open stacks instead of their names, for better readability, but you need the name, not the label, to refer to a stack. There's a simpler way to do this, if you don't mind displaying the name of each stack instead of its label in the Window menu. If the Window menu consists of a list of stack names, a handler to choose one of them looks like this:

on menuPick theStackName

go stack theStackName

end menuPick


Add your comment

E-Mail me when someone replies to this comment