📄 optionmenu.itk
字号:
_setitems [lreplace $_items $first $last] # # Make sure "nextAvail" is still in the list. # set index [lsearch -exact $_items $nextAvail] _setItem [expr {$index != -1 ? $nextAvail : ""}]}# ------------------------------------------------------------------# METHOD: disable index## Disable a menu item in the option menu. This will prevent the user# from being able to select this item from the menu. This only effects# the state of the item in the menu, in other words, should the item# be the currently selected item, the user is responsible for # determining this condition and taking appropriate action.# ------------------------------------------------------------------body iwidgets::Optionmenu::disable {index} { set index [index $index] $itk_component(popupMenu) entryconfigure $index -state disabled}# ------------------------------------------------------------------# METHOD: enable index## Enable a menu item in the option menu. This will allow the user# to select this item from the menu. # ------------------------------------------------------------------body iwidgets::Optionmenu::enable {index} { set index [index $index] $itk_component(popupMenu) entryconfigure $index -state normal}# ------------------------------------------------------------------# METHOD: get## Returns the current menu item.# ------------------------------------------------------------------body iwidgets::Optionmenu::get {{first "current"} {last ""}} { if {"current" == $first} { return $_currentItem } set first [index $first] if {"" == $last} { return [$itk_component(popupMenu) entrycget $first -label] } if {"end" == $last} { set last [$itk_component(popupMenu) index end] } else { set last [index $last] } set rval "" while {$first <= $last} { lappend rval [$itk_component(popupMenu) entrycget $first -label] incr first } return $rval}# ------------------------------------------------------------------# METHOD: insert index string ?string?## Insert an item in the popup menu.# ------------------------------------------------------------------body iwidgets::Optionmenu::insert {index string args} { set index [index $index] set args [linsert $args 0 $string] _setitems [eval linsert {$_items} $index $args] return ""}# ------------------------------------------------------------------# METHOD: select index## Select an item from the popup menu to display on the menu label# button. # ------------------------------------------------------------------body iwidgets::Optionmenu::select {index} { set index [index $index] if {$index > [expr $_numitems - 1]} { incr index -1 } _setItem [lindex $_items $index]}# ------------------------------------------------------------------# METHOD: popupMenu## Evaluates the specified args against the popup menu component# and returns the result.# ------------------------------------------------------------------body iwidgets::Optionmenu::popupMenu {args} { return [eval $itk_component(popupMenu) $args] }# ------------------------------------------------------------------# METHOD: sort mode## Sort the current menu in either "ascending" or "descending" order.# ------------------------------------------------------------------body iwidgets::Optionmenu::sort {{mode "increasing"}} { switch $mode { ascending - increasing { _setitems [lsort -increasing $_items] } descending - decreasing { _setitems [lsort -decreasing $_items] } default { error "bad sort argument \"$mode\": should be ascending,\ descending, increasing, or decreasing" } }}# ------------------------------------------------------------------# PRIVATE METHOD: _buttonRelease## Display the popup menu. Menu position is calculated.# ------------------------------------------------------------------body iwidgets::Optionmenu::_buttonRelease {time} { if {[expr abs([expr $_postTime - $time])] <= $itk_option(-clicktime)} { return -code break }}# ------------------------------------------------------------------# PRIVATE METHOD: _getNextItem index## Allows either a string or index number to be passed in, and returns# the next item in the list in string format. Wrap around is automatic.# ------------------------------------------------------------------body iwidgets::Optionmenu::_getNextItem {index} { if {[incr index] >= $_numitems} { set index 0 ;# wrap around } return [lindex $_items $index]}# ------------------------------------------------------------------# PRIVATE METHOD: _next## Sets the current option label to next item in list if that item is# not disbaled.# ------------------------------------------------------------------body iwidgets::Optionmenu::_next {} { if {$itk_option(-state) != "normal"} { return } set i [lsearch -exact $_items $_currentItem] for {set cnt 0} {$cnt < $_numitems} {incr cnt} { if {[incr i] >= $_numitems} { set i 0 } if {[$itk_component(popupMenu) entrycget $i -state] != "disabled"} { _setItem [lindex $_items $i] break } }}# ------------------------------------------------------------------# PRIVATE METHOD: _previous## Sets the current option label to previous item in list if that # item is not disbaled.# ------------------------------------------------------------------body iwidgets::Optionmenu::_previous {} { if {$itk_option(-state) != "normal"} { return } set i [lsearch -exact $_items $_currentItem] for {set cnt 0} {$cnt < $_numitems} {incr cnt} { set i [expr $i - 1] if {$i < 0} { set i [expr $_numitems - 1] } if {[$itk_component(popupMenu) entrycget $i -state] != "disabled"} { _setItem [lindex $_items $i] break } }}# ------------------------------------------------------------------# PRIVATE METHOD: _postMenu time## Display the popup menu. Menu position is calculated.# ------------------------------------------------------------------body iwidgets::Optionmenu::_postMenu {time} { # # Don't bother to post if menu is empty. # if {[llength $_items] > 0 && $itk_option(-state) == "normal"} { set _postTime $time set itemIndex [lsearch -exact $_items $_currentItem] set margin [expr $itk_option(-borderwidth) \ + $itk_option(-highlightthickness)] set x [expr [winfo rootx $itk_component(menuBtn)] + $margin] set y [expr [winfo rooty $itk_component(menuBtn)] \ - [$itk_component(popupMenu) yposition $itemIndex] + $margin] tk_popup $itk_component(popupMenu) $x $y }}# ------------------------------------------------------------------# PRIVATE METHOD: _setItem## Set the menu button label to item, then dismiss the popup menu.# Also check if item has been changed. If so, also call user-supplied# command.# ------------------------------------------------------------------body iwidgets::Optionmenu::_setItem {item} { if {$_currentItem != $item} { set _currentItem $item if {[winfo ismapped $itk_component(hull)]} { uplevel #0 $itk_option(-command) } }}# ------------------------------------------------------------------# PRIVATE METHOD: _setitems items## Create a list of items available on the menu. Used to create the# popup menu.# ------------------------------------------------------------------body iwidgets::Optionmenu::_setitems {items_} { # # Delete the old menu entries, and set the new list of # menu entries to those specified in "items_". # $itk_component(popupMenu) delete 0 last set _items "" set _numitems [llength $items_] # # Clear the menu button label. # if {$_numitems == 0} { _setItem "" return } set savedCurrentItem $_currentItem foreach opt $items_ { lappend _items $opt $itk_component(popupMenu) add command -label $opt \ -command [code $this _setItem $opt] } set first [lindex $_items 0] # # Make sure "savedCurrentItem" is still in the list. # if {$first != ""} { set i [lsearch -exact $_items $savedCurrentItem] #------------------------------------------------------------- # BEGIN BUG FIX: csmith (Chad Smith: csmith@adc.com), 11/18/99 #------------------------------------------------------------- # The previous code fragment: # <select [expr {$i != -1 ? $savedCurrentItem : $first}]> # is faulty because of exponential numbers. For example, # 2e-4 is numerically equal to 2e-04, but the string representation # is of course different. As a result, the select invocation # fails, and an error message is printed. #------------------------------------------------------------- if {$i != -1} { select $savedCurrentItem } else { select $first } #------------------------------------------------------------- # END BUG FIX #------------------------------------------------------------- } else { _setItem "" } _setSize}# ------------------------------------------------------------------# PRIVATE METHOD: _setSize ?when?## Set the size of the option menu. If "when" is "now", the change # is applied immediately. If it is "later" or it is not specified, # then the change is applied later, when the application is idle.# ------------------------------------------------------------------body iwidgets::Optionmenu::_setSize {{when later}} { if {$when == "later"} { if {$_calcSize == ""} { set _calcSize [after idle [code $this _setSize now]] } return } set margin [expr 2*($itk_option(-borderwidth) \ + $itk_option(-highlightthickness))] if {"0" != $itk_option(-width)} { set width $itk_option(-width) } else { set width [expr [winfo reqwidth $itk_component(popupMenu)]+$margin+20] } set height [winfo reqheight $itk_component(menuBtn)] $itk_component(lwchildsite) configure -width $width -height $height set _calcSize ""}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -