source: trunk/gui/scripts/balloon.tcl @ 1399

Last change on this file since 1399 was 1394, checked in by gah, 16 years ago

sequenceresult.tcl:

Playing a sequence continuously and then clicking "Clear" will generate an
infinite number of automatic error 'invalid command name ""' messages.

_fixValue Check for _topmost (top most data object) to be "".

Deletes the viewer.

delete Calls pause method to stop the player.

balloon.tcl:

Bandaid to fix 'bad geometry specifier "-44x184+864+247"' errors. This has to
do with the placement of the popup menu when clicking on axes in the xyresult
widget. Added a test if the width or height is negative and clip the geometry
to +x+y.

xyresult.tcl:

Graph issues 'can't find element "elem1"' error when attempting to
to deactivate an element that doesn't exist. The highlighted element name
is stored in the variable _hilite(elem). It goes out of sync with the graph.
Test for the existence of the element in the graph explicitly.

File size: 17.5 KB
RevLine 
[46]1# ----------------------------------------------------------------------
2#  COMPONENT: Balloon - toplevel popup window, like a cartoon balloon
3#
4#  This widget is used as a container for pop-up panels in Rappture.
5#  If shape extensions are supported, then the window is drawn as
6#  a cartoon balloon.  Otherwise, it is a raised panel, with a little
7#  line connecting it to the widget that brought it up.  When active,
8#  the panel has a global grab and focus, so the user must interact
9#  with it or dismiss it.
10# ======================================================================
11#  AUTHOR:  Michael McLennan, Purdue University
[115]12#  Copyright (c) 2004-2005  Purdue Research Foundation
13#
14#  See the file "license.terms" for information on usage and
15#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
[46]16# ======================================================================
17package require Itk
18
19namespace eval Rappture { # forward declaration }
20
[413]21option add *Balloon.dismissButton on widgetDefault
22option add *Balloon.padX 4 widgetDefault
23option add *Balloon.padY 4 widgetDefault
24option add *Balloon.titleBackground #999999 widgetDefault
25option add *Balloon.titleForeground white widgetDefault
26option add *Balloon.relief raised widgetDefault
[46]27option add *Balloon.stemLength 16 widgetDefault
28
29itcl::class Rappture::Balloon {
30    inherit itk::Toplevel
31
[413]32    itk_option define -deactivatecommand deactivateCommand DeactivateCommand ""
33    itk_option define -dismissbutton dismissButton DismissButton "on"
34    itk_option define -padx padX Pad 0
35    itk_option define -pady padY Pad 0
36    itk_option define -title title Title ""
37    itk_option define -titlebackground titleBackground Background ""
38    itk_option define -titleforeground titleForeground Foreground ""
[46]39    itk_option define -stemlength stemLength StemLength 20
40
41    constructor {args} { # defined below }
42
43    public method activate {where placement}
44    public method deactivate {}
45
46    protected method _createStems {}
47
48    protected variable _stems   ;# windows for cartoon balloon stems
49    protected variable _masks   ;# masks for cartoon balloon stems
50    protected variable _fills   ;# lines for cartoon balloon stems
51
52    public proc outside {widget x y}
53
54    bind RapptureBalloon <ButtonPress> \
[1342]55        {if {[Rappture::Balloon::outside %W %X %Y]} {%W deactivate}}
[46]56}
57
58itk::usual Balloon {
59}
60
61# ----------------------------------------------------------------------
62# CONSTRUCTOR
63# ----------------------------------------------------------------------
64itcl::body Rappture::Balloon::constructor {args} {
65    wm overrideredirect $itk_component(hull) yes
66    wm withdraw $itk_component(hull)
[413]67    component hull configure -borderwidth 1 -relief solid -padx 0 -pady 0
[46]68
[413]69    itk_component add border {
[1342]70        frame $itk_interior.border -borderwidth 2
[413]71    } {
[1342]72        usual
73        keep -relief
[413]74    }
75    pack $itk_component(border) -expand yes -fill both
76
77    itk_component add titlebar {
[1342]78        frame $itk_component(border).tbar
[413]79    } {
[1342]80        usual
81        rename -background -titlebackground titleBackground Background
[413]82    }
83
84    itk_component add title {
[1342]85        label $itk_component(titlebar).title -width 1 -anchor w
[413]86    } {
[1342]87        usual
88        rename -background -titlebackground titleBackground Background
89        rename -foreground -titleforeground titleForeground Foreground
90        rename -highlightbackground -titlebackground titleBackground Background
91        rename -text -title title Title
[413]92    }
93    pack $itk_component(title) -side left -expand yes -fill both -padx 2
94
95    itk_component add dismiss {
[1342]96        button $itk_component(titlebar).dismiss \
97            -bitmap [Rappture::icon dismiss] \
98            -relief flat -overrelief raised -command "
99              Rappture::Tooltip::cue hide
100              [list $itk_component(hull) deactivate]
101            "
[413]102    } {
[1342]103        usual
104        rename -background -titlebackground titleBackground Background
105        rename -foreground -titleforeground titleForeground Foreground
106        rename -highlightbackground -titlebackground titleBackground Background
[413]107    }
108
[46]109    itk_component add inner {
[1342]110        frame $itk_component(border).inner
[46]111    }
112    pack $itk_component(inner) -expand yes -fill both
113
114    # add bindings to release the grab
115    set btags [bindtags $itk_component(hull)]
116    bindtags $itk_component(hull) [linsert $btags 1 RapptureBalloon]
117
118    eval itk_initialize $args
119
120    _createStems
121}
122
123# ----------------------------------------------------------------------
124# USAGE: activate <where> <placement>
125#
126# Clients use this to pop up this balloon panel pointing to the
127# <where> location, which should be a widget name or @X,Y.  The
128# <placement> indicates whether the panel should be left, right,
129# above, or below the <where> coordinate.
130# ----------------------------------------------------------------------
131itcl::body Rappture::Balloon::activate {where placement} {
132    if {![info exists _stems($placement)]} {
[1342]133        error "bad placement \"$placement\": should be [join [lsort [array names _stems]] {, }]"
[46]134    }
135    set s $_stems($placement)
136    set sw [image width $_fills($placement)]
137    set sh [image height $_fills($placement)]
138    set p $itk_component(hull)
[413]139    set screenw [winfo screenwidth $p]
140    set screenh [winfo screenheight $p]
[46]141
142    if {[winfo exists $where]} {
[1342]143        set x [expr {[winfo rootx $where]+[winfo width $where]/2}]
144        set y [expr {[winfo rooty $where]+[winfo height $where]/2}]
145        switch -- $placement {
146            left { set x [expr {[winfo rootx $where]+5}] }
147            right { set x [expr {[winfo rootx $where]+[winfo width $where]-5}] }
148            above { set y [expr {[winfo rooty $where]+5}] }
149            below { set y [expr {[winfo rooty $where]+[winfo height $where]-5}] }
150        }
[46]151    } elseif {[regexp {^@([0-9]+),([0-9]+)$} $where match x y]} {
[1342]152        # got x and y
[46]153    } else {
[1342]154        error "bad location \"$where\": should be widget or @x,y"
[46]155    }
156
157    # if the panel is already up, take it down
158    deactivate
159
[413]160    set pw [winfo reqwidth $p]
161    if {$pw > $screenw} { set pw [expr {$screenw-10}] }
162    set ph [winfo reqheight $p]
163    if {$ph > $screenh} { set ph [expr {$screenh-10}] }
164
[46]165    switch -- $placement {
[1342]166        left {
167            set sx [expr {$x-$sw+3}]
168            set sy [expr {$y-$sh/2}]
169            set px [expr {$sx-$pw+3}]
170            set py [expr {$y-$ph/2}]
[413]171
[1342]172            # make sure that the panel doesn't go off-screen
173            if {$py < 0} { set py 0 }
174            if {$py+$ph > $screenh} { set py [expr {$screenh-$ph}] }
175            if {$px < 0} { set pw [expr {$pw+$px}]; set px 0 }
176        }
177        right {
178            set sx $x
179            set sy [expr {$y-$sh/2}]
180            set px [expr {$x+$sw-3}]
181            set py [expr {$y-$ph/2}]
[413]182
[1342]183            # make sure that the panel doesn't go off-screen
184            if {$py < 0} { set py 0 }
185            if {$py+$ph > $screenh} { set py [expr {$screenh-$ph}] }
186            if {$px+$pw > $screenw} { set pw [expr {$screenw-$px}] }
187        }
188        above {
189            set sx [expr {$x-$sw/2}]
190            set sy [expr {$y-$sh+3}]
191            set px [expr {$x-$pw/2}]
192            set py [expr {$sy-$ph+3}]
[413]193
[1342]194            # make sure that the panel doesn't go off-screen
195            if {$px < 0} { set px 0 }
196            if {$px+$pw > $screenw} { set px [expr {$screenw-$pw}] }
197            if {$py < 0} { set ph [expr {$ph+$py}]; set py 0 }
198        }
199        below {
200            set sx [expr {$x-$sw/2}]
201            set sy $y
202            set px [expr {$x-$pw/2}]
203            set py [expr {$y+$sh-3}]
[413]204
[1342]205            # make sure that the panel doesn't go off-screen
206            if {$px < 0} { set px 0 }
207            if {$px+$pw > $screenw} { set px [expr {$screenw-$pw}] }
208            if {$py+$ph > $screenh} { set ph [expr {$screenh-$py}] }
209        }
[46]210    }
[48]211    if {[info exists _masks($placement)]} {
[1342]212        shape set $s -bound photo $_masks($placement)
[48]213    }
[1394]214    if { $pw < 1 || $ph < 1 }  {
215        # I really don't know why this is happenning.  I believe this occurs
216        # when in a work space (i.e the main window is smaller than the root
217        # window). So for now, better to place the ballon window somewhere
218        # than to fail with a bad geometry.
219        wm geometry $p +$px+$py
220    } else {
221        wm geometry $p ${pw}x${ph}+$px+$py
222    }
[46]223    wm deiconify $p
224    raise $p
225
226    wm geometry $s +$sx+$sy
227    wm deiconify $s
228    raise $s
229
230    # grab the mouse pointer
231    update
232    while {[catch {grab set -global $itk_component(hull)}]} {
[1342]233        after 100
[46]234    }
235    focus $itk_component(hull)
236}
237
238# ----------------------------------------------------------------------
239# USAGE: deactivate
240#
241# Clients use this to take down the balloon panel if it is on screen.
242# ----------------------------------------------------------------------
243itcl::body Rappture::Balloon::deactivate {} {
[52]244    if {[string length $itk_option(-deactivatecommand)] > 0} {
[1342]245        uplevel #0 $itk_option(-deactivatecommand)
[52]246    }
247
[46]248    grab release $itk_component(hull)
249
250    wm withdraw $itk_component(hull)
251    foreach dir {left right above below} {
[1342]252        wm withdraw $_stems($dir)
[46]253    }
254}
255
256# ----------------------------------------------------------------------
257# USAGE: _createStems
258#
259# Used internally to create the stems that connect a balloon panel
260# to its anchor point, in all four possible directions:  left, right,
261# above, and below.
262# ----------------------------------------------------------------------
263itcl::body Rappture::Balloon::_createStems {} {
264    # destroy any existing stems
265    foreach dir [array names _stems] {
[1342]266        destroy $_stems($dir)
267        unset _stems($dir)
[46]268    }
269    foreach dir [array names _masks] {
[1342]270        image delete $_masks($dir)
271        unset _masks($dir)
[46]272    }
273    foreach dir [array names _fills] {
[1342]274        image delete $_fills($dir)
275        unset _fills($dir)
[46]276    }
277
278    if {[catch {package require Shape}] == 0} {
[1342]279        #
280        # We have the Shape extension.  Use it to create nice
281        # looking (triangle-shaped) stems.
282        #
283        set s $itk_option(-stemlength)
284        foreach dir {left right above below} {
285            switch -- $dir {
286                left - right {
287                    set sw [expr {$s+2}]
288                    set sh $s
289                }
290                above - below {
291                    set sw $s
292                    set sh [expr {$s+2}]
293                }
294            }
[46]295
[1342]296            set _stems($dir) [toplevel $itk_interior.s$dir -borderwidth 0]
297            label $_stems($dir).l \
298                -width $sw -height $sh -borderwidth 0
299            pack $_stems($dir).l -expand yes -fill both
[46]300
[1342]301            wm withdraw $_stems($dir)
302            wm overrideredirect $_stems($dir) yes
[46]303
[1342]304            #
305            # Draw the triangle part of the stem, with a black outline
306            # and light/dark highlights:
307            #
308            #     --------  ---       LEFT STEM
309            #    |..##    |  ^ 
310            #    |  ..##  |  |        . = light color
311            #    |    ..##|  | s      @ = dark color
312            #    |    @@##|  |        # = black
313            #    |  @@##  |  |
314            #    |@@##    |  v
315            #     --------  ---
316            #    |<------>|
317            #        s+2
318            #
319            set _masks($dir) [image create photo -width $sw -height $sh]
320            set _fills($dir) [image create photo -width $sw -height $sh]
[46]321
[1342]322            set bg $itk_option(-background)
323            set light [Rappture::color::brightness $bg 0.4]
324            set dark [Rappture::color::brightness $bg -0.4]
325            set rgb [winfo rgb . $bg]
326            set bg [format "#%03x%03x%03x" [lindex $rgb 0] [lindex $rgb 1] [lindex $rgb 2]]
[46]327
[1342]328            $_fills($dir) put $bg -to 0 0 $sw $sh
[46]329
[1342]330            switch -- $dir {
331              left {
332                set i 0
333                for {set j 0} {$j < $s/2} {incr j} {
334                    set ybtm [expr {$s-$j-1}]
335                    $_fills($dir) put $dark \
336                        -to $i [expr {$ybtm-1}] [expr {$i+2}] [expr {$ybtm+1}]
337                    $_fills($dir) put black \
338                        -to [expr {$i+2}] $ybtm [expr {$i+4}] [expr {$ybtm+1}]
[46]339
[1342]340                    set ytop $j
341                    set ytoffs [expr {($j == $s/2-1) ? 1 : 2}]
342                    $_fills($dir) put $light \
343                        -to $i $ytop [expr {$i+2}] [expr {$ytop+$ytoffs}]
344                    $_fills($dir) put black \
345                        -to [expr {$i+2}] $ytop [expr {$i+4}] [expr {$ytop+1}]
346                    incr i 2
347                }
348                $_stems($dir).l configure -image $_fills($dir)
[46]349
[1342]350                $_masks($dir) put black -to 0 0 $sw $sh
351                set i 0
352                for {set j 0} {$j < $s/2} {incr j} {
353                    for {set k [expr {$i+4}]} {$k < $s+2} {incr k} {
354                        $_masks($dir) transparency set $k $j yes
355                        $_masks($dir) transparency set $k [expr {$s-$j-1}] yes
356                    }
357                    incr i 2
358                }
359              }
360              right {
361                set i $sw
362                for {set j 0} {$j < $s/2} {incr j} {
363                    set ybtm [expr {$s-$j-1}]
364                    $_fills($dir) put $dark \
365                        -to [expr {$i-2}] [expr {$ybtm-1}] $i [expr {$ybtm+1}]
366                    $_fills($dir) put black \
367                        -to [expr {$i-4}] $ybtm [expr {$i-2}] [expr {$ybtm+1}]
[46]368
[1342]369                    set ytop $j
370                    set ytoffs [expr {($j == $s/2-1) ? 1 : 2}]
371                    $_fills($dir) put $light \
372                        -to [expr {$i-2}] $ytop $i [expr {$ytop+$ytoffs}]
373                    $_fills($dir) put black \
374                        -to [expr {$i-4}] $ytop [expr {$i-2}] [expr {$ytop+1}]
375                    incr i -2
376                }
377                $_stems($dir).l configure -image $_fills($dir)
[46]378
[1342]379                $_masks($dir) put black -to 0 0 $sw $sh
380                set i $sw
381                for {set j 0} {$j < $s/2} {incr j} {
382                    for {set k 0} {$k < $i-4} {incr k} {
383                        $_masks($dir) transparency set $k $j yes
384                        $_masks($dir) transparency set $k [expr {$s-$j-1}] yes
385                    }
386                    incr i -2
387                }
388              }
389              above {
390                set i 0
391                for {set j 0} {$j < $s/2} {incr j} {
392                    set xrhs [expr {$s-$j-1}]
393                    $_fills($dir) put $dark \
394                        -to [expr {$xrhs-1}] $i [expr {$xrhs+1}] [expr {$i+2}]
395                    $_fills($dir) put black \
396                        -to $xrhs [expr {$i+2}] [expr {$xrhs+1}] [expr {$i+4}]
[46]397
[1342]398                    set xlhs $j
399                    set xloffs [expr {($j == $s/2-1) ? 1 : 2}]
400                    $_fills($dir) put $light \
401                        -to $xlhs $i [expr {$xlhs+$xloffs}] [expr {$i+2}]
402                    $_fills($dir) put black \
403                        -to $xlhs [expr {$i+2}] [expr {$xlhs+1}] [expr {$i+4}]
404                    incr i 2
405                }
406                $_stems($dir).l configure -image $_fills($dir)
[46]407
[1342]408                $_masks($dir) put black -to 0 0 $sw $sh
409                set i 0
410                for {set j 0} {$j < $s/2} {incr j} {
411                    for {set k [expr {$i+4}]} {$k < $s+2} {incr k} {
412                        $_masks($dir) transparency set $j $k yes
413                        $_masks($dir) transparency set [expr {$s-$j-1}] $k yes
414                    }
415                    incr i 2
416                }
417              }
418              below {
419                set i $sh
420                for {set j 0} {$j < $s/2} {incr j} {
421                    set xrhs [expr {$s-$j-1}]
422                    $_fills($dir) put $dark \
423                        -to [expr {$xrhs-1}] [expr {$i-2}] [expr {$xrhs+1}] $i
424                    $_fills($dir) put black \
425                        -to $xrhs [expr {$i-4}] [expr {$xrhs+1}] [expr {$i-2}]
[46]426
[1342]427                    set xlhs $j
428                    set xloffs [expr {($j == $s/2-1) ? 1 : 2}]
429                    $_fills($dir) put $light \
430                        -to $xlhs [expr {$i-2}] [expr {$xlhs+$xloffs}] $i
431                    $_fills($dir) put black \
432                        -to $xlhs [expr {$i-4}] [expr {$xlhs+1}] [expr {$i-2}]
433                    incr i -2
434                }
435                $_stems($dir).l configure -image $_fills($dir)
[46]436
[1342]437                $_masks($dir) put black -to 0 0 $sw $sh
438                set i $sh
439                for {set j 0} {$j < $s/2} {incr j} {
440                    for {set k 0} {$k < $i-4} {incr k} {
441                        $_masks($dir) transparency set $j $k yes
442                        $_masks($dir) transparency set [expr {$s-$j-1}] $k yes
443                    }
444                    incr i -2
445                }
446              }
447            }
448        }
[46]449    } else {
[1342]450        #
451        # No shape extension.  Do the best we can by creating a
452        # black line for all directions.
453        #
454        foreach {dir w h} [list \
455            left   $itk_option(-stemlength) 3 \
456            right  $itk_option(-stemlength) 3 \
457            above  3 $itk_option(-stemlength) \
458            below  3 $itk_option(-stemlength) \
459        ] {
460            set _stems($dir) [toplevel $itk_interior.s$dir \
461                -width $w -height $h \
462                -borderwidth 1 -relief solid -background black]
463            wm withdraw $_stems($dir)
464            wm overrideredirect $_stems($dir) yes
[48]465
[1342]466            # create this for size, even though we don't really use it
467            set _fills($dir) [image create photo -width $w -height $h]
468        }
[46]469    }
470}
471
472# ----------------------------------------------------------------------
473# USAGE: outside <widget> <x> <y>
474#
475# Used internally to see if the click point <x>,<y> is outside of
476# this widget.  If so, the widget usually releases is grab and
477# deactivates.
478# ----------------------------------------------------------------------
479itcl::body Rappture::Balloon::outside {widget x y} {
480    return [expr {$x < [winfo rootx $widget]
[1342]481             || $x > [winfo rootx $widget]+[winfo width $widget]
482             || $y < [winfo rooty $widget]
483             || $y > [winfo rooty $widget]+[winfo height $widget]}]
[46]484}
485
486# ----------------------------------------------------------------------
487# CONFIGURATION OPTION: -stemlength
488#
489# Used internally to create the stems that connect a balloon panel
490# to its anchor point, in all four possible directions:  left, right,
491# above, and below.
492# ----------------------------------------------------------------------
493itcl::configbody Rappture::Balloon::stemlength {
494    if {$itk_option(-stemlength) % 2 != 0} {
[1342]495        error "stem length should be an even number of pixels"
[46]496    }
497}
[413]498
499# ----------------------------------------------------------------------
500# CONFIGURATION OPTION: -dismissbutton
501# ----------------------------------------------------------------------
502itcl::configbody Rappture::Balloon::dismissbutton {
503    if {![string is boolean $itk_option(-dismissbutton)]} {
[1342]504        error "bad value \"$itk_option(-dismissbutton)\": should be on/off, 1/0, true/false, yes/no"
[413]505    }
506    if {$itk_option(-dismissbutton)} {
[1342]507        pack $itk_component(titlebar) -before $itk_component(inner) \
508            -side top -fill x
509        pack $itk_component(dismiss) -side right -padx 4
[413]510    } elseif {"" != $itk_option(-title)} {
[1342]511        pack $itk_component(titlebar) -before $itk_component(inner) \
512            -side top -fill x
513        pack forget $itk_component(dismiss)
[413]514    } else {
[1342]515        pack forget $itk_component(titlebar)
[413]516    }
517}
518
519# ----------------------------------------------------------------------
520# CONFIGURATION OPTION: -padx
521# ----------------------------------------------------------------------
522itcl::configbody Rappture::Balloon::padx {
523    pack $itk_component(inner) -padx $itk_option(-padx)
524}
525
526# ----------------------------------------------------------------------
527# CONFIGURATION OPTION: -pady
528# ----------------------------------------------------------------------
529itcl::configbody Rappture::Balloon::pady {
530    pack $itk_component(inner) -pady $itk_option(-pady)
531}
532
533# ----------------------------------------------------------------------
534# CONFIGURATION OPTION: -title
535# ----------------------------------------------------------------------
536itcl::configbody Rappture::Balloon::title {
537    if {"" != $itk_option(-title) || $itk_option(-dismissbutton)} {
[1342]538        pack $itk_component(titlebar) -before $itk_component(inner) \
539            -side top -fill x
540        if {$itk_option(-dismissbutton)} {
541            pack $itk_component(dismiss) -side right -padx 4
542        } else {
543            pack forget $itk_component(dismiss)
544        }
[413]545    } else {
[1342]546        pack forget $itk_component(titlebar)
[413]547    }
548}
Note: See TracBrowser for help on using the repository browser.