source: trunk/gui/scripts/scroller.tcl @ 11

Last change on this file since 11 was 11, checked in by mmc, 16 years ago

Major reorganization of the entire package. The config.xml file
is now irrelevant. All the action is in the tool.xml file. The
main program now organizes all input into 1) side-by-side pages,
2) input/result (wizard-style) pages, or 3) a series of wizard-
style pages. The <input> can have <phase> parts representing
the various pages.

Added a new ContourResult? widget based on Swaroop's vtk plotting
code.

Also, added easymesh and showmesh to the "tools" directory.
We need these for Eric Polizzi's code.

File size: 9.9 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: scroller - adds scrollbars to any standard Tk widget
3#
4#  This widget provides automatic scrollbars for any standard Tk
5#  widget.  The scrolled widget should be created as a child of this
6#  widget, and is connected by calling the "contents" method.  Calling
7#  contents with the keyword "frame" creates an internal frame that
8#  allows any collection of widgets to be scrolled.
9# ======================================================================
10#  AUTHOR:  Michael McLennan, Purdue University
11#  Copyright (c) 2004-2005
12#  Purdue Research Foundation, West Lafayette, IN
13# ======================================================================
14package require Itk
15
16option add *Scroller.xScrollMode off widgetDefault
17option add *Scroller.yScrollMode auto widgetDefault
18option add *Scroller.width 0 widgetDefault
19option add *Scroller.height 0 widgetDefault
20
21itcl::class Rappture::Scroller {
22    inherit itk::Widget
23
24    itk_option define -xscrollmode xScrollMode XScrollMode ""
25    itk_option define -yscrollmode yScrollMode YScrollMode ""
26    itk_option define -width width Width 0
27    itk_option define -height height Height 0
28
29    constructor {args} { # defined below }
30
31    public method contents {{frame "!@#query"}}
32
33    protected method _widget2sbar {which args}
34    protected method _fixsbar {which {state ""}}
35    protected method _fixframe {which}
36    protected method _lock {option}
37
38    private variable _contents ""   ;# widget being controlled
39    private variable _frame ""      ;# for "contents frame" calls
40    private variable _lock 0        ;# for _lock on x-scrollbar
41}
42                                                                               
43itk::usual Scroller {
44    keep -background -activebackground -activerelief
45    keep -cursor
46    keep -highlightcolor -highlightthickness
47    keep -troughcolor
48}
49
50# ----------------------------------------------------------------------
51# CONSTRUCTOR
52# ----------------------------------------------------------------------
53itcl::body Rappture::Scroller::constructor {args} {
54    itk_component add xsbar {
55        scrollbar $itk_interior.xsbar -orient horizontal
56    }
57    itk_component add ysbar {
58        scrollbar $itk_interior.ysbar -orient vertical
59    }
60
61    grid rowconfigure $itk_component(hull) 0 -weight 1
62    grid columnconfigure $itk_component(hull) 0 -weight 1
63
64    eval itk_initialize $args
65}
66
67# ----------------------------------------------------------------------
68# USAGE: contents ?<widget>|frame?
69#
70# Used to get/set the widget that is being scrolled.  With no args,
71# it returns the name of the widget currently connected to the
72# scrollbars.  Otherwise, the argument specifies a widget to be
73# controlled by the scrollbar.  If the argument is the keyword
74# "frame", then this method creates its own internal frame, which
75# can be packed with other widgets, and returns its name.
76# ----------------------------------------------------------------------
77itcl::body Rappture::Scroller::contents {{widget "!@#query"}} {
78    if {$widget == "!@#query"} {
79        return $_contents
80    }
81
82    #
83    # If the widget is "", then unhook any existing widget.
84    #
85    if {$widget == ""} {
86        if {$_contents != ""} {
87            $_contents configure -xscrollcommand "" -yscrollcommand ""
88            grid forget $_contents
89        }
90        $itk_component(xsbar) configure -command ""
91        $itk_component(ysbar) configure -command ""
92        set _contents ""
93
94        return ""
95    }
96
97    #
98    # For the "frame" keyword, create a canvas that can be scrolled
99    # and return it as the frame being scrolled.
100    #
101    if {$widget == "frame"} {
102        if {$_frame == ""} {
103            set _frame [canvas $itk_component(hull).ifr -highlightthickness 0]
104            frame $_frame.f
105            $_frame create window 0 0 -anchor nw -window $_frame.f -tags frame
106            bind $_frame.f <Configure> [itcl::code $this _fixframe inner]
107            bind $_frame <Configure> [itcl::code $this _fixframe outer]
108        }
109        set widget $_frame
110    }
111
112    #
113    # Plug the specified widget into the scrollbars for this widget.
114    #
115    contents ""
116    grid $widget -row 0 -column 0 -sticky nsew
117    $widget configure \
118        -xscrollcommand [itcl::code $this _widget2sbar x] \
119        -yscrollcommand [itcl::code $this _widget2sbar y]
120
121    $itk_component(xsbar) configure -command [list $widget xview]
122    $itk_component(ysbar) configure -command [list $widget yview]
123    set _contents $widget
124
125    if {$widget == $_frame} {
126        return $_frame.f
127    }
128    return $widget
129}
130
131# ----------------------------------------------------------------------
132# USAGE: _widget2sbar <which> ?args...?
133#
134# Used internally to handle communication from the widget to the
135# scrollbar.  If the scrollbars are in "auto" mode, this provides a
136# hook where we can fix their display.
137# ----------------------------------------------------------------------
138itcl::body Rappture::Scroller::_widget2sbar {which args} {
139    if {$itk_option(-xscrollmode) == "auto"} {
140        _fixsbar x
141    }
142    if {$itk_option(-yscrollmode) == "auto"} {
143        _fixsbar y
144    }
145    eval $itk_component(${which}sbar) set $args
146}
147
148# ----------------------------------------------------------------------
149# USAGE: _fixsbar <which> ?<state>?
150#
151# Used internally to show/hide the scrollbar in the <which> direction,
152# which is either "x" or "y".  If the scrollbar is "on", then it is
153# always displayed.  If "off", never displayed.  And if "auto", then
154# it is displayed if needed for the current view.
155# ----------------------------------------------------------------------
156itcl::body Rappture::Scroller::_fixsbar {which {state ""}} {
157    if {$state == ""} {
158        switch -- [string tolower $itk_option(-${which}scrollmode)] {
159            on - 1 - true - yes  { set state 1 }
160            off - 0 - false - no { set state 0 }
161            auto {
162                set state 0
163                if {$_contents != ""} {
164                    set lims [$_contents ${which}view]
165                    if {[lindex $lims 0] != 0 || [lindex $lims 1] != 1} {
166                        set state 1
167                    }
168                }
169            }
170            default {
171                set state 0
172            }
173        }
174    }
175
176    # show/hide the scrollbar depending on the desired state
177    switch -- $which {
178        x {
179            if {$state} {
180                grid $itk_component(xsbar) -row 1 -column 0 -sticky ew
181                _lock set
182            } else {
183                if {![_lock active]} {
184                    grid forget $itk_component(xsbar)
185                }
186            }
187        }
188        y {
189            if {$state} {
190                grid $itk_component(ysbar) -row 0 -column 1 -sticky ns
191            } else {
192                grid forget $itk_component(ysbar)
193            }
194        }
195    }
196}
197
198# ----------------------------------------------------------------------
199# USAGE: _fixframe <which>
200#
201# Invoked automatically whenever the canvas representing the "frame"
202# keyword is resized.  Updates the scrolling limits for the canvas
203# to the new size.
204# ----------------------------------------------------------------------
205itcl::body Rappture::Scroller::_fixframe {which} {
206    switch -- $which {
207        inner {
208            $_frame configure -scrollregion [$_frame bbox all]
209        }
210        outer {
211            $_frame itemconfigure frame -width [winfo width $_frame]
212        }
213    }
214}
215
216# ----------------------------------------------------------------------
217# USAGE: _lock set
218# USAGE: _lock reset
219# USAGE: _lock active
220#
221# Used internally to lock out vibrations when the x-scrollbar pops
222# into view.  When the x-scrollbar pops up, it reduces the space
223# available for the widget.  For some widgets (e.g., text widget)
224# this changes the view.  A long line may fall off screen, and the
225# x-scrollbar will no longer be necessary.  If the x-scrollbar just
226# appeared, then its lock is active, signalling that it should stay
227# up.
228# ----------------------------------------------------------------------
229itcl::body Rappture::Scroller::_lock {option} {
230    switch -- $option {
231        set {
232            set _lock 1
233            after cancel [itcl::code $this _lock reset]
234            after 50 [itcl::code $this _lock reset]
235        }
236        reset {
237            set _lock 0
238        }
239        active {
240            return $_lock
241        }
242        default {
243            error "bad option \"$option\": should be set, reset, active"
244        }
245    }
246}
247
248# ----------------------------------------------------------------------
249# OPTION: -xscrollmode
250# ----------------------------------------------------------------------
251itcl::configbody Rappture::Scroller::xscrollmode {
252    _fixsbar x
253}
254
255# ----------------------------------------------------------------------
256# OPTION: -yscrollmode
257# ----------------------------------------------------------------------
258itcl::configbody Rappture::Scroller::yscrollmode {
259    _fixsbar y
260}
261
262# ----------------------------------------------------------------------
263# OPTION: -width
264# ----------------------------------------------------------------------
265itcl::configbody Rappture::Scroller::width {
266    if {$itk_option(-width) == "0"} {
267        if {$itk_option(-height) == "0"} {
268            grid propagate $itk_component(hull) yes
269        } else {
270            component hull configure -width 1i
271        }
272    } else {
273        grid propagate $itk_component(hull) no
274        component hull configure -width $itk_option(-width)
275    }
276}
277
278# ----------------------------------------------------------------------
279# OPTION: -height
280# ----------------------------------------------------------------------
281itcl::configbody Rappture::Scroller::height {
282    if {$itk_option(-height) == "0"} {
283        if {$itk_option(-width) == "0"} {
284            grid propagate $itk_component(hull) yes
285        } else {
286            component hull configure -height 1i
287        }
288    } else {
289        grid propagate $itk_component(hull) no
290        component hull configure -height $itk_option(-height)
291    }
292}
Note: See TracBrowser for help on using the repository browser.