hwtk::menu

Create and manipulate menu widgets.

Format

hwtk::menu - pathName ?option value? …

Description

The menu command creates a new top-level window (given by the pathName argument) and makes it into a menu widget. Additional options, described above, may be specified on the command line or in the option database to configure aspects of the menu such as its colors and font. The menu command returns its pathName argument. At the time this command is invoked, there must not exist a window named pathName, but pathName’s parent must exist.

A menu is a widget that displays a collection of one-line entries arranged in one or more columns. There exist several different types of entries, each with different properties. Entries of different types may be combined in a single menu. Menu entries are not the same as entry widgets. In fact, menu entries are not even distinct widgets; the entire menu is one widget.

Standard Options

-clientdata
Database name: clientData
Database class: ClientData
Acts as a data storage for a widgets. User can store any data and this will not have any effect on widget property.
-cursor
Database name: cursor
Database class: Cursor
Specifies the mouse cursor to be used for the widget. See Tk_GetCursor and cursors(n) in the Tk reference manual for the legal values. If set to the empty string (the default), the cursor is inherited from the parent widget.

Widget-Specific Options

-configcommand
Database name: configCommand
Database class: ConfigCommand
Callback script will be executed just before populating the menu. Menu item properties can be configured here such as -state, -visible.
-gridlayout
Database name: gridlayout
Database class: Layout
Specifies whether or not the menu should be displayed as a grid, containing images only. Acceptable values are 1 (menu will be displayed as a grid) and 0 (menu will not be displayed as a grid). The default value is 0.
-tearoff
Database name: tearOff
Database class: TearOff
This option must have a proper boolean value, which specifies whether or not the menu should include a tear-off entry at the top. If so, it will exist as entry 0 of the menu and the other entries will number starting at 1. The default menu bindings arrange for the menu to be torn off when the tear-off entry is invoked.
-type
Database name: type
Database class: Type
Specifies the type of menu item. Acceptable values are command, radiobutton, and checkbutton.

Widget Command

pathName configure ?option? ?value option value …?
Query or modify the configuration options of the widget. If one or more option-value pairs are specified, then the command modifies the given widget option(s) to have the given value(s); in this case the command returns an empty string. If option is specified with no value, then the command returns a list describing the named option: the elements of the list are the option name, database name, database class, default value, and current value. If no option is specified, returns a list describing all of the available options for pathName.
pathName cget option
Returns the current value of the configuration option given by option.
pathName identify element x y
Returns the name of the element under the point given by x and y, or an empty string if the point does not lie within any element. x and y are pixel coordinates relative to the widget. Some widgets accept other identify subcommands.
pathName instate statespec ?script?
Test the widget’s state. If script is not specified, returns 1 if the widget state matches statespec and 0 otherwise. If script is specified, equivalent to
if{[pathNameinstatestateSpec]}script
pathName state ?stateSpec?
Modify or inquire widget state. If stateSpec is present, sets the widget state: for each flag in stateSpec, sets the corresponding flag or clears it if prefixed by an exclamation point. Returns a new state spec indicating which flags were changed:
setchanges[pathNamestatespec]
pathNamestate$changes
will restore pathName to the original state. If stateSpec is not specified, returns a list of the currently-enabled state flags.
pathName activate index
Change the state of the entry indicated by index to active and redisplay it using its active colors. Any previously-active entry is deactivated. If index is specified as none, or if the specified entry is disabled, then the menu ends up with no active entry. Returns an empty string.
pathName clear
Clears the menu.
pathName entrycget index option
This is internal command. Instead use itemcget command.
pathName index index
Returns the numerical index corresponding to index, or none if index was specified as none.
pathName invoke index
Invoke the action of the menu entry. See the sections on the individual entries above for details on what happens. If the menu entry is disabled then nothing happens. If the entry has a command associated with it then the result of that command is returned as the result of the invoke widget command. Otherwise the result is an empty string.
Note: Invoking a menu entry does not automatically unpost the menu; the default bindings normally take care of this before invoking the invoke widget command.
pathName item name ?arg arg …?
Creates a menu item.
pathName itemconfigure tag ?arg arg …?
Same as configure command. Items can be configured supports only -state option of an item.
pathName itemcget tag option
Returns the current value of a configuration option for the entry given by index. Option may have any of the values accepted by the item widget command.
pathName itemdelete ?arg arg …?
Deletes one or more items from the menu.
pathName itemexists tag
Returns a value specifying whether or not the given item exists in the menu; returns 1 if the item exists, or 0 if it does not.
pathName itemlist
Returns a list of the items contained in the menu.
pathName items ?arg arg …?
Returns the list of all item tag names.
pathName post X Y
Arrange for the menu to be displayed on the screen at the root-window coordinates given by x and y. These coordinates are adjusted if necessary to guarantee that the entire menu is visible on the screen. This command normally returns an empty string. If the postCommand option has been specified, then its value is executed as a Tcl script before posting the menu and the result of that script is returned as the result of the post widget command. If an error returns while executing the command, then the error is returned without posting the menu.
pathName postcascade index
This is an internal command.
pathName unpost
Unmap the window so that it is no longer displayed. If a lower-level cascaded menu is posted, unpost that menu. Returns an empty string. This subcommand does not work on Windows and the Macintosh, as those platforms have their own way of unposting menus.

Example

# hwtk::menu

proc ConfigMenu {W} {
        foreach item [$W items] {
                switch -- $item {
                        radioss {
                                $W itemconfigure radioss -state disabled
                        }
                }
        }
}

proc OpenRegularMenu {w} {
        set m $w.m
        if {![winfo exists $m]} {
                hwtk::menu $m -configcommand "ConfigMenu %W"
                $m item new -caption "New" -command "puts New" -accelerator "Ctrl + N"
                $m item open -caption "Open" -command "puts Open" -accelerator "Ctrl + O"
                $m item save -caption "Save" -command "puts Save" -accelerator "Ctrl + S" -image fileSaveModel-16.png
                $m item saveas -caption "Save As" -command "puts SaveAs" -accelerator "Ctrl + A"
                $m item separator
                $m item import -caption "Import"
                $m item model -caption "Model" -command "puts Model" -parent import -accelerator "Alt + I"
                $m item solver -caption "Solver Deck" -parent import -command "puts \"Solver Deck\""
                $m item optistruct -caption "OptiStruct Deck" -parent solver -command "puts \"OptiStruct Deck\""
                $m item radioss -caption "RADIOSS Deck" -parent solver -command "puts \"RADIOSS Deck\""
                $m item connectors -parent import -caption "Connectors" -command "puts \"Connectors\"" \
                        -image fileImportModel-16.png
                $m item load -caption "Load"
                $m item results -parent load -caption "Results" -command "puts Results"
                $m item up -parent load -caption "User Profile" -command "puts \"User Profile\""
                $m item run -caption "Run" -accelerator "Ctrl + R"
                $m item script -parent script -caption "Script" -command "puts Script"
                $m item export -caption "Export"
                $m item emodel -parent export -caption "Model" -command "puts Model" -image fileExportModel-16.png
                $m item separator
                $m item exit -caption "Exit" -command "puts Exit" -accelerator "Alt + F4" -image closeReverse-16.png
        }
        tk_popup $m [winfo pointerx .] [winfo pointery .]
}

proc OpenColorMenu {w} {
        set c $w.color
        if {![winfo exists $c]} {
        ::menu $c
                for {set i 0} {$i < 64} {incr i} {
                        $c add radiobutton -image [hwtk::image cache color[expr {$i + 1}]-16.png] \
                                -columnbreak [expr {$i % 8 ? 0 : 1}] -hidemargin 1 -indicatoron 1 -compound left \
                                -command "puts \"Selected color is [expr {$i + 1}]\""
                }
        }
        tk_popup $c [winfo pointerx .] [winfo pointery .]
}

hwtk::dialog .dlg
set w [.dlg recess]
pack [hwtk::frame $w.frame] -fill x

pack [hwtk::toolbutton $w.frame.tb -text "Right click here to open menu 1" -rmbcommand "OpenRegularMenu $w" \
        -help "Open regular menu" ] -padx 12 -side left -anchor nw

pack [hwtk::toolbutton $w.frame.tb1 -text "Left click here to open menu 2" -command "OpenColorMenu $w" \
        -help "Open color menu" ] -padx 6 -anchor nw

.dlg post