📄 utils.tcl
字号:
# ----------------------------------------------------------------------------# utils.tcl# This file is part of Unifix BWidget Toolkit# $Id: utils.tcl 3417 2004-12-03 00:31:24Z tjikkun $# ----------------------------------------------------------------------------# Index of commands:# - GlobalVar::exists# - GlobalVar::setvarvar# - GlobalVar::getvarvar# - BWidget::assert# - BWidget::clonename# - BWidget::get3dcolor# - BWidget::XLFDfont# - BWidget::place# - BWidget::grab# - BWidget::focus# ----------------------------------------------------------------------------namespace eval GlobalVar { proc use {} {}}namespace eval BWidget { variable _top variable _gstack {} variable _fstack {} proc use {} {}}# ----------------------------------------------------------------------------# Command GlobalVar::exists# ----------------------------------------------------------------------------proc GlobalVar::exists { varName } { return [uplevel \#0 [list info exists $varName]]}# ----------------------------------------------------------------------------# Command GlobalVar::setvar# ----------------------------------------------------------------------------proc GlobalVar::setvar { varName value } { return [uplevel \#0 [list set $varName $value]]}# ----------------------------------------------------------------------------# Command GlobalVar::getvar# ----------------------------------------------------------------------------proc GlobalVar::getvar { varName } { return [uplevel \#0 [list set $varName]]}# ----------------------------------------------------------------------------# Command GlobalVar::tracevar# ----------------------------------------------------------------------------proc GlobalVar::tracevar { cmd varName args } { return [uplevel \#0 [list trace $cmd $varName] $args]}# ----------------------------------------------------------------------------# Command BWidget::lreorder# ----------------------------------------------------------------------------proc BWidget::lreorder { list neworder } { set pos 0 set newlist {} foreach e $neworder { if { [lsearch -exact $list $e] != -1 } { lappend newlist $e set tabelt($e) 1 } } set len [llength $newlist] if { !$len } { return $list } if { $len == [llength $list] } { return $newlist } set pos 0 foreach e $list { if { ![info exists tabelt($e)] } { set newlist [linsert $newlist $pos $e] } incr pos } return $newlist}# ----------------------------------------------------------------------------# Command BWidget::assert# ----------------------------------------------------------------------------proc BWidget::assert { exp {msg ""}} { set res [uplevel 1 expr $exp] if { !$res} { if { $msg == "" } { return -code error "Assertion failed: {$exp}" } else { return -code error $msg } }}# ----------------------------------------------------------------------------# Command BWidget::clonename# ----------------------------------------------------------------------------proc BWidget::clonename { menu } { set path "" set menupath "" set found 0 foreach widget [lrange [split $menu "."] 1 end] { if { $found || [winfo class "$path.$widget"] == "Menu" } { set found 1 append menupath "#" $widget append path "." $menupath } else { append menupath "#" $widget append path "." $widget } } return $path}# ----------------------------------------------------------------------------# Command BWidget::getname# ----------------------------------------------------------------------------proc BWidget::getname { name } { if { [string length $name] } { set text [option get . "${name}Name" ""] if { [string length $text] } { return [parsetext $text] } } return {} }# ----------------------------------------------------------------------------# Command BWidget::parsetext# ----------------------------------------------------------------------------proc BWidget::parsetext { text } { set result "" set index -1 set start 0 while { [string length $text] } { set idx [string first "&" $text] if { $idx == -1 } { append result $text set text "" } else { set char [string index $text [expr {$idx+1}]] if { $char == "&" } { append result [string range $text 0 $idx] set text [string range $text [expr {$idx+2}] end] set start [expr {$start+$idx+1}] } else { append result [string range $text 0 [expr {$idx-1}]] set text [string range $text [expr {$idx+1}] end] incr start $idx set index $start } } } return [list $result $index]}# ----------------------------------------------------------------------------# Command BWidget::get3dcolor# ----------------------------------------------------------------------------proc BWidget::get3dcolor { path bgcolor } { foreach val [winfo rgb $path $bgcolor] { lappend dark [expr {60*$val/100}] set tmp1 [expr {14*$val/10}] if { $tmp1 > 65535 } { set tmp1 65535 } set tmp2 [expr {(65535+$val)/2}] lappend light [expr {($tmp1 > $tmp2) ? $tmp1:$tmp2}] } return [list [eval format "#%04x%04x%04x" $dark] [eval format "#%04x%04x%04x" $light]]}# ----------------------------------------------------------------------------# Command BWidget::XLFDfont# ----------------------------------------------------------------------------proc BWidget::XLFDfont { cmd args } { switch -- $cmd { create { set font "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" } configure { set font [lindex $args 0] set args [lrange $args 1 end] } default { return -code error "XLFDfont: commande incorrect: $cmd" } } set lfont [split $font "-"] if { [llength $lfont] != 15 } { return -code error "XLFDfont: description XLFD incorrect: $font" } foreach {option value} $args { switch -- $option { -foundry { set index 1 } -family { set index 2 } -weight { set index 3 } -slant { set index 4 } -size { set index 7 } default { return -code error "XLFDfont: option incorrecte: $option" } } set lfont [lreplace $lfont $index $index $value] } return [join $lfont "-"]}# ----------------------------------------------------------------------------# Command BWidget::place# ----------------------------------------------------------------------------## Notes:# For Windows systems with more than one monitor the available screen area may# have negative positions. Geometry settings with negative numbers are used# under X to place wrt the right or bottom of the screen. On windows, Tk# continues to do this. However, a geometry such as 100x100+-200-100 can be# used to place a window onto a secondary monitor. Passing the + gets Tk# to pass the remainder unchanged so the Windows manager then handles -200# which is a position on the left hand monitor.# I've tested this for left, right, above and below the primary monitor.# Currently there is no way to ask Tk the extent of the Windows desktop in # a multi monitor system. Nor what the legal co-ordinate range might be.#proc BWidget::place { path w h args } { variable _top update idletasks set reqw [winfo reqwidth $path] set reqh [winfo reqheight $path] if { $w == 0 } {set w $reqw} if { $h == 0 } {set h $reqh} set arglen [llength $args] if { $arglen > 3 } { return -code error "BWidget::place: bad number of argument" } if { $arglen > 0 } { set where [lindex $args 0] set list [list "at" "center" "left" "right" "above" "below"] set idx [lsearch $list $where] if { $idx == -1 } { return -code error [BWidget::badOptionString position $where $list] } if { $idx == 0 } { set err [catch { # purposely removed the {} around these expressions - [PT] set x [expr int([lindex $args 1])] set y [expr int([lindex $args 2])] }] if { $err } { return -code error "BWidget::place: incorrect position" } if {$::tcl_platform(platform) == "windows"} { # handle windows multi-screen. -100 != +-100 if {[string index [lindex $args 1] 0] != "-"} { set x "+$x" } if {[string index [lindex $args 2] 0] != "-"} { set y "+$y" } } else { if { $x >= 0 } { set x "+$x" } if { $y >= 0 } { set y "+$y" }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -