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 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
# ------------------------------------------------------------------------------
# arrow.tcl
# This file is part of Unifix BWidget Toolkit
# ------------------------------------------------------------------------------
# Index of commands:
# Public commands
# - ArrowButton::create
# - ArrowButton::configure
# - ArrowButton::cget
# - ArrowButton::invoke
# Private commands (redraw commands)
# - ArrowButton::_redraw
# - ArrowButton::_redraw_state
# - ArrowButton::_redraw_relief
# - ArrowButton::_redraw_whole
# Private commands (event bindings)
# - ArrowButton::_destroy
# - ArrowButton::_enter
# - ArrowButton::_leave
# - ArrowButton::_press
# - ArrowButton::_release
# - ArrowButton::_repeat
# ------------------------------------------------------------------------------
# BWidget ToolKit
# Copyright (c) 1998-1999 UNIFIX.
# Copyright (c) 2001-2002 ActiveState Corp.
# The following terms apply to all files associated with the software
# unless explicitly disclaimed in individual files.
# The authors hereby grant permission to use, copy, modify, distribute,
# and license this software and its documentation for any purpose, provided
# that existing copyright notices are retained in all copies and that this
# notice is included verbatim in any distributions. No written agreement,
# license, or royalty fee is required for any of the authorized uses.
# Modifications to this software may be copyrighted by their authors
# and need not follow the licensing terms described here, provided that
# the new terms are clearly indicated on the first page of each file where
# they apply.
# IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
# ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
# DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
# IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
# NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
# MODIFICATIONS.
# GOVERNMENT USE: If you are acquiring this software on behalf of the
# U.S. government, the Government shall have only "Restricted Rights"
# in the software and related documentation as defined in the Federal
# Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
# are acquiring the software on behalf of the Department of Defense, the
# software shall be classified as "Commercial Computer Software" and the
# Government shall have only "Restricted Rights" as defined in Clause
# 252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
# authors grant the U.S. Government and others acting in its behalf
# permission to use and distribute the software in accordance with the
# terms specified in this license.
namespace eval ArrowButton {
Widget::tkinclude ArrowButton button .c \
include [list \
-borderwidth -bd \
-relief -highlightbackground \
-highlightcolor -highlightthickness -takefocus]
Widget::declare ArrowButton [list \
[list -type Enum button 0 [list arrow button]] \
[list -dir Enum top 0 [list top bottom left right]] \
[list -width Int 15 0 "%d >= 0"] \
[list -height Int 15 0 "%d >= 0"] \
[list -ipadx Int 0 0 "%d >= 0"] \
[list -ipady Int 0 0 "%d >= 0"] \
[list -clean Int 2 0 "%d >= 0 && %d <= 2"] \
[list -activeforeground TkResource "" 0 button] \
[list -activebackground TkResource "" 0 button] \
[list -disabledforeground TkResource "" 0 button] \
[list -foreground TkResource "" 0 button] \
[list -background TkResource "" 0 button] \
[list -state TkResource "" 0 button] \
[list -troughcolor TkResource "" 0 scrollbar] \
[list -arrowbd Int 1 0 "%d >= 0 && %d <= 2"] \
[list -arrowrelief Enum raised 0 [list raised sunken]] \
[list -command String "" 0] \
[list -armcommand String "" 0] \
[list -disarmcommand String "" 0] \
[list -repeatdelay Int 0 0 "%d >= 0"] \
[list -repeatinterval Int 0 0 "%d >= 0"] \
[list -fg Synonym -foreground] \
[list -bg Synonym -background] \
]
DynamicHelp::include ArrowButton balloon
proc ::ArrowButton { path args } { return [eval ArrowButton::create $path $args] }
proc use {} {}
bind BwArrowButtonC <Enter> {ArrowButton::_enter %W}
bind BwArrowButtonC <Leave> {ArrowButton::_leave %W}
bind BwArrowButtonC <ButtonPress-1> {ArrowButton::_press %W}
bind BwArrowButtonC <ButtonRelease-1> {ArrowButton::_release %W}
bind BwArrowButtonC <Key-space> {ArrowButton::invoke %W; break}
bind BwArrowButtonC <Return> {ArrowButton::invoke %W; break}
bind BwArrowButton <Configure> {ArrowButton::_redraw_whole %W %w %h}
bind BwArrowButton <Destroy> {ArrowButton::_destroy %W}
variable _grab
variable _moved
array set _grab {current "" pressed "" oldstate "" oldrelief ""}
}
# -----------------------------------------------------------------------------
# Command ArrowButton::create
# -----------------------------------------------------------------------------
proc ArrowButton::create { path args } {
# Initialize configuration mappings and parse arguments
array set submaps [list ArrowButton [list ] .c [list ]]
array set submaps [Widget::parseArgs ArrowButton $args]
# Create the class frame (so we can do the option db queries)
frame $path -class ArrowButton -borderwidth 0 -highlightthickness 0
Widget::initFromODB ArrowButton $path $submaps(ArrowButton)
# Create the canvas with the initial options
eval canvas $path.c $submaps(.c)
# Compute the width and height of the canvas from the width/height
# of the ArrowButton and the borderwidth/hightlightthickness.
set w [Widget::getMegawidgetOption $path -width]
set h [Widget::getMegawidgetOption $path -height]
set bd [Widget::cget $path -borderwidth]
set ht [Widget::cget $path -highlightthickness]
set pad [expr {2*($bd+$ht)}]
$path.c configure -width [expr {$w-$pad}] -height [expr {$h-$pad}]
bindtags $path [list $path BwArrowButton [winfo toplevel $path] all]
bindtags $path.c [list $path.c BwArrowButtonC [winfo toplevel $path.c] all]
pack $path.c -expand yes -fill both
DynamicHelp::sethelp $path $path.c 1
set ::ArrowButton::_moved($path) 0
rename $path ::$path:cmd
proc ::$path { cmd args } "return \[eval ArrowButton::\$cmd $path \$args\]"
return $path
}
# -----------------------------------------------------------------------------
# Command ArrowButton::configure
# -----------------------------------------------------------------------------
proc ArrowButton::configure { path args } {
set res [Widget::configure $path $args]
set ch1 [expr {[Widget::hasChanged $path -width w] |
[Widget::hasChanged $path -height h] |
[Widget::hasChanged $path -borderwidth bd] |
[Widget::hasChanged $path -highlightthickness ht]}]
set ch2 [expr {[Widget::hasChanged $path -type val] |
[Widget::hasChanged $path -ipadx val] |
[Widget::hasChanged $path -ipady val] |
[Widget::hasChanged $path -arrowbd val] |
[Widget::hasChanged $path -clean val] |
[Widget::hasChanged $path -dir val]}]
if { $ch1 } {
set pad [expr {2*($bd+$ht)}]
$path.c configure \
-width [expr {$w-$pad}] -height [expr {$h-$pad}] \
-borderwidth $bd -highlightthickness $ht
set ch2 1
}
if { $ch2 } {
_redraw_whole $path [winfo width $path] [winfo height $path]
} else {
_redraw_relief $path
_redraw_state $path
}
DynamicHelp::sethelp $path $path.c
return $res
}
# -----------------------------------------------------------------------------
# Command ArrowButton::cget
# -----------------------------------------------------------------------------
proc ArrowButton::cget { path option } {
return [Widget::cget $path $option]
}
# ------------------------------------------------------------------------------
# Command ArrowButton::invoke
# ------------------------------------------------------------------------------
proc ArrowButton::invoke { path } {
if { ![string equal [winfo class $path] "ArrowButton"] } {
set path [winfo parent $path]
}
if { [string compare [Widget::getoption $path -state] "disabled"] } {
set oldstate [Widget::getoption $path -state]
if { ![string compare [Widget::getoption $path -type] "button"] } {
set oldrelief [Widget::getoption $path -relief]
configure $path -state active -relief sunken
} else {
set oldrelief [Widget::getoption $path -arrowrelief]
configure $path -state active -arrowrelief sunken
}
update idletasks
if { [set cmd [Widget::getoption $path -armcommand]] != "" } {
uplevel \#0 $cmd
}
after 10
if { ![string compare [Widget::getoption $path -type] "button"] } {
configure $path -state $oldstate -relief $oldrelief
} else {
configure $path -state $oldstate -arrowrelief $oldrelief
}
if { [set cmd [Widget::getoption $path -disarmcommand]] != "" } {
uplevel \#0 $cmd
}
if { [set cmd [Widget::getoption $path -command]] != "" } {
uplevel \#0 $cmd
}
}
}
# ------------------------------------------------------------------------------
# Command ArrowButton::_redraw
# ------------------------------------------------------------------------------
proc ArrowButton::_redraw { path width height } {
variable _moved
set _moved($path) 0
set type [Widget::getoption $path -type]
set dir [Widget::getoption $path -dir]
set bd [expr {[$path.c cget -borderwidth] + [$path.c cget -highlightthickness] + 1}]
set clean [Widget::getoption $path -clean]
if { ![string compare $type "arrow"] } {
if { [set id [$path.c find withtag rect]] == "" } {
$path.c create rectangle $bd $bd [expr {$width-$bd-1}] [expr {$height-$bd-1}] -tags rect
} else {
$path.c coords $id $bd $bd [expr {$width-$bd-1}] [expr {$height-$bd-1}]
}
$path.c lower rect
set arrbd [Widget::getoption $path -arrowbd]
set bd [expr {$bd+$arrbd-1}]
} else {
$path.c delete rect
}
# w and h are max width and max height of arrow
set w [expr {$width - 2*([Widget::getoption $path -ipadx]+$bd)}]
set h [expr {$height - 2*([Widget::getoption $path -ipady]+$bd)}]
if { $w < 2 } {set w 2}
if { $h < 2 } {set h 2}
if { $clean > 0 } {
# arrange for base to be odd
if { ![string compare $dir "top"] ||
![string compare $dir "bottom"] } {
if { !($w % 2) } {
incr w -1
}
if { $clean == 2 } {
# arrange for h = (w+1)/2
set h2 [expr {($w+1)/2}]
if { $h2 > $h } {
set w [expr {2*$h-1}]
} else {
set h $h2
}
}
} else {
if { !($h % 2) } {
incr h -1
}
if { $clean == 2 } {
# arrange for w = (h+1)/2
set w2 [expr {($h+1)/2}]
if { $w2 > $w } {
set h [expr {2*$w-1}]
} else {
set w $w2
}
}
}
}
set x0 [expr {($width-$w)/2}]
set y0 [expr {($height-$h)/2}]
set x1 [expr {$x0+$w-1}]
set y1 [expr {$y0+$h-1}]
switch $dir {
top {
set xd [expr {($x0+$x1)/2}]
if { [set id [$path.c find withtag poly]] == "" } {
$path.c create polygon $x0 $y1 $x1 $y1 $xd $y0 -tags poly
} else {
$path.c coords $id $x0 $y1 $x1 $y1 $xd $y0
}
if { ![string compare $type "arrow"] } {
if { [set id [$path.c find withtag bot]] == "" } {
$path.c create line $x0 $y1 $x1 $y1 $xd $y0 -tags bot
} else {
$path.c coords $id $x0 $y1 $x1 $y1 $xd $y0
}
if { [set id [$path.c find withtag top]] == "" } {
$path.c create line $x0 $y1 $xd $y0 -tags top
} else {
$path.c coords $id $x0 $y1 $xd $y0
}
$path.c itemconfigure top -width $arrbd
$path.c itemconfigure bot -width $arrbd
} else {
$path.c delete top
$path.c delete bot
}
}
bottom {
set xd [expr {($x0+$x1)/2}]
if { [set id [$path.c find withtag poly]] == "" } {
$path.c create polygon $x1 $y0 $x0 $y0 $xd $y1 -tags poly
} else {
$path.c coords $id $x1 $y0 $x0 $y0 $xd $y1
}
if { ![string compare $type "arrow"] } {
if { [set id [$path.c find withtag top]] == "" } {
$path.c create line $x1 $y0 $x0 $y0 $xd $y1 -tags top
} else {
$path.c coords $id $x1 $y0 $x0 $y0 $xd $y1
}
if { [set id [$path.c find withtag bot]] == "" } {
$path.c create line $x1 $y0 $xd $y1 -tags bot
} else {
$path.c coords $id $x1 $y0 $xd $y1
}
$path.c itemconfigure top -width $arrbd
$path.c itemconfigure bot -width $arrbd
} else {
$path.c delete top
$path.c delete bot
}
}
left {
set yd [expr {($y0+$y1)/2}]
if { [set id [$path.c find withtag poly]] == "" } {
$path.c create polygon $x1 $y0 $x1 $y1 $x0 $yd -tags poly
} else {
$path.c coords $id $x1 $y0 $x1 $y1 $x0 $yd
}
if { ![string compare $type "arrow"] } {
if { [set id [$path.c find withtag bot]] == "" } {
$path.c create line $x1 $y0 $x1 $y1 $x0 $yd -tags bot
} else {
$path.c coords $id $x1 $y0 $x1 $y1 $x0 $yd
}
if { [set id [$path.c find withtag top]] == "" } {
$path.c create line $x1 $y0 $x0 $yd -tags top
} else {
$path.c coords $id $x1 $y0 $x0 $yd
}
$path.c itemconfigure top -width $arrbd
$path.c itemconfigure bot -width $arrbd
} else {
$path.c delete top
$path.c delete bot
}
}
right {
set yd [expr {($y0+$y1)/2}]
if { [set id [$path.c find withtag poly]] == "" } {
$path.c create polygon $x0 $y1 $x0 $y0 $x1 $yd -tags poly
} else {
$path.c coords $id $x0 $y1 $x0 $y0 $x1 $yd
}
if { ![string compare $type "arrow"] } {
if { [set id [$path.c find withtag top]] == "" } {
$path.c create line $x0 $y1 $x0 $y0 $x1 $yd -tags top
} else {
$path.c coords $id $x0 $y1 $x0 $y0 $x1 $yd
}
if { [set id [$path.c find withtag bot]] == "" } {
$path.c create line $x0 $y1 $x1 $yd -tags bot
} else {
$path.c coords $id $x0 $y1 $x1 $yd
}
$path.c itemconfigure top -width $arrbd
$path.c itemconfigure bot -width $arrbd
} else {
$path.c delete top
$path.c delete bot
}
}
}
}
# ------------------------------------------------------------------------------
# Command ArrowButton::_redraw_state
# ------------------------------------------------------------------------------
proc ArrowButton::_redraw_state { path } {
set state [Widget::getoption $path -state]
if { ![string compare [Widget::getoption $path -type] "button"] } {
switch $state {
normal {set bg -background; set fg -foreground}
active {set bg -activebackground; set fg -activeforeground}
disabled {set bg -background; set fg -disabledforeground}
}
set fg [Widget::getoption $path $fg]
$path.c configure -background [Widget::getoption $path $bg]
$path.c itemconfigure poly -fill $fg -outline $fg
} else {
switch $state {
normal {set stipple ""; set bg [Widget::getoption $path -background] }
active {set stipple ""; set bg [Widget::getoption $path -activebackground] }
disabled {set stipple gray50; set bg black }
}
set thrc [Widget::getoption $path -troughcolor]
$path.c configure -background [Widget::getoption $path -background]
$path.c itemconfigure rect -fill $thrc -outline $thrc
$path.c itemconfigure poly -fill $bg -outline $bg -stipple $stipple
}
}
# ------------------------------------------------------------------------------
# Command ArrowButton::_redraw_relief
# ------------------------------------------------------------------------------
proc ArrowButton::_redraw_relief { path } {
variable _moved
if { ![string compare [Widget::getoption $path -type] "button"] } {
if { ![string compare [Widget::getoption $path -relief] "sunken"] } {
if { !$_moved($path) } {
$path.c move poly 1 1
set _moved($path) 1
}
} else {
if { $_moved($path) } {
$path.c move poly -1 -1
set _moved($path) 0
}
}
} else {
set col3d [BWidget::get3dcolor $path [Widget::getoption $path -background]]
switch [Widget::getoption $path -arrowrelief] {
raised {set top [lindex $col3d 1]; set bot [lindex $col3d 0]}
sunken {set top [lindex $col3d 0]; set bot [lindex $col3d 1]}
}
$path.c itemconfigure top -fill $top
$path.c itemconfigure bot -fill $bot
}
}
# ------------------------------------------------------------------------------
# Command ArrowButton::_redraw_whole
# ------------------------------------------------------------------------------
proc ArrowButton::_redraw_whole { path width height } {
_redraw $path $width $height
_redraw_relief $path
_redraw_state $path
}
# ------------------------------------------------------------------------------
# Command ArrowButton::_destroy
# ------------------------------------------------------------------------------
proc ArrowButton::_destroy { path } {
variable _moved
Widget::destroy $path
unset _moved($path)
rename $path {}
}
# ------------------------------------------------------------------------------
# Command ArrowButton::_enter
# ------------------------------------------------------------------------------
proc ArrowButton::_enter { path } {
variable _grab
set path [winfo parent $path]
set _grab(current) $path
if { [string compare [Widget::getoption $path -state] "disabled"] } {
set _grab(oldstate) [Widget::getoption $path -state]
configure $path -state active
if { $_grab(pressed) == $path } {
if { ![string compare [Widget::getoption $path -type] "button"] } {
set _grab(oldrelief) [Widget::getoption $path -relief]
configure $path -relief sunken
} else {
set _grab(oldrelief) [Widget::getoption $path -arrowrelief]
configure $path -arrowrelief sunken
}
}
}
}
# ------------------------------------------------------------------------------
# Command ArrowButton::_leave
# ------------------------------------------------------------------------------
proc ArrowButton::_leave { path } {
variable _grab
set path [winfo parent $path]
set _grab(current) ""
if { [string compare [Widget::getoption $path -state] "disabled"] } {
configure $path -state $_grab(oldstate)
if { $_grab(pressed) == $path } {
if { ![string compare [Widget::getoption $path -type] "button"] } {
configure $path -relief $_grab(oldrelief)
} else {
configure $path -arrowrelief $_grab(oldrelief)
}
}
}
}
# ------------------------------------------------------------------------------
# Command ArrowButton::_press
# ------------------------------------------------------------------------------
proc ArrowButton::_press { path } {
variable _grab
set path [winfo parent $path]
if { [string compare [Widget::getoption $path -state] "disabled"] } {
set _grab(pressed) $path
if { ![string compare [Widget::getoption $path -type] "button"] } {
set _grab(oldrelief) [Widget::getoption $path -relief]
configure $path -relief sunken
} else {
set _grab(oldrelief) [Widget::getoption $path -arrowrelief]
configure $path -arrowrelief sunken
}
if { [set cmd [Widget::getoption $path -armcommand]] != "" } {
uplevel \#0 $cmd
if { [set delay [Widget::getoption $path -repeatdelay]] > 0 ||
[set delay [Widget::getoption $path -repeatinterval]] > 0 } {
after $delay "ArrowButton::_repeat $path"
}
}
}
}
# ------------------------------------------------------------------------------
# Command ArrowButton::_release
# ------------------------------------------------------------------------------
proc ArrowButton::_release { path } {
variable _grab
set path [winfo parent $path]
if { $_grab(pressed) == $path } {
set _grab(pressed) ""
if { ![string compare [Widget::getoption $path -type] "button"] } {
configure $path -relief $_grab(oldrelief)
} else {
configure $path -arrowrelief $_grab(oldrelief)
}
if { [set cmd [Widget::getoption $path -disarmcommand]] != "" } {
uplevel \#0 $cmd
}
if { $_grab(current) == $path &&
[string compare [Widget::getoption $path -state] "disabled"] &&
[set cmd [Widget::getoption $path -command]] != "" } {
uplevel \#0 $cmd
}
}
}
# ------------------------------------------------------------------------------
# Command ArrowButton::_repeat
# ------------------------------------------------------------------------------
proc ArrowButton::_repeat { path } {
variable _grab
if { $_grab(current) == $path && $_grab(pressed) == $path &&
[string compare [Widget::getoption $path -state] "disabled"] &&
[set cmd [Widget::getoption $path -armcommand]] != "" } {
uplevel \#0 $cmd
}
if { $_grab(pressed) == $path &&
([set delay [Widget::getoption $path -repeatinterval]] > 0 ||
[set delay [Widget::getoption $path -repeatdelay]] > 0) } {
after $delay "ArrowButton::_repeat $path"
}
}
|