1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: Panes - creates a series of adjustable panes |
---|
3 | # |
---|
4 | # This is a simple paned window with an adjustable sash. |
---|
5 | # the same quantity, but for various ranges of input values. |
---|
6 | # It also manages the controls to select and visualize the data. |
---|
7 | # ====================================================================== |
---|
8 | # AUTHOR: Michael McLennan, Purdue University |
---|
9 | # Copyright (c) 2004-2005 |
---|
10 | # Purdue Research Foundation, West Lafayette, IN |
---|
11 | # ====================================================================== |
---|
12 | package require Itk |
---|
13 | |
---|
14 | option add *Panes.width 3i widgetDefault |
---|
15 | option add *Panes.height 3i widgetDefault |
---|
16 | option add *Panes.sashCursor sb_v_double_arrow |
---|
17 | |
---|
18 | itcl::class Rappture::Panes { |
---|
19 | inherit itk::Widget |
---|
20 | |
---|
21 | itk_option define -sashcursor sashCursor SashCursor "" |
---|
22 | |
---|
23 | constructor {args} { # defined below } |
---|
24 | |
---|
25 | public method insert {pos args} |
---|
26 | public method pane {pos} |
---|
27 | public method fraction {pos {newval ""}} |
---|
28 | |
---|
29 | protected method _grab {pane X Y} |
---|
30 | protected method _drag {pane X Y} |
---|
31 | protected method _drop {pane X Y} |
---|
32 | protected method _fixLayout {args} |
---|
33 | |
---|
34 | private variable _dispatcher "" ;# dispatcher for !events |
---|
35 | private variable _panes "" ;# list of pane frames |
---|
36 | private variable _counter 0 ;# counter for auto-generated names |
---|
37 | private variable _frac 1.0 ;# list of fractions |
---|
38 | } |
---|
39 | |
---|
40 | itk::usual Panes { |
---|
41 | keep -background -cursor |
---|
42 | } |
---|
43 | |
---|
44 | # ---------------------------------------------------------------------- |
---|
45 | # CONSTRUCTOR |
---|
46 | # ---------------------------------------------------------------------- |
---|
47 | itcl::body Rappture::Panes::constructor {args} { |
---|
48 | itk_option add hull.width hull.height |
---|
49 | |
---|
50 | # create a dispatcher for events |
---|
51 | Rappture::dispatcher _dispatcher |
---|
52 | $_dispatcher register !layout |
---|
53 | $_dispatcher dispatch $this !layout [itcl::code $this _fixLayout] |
---|
54 | |
---|
55 | # fix the layout whenever the window size changes |
---|
56 | bind Panes <Configure> [itcl::code %W _fixLayout] |
---|
57 | |
---|
58 | set pname "pane[incr _counter]" |
---|
59 | itk_component add $pname { |
---|
60 | frame $itk_interior.$pname |
---|
61 | } |
---|
62 | |
---|
63 | lappend _panes $pname |
---|
64 | |
---|
65 | eval itk_initialize $args |
---|
66 | |
---|
67 | # make sure we fix up the layout at some point |
---|
68 | $_dispatcher event -idle !layout |
---|
69 | } |
---|
70 | |
---|
71 | # ---------------------------------------------------------------------- |
---|
72 | # USAGE: insert <pos> ?-fraction f? |
---|
73 | # |
---|
74 | # Adds a new page to this widget at the given position <pos>. |
---|
75 | # ---------------------------------------------------------------------- |
---|
76 | itcl::body Rappture::Panes::insert {pos args} { |
---|
77 | Rappture::getopts args params { |
---|
78 | value -fraction 0.5 |
---|
79 | } |
---|
80 | if {[llength $args] > 0} { |
---|
81 | error "wrong # args: should be \"insert pos ?-fraction f?\"" |
---|
82 | } |
---|
83 | |
---|
84 | set pname "pane[incr _counter]" |
---|
85 | set sash "${pname}sash" |
---|
86 | itk_component add $sash { |
---|
87 | frame $itk_interior.$sash |
---|
88 | } { |
---|
89 | usual |
---|
90 | rename -cursor -sashcursor sashCursor SashCursor |
---|
91 | } |
---|
92 | |
---|
93 | itk_component add ${sash}ridge { |
---|
94 | frame $itk_component($sash).ridge \ |
---|
95 | -height 2 -borderwidth 1 -relief sunken |
---|
96 | } { |
---|
97 | usual |
---|
98 | rename -cursor -sashcursor sashCursor SashCursor |
---|
99 | } |
---|
100 | pack $itk_component(${sash}ridge) -fill x -pady 4 |
---|
101 | |
---|
102 | foreach comp [list $sash ${sash}ridge] { |
---|
103 | bind $itk_component($comp) <ButtonPress-1> \ |
---|
104 | [itcl::code $this _grab $pname %X %Y] |
---|
105 | bind $itk_component($comp) <B1-Motion> \ |
---|
106 | [itcl::code $this _drag $pname %X %Y] |
---|
107 | bind $itk_component($comp) <ButtonRelease-1> \ |
---|
108 | [itcl::code $this _drop $pname %X %Y] |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | itk_component add $pname { |
---|
113 | frame $itk_interior.$pname |
---|
114 | } |
---|
115 | lappend _panes $pname |
---|
116 | |
---|
117 | # fix the fractional sizes |
---|
118 | set f $params(-fraction) |
---|
119 | set _frac [list [expr {1-$f}] $f] |
---|
120 | |
---|
121 | # make sure we fix up the layout at some point |
---|
122 | $_dispatcher event -idle !layout |
---|
123 | |
---|
124 | return $itk_component($pname) |
---|
125 | } |
---|
126 | |
---|
127 | # ---------------------------------------------------------------------- |
---|
128 | # USAGE: pane <pos> |
---|
129 | # |
---|
130 | # Returns the frame representing the pane at position <pos>. |
---|
131 | # ---------------------------------------------------------------------- |
---|
132 | itcl::body Rappture::Panes::pane {pos} { |
---|
133 | set pname [lindex $_panes $pos] |
---|
134 | if {[info exists itk_component($pname)]} { |
---|
135 | return $itk_component($pname) |
---|
136 | } |
---|
137 | return "" |
---|
138 | } |
---|
139 | |
---|
140 | # ---------------------------------------------------------------------- |
---|
141 | # USAGE: fraction <pos> ?<newval>? |
---|
142 | # |
---|
143 | # Clients use this to get/set the fraction of real estate associated |
---|
144 | # with the pane at position <pos>. |
---|
145 | # ---------------------------------------------------------------------- |
---|
146 | itcl::body Rappture::Panes::fraction {pos {newval ""}} { |
---|
147 | if {"" == $newval} { |
---|
148 | return [lindex $_frac $pos] |
---|
149 | } |
---|
150 | if {![string is double $newval]} { |
---|
151 | error "bad value \"$newval\": should be fraction 0-1" |
---|
152 | } |
---|
153 | if {$pos == "end" || ($pos >= 0 && $pos < [llength $_frac])} { |
---|
154 | # if there are other panes, adjust their size according to this |
---|
155 | if {[llength $_frac] > 1} { |
---|
156 | set oldval [lindex $_frac $pos] |
---|
157 | set delta [expr {double($oldval-$newval)/([llength $_frac]-1)}] |
---|
158 | for {set i 0} {$i < [llength $_frac]} {incr i} { |
---|
159 | set v [lindex $_frac $i] |
---|
160 | set _frac [lreplace $_frac $i $i [expr {$v+$delta}]] |
---|
161 | } |
---|
162 | } |
---|
163 | set _frac [lreplace $_frac $pos $pos $newval] |
---|
164 | $_dispatcher event -idle !layout |
---|
165 | } else { |
---|
166 | error "bad index \"$pos\": out of range" |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | # ---------------------------------------------------------------------- |
---|
171 | # USAGE: _grab <pane> <X> <Y> |
---|
172 | # |
---|
173 | # Invoked automatically when the user clicks on a sash, to initiate |
---|
174 | # movement. |
---|
175 | # ---------------------------------------------------------------------- |
---|
176 | itcl::body Rappture::Panes::_grab {pname X Y} { |
---|
177 | } |
---|
178 | |
---|
179 | # ---------------------------------------------------------------------- |
---|
180 | # USAGE: _drag <pane> <X> <Y> |
---|
181 | # |
---|
182 | # Invoked automatically as the user drags a sash, to resize the panes. |
---|
183 | # ---------------------------------------------------------------------- |
---|
184 | itcl::body Rappture::Panes::_drag {pname X Y} { |
---|
185 | set realY [expr {$Y-[winfo rooty $itk_component(hull)]}] |
---|
186 | set Ymax [winfo height $itk_component(hull)] |
---|
187 | set frac [expr double($realY)/$Ymax] |
---|
188 | if {$frac < 0.05} { |
---|
189 | set frac 0.05 |
---|
190 | } |
---|
191 | if {$frac > 0.95} { |
---|
192 | set frac 0.95 |
---|
193 | } |
---|
194 | |
---|
195 | set _frac [list $frac [expr {1-$frac}]] |
---|
196 | _fixLayout |
---|
197 | |
---|
198 | return $frac |
---|
199 | } |
---|
200 | |
---|
201 | # ---------------------------------------------------------------------- |
---|
202 | # USAGE: _drop <pane> <X> <Y> |
---|
203 | # |
---|
204 | # Invoked automatically as the user drops a sash, to resize the panes. |
---|
205 | # ---------------------------------------------------------------------- |
---|
206 | itcl::body Rappture::Panes::_drop {pname X Y} { |
---|
207 | set frac [_drag $pname $X $Y] |
---|
208 | } |
---|
209 | |
---|
210 | # ---------------------------------------------------------------------- |
---|
211 | # USAGE: _fixLayout ?<eventArgs>...? |
---|
212 | # |
---|
213 | # Used internally to update the layout of panes whenever a new pane |
---|
214 | # is added or a sash is moved. |
---|
215 | # ---------------------------------------------------------------------- |
---|
216 | itcl::body Rappture::Panes::_fixLayout {args} { |
---|
217 | set h [winfo height $itk_component(hull)] |
---|
218 | foreach p [lrange $_panes 1 end] { |
---|
219 | set h [expr {$h - [winfo height $itk_component(${p}sash)]}] |
---|
220 | } |
---|
221 | |
---|
222 | set y 0 |
---|
223 | foreach p $_panes f $_frac { |
---|
224 | set sash ${p}sash |
---|
225 | if {[info exists itk_component($sash)]} { |
---|
226 | set sh [winfo reqheight $itk_component($sash)] |
---|
227 | place $itk_component($sash) -y $y -relx 0.5 -anchor n \ |
---|
228 | -relwidth 1.0 -height $sh |
---|
229 | set y [expr {$y + $sh}] |
---|
230 | } |
---|
231 | |
---|
232 | set ph [expr {$h*$f}] |
---|
233 | place $itk_component($p) -y $y -relx 0.5 -anchor n \ |
---|
234 | -relwidth 1.0 -height $ph |
---|
235 | set y [expr {$y + $ph}] |
---|
236 | } |
---|
237 | } |
---|