📄 combobox.itk
字号:
### Retrieve entry contents if no args OR use args as list # index and retrieve list item at index .## ------------------------------------------------------body iwidgets::Combobox::get {{index {}}} { # no args means to get the current text in the entry field area if {$index == {}} { iwidgets::Entryfield::get } else { eval $itk_component(list) get $index }}# ------------------------------------------------------# PUBLIC METHOD: getcurselection## Return currently selected item in the listbox. Shortcut# version of get curselection command combination.## ------------------------------------------------------body iwidgets::Combobox::getcurselection {} { return [$itk_component(list) getcurselection]}# ------------------------------------------------------------------# PUBLIC METHOD: ivoke## Pops up or down a dropdown combobox.# # ------------------------------------------------------------------body iwidgets::Combobox::invoke {} { if {$itk_option(-dropdown)} { return [_toggleList] } return }# ------------------------------------------------------------# PUBLIC METHOD: insert comonent index string ?string ...?## Insert an item into the listbox OR text into the entry area.# Valid component names are entry or list.## ------------------------------------------------------------body iwidgets::Combobox::insert {component index args} { set nargs [llength $args] if {$nargs == 0} { error "no value given for parameter \"string\" in function\ \"Combobox::insert\"" } switch -- $component { entry { if { $nargs > 1} { error "called function \"Combobox::insert entry\"\ with too many arguments" } else { if {$itk_option(-state) == "normal"} { eval iwidgets::Entryfield::insert $index $args [code $this _lookup ""] } } } list { if {$itk_option(-state) == "normal"} { eval $itk_component(list) insert $index $args } } default { error "bad Combobox component \"$component\": must\ be entry or list." } }}# ------------------------------------------------------# PUBLIC METHOD: justify direction## Wrapper for justifying the listbox items in one of# 4 directions: top, bottom, left, or right.## ------------------------------------------------------body iwidgets::Combobox::justify {direction} { return [$itk_component(list) justify $direction]}# ------------------------------------------------------------------# PUBLIC METHOD: see index## Adjusts the view such that the element given by index is visible.# ------------------------------------------------------------------body iwidgets::Combobox::see {index} { return [$itk_component(list) see $index]}# ------------------------------------------------------------------# PUBLIC METHOD: selection option first ?last?## Adjusts the selection within the listbox and changes the contents# of the entry component to be the value of the selected list item.# ------------------------------------------------------------------body iwidgets::Combobox::selection {option first {last {}}} { # thin wrap if {$option == "set"} { $itk_component(list) selection clear 0 end $itk_component(list) selection set $first set rtn "" } else { set rtn [eval $itk_component(list) selection $option $first $last] } set _currItem $first # combobox additions set theText [getcurselection] if {$theText != [$itk_component(entry) get]} { clear entry if {$theText != ""} { insert entry 0 $theText } } return $rtn}# ------------------------------------------------------------------# PUBLIC METHOD: size ## Returns a decimal string indicating the total number of elements # in the listbox.# ------------------------------------------------------------------body iwidgets::Combobox::size {} { return [$itk_component(list) size]}# ------------------------------------------------------# PUBLIC METHOD: sort ?mode?## Sort the current list in either "ascending" or "descending" order.## jss: how should i handle selected items?## ------------------------------------------------------body iwidgets::Combobox::sort {{mode ascending}} { $itk_component(list) sort $mode # return [$itk_component(list) sort $mode]}# ------------------------------------------------------------------# PUBLIC METHOD: xview ?arg arg ...?## Change or query the vertical position of the text in the list box.# ------------------------------------------------------------------body iwidgets::Combobox::xview {args} { return [eval $itk_component(list) xview $args]}# ------------------------------------------------------------------# PUBLIC METHOD: yview ?arg arg ...?## Change or query the horizontal position of the text in the list box.# ------------------------------------------------------------------body iwidgets::Combobox::yview {args} { return [eval $itk_component(list) yview $args]}# ------------------------------------------------------# PROTECTED METHOD: _addToList## Add the current item in the entry to the listbox.## ------------------------------------------------------body iwidgets::Combobox::_addToList {} { set input [get] if {$input != ""} { if {$itk_option(-unique)} { # if item is already in list, select it and exit set item [lsearch -exact [$itk_component(list) get 0 end] $input] if {$item != -1} { selection clear 0 end if {$item != {}} { selection set $item $item set _currItem $item } return } } # add the item to end of list selection clear 0 end insert list end $input selection set end end }}# ------------------------------------------------------# PROTECTED METHOD: _createComponents## Create deferred combobox components and add bindings.## ------------------------------------------------------body iwidgets::Combobox::_createComponents {} { if {$itk_option(-dropdown)} { # --- build a dropdown combobox --- # make the arrow childsite be on the right hand side #------------------------------------------------------------- # BUG FIX: csmith (Chad Smith: csmith@adc.com), 3/4/99 #------------------------------------------------------------- # The following commented line of code overwrites the -command # option when passed into the constructor. The order of calls # in the constructor is: # 1) eval itk_initalize $args (initializes -command) # 2) _doLayout # 3) _createComponents (overwrites -command) # The solution is to only set the -command option if it hasn't # already been set. The following 4 lines of code do this. #------------------------------------------------------------- # ** configure -childsitepos e -command [code $this _addToList] #------------------------------------------------------------- configure -childsitepos e if ![llength [cget -command]] { configure -command [code $this _addToList] } # arrow button to popup the list itk_component add arrowBtn { button $itk_interior.arrowBtn -borderwidth 2 \ -width 15 -height 15 -image downarrow \ -command [code $this _toggleList] -state $itk_option(-state) } { keep -background -borderwidth -cursor -state \ -highlightcolor -highlightthickness rename -relief -arrowrelief arrowRelief Relief rename -highlightbackground -background background Background } # popup list container itk_component add popup { toplevel $itk_interior.popup } { keep -background -cursor } wm withdraw $itk_interior.popup # the listbox itk_component add list { iwidgets::Scrolledlistbox $itk_interior.popup.list -exportselection no \ -vscrollmode dynamic -hscrollmode dynamic -selectmode browse } { keep -background -borderwidth -cursor -foreground \ -highlightcolor -highlightthickness \ -hscrollmode -selectbackground \ -selectborderwidth -selectforeground -textbackground \ -textfont -vscrollmode rename -height -listheight listHeight Height rename -cursor -popupcursor popupCursor Cursor } # mode specific bindings _dropdownBindings # Ugly hack to avoid tk buglet revealed in _dropdownBtnRelease where # relief is used but not set in scrollbar.tcl. global tkPriv set tkPriv(relief) raise } else { # --- build a simple combobox --- configure -childsitepos s itk_component add list { iwidgets::Scrolledlistbox $itk_interior.list -exportselection no \ -vscrollmode dynamic -hscrollmode dynamic } { keep -background -borderwidth -cursor -foreground \ -highlightcolor -highlightthickness \ -hscrollmode -selectbackground \ -selectborderwidth -selectforeground -textbackground \ -textfont -visibleitems -vscrollmode rename -height -listheight listHeight Height } # add mode specific bindings _simpleBindings } # popup cursor applies only to the list within the combobox configure -popupcursor $itk_option(-popupcursor) # add mode independent bindings _commonBindings}# ------------------------------------------------------# PROTECTED METHOD: _deleteList first ?last?## Delete an item or items from the listbox. Called via # "delete list args".## ------------------------------------------------------body iwidgets::Combobox::_deleteList {first {last {}}} { if {$last == {}} { set last $first } $itk_component(list) delete $first $last # remove the item if it is no longer in the list set text [$this get] if {$text != ""} { set index [lsearch -exact [$itk_component(list) get 0 end] $text ] if {$index == -1} { clear entry } } return}# ------------------------------------------------------# PROTECTED METHOD: _deleteText first ?last?## Renamed Entryfield delete method. Called via "delete entry args".## ------------------------------------------------------body iwidgets::Combobox::_deleteText {first {last {}}} { $itk_component(entry) configure -state normal set rtrn [delete $first $last] switch -- $itk_option(-editable) { 0 - false - no - off { $itk_component(entry) configure -state disabled } } return $rtrn}# ------------------------------------------------------# PROTECTED METHOD: _doLayout ?when?## Call methods to create and pack the Combobox components.## ------------------------------------------------------body iwidgets::Combobox::_doLayout {{when later}} { _createComponents _packComponents $when}# ------------------------------------------------------# PROTECTED METHOD: _drawArrow ## Draw the arrow button. Determines packing according to# -labelpos.## ------------------------------------------------------body iwidgets::Combobox::_drawArrow {} { set flip false set relief "" set fg [cget -foreground] if {$_isPosted} { set flip true set relief "-relief sunken" } else { set relief "-relief $itk_option(-arrowrelief)" } if {$flip} { # # draw up arrow # eval $itk_component(arrowBtn) configure -image uparrow $relief } else { # # draw down arrow # eval $itk_component(arrowBtn) configure -image downarrow $relief }}# ------------------------------------------------------# PROTECTED METHOD: _dropdownBtnRelease window x y## Event handler for button releases while a dropdown list# is posted.## ------------------------------------------------------body iwidgets::Combobox::_dropdownBtnRelease {{window {}} {x 1} {y 1}} { # if it's a scrollbar then ignore the release if {($window == [$itk_component(list) component vertsb]) || ($window == [$itk_component(list) component horizsb])} { return } # 1st release allows list to stay up unless we are in listbox if {$_ignoreRelease} { _ignoreNextBtnRelease false return } # should I use just the listbox or also include the scrollbars if { ($x >= 0) && ($x < [winfo width [_slbListbox]]) && ($y >= 0) && ($y < [winfo height [_slbListbox]])} { _stateSelect } _unpostList}# ------------------------------------------------------# PROTECTED METHOD: _ignoreNextBtnRelease ignore## Set private variable _ignoreRelease. If this variable# is true then the next button release will not remove# a dropdown list.## ------------------------------------------------------body iwidgets::Combobox::_ignoreNextBtnRelease {ignore} { set _ignoreRelease $ignore}# ------------------------------------------------------# PROTECTED METHOD: _next## Select the next item in the list.## ------------------------------------------------------body iwidgets::Combobox::_next {} { if {[size] <= 1} { return } set i [curselection] if {($i == {}) || ($i == [expr [size]-1]) } { set i 0 } else { incr i } selection clear 0 end selection set $i $i see $i set _currItem $i}# ------------------------------------------------------# PROTECTED METHOD: _packComponents ?when?## Pack the components of the combobox and add bindings.## ------------------------------------------------------body iwidgets::Combobox::_packComponents {{when later}} { if {$when == "later"} { if {$_repacking == ""} { set _repacking [after idle [code $this _packComponents now]] return }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -