Changeset 1929 for trunk/gui/scripts/controls.tcl
- Timestamp:
- Oct 22, 2010 4:06:10 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gui/scripts/controls.tcl
r1624 r1929 51 51 private variable _scheme "" ;# layout scheme (tabs/hlabels) 52 52 } 53 53 54 54 itk::usual Controls { 55 55 } … … 72 72 73 73 set _tabs [blt::tabset $f.tabs -borderwidth 0 -relief flat \ 74 75 76 74 -side top -tearoff 0 -highlightthickness 0 \ 75 -selectbackground $itk_option(-background) \ 76 -selectcommand [itcl::code $this _changeTabs]] 77 77 78 78 set _frame [frame $f.inner] … … 90 90 # 91 91 bind Controls-$this <Configure> \ 92 92 [list $_dispatcher event -idle !resize] 93 93 94 94 eval itk_initialize $args … … 115 115 itcl::body Rappture::Controls::insert {pos path} { 116 116 if {"end" == $pos} { 117 117 set pos [llength $_controls] 118 118 } elseif {![string is integer $pos]} { 119 119 error "bad index \"$pos\": should be integer or \"end\"" 120 120 } 121 121 … … 133 133 set _name2info($name-type) $type 134 134 switch -- $type { 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 135 choice { 136 Rappture::ChoiceEntry $w $_owner $path 137 bind $w <<Value>> [itcl::code $this _controlChanged $name] 138 } 139 group { 140 Rappture::GroupEntry $w $_owner $path 141 } 142 loader { 143 Rappture::Loader $w $_owner $path -tool [$_owner tool] 144 bind $w <<Value>> [itcl::code $this _controlChanged $name] 145 } 146 number { 147 Rappture::NumberEntry $w $_owner $path 148 bind $w <<Value>> [itcl::code $this _controlChanged $name] 149 } 150 integer { 151 Rappture::IntegerEntry $w $_owner $path 152 bind $w <<Value>> [itcl::code $this _controlChanged $name] 153 } 154 boolean { 155 Rappture::BooleanEntry $w $_owner $path 156 bind $w <<Value>> [itcl::code $this _controlChanged $name] 157 } 158 string { 159 Rappture::TextEntry $w $_owner $path 160 bind $w <<Value>> [itcl::code $this _controlChanged $name] 161 } 162 image { 163 Rappture::ImageEntry $w $_owner $path 164 } 165 control { 166 set label [$_owner xml get $path.label] 167 if {"" == $label} { set label "Simulate" } 168 set service [$_owner xml get $path.service] 169 button $w -text $label -command [list $service run] 170 } 171 separator { 172 # no widget to create 173 set _name2info($name-value) "--" 174 } 175 note { 176 Rappture::Note $w $_owner $path 177 } 178 periodicelement { 179 Rappture::PeriodicElementEntry $w $_owner $path 180 bind $w <<Value>> [itcl::code $this _controlChanged $name] 181 } 182 default { 183 error "don't know how to add control type \"$type\"" 184 } 185 185 } 186 186 … … 191 191 set enable [string trim [$_owner xml get $path.about.enable]] 192 192 if {"" == $enable} { 193 193 set enable yes 194 194 } 195 195 if {![string is boolean $enable]} { 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 196 set re {([a-zA-Z_]+[0-9]*|\([^\(\)]+\)|[a-zA-Z_]+[0-9]*\([^\(\)]+\))(\.([a-zA-Z_]+[0-9]*|\([^\(\)]+\)|[a-zA-Z_]+[0-9]*\([^\(\)]+\)))*(:[-a-zA-Z0-9/]+)?} 197 set rest $enable 198 set enable "" 199 set deps "" 200 while {1} { 201 if {[regexp -indices $re $rest match]} { 202 foreach {s0 s1} $match break 203 204 if {[string index $rest [expr {$s0-1}]] == "\"" 205 && [string index $rest [expr {$s1+1}]] == "\""} { 206 # string in ""'s? then leave it alone 207 append enable [string range $rest 0 $s1] 208 set rest [string range $rest [expr {$s1+1}] end] 209 } else { 210 # 211 # This is a symbol which should be substituted 212 # it can be either: 213 # input.foo.bar 214 # input.foo.bar:units 215 # 216 set cpath [string range $rest $s0 $s1] 217 set parts [split $cpath :] 218 set ccpath [lindex $parts 0] 219 set units [lindex $parts 1] 220 221 # make sure we have the standard path notation 222 set stdpath [$_owner regularize $ccpath] 223 if {"" == $stdpath} { 224 puts stderr "WARNING: don't recognize parameter $cpath in <enable> expression for $path. This may be buried in a structure that is not yet loaded." 225 set stdpath $ccpath 226 } 227 # substitute [_controlValue ...] call in place of path 228 append enable [string range $rest 0 [expr {$s0-1}]] 229 append enable [format {[_controlValue %s %s]} $stdpath $units] 230 lappend deps $stdpath 231 set rest [string range $rest [expr {$s1+1}] end] 232 } 233 } else { 234 append enable $rest 235 break 236 } 237 } 238 239 foreach cpath $deps { 240 $_owner dependenciesfor $cpath $path 241 } 242 242 } 243 243 set _name2info($name-enable) $enable … … 246 246 247 247 if {[lsearch {control group separator note} $type] < 0} { 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 248 # make a label for this control 249 set label [$w label] 250 if {"" != $label} { 251 set _name2info($name-label) $_frame.l$name 252 set font [option get $itk_component(hull) labelFont Font] 253 label $_name2info($name-label) -text [_formatLabel $label] \ 254 -font $font 255 } 256 257 # register the tooltip for this control 258 set tip [$w tooltip] 259 if {"" != $tip} { 260 Rappture::Tooltip::for $w $tip 261 262 # add the tooltip to the label too, if there is one 263 if {$_name2info($name-label) != ""} { 264 Rappture::Tooltip::for $_name2info($name-label) $tip 265 } 266 } 267 267 } 268 268 … … 290 290 itcl::body Rappture::Controls::delete {first {last ""}} { 291 291 if {$last == ""} { 292 292 set last $first 293 293 } 294 294 if {![regexp {^[0-9]+|end$} $first]} { 295 295 error "bad index \"$first\": should be integer or \"end\"" 296 296 } 297 297 if {![regexp {^[0-9]+|end$} $last]} { 298 298 error "bad index \"$last\": should be integer or \"end\"" 299 299 } 300 300 301 301 foreach name [lrange $_controls $first $last] { 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 302 _monitor $name off 303 304 if {"" != $_name2info($name-label)} { 305 destroy $_name2info($name-label) 306 } 307 if {"" != $_name2info($name-value)} { 308 destroy $_name2info($name-value) 309 } 310 $_owner widgetfor $_name2info($name-path) "" 311 312 unset _name2info($name-path) 313 unset _name2info($name-label) 314 unset _name2info($name-type) 315 unset _name2info($name-value) 316 unset _name2info($name-enable) 317 317 } 318 318 set _controls [lreplace $_controls $first $last] … … 330 330 set i [lsearch $_controls $name] 331 331 if {$i >= 0} { 332 332 return $i 333 333 } 334 334 if {[regexp {^@([0-9]+)$} $name match i]} { 335 335 return $i 336 336 } 337 337 if {$name == "end"} { 338 338 return [expr {[llength $_controls]-1}] 339 339 } 340 340 error "bad control name \"$name\": should be @int or one of [join [lsort $_controls] {, }]" … … 353 353 itcl::body Rappture::Controls::control {args} { 354 354 if {[llength $args] == 0} { 355 355 return $_controls 356 356 } 357 357 Rappture::getopts args params { 358 359 360 361 358 flag switch -value default 359 flag switch -label 360 flag switch -path 361 flag switch -enable 362 362 } 363 363 if {[llength $args] == 0} { 364 364 error "missing control name" 365 365 } 366 366 set i [index [lindex $args 0]] … … 394 394 # 395 395 foreach name $_controls { 396 397 398 399 400 401 396 foreach elem {label value} { 397 set w $_name2info($name-$elem) 398 if {$w != "" && [winfo exists $w]} { 399 grid forget $w 400 } 401 } 402 402 } 403 403 if {[$_tabs size] > 0} { 404 404 $_tabs delete 0 end 405 405 } 406 406 grid forget $_frame.empty … … 412 412 set showing "" 413 413 foreach name $_controls { 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 414 set show 1 415 set cond $_name2info($name-enable) 416 if {[string is boolean $cond] && !$cond} { 417 # hard-coded "off" -- ignore completely 418 } elseif {[catch {expr $cond} show] == 0} { 419 set type $_name2info($name-type) 420 set lwidget $_name2info($name-label) 421 set vwidget $_name2info($name-value) 422 if {[lsearch -exact {group image structure} $type] >= 0} { 423 if {$show} { 424 lappend showing $name 425 } else { 426 lappend hidden $name 427 } 428 } else { 429 # show other objects, but enable/disable them 430 lappend showing $name 431 if {$show} { 432 if {[winfo exists $vwidget]} { 433 $vwidget configure -state normal 434 } 435 if {[winfo exists $lwidget]} { 436 $lwidget configure -foreground \ 437 [lindex [$lwidget configure -foreground] 3] 438 } 439 } else { 440 if {[winfo exists $vwidget]} { 441 $vwidget configure -state disabled 442 } 443 if {[winfo exists $lwidget]} { 444 $lwidget configure -foreground gray 445 } 446 } 447 } 448 } else { 449 bgerror "Error in <enable> expression for \"$_name2info($name-path)\":\n $show" 450 } 451 451 } 452 452 … … 460 460 # 461 461 if {[llength $showing] >= 2} { 462 463 464 465 466 467 468 469 470 471 472 462 # assume tabs for multiple groups 463 set _scheme tabs 464 foreach name $showing { 465 set w $_name2info($name-value) 466 467 if {$w == "--" || [winfo class $w] != "GroupEntry"} { 468 # something other than a group? then fall back on hlabels 469 set _scheme hlabels 470 break 471 } 472 } 473 473 } else { 474 474 set _scheme hlabels 475 475 } 476 476 477 477 switch -- $_scheme { 478 478 tabs { 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 479 # 480 # SCHEME: tabs 481 # put a series of groups into a tabbed notebook 482 # 483 484 # use inner frame within tabs to show current group 485 pack $_tabs -before $_frame -fill x 486 487 set gn 1 488 foreach name $showing { 489 set wv $_name2info($name-value) 490 $wv configure -heading no 491 492 set label [$wv component heading cget -text] 493 if {"" == $label} { 494 set label "Group #$gn" 495 } 496 set _name2info($name-label) $label 497 498 $_tabs insert end $label \ 499 -activebackground $itk_option(-background) 500 501 incr gn 502 } 503 504 # compute the overall size 505 # BE CAREFUL: do this after setting "-heading no" above 506 $_dispatcher event -now !resize 507 508 grid propagate $_frame off 509 grid columnconfigure $_frame 0 -weight 1 510 grid rowconfigure $_frame 0 -weight 1 511 512 $_tabs select 0; _changeTabs 513 513 } 514 514 515 515 hlabels { 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 606 607 608 516 # 517 # SCHEME: hlabels 518 # simple "Label: Value" layout 519 # 520 pack forget $_tabs 521 grid propagate $_frame on 522 grid columnconfigure $_frame 0 -weight 0 523 grid rowconfigure $_frame 0 -weight 0 524 525 set expand 0 ;# most controls float to top 526 set row 0 527 foreach name $showing { 528 set wl $_name2info($name-label) 529 if {$wl != "" && [winfo exists $wl]} { 530 grid $wl -row $row -column 0 -sticky e 531 } 532 533 set wv $_name2info($name-value) 534 if {$wv != "" && [winfo exists $wv]} { 535 if {$wl != ""} { 536 grid $wv -row $row -column 1 -sticky ew 537 } else { 538 grid $wv -row $row -column 0 -columnspan 2 -sticky ew 539 } 540 541 grid rowconfigure $_frame $row -weight 0 542 543 switch -- [winfo class $wv] { 544 TextEntry { 545 if {[regexp {[0-9]+x[0-9]+} [$wv size]]} { 546 grid $wl -sticky n -pady 4 547 grid $wv -sticky nsew 548 grid rowconfigure $_frame $row -weight 1 549 grid columnconfigure $_frame 1 -weight 1 550 set expand 1 551 } 552 } 553 GroupEntry { 554 $wv configure -heading yes 555 556 # 557 # Scan through all children in this group 558 # and see if any demand more space. If the 559 # group contains a structure or a note, then 560 # make sure that the group itself is set to 561 # expand/fill. 562 # 563 set queue [winfo children $wv] 564 set expandgroup 0 565 while {[llength $queue] > 0} { 566 set w [lindex $queue 0] 567 set queue [lrange $queue 1 end] 568 set c [winfo class $w] 569 if {[lsearch {DeviceEditor Note} $c] >= 0} { 570 set expandgroup 1 571 break 572 } 573 eval lappend queue [winfo children $w] 574 } 575 if {$expandgroup} { 576 set expand 1 577 grid $wv -sticky nsew 578 grid rowconfigure $_frame $row -weight 1 579 } 580 } 581 Note { 582 grid $wv -sticky nsew 583 grid rowconfigure $_frame $row -weight 1 584 set expand 1 585 } 586 } 587 grid columnconfigure $_frame 1 -weight 1 588 } elseif {$wv == "--"} { 589 grid rowconfigure $_frame $row -minsize 10 590 } 591 592 incr row 593 grid rowconfigure $_frame $row -minsize $itk_option(-padding) 594 incr row 595 } 596 grid $_frame.empty -row $row 597 598 # 599 # If there are any hidden items, then make the bottom of 600 # this form fill up any extra space, so the form floats 601 # to the top. Otherwise, it will jitter around as the 602 # hidden items come and go. 603 # 604 if {[llength $hidden] > 0 && !$expand} { 605 grid rowconfigure $_frame 99 -weight 1 606 } else { 607 grid rowconfigure $_frame 99 -weight 0 608 } 609 609 } 610 610 } … … 623 623 set wv $_name2info($name-value) 624 624 if {$wv == "--" || [catch {bindtags $wv} btags]} { 625 625 return 626 626 } 627 627 set i [lsearch $btags $tag] 628 628 629 629 if {$state} { 630 631 632 630 if {$i < 0} { 631 bindtags $wv [linsert $btags 0 $tag] 632 } 633 633 } else { 634 635 636 634 if {$i >= 0} { 635 bindtags $wv [lreplace $btags $i $i] 636 } 637 637 } 638 638 } … … 651 651 # 652 652 if {"" != $_owner} { 653 653 $_owner changed $path 654 654 } 655 655 } … … 663 663 itcl::body Rappture::Controls::_controlValue {path {units ""}} { 664 664 if {"" != $_owner} { 665 666 667 668 669 665 set val [$_owner valuefor $path] 666 if {"" != $units} { 667 set val [Rappture::Units::convert $val -to $units -units off] 668 } 669 return $val 670 670 } 671 671 return "" … … 682 682 set str [string trim $str] 683 683 if {"" != $str && [string index $str end] != ":"} { 684 684 append str ":" 685 685 } 686 686 return $str … … 702 702 set name [lindex $_showing $i] 703 703 if {"" != $name} { 704 705 706 707 708 709 704 foreach w [grid slaves $_frame] { 705 grid forget $w 706 } 707 708 set wv $_name2info($name-value) 709 grid $wv -row 0 -column 0 -sticky new 710 710 } 711 711 } … … 718 718 itcl::body Rappture::Controls::_resize {} { 719 719 switch -- $_scheme { 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 720 tabs { 721 # compute the overall size 722 # BE CAREFUL: do this after setting "-heading no" above 723 set maxw 0 724 set maxh 0 725 update idletasks 726 foreach name $_controls { 727 set wv $_name2info($name-value) 728 set w [winfo reqwidth $wv] 729 if {$w > $maxw} { set maxw $w } 730 set h [winfo reqheight $wv] 731 if {$h > $maxh} { set maxh $h } 732 } 733 $_frame configure -width $maxw -height $maxh 734 } 735 hlabels { 736 # do nothing 737 } 738 738 } 739 739 }
Note: See TracChangeset
for help on using the changeset viewer.