1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
#==============================================================================
# Contains utility procedures for mega-widgets.
#
# Structure of the module:
# - Namespace initialization
# - Public utility procedures
#
# Copyright (c) 2000-2011 Csaba Nemethi (E-mail: csaba.nemethi@t-online.de)
#==============================================================================
package require Tcl 8
package require Tk 8
#
# Namespace initialization
# ========================
#
namespace eval mwutil {
#
# Public variables:
#
variable version 2.7
variable library [file dirname [info script]]
#
# Public procedures:
#
namespace export wrongNumArgs getAncestorByClass convEventFields \
defineKeyNav processTraversal focusNext focusPrev \
configureWidget fullConfigOpt fullOpt enumOpts \
configureSubCmd attribSubCmd hasattribSubCmd \
unsetattribSubCmd getScrollInfo
#
# Make modified versions of the procedures tk_focusNext and
# tk_focusPrev, to be invoked in the processTraversal command
#
proc makeFocusProcs {} {
#
# Enforce the evaluation of the Tk library file "focus.tcl"
#
tk_focusNext .
#
# Build the procedures focusNext and focusPrev
#
foreach direction {Next Prev} {
set procBody [info body tk_focus$direction]
regsub -all {winfo children} $procBody {getChildren $class} procBody
proc focus$direction {w class} $procBody
}
}
makeFocusProcs
#
# Invoked in the procedures focusNext and focusPrev defined above:
#
proc getChildren {class w} {
if {[string compare [winfo class $w] $class] == 0} {
return {}
} else {
return [winfo children $w]
}
}
}
#
# Public utility procedures
# =========================
#
#------------------------------------------------------------------------------
# mwutil::wrongNumArgs
#
# Generates a "wrong # args" error message.
#------------------------------------------------------------------------------
proc mwutil::wrongNumArgs args {
set optList {}
foreach arg $args {
lappend optList \"$arg\"
}
return -code error "wrong # args: should be [enumOpts $optList]"
}
#------------------------------------------------------------------------------
# mwutil::getAncestorByClass
#
# Gets the path name of the widget of the specified class from the path name w
# of one of its descendants. It is assumed that all of the ancestors of w
# exist (but w itself needn't exist).
#------------------------------------------------------------------------------
proc mwutil::getAncestorByClass {w class} {
regexp {^(\..+)\..+$} $w dummy win
while {[string compare [winfo class $win] $class] != 0} {
set win [winfo parent $win]
}
return $win
}
#------------------------------------------------------------------------------
# mwutil::convEventFields
#
# Gets the path name of the widget of the specified class and the x and y
# coordinates relative to the latter from the path name w of one of its
# descendants and from the x and y coordinates relative to the latter.
#------------------------------------------------------------------------------
proc mwutil::convEventFields {w x y class} {
set win [getAncestorByClass $w $class]
set _x [expr {$x + [winfo rootx $w] - [winfo rootx $win]}]
set _y [expr {$y + [winfo rooty $w] - [winfo rooty $win]}]
return [list $win $_x $_y]
}
#------------------------------------------------------------------------------
# mwutil::defineKeyNav
#
# For a given mega-widget class, the procedure defines the binding tag
# ${class}KeyNav as a partial replacement for "all", by substituting the
# scripts bound to the events <Tab>, <Shift-Tab>, and <<PrevWindow>> with new
# ones which propagate these events to the mega-widget of the given class
# containing the widget to which the event was reported. (The event
# <Shift-Tab> was replaced with <<PrevWindow>> in Tk 8.3.0.) This tag is
# designed to be inserted before "all" in the list of binding tags of a
# descendant of a mega-widget of the specified class.
#------------------------------------------------------------------------------
proc mwutil::defineKeyNav class {
foreach event {<Tab> <Shift-Tab> <<PrevWindow>>} {
bind ${class}KeyNav $event \
[list mwutil::processTraversal %W $class $event]
}
bind Entry <<TraverseIn>> { %W selection range 0 end; %W icursor end }
bind Spinbox <<TraverseIn>> { %W selection range 0 end; %W icursor end }
}
#------------------------------------------------------------------------------
# mwutil::processTraversal
#
# Processes the given traversal event for the mega-widget of the specified
# class containing the widget w if that mega-widget is not the only widget
# receiving the focus during keyboard traversal within its top-level widget.
#------------------------------------------------------------------------------
proc mwutil::processTraversal {w class event} {
set win [getAncestorByClass $w $class]
if {[string compare $event "<Tab>"] == 0} {
set target [focusNext $win $class]
} else {
set target [focusPrev $win $class]
}
if {[string compare $target $win] != 0} {
set focus [focus]
if {[string compare $focus ""] != 0} {
event generate $focus <<TraverseOut>>
}
focus $target
event generate $target <<TraverseIn>>
}
return -code break ""
}
#------------------------------------------------------------------------------
# mwutil::configureWidget
#
# Configures the widget win by processing the command-line arguments specified
# in optValPairs and, if the value of initialize is true, also those database
# options that don't match any command-line arguments.
#------------------------------------------------------------------------------
proc mwutil::configureWidget {win configSpecsName configCmd cgetCmd \
optValPairs initialize} {
upvar $configSpecsName configSpecs
#
# Process the command-line arguments
#
set cmdLineOpts {}
set savedVals {}
set failed 0
set count [llength $optValPairs]
foreach {opt val} $optValPairs {
if {[catch {fullConfigOpt $opt configSpecs} result] != 0} {
set failed 1
break
}
if {$count == 1} {
set result "value for \"$opt\" missing"
set failed 1
break
}
set opt $result
lappend cmdLineOpts $opt
lappend savedVals [eval $cgetCmd [list $win $opt]]
if {[catch {eval $configCmd [list $win $opt $val]} result] != 0} {
set failed 1
break
}
incr count -2
}
if {$failed} {
#
# Restore the saved values
#
foreach opt $cmdLineOpts val $savedVals {
eval $configCmd [list $win $opt $val]
}
return -code error $result
}
if {$initialize} {
#
# Process those configuration options that were not
# given as command-line arguments; use the corresponding
# values from the option database if available
#
foreach opt [lsort [array names configSpecs]] {
if {[llength $configSpecs($opt)] == 1 ||
[lsearch -exact $cmdLineOpts $opt] >= 0} {
continue
}
set dbName [lindex $configSpecs($opt) 0]
set dbClass [lindex $configSpecs($opt) 1]
set dbValue [option get $win $dbName $dbClass]
if {[string compare $dbValue ""] == 0} {
set default [lindex $configSpecs($opt) 3]
eval $configCmd [list $win $opt $default]
} else {
if {[catch {
eval $configCmd [list $win $opt $dbValue]
} result] != 0} {
return -code error $result
}
}
}
}
return ""
}
#------------------------------------------------------------------------------
# mwutil::fullConfigOpt
#
# Returns the full configuration option corresponding to the possibly
# abbreviated option opt.
#------------------------------------------------------------------------------
proc mwutil::fullConfigOpt {opt configSpecsName} {
upvar $configSpecsName configSpecs
if {[info exists configSpecs($opt)]} {
if {[llength $configSpecs($opt)] == 1} {
return $configSpecs($opt)
} else {
return $opt
}
}
set optList [lsort [array names configSpecs]]
set count 0
foreach elem $optList {
if {[string first $opt $elem] == 0} {
incr count
if {$count == 1} {
set option $elem
} else {
break
}
}
}
if {$count == 1} {
if {[llength $configSpecs($option)] == 1} {
return $configSpecs($option)
} else {
return $option
}
} elseif {$count == 0} {
### return -code error "unknown option \"$opt\""
return -code error \
"bad option \"$opt\": must be [enumOpts $optList]"
} else {
### return -code error "unknown option \"$opt\""
return -code error \
"ambiguous option \"$opt\": must be [enumOpts $optList]"
}
}
#------------------------------------------------------------------------------
# mwutil::fullOpt
#
# Returns the full option corresponding to the possibly abbreviated option opt.
#------------------------------------------------------------------------------
proc mwutil::fullOpt {kind opt optList} {
if {[lsearch -exact $optList $opt] >= 0} {
return $opt
}
set count 0
foreach elem $optList {
if {[string first $opt $elem] == 0} {
incr count
if {$count == 1} {
set option $elem
} else {
break
}
}
}
if {$count == 1} {
return $option
} elseif {$count == 0} {
return -code error \
"bad $kind \"$opt\": must be [enumOpts $optList]"
} else {
return -code error \
"ambiguous $kind \"$opt\": must be [enumOpts $optList]"
}
}
#------------------------------------------------------------------------------
# mwutil::enumOpts
#
# Returns a string consisting of the elements of the given list, separated by
# commas and spaces.
#------------------------------------------------------------------------------
proc mwutil::enumOpts optList {
set optCount [llength $optList]
set n 1
foreach opt $optList {
if {$n == 1} {
set str $opt
} elseif {$n < $optCount} {
append str ", $opt"
} else {
if {$optCount > 2} {
append str ","
}
append str " or $opt"
}
incr n
}
return $str
}
#------------------------------------------------------------------------------
# mwutil::configureSubCmd
#
# This procedure is invoked to process configuration subcommands.
#------------------------------------------------------------------------------
proc mwutil::configureSubCmd {win configSpecsName configCmd cgetCmd argList} {
upvar $configSpecsName configSpecs
set argCount [llength $argList]
if {$argCount > 1} {
#
# Set the specified configuration options to the given values
#
return [configureWidget $win configSpecs $configCmd $cgetCmd $argList 0]
} elseif {$argCount == 1} {
#
# Return the description of the specified configuration option
#
set opt [fullConfigOpt [lindex $argList 0] configSpecs]
set dbName [lindex $configSpecs($opt) 0]
set dbClass [lindex $configSpecs($opt) 1]
set default [lindex $configSpecs($opt) 3]
return [list $opt $dbName $dbClass $default \
[eval $cgetCmd [list $win $opt]]]
} else {
#
# Return a list describing all available configuration options
#
foreach opt [lsort [array names configSpecs]] {
if {[llength $configSpecs($opt)] == 1} {
set alias $configSpecs($opt)
if {$::tk_version >= 8.1} {
lappend result [list $opt $alias]
} else {
set dbName [lindex $configSpecs($alias) 0]
lappend result [list $opt $dbName]
}
} else {
set dbName [lindex $configSpecs($opt) 0]
set dbClass [lindex $configSpecs($opt) 1]
set default [lindex $configSpecs($opt) 3]
lappend result [list $opt $dbName $dbClass $default \
[eval $cgetCmd [list $win $opt]]]
}
}
return $result
}
}
#------------------------------------------------------------------------------
# mwutil::attribSubCmd
#
# This procedure is invoked to process *attrib subcommands.
#------------------------------------------------------------------------------
proc mwutil::attribSubCmd {win prefix argList} {
set classNs [string tolower [winfo class $win]]
upvar ::${classNs}::ns${win}::attribs attribs
set argCount [llength $argList]
if {$argCount > 1} {
#
# Set the specified attributes to the given values
#
if {$argCount % 2 != 0} {
return -code error "value for \"[lindex $argList end]\" missing"
}
foreach {attr val} $argList {
set attribs($prefix-$attr) $val
}
return ""
} elseif {$argCount == 1} {
#
# Return the value of the specified attribute
#
set attr [lindex $argList 0]
set name $prefix-$attr
if {[info exists attribs($name)]} {
return $attribs($name)
} else {
return ""
}
} else {
#
# Return the current list of attribute names and values
#
set len [string length "$prefix-"]
set result {}
foreach name [lsort [array names attribs "$prefix-*"]] {
set attr [string range $name $len end]
lappend result [list $attr $attribs($name)]
}
return $result
}
}
#------------------------------------------------------------------------------
# mwutil::hasattribSubCmd
#
# This procedure is invoked to process has*attrib subcommands.
#------------------------------------------------------------------------------
proc mwutil::hasattribSubCmd {win prefix attr} {
set classNs [string tolower [winfo class $win]]
upvar ::${classNs}::ns${win}::attribs attribs
return [info exists attribs($prefix-$attr)]
}
#------------------------------------------------------------------------------
# mwutil::unsetattribSubCmd
#
# This procedure is invoked to process unset*attrib subcommands.
#------------------------------------------------------------------------------
proc mwutil::unsetattribSubCmd {win prefix attr} {
set classNs [string tolower [winfo class $win]]
upvar ::${classNs}::ns${win}::attribs attribs
set name $prefix-$attr
if {[info exists attribs($name)]} {
unset attribs($name)
}
return ""
}
#------------------------------------------------------------------------------
# mwutil::getScrollInfo
#
# Parses a list of arguments of the form "moveto <fraction>" or "scroll
# <number> units|pages" and returns the corresponding list consisting of two or
# three properly formatted elements.
#------------------------------------------------------------------------------
proc mwutil::getScrollInfo argList {
set argCount [llength $argList]
set opt [lindex $argList 0]
if {[string first $opt "moveto"] == 0} {
if {$argCount != 2} {
wrongNumArgs "moveto fraction"
}
set fraction [format "%f" [lindex $argList 1]]
return [list moveto $fraction]
} elseif {[string first $opt "scroll"] == 0} {
if {$argCount != 3} {
wrongNumArgs "scroll number units|pages"
}
set number [format "%d" [lindex $argList 1]]
set what [lindex $argList 2]
if {[string first $what "units"] == 0} {
return [list scroll $number units]
} elseif {[string first $what "pages"] == 0} {
return [list scroll $number pages]
} else {
return -code error "bad argument \"$what\": must be units or pages"
}
} else {
return -code error "unknown option \"$opt\": must be moveto or scroll"
}
}
|