📄 widget.tcl
字号:
# value option value.## Results:# value option value.proc Widget::setMegawidgetOption {path option value} { set class $::Widget::_class($path) upvar 0 ${class}::${path}:opt pathopt set pathopt($option) $value}# ----------------------------------------------------------------------------# Command Widget::_get_window# returns the window corresponding to widget path# ----------------------------------------------------------------------------proc Widget::_get_window { class path } { set idx [string last "#" $path] if { $idx != -1 && ![string compare [string range $path [expr {$idx+1}] end] $class] } { return [string range $path 0 [expr {$idx-1}]] } else { return $path }}# ----------------------------------------------------------------------------# Command Widget::_get_configure# returns the configuration list of options# (as tk widget do - [$w configure ?option?])# ----------------------------------------------------------------------------proc Widget::_get_configure { path options } { variable _class set class $_class($path) upvar 0 ${class}::opt classopt upvar 0 ${class}::map classmap upvar 0 ${class}::$path:opt pathopt upvar 0 ${class}::$path:mod pathmod set len [llength $options] if { !$len } { set result {} foreach option [lsort [array names classopt]] { set optdesc $classopt($option) set type [lindex $optdesc 0] if { ![string compare $type "Synonym"] } { set syn $option set option [lindex $optdesc 1] set optdesc $classopt($option) set type [lindex $optdesc 0] } else { set syn "" } if { ![string compare $type "TkResource"] } { set alt [lindex [lindex $optdesc 3] 1] } else { set alt "" } set res [_configure_option $option $alt] if { $syn == "" } { lappend result [concat $option $res [list [lindex $optdesc 1]] [list [cget $path $option]]] } else { lappend result [list $syn [lindex $res 0]] } } return $result } elseif { $len == 1 } { set option [lindex $options 0] if { ![info exists classopt($option)] } { return -code error "unknown option \"$option\"" } set optdesc $classopt($option) set type [lindex $optdesc 0] if { ![string compare $type "Synonym"] } { set option [lindex $optdesc 1] set optdesc $classopt($option) set type [lindex $optdesc 0] } if { ![string compare $type "TkResource"] } { set alt [lindex [lindex $optdesc 3] 1] } else { set alt "" } set res [_configure_option $option $alt] return [concat $option $res [list [lindex $optdesc 1]] [list [cget $path $option]]] }}# ----------------------------------------------------------------------------# Command Widget::_configure_option# ----------------------------------------------------------------------------proc Widget::_configure_option { option altopt } { variable _optiondb variable _optionclass if { [info exists _optiondb($option)] } { set optdb $_optiondb($option) } else { set optdb [string range $option 1 end] } if { [info exists _optionclass($option)] } { set optclass $_optionclass($option) } elseif { [string length $altopt] } { if { [info exists _optionclass($altopt)] } { set optclass $_optionclass($altopt) } else { set optclass [string range $altopt 1 end] } } else { set optclass [string range $option 1 end] } return [list $optdb $optclass]}# ----------------------------------------------------------------------------# Command Widget::_get_tkwidget_options# ----------------------------------------------------------------------------proc Widget::_get_tkwidget_options { tkwidget } { variable _tk_widget variable _optiondb variable _optionclass set widget ".#BWidget#$tkwidget" if { ![winfo exists $widget] || ![info exists _tk_widget($tkwidget)] } { set widget [$tkwidget $widget] set config [$widget configure] foreach optlist $config { set opt [lindex $optlist 0] if { [llength $optlist] == 2 } { set refsyn [lindex $optlist 1] # search for class set idx [lsearch $config [list * $refsyn *]] if { $idx == -1 } { if { [string index $refsyn 0] == "-" } { # search for option (tk8.1b1 bug) set idx [lsearch $config [list $refsyn * *]] } else { # last resort set idx [lsearch $config [list -[string tolower $refsyn] * *]] } if { $idx == -1 } { # fed up with "can't read classopt()" return -code error "can't find option of synonym $opt" } } set syn [lindex [lindex $config $idx] 0] # JDC: used 4 (was 3) to get def from optiondb set def [lindex [lindex $config $idx] 4] lappend _tk_widget($tkwidget) [list $opt $syn $def] } else { # JDC: used 4 (was 3) to get def from optiondb set def [lindex $optlist 4] lappend _tk_widget($tkwidget) [list $opt $def] set _optiondb($opt) [lindex $optlist 1] set _optionclass($opt) [lindex $optlist 2] } } } return $_tk_widget($tkwidget)}# ----------------------------------------------------------------------------# Command Widget::_test_tkresource# ----------------------------------------------------------------------------proc Widget::_test_tkresource { option value arg } {# set tkwidget [lindex $arg 0]# set realopt [lindex $arg 1] foreach {tkwidget realopt} $arg break set path ".#BWidget#$tkwidget" set old [$path cget $realopt] $path configure $realopt $value set res [$path cget $realopt] $path configure $realopt $old return $res}# ----------------------------------------------------------------------------# Command Widget::_test_bwresource# ----------------------------------------------------------------------------proc Widget::_test_bwresource { option value arg } { return -code error "bad option type BwResource in widget"}# ----------------------------------------------------------------------------# Command Widget::_test_synonym# ----------------------------------------------------------------------------proc Widget::_test_synonym { option value arg } { return -code error "bad option type Synonym in widget"}# ----------------------------------------------------------------------------# Command Widget::_test_color# ----------------------------------------------------------------------------proc Widget::_test_color { option value arg } { if {[catch {winfo rgb . $value} color]} { return -code error "bad $option value \"$value\": must be a colorname \ or #RRGGBB triplet" } return $value}# ----------------------------------------------------------------------------# Command Widget::_test_string# ----------------------------------------------------------------------------proc Widget::_test_string { option value arg } { set value}# ----------------------------------------------------------------------------# Command Widget::_test_flag# ----------------------------------------------------------------------------proc Widget::_test_flag { option value arg } { set len [string length $value] set res "" for {set i 0} {$i < $len} {incr i} { set c [string index $value $i] if { [string first $c $arg] == -1 } { return -code error "bad [string range $option 1 end] value \"$value\": characters must be in \"$arg\"" } if { [string first $c $res] == -1 } { append res $c } } return $res}# -----------------------------------------------------------------------------# Command Widget::_test_enum# -----------------------------------------------------------------------------proc Widget::_test_enum { option value arg } { if { [lsearch $arg $value] == -1 } { set last [lindex $arg end] set sub [lreplace $arg end end] if { [llength $sub] } { set str "[join $sub ", "] or $last" } else { set str $last } return -code error "bad [string range $option 1 end] value \"$value\": must be $str" } return $value}# -----------------------------------------------------------------------------# Command Widget::_test_int# -----------------------------------------------------------------------------proc Widget::_test_int { option value arg } { if { ![string is int -strict $value] || \ ([string length $arg] && \ ![expr [string map [list %d $value] $arg]]) } { return -code error "bad $option value\ \"$value\": must be integer ($arg)" } return $value}# -----------------------------------------------------------------------------# Command Widget::_test_boolean# -----------------------------------------------------------------------------proc Widget::_test_boolean { option value arg } { if { ![string is boolean -strict $value] } { return -code error "bad $option value \"$value\": must be boolean" } # Get the canonical form of the boolean value (1 for true, 0 for false) return [string is true $value]}# -----------------------------------------------------------------------------# Command Widget::focusNext# Same as tk_focusNext, but call Widget::focusOK# -----------------------------------------------------------------------------proc Widget::focusNext { w } { set cur $w while 1 { # Descend to just before the first child of the current widget. set parent $cur set children [winfo children $cur] set i -1 # Look for the next sibling that isn't a top-level. while 1 { incr i if {$i < [llength $children]} { set cur [lindex $children $i] if {[winfo toplevel $cur] == $cur} { continue } else { break } } # No more siblings, so go to the current widget's parent. # If it's a top-level, break out of the loop, otherwise # look for its next sibling. set cur $parent if {[winfo toplevel $cur] == $cur} { break } set parent [winfo parent $parent] set children [winfo children $parent] set i [lsearch -exact $children $cur] } if {($cur == $w) || [focusOK $cur]} { return $cur } }}# -----------------------------------------------------------------------------# Command Widget::focusPrev# Same as tk_focusPrev, but call Widget::focusOK# -----------------------------------------------------------------------------proc Widget::focusPrev { w } { set cur $w while 1 { # Collect information about the current window's position # among its siblings. Also, if the window is a top-level, # then reposition to just after the last child of the window. if {[winfo toplevel $cur] == $cur} { set parent $cur set children [winfo children $cur] set i [llength $children] } else { set parent [winfo parent $cur] set children [winfo children $parent] set i [lsearch -exact $children $cur] } # Go to the previous sibling, then descend to its last descendant # (highest in stacking order. While doing this, ignore top-levels # and their descendants. When we run out of descendants, go up # one level to the parent. while {$i > 0} { incr i -1 set cur [lindex $children $i] if {[winfo toplevel $cur] == $cur} { continue } set parent $cur set children [winfo children $parent] set i [llength $children] } set cur $parent if {($cur == $w) || [focusOK $cur]} { return $cur } }}# ----------------------------------------------------------------------------# Command Widget::focusOK# Same as tk_focusOK, but handles -editable option and whole tags list.# ----------------------------------------------------------------------------proc Widget::focusOK { w } { set code [catch {$w cget -takefocus} value] if { $code == 1 } { return 0 } if {($code == 0) && ($value != "")} { if {$value == 0} { return 0 } elseif {$value == 1} { return [winfo viewable $w] } else { set value [uplevel \#0 $value $w] if {$value != ""} { return $value } } } if {![winfo viewable $w]} { return 0 } set code [catch {$w cget -state} value] if {($code == 0) && ($value == "disabled")} { return 0 } set code [catch {$w cget -editable} value] if {($code == 0) && !$value} { return 0 } set top [winfo toplevel $w] foreach tags [bindtags $w] { if { [string compare $tags $top] && [string compare $tags "all"] && [regexp Key [bind $tags]] } { return 1 } } return 0}# Widget::varForOption --## Retrieve a fully qualified variable name for the option specified.# If the option is not one for which a variable exists, throw an error # (ie, those options that map directly to widget options).## Arguments:# path megawidget to get an option var for.# option option to get a var for.## Results:# varname name of the variable, fully qualified, suitable for tracing.proc Widget::varForOption {path option} { variable _class variable _optiontype set class $_class($path) upvar 0 ${class}::$path:opt pathopt if { ![info exists pathopt($option)] } { error "unable to find variable for option \"$option\"" } set varname "::Widget::${class}::$path:opt($option)" return $varname}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -