📄 utils.tcl
字号:
}
} else {
if { $arglen == 2 } {
set widget [lindex $args 1]
if { ![winfo exists $widget] } {
return -code error "BWidget::place: \"$widget\" does not exist"
}
} else {
set widget .
}
set sw [winfo screenwidth $path]
set sh [winfo screenheight $path]
if { $idx == 1 } {
if { $arglen == 2 } {
# center to widget
set x0 [expr {[winfo rootx $widget] + ([winfo width $widget] - $w)/2}]
set y0 [expr {[winfo rooty $widget] + ([winfo height $widget] - $h)/2}]
} else {
# center to screen
set x0 [expr {([winfo screenwidth $path] - $w)/2 - [winfo vrootx $path]}]
set y0 [expr {([winfo screenheight $path] - $h)/2 - [winfo vrooty $path]}]
}
set x "+$x0"
set y "+$y0"
if {$::tcl_platform(platform) != "windows"} {
if { $x0+$w > $sw } {set x "-0"; set x0 [expr {$sw-$w}]}
if { $x0 < 0 } {set x "+0"}
if { $y0+$h > $sh } {set y "-0"; set y0 [expr {$sh-$h}]}
if { $y0 < 0 } {set y "+0"}
}
} else {
set x0 [winfo rootx $widget]
set y0 [winfo rooty $widget]
set x1 [expr {$x0 + [winfo width $widget]}]
set y1 [expr {$y0 + [winfo height $widget]}]
if { $idx == 2 || $idx == 3 } {
set y "+$y0"
if {$::tcl_platform(platform) != "windows"} {
if { $y0+$h > $sh } {set y "-0"; set y0 [expr {$sh-$h}]}
if { $y0 < 0 } {set y "+0"}
}
if { $idx == 2 } {
# try left, then right if out, then 0 if out
if { $x0 >= $w } {
set x "+[expr {$x0-$sw}]"
} elseif { $x1+$w <= $sw } {
set x "+$x1"
} else {
set x "+0"
}
} else {
# try right, then left if out, then 0 if out
if { $x1+$w <= $sw } {
set x "+$x1"
} elseif { $x0 >= $w } {
set x "+[expr {$x0-$sw}]"
} else {
set x "-0"
}
}
} else {
set x "+$x0"
if {$::tcl_platform(platform) != "windows"} {
if { $x0+$w > $sw } {set x "-0"; set x0 [expr {$sw-$w}]}
if { $x0 < 0 } {set x "+0"}
}
if { $idx == 4 } {
# try top, then bottom, then 0
if { $h <= $y0 } {
set y "+[expr {$y0-$sh}]"
} elseif { $y1+$h <= $sh } {
set y "+$y1"
} else {
set y "+0"
}
} else {
# try bottom, then top, then 0
if { $y1+$h <= $sh } {
set y "+$y1"
} elseif { $h <= $y0 } {
set y "+[expr {$y0-$sh}]"
} else {
set y "-0"
}
}
}
}
}
wm geometry $path "${w}x${h}${x}${y}"
} else {
wm geometry $path "${w}x${h}"
}
update idletasks
}
# ----------------------------------------------------------------------------
# Command BWidget::grab
# ----------------------------------------------------------------------------
proc BWidget::grab { option path } {
variable _gstack
if { $option == "release" } {
catch {::grab release $path}
while { [llength $_gstack] } {
set grinfo [lindex $_gstack end]
set _gstack [lreplace $_gstack end end]
foreach {oldg mode} $grinfo {
if { ![string equal $oldg $path] && [winfo exists $oldg] } {
if { $mode == "global" } {
catch {::grab -global $oldg}
} else {
catch {::grab $oldg}
}
return
}
}
}
} else {
set oldg [::grab current]
if { $oldg != "" } {
lappend _gstack [list $oldg [::grab status $oldg]]
}
if { $option == "global" } {
::grab -global $path
} else {
::grab $path
}
}
}
# ----------------------------------------------------------------------------
# Command BWidget::focus
# ----------------------------------------------------------------------------
proc BWidget::focus { option path {refocus 1} } {
variable _fstack
if { $option == "release" } {
while { [llength $_fstack] } {
set oldf [lindex $_fstack end]
set _fstack [lreplace $_fstack end end]
if { ![string equal $oldf $path] && [winfo exists $oldf] } {
if {$refocus} {catch {::focus -force $oldf}}
return
}
}
} elseif { $option == "set" } {
lappend _fstack [::focus]
::focus -force $path
}
}
# BWidget::refocus --
#
# Helper function used to redirect focus from a container frame in
# a megawidget to a component widget. Only redirects focus if
# focus is already on the container.
#
# Arguments:
# container container widget to redirect from.
# component component widget to redirect to.
#
# Results:
# None.
proc BWidget::refocus {container component} {
if { [string equal $container [::focus]] } {
::focus $component
}
return
}
# BWidget::badOptionString --
#
# Helper function to return a proper error string when an option
# doesn't match a list of given options.
#
# Arguments:
# type A string that represents the type of option.
# value The value that is in-valid.
# list A list of valid options.
#
# Results:
# None.
proc BWidget::badOptionString {type value list} {
set last [lindex $list end]
set list [lreplace $list end end]
return "bad $type \"$value\": must be [join $list ", "], or $last"
}
proc BWidget::wrongNumArgsString { string } {
return "wrong # args: should be \"$string\""
}
proc BWidget::read_file { file } {
set fp [open $file]
set x [read $fp [file size $file]]
close $fp
return $x
}
proc BWidget::classes { class } {
variable use
${class}::use
set classes [list $class]
if {![info exists use($class)]} { return }
foreach class $use($class) {
eval lappend classes [classes $class]
}
return [lsort -unique $classes]
}
proc BWidget::library { args } {
variable use
set libs [list widget init utils]
set classes [list]
foreach class $args {
${class}::use
eval lappend classes [classes $class]
}
eval lappend libs [lsort -unique $classes]
set library ""
foreach lib $libs {
if {![info exists use($lib,file)]} {
set file [file join $::BWIDGET::LIBRARY $lib.tcl]
} else {
set file [file join $::BWIDGET::LIBRARY $use($lib,file).tcl]
}
append library [read_file $file]
}
return $library
}
proc BWidget::inuse { class } {
variable ::Widget::_inuse
if {![info exists _inuse($class)]} { return 0 }
return [expr $_inuse($class) > 0]
}
proc BWidget::write { filename {mode w} } {
variable use
if {![info exists use(classes)]} { return }
set classes [list]
foreach class $use(classes) {
if {![inuse $class]} { continue }
lappend classes $class
}
set fp [open $filename $mode]
puts $fp [eval library $classes]
close $fp
return
}
# BWidget::bindMouseWheel --
#
# Bind mouse wheel actions to a given widget.
#
# Arguments:
# widget - The widget to bind.
#
# Results:
# None.
proc BWidget::bindMouseWheel { widget } {
bind $widget <MouseWheel> {%W yview scroll [expr {-%D/24}] units}
bind $widget <Shift-MouseWheel> {%W yview scroll [expr {-%D/120}] pages}
bind $widget <Control-MouseWheel> {%W yview scroll [expr {-%D/120}] units}
bind $widget <Button-4> {event generate %W <MouseWheel> -delta 120}
bind $widget <Button-5> {event generate %W <MouseWheel> -delta -120}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -