author | Claus Gittinger <cg@exept.de> |
Tue, 02 Aug 2005 15:14:45 +0200 | |
changeset 2840 | 93e2b635838d |
parent 1877 | 3b415fa4710f |
child 2955 | 57c6b1a672dd |
permissions | -rw-r--r-- |
790 | 1 |
" |
2 |
COPYRIGHT (c) 1998 by Andreas Vogel / eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
||
14 |
||
15 |
||
1877
3b415fa4710f
only access originChanged, extentChanged and cornerChanged
Claus Gittinger <cg@exept.de>
parents:
1519
diff
changeset
|
16 |
"{ Package: 'stx:libwidg2' }" |
3b415fa4710f
only access originChanged, extentChanged and cornerChanged
Claus Gittinger <cg@exept.de>
parents:
1519
diff
changeset
|
17 |
|
721 | 18 |
PanelView subclass:#GridBagLayoutView |
19 |
instanceVariableNames:'columnWidths rowHeights columnWeights rowWeights layoutInfo' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Views-Layout' |
|
23 |
! |
|
24 |
||
25 |
!GridBagLayoutView class methodsFor:'documentation'! |
|
26 |
||
790 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 1998 by Andreas Vogel / eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
||
40 |
||
41 |
||
42 |
! |
|
43 |
||
721 | 44 |
documentation |
45 |
" |
|
46 |
GridBagLayout is a flexible layout manager that aligns components vertically and horizontally, |
|
47 |
without requiring that the components be the same size. Each GridBagLayout uses a dynamic rectangular |
|
48 |
grid of cells, with each component occupying one or more cells (called its display area). |
|
49 |
Each component managed by a GridBagLayout is associated with a GridBagConstraints instance that |
|
50 |
specifies how the component is laid out within its display area. How a GridBagLayout places a set of |
|
51 |
components depends on each component's GridBagConstraints and minimum size, as well as the preferred |
|
52 |
size of the components' container. |
|
53 |
||
54 |
To use a GridBagLayout effectively, you must customize one or more of its components' GridBagConstraints. |
|
55 |
You customize a GridBagConstraints object by setting one or more of its instance variables: |
|
56 |
||
57 |
gridX |
|
58 |
gridY |
|
59 |
Specifies the cell at the upper left of the component's display area, where the |
|
60 |
upper-left-most cell has address gridX=0, gridY=0. Use #RELATIVE (the default value) |
|
61 |
to specify that the component be just placed just to the right of (for gridX) |
|
62 |
or just below (for gridY) the component that was added to the container just before |
|
63 |
this component was added. |
|
64 |
||
65 |
gridWidth |
|
66 |
gridHeight |
|
67 |
Specifies the number of cells in a row (for gridWidth) or column (for gridHeight) |
|
68 |
in the component's display area. The default value is 1. Use #REMAINDER to specify |
|
69 |
that the component be the last one in its row (for gridWidth) or column (for gridHeight). |
|
70 |
Use #RELATIVE to specify that the component be the next to last one in its row |
|
71 |
(for gridWidth) or column (for gridHeight). |
|
72 |
||
73 |
fill |
|
74 |
Used when the component's display area is larger than the component's requested size |
|
75 |
to determine whether (and how) to resize the component. |
|
76 |
Valid values are |
|
77 |
#NONE |
|
78 |
(the default), |
|
79 |
#HORIZONTAL |
|
80 |
(make the component wide enough to fill its display area |
|
81 |
horizontally, but don't change its height), |
|
82 |
#VERTICAL |
|
83 |
(make the component tall enough to fill its display area |
|
84 |
vertically, but don't change its width), and |
|
85 |
#BOTH |
|
86 |
(make the component fill its display area entirely). |
|
87 |
||
88 |
ipadX |
|
89 |
ipadY |
|
90 |
Specifies the internal padding: how much to add to the minimum size of the component. |
|
91 |
The width of the component will be at least its minimum width plus ipadX*2 pixels |
|
92 |
(since the padding applies to both sides of the component). Similarly, the height of |
|
93 |
the component will be at least the minimum height plus ipadY*2 pixels. |
|
94 |
||
95 |
insets |
|
96 |
Specifies the external padding of the component -- the minimum amount of space between |
|
97 |
the component and the edges of its display area. |
|
98 |
||
99 |
anchor |
|
100 |
Used when the component is smaller than its display area to determine where (within the area) |
|
101 |
to place the component. |
|
102 |
Valid values are |
|
103 |
#CENTER (the default), |
|
104 |
#NORTH, |
|
105 |
#NORTHEAST, |
|
106 |
#EAST, |
|
107 |
#SOUTHEAST, |
|
108 |
#SOUTH, |
|
109 |
#SOUTHWEST, |
|
110 |
#WEST, |
|
111 |
#NORTHWEST. |
|
112 |
||
113 |
weightX |
|
114 |
weightY |
|
115 |
Used to determine how to distribute space; this is important for specifying resizing |
|
116 |
behavior. Unless you specify a weight for at least one component in a row (weightX) |
|
117 |
and column (weightY), all the components clump together in the center of their container. |
|
118 |
This is because when the weight is zero (the default), the GridBagLayout puts any extra |
|
119 |
space between its grid of cells and the edges of the container. |
|
120 |
||
121 |
||
122 |
[see also:] |
|
123 |
GridBagConstraints |
|
124 |
GridBagLayoutInfo |
|
125 |
Insets |
|
126 |
||
127 |
[author:] |
|
128 |
Andreas Vogel |
|
129 |
" |
|
130 |
! |
|
131 |
||
132 |
examples |
|
133 |
" |
|
134 |
This example is taken from the java source and should produce the same layout. Check the file |
|
135 |
Grid*.gif in the java distribution. |
|
136 |
||
137 |
[exBegin] |
|
138 |
| v p c | |
|
139 |
c := #( |
|
140 |
#( fill: #BOTH weightX: 1.0 ) |
|
141 |
#( fill: #BOTH weightX: 1.0 ) |
|
142 |
#( fill: #BOTH weightX: 1.0 ) |
|
143 |
#( fill: #BOTH weightX: 1.0 gridWidth: #REMAINDER ) |
|
144 |
#( fill: #BOTH gridWidth: #REMAINDER ) |
|
145 |
#( fill: #BOTH gridWidth: #RELATIVE ) |
|
146 |
#( fill: #BOTH gridWidth: #REMAINDER ) |
|
147 |
#( fill: #BOTH weightY: 1.0 gridHeight: 2 ) |
|
148 |
#( fill: #BOTH gridWidth: #REMAINDER ) |
|
149 |
#( fill: #BOTH gridWidth: #REMAINDER ) |
|
150 |
). |
|
151 |
v := StandardSystemView new. v label:'GridBagLayoutView: Example 1'. |
|
152 |
p := GridBagLayoutView in:v. p origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
153 |
1 to: c size do:[ :i | | b | |
|
154 |
b := Button label:('Button ',(i displayString)) in:p. |
|
155 |
b objectAttributeAt:#GridBagConstraints put:((#(GridBagConstraints) , (c at:i)) decodeAsLiteralArray). |
|
156 |
]. |
|
157 |
v extent:(p preferredExtent). v open. |
|
158 |
[exEnd] |
|
159 |
||
160 |
||
161 |
[exBegin] |
|
162 |
| v p c | |
|
163 |
c := #( |
|
164 |
#( insets: #(Insets 2 2 2 2) fill: #BOTH weightX: 1.0 ) |
|
165 |
#( insets: #(Insets 2 2 2 2) fill: #BOTH weightX: 1.0 ) |
|
166 |
#( insets: #(Insets 2 2 2 2) fill: #BOTH weightX: 1.0 ) |
|
167 |
#( insets: #(Insets 2 2 2 2) fill: #BOTH weightX: 1.0 gridWidth: #REMAINDER ) |
|
168 |
#( insets: #(Insets 2 2 2 2) fill: #BOTH gridWidth: #REMAINDER ) |
|
169 |
#( insets: #(Insets 2 2 2 2) fill: #BOTH gridWidth: #RELATIVE ) |
|
170 |
#( insets: #(Insets 2 2 2 2) fill: #BOTH gridWidth: #REMAINDER ) |
|
171 |
#( insets: #(Insets 2 2 2 2) fill: #BOTH weightY: 1.0 gridHeight: 2 ) |
|
172 |
#( insets: #(Insets 2 2 2 2) fill: #BOTH gridWidth: #REMAINDER ) |
|
173 |
#( insets: #(Insets 2 2 2 2) fill: #BOTH gridWidth: #REMAINDER ) |
|
174 |
). |
|
175 |
v := StandardSystemView new. v label:'GridBagLayoutView: Example 2'. |
|
176 |
p := GridBagLayoutView in:v. p origin:(0.0 @ 0.0) corner:(1.0 @ 1.0). |
|
177 |
1 to: c size do:[ :i | | b | |
|
178 |
b := Button label:('Button ',(i displayString)) in:p. |
|
179 |
b objectAttributeAt:#GridBagConstraints put:((#(GridBagConstraints) , (c at:i)) decodeAsLiteralArray). |
|
180 |
]. |
|
181 |
v extent:(p preferredExtent). v open. |
|
182 |
[exEnd] |
|
183 |
||
184 |
||
185 |
||
186 |
" |
|
187 |
! ! |
|
188 |
||
189 |
!GridBagLayoutView class methodsFor:'instance creation'! |
|
190 |
||
191 |
new |
|
192 |
"Create a new instance of my class and do explizit initialization of it." |
|
193 |
||
2840 | 194 |
^ self basicNew initialize |
721 | 195 |
|
196 |
"Created: / 19.1.1998 / 13:53:11 / av" |
|
197 |
"Modified: / 1.2.1998 / 13:02:59 / av" |
|
198 |
! ! |
|
199 |
||
2840 | 200 |
!GridBagLayoutView methodsFor:'initialization'! |
201 |
||
202 |
initialize |
|
203 |
"Initialize the instance. Mainly set our instance variables to required values." |
|
204 |
||
205 |
super initialize. |
|
206 |
||
207 |
columnWidths := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
208 |
rowHeights := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
209 |
||
210 |
columnWeights := IdentityDictionaryWithDefault newWithDefaultValue:0.0. |
|
211 |
rowWeights := IdentityDictionaryWithDefault newWithDefaultValue:0.0. |
|
212 |
||
213 |
layoutInfo := nil. |
|
214 |
||
215 |
"Created: / 19.1.1998 / 13:53:59 / av" |
|
216 |
"Modified: / 1.2.1998 / 12:57:05 / av" |
|
217 |
! ! |
|
218 |
||
721 | 219 |
!GridBagLayoutView methodsFor:'layout'! |
220 |
||
221 |
setChildPositions |
|
222 |
"(re)compute position of every child whenever childs are added or my size has changed" |
|
223 |
||
224 |
self arrangeGrid. |
|
225 |
||
226 |
"Modified: / 1.2.1998 / 12:52:22 / av" |
|
227 |
! ! |
|
228 |
||
229 |
!GridBagLayoutView methodsFor:'private'! |
|
230 |
||
231 |
adjustForGravity:c in:r |
|
232 |
||
233 |
| diffx diffy add | |
|
234 |
||
235 |
r setLeft: ((r left) + (c insets left)). |
|
236 |
r width: ((r width) - ((c insets left) + (c insets right))). |
|
237 |
r setTop: ((r top) + (c insets top)). |
|
238 |
r height: ((r height) - ((c insets top) + (c insets bottom))). |
|
239 |
||
240 |
diffx := 0. |
|
241 |
(((c fill) ~~ #HORIZONTAL and:[(c fill) ~~ #BOTH]) and:[(r width) > (add := (c minWidth) + (c ipadX))]) ifTrue:[ |
|
242 |
diffx := (r width) - add. |
|
243 |
r width:add. |
|
244 |
]. |
|
245 |
||
246 |
diffy := 0. |
|
247 |
(((c fill) ~~ #VERTICAL and:[(c fill) ~~ #BOTH]) and:[(r height) > (add := (c minHeight) + (c ipadY))]) ifTrue:[ |
|
248 |
diffy := (r height) - add. |
|
249 |
r height:add. |
|
250 |
]. |
|
251 |
||
252 |
((c anchor) == #CENTER) ifTrue:[ |
|
253 |
r setLeft:((r left) + (diffx / 2)). |
|
254 |
r setTop:((r top) + (diffy / 2)). |
|
255 |
]. |
|
256 |
((c anchor) == #NORTH) ifTrue:[ |
|
257 |
r setLeft:((r left) + (diffx / 2)). |
|
258 |
]. |
|
259 |
((c anchor) == #NORTHEAST) ifTrue:[ |
|
260 |
r setLeft:((r left) + diffx). |
|
261 |
]. |
|
262 |
((c anchor) == #EAST) ifTrue:[ |
|
263 |
r setLeft:((r left) + diffx). |
|
264 |
r setTop:((r top) + (diffy / 2)). |
|
265 |
]. |
|
266 |
((c anchor) == #SOUTHEAST) ifTrue:[ |
|
267 |
r setLeft:((r left) + diffx). |
|
268 |
r setTop:((r top) + diffy). |
|
269 |
]. |
|
270 |
((c anchor) == #SOUTH) ifTrue:[ |
|
271 |
r setLeft:((r left) + (diffx / 2)). |
|
272 |
r setTop:((r top) + diffy). |
|
273 |
]. |
|
274 |
((c anchor) == #SOUTHWEST) ifTrue:[ |
|
275 |
r setTop:((r top) + diffy). |
|
276 |
]. |
|
277 |
((c anchor) == #WEST) ifTrue:[ |
|
278 |
r setTop:((r top) + (diffy / 2)). |
|
279 |
]. |
|
280 |
((c anchor) == #NORTHWEST) ifTrue:[ |
|
281 |
]. |
|
282 |
||
283 |
r setLeft: ((r left) asInteger). |
|
284 |
r setTop: ((r top) asInteger). |
|
285 |
r width: ((r width) asInteger). |
|
286 |
r height: ((r height) asInteger). |
|
287 |
||
288 |
^ r |
|
289 |
||
290 |
"Modified: / 22.1.1998 / 09:50:01 / av" |
|
291 |
! |
|
292 |
||
293 |
arrangeGrid |
|
294 |
" Lay out the grid." |
|
295 |
||
296 |
| insets subViews info ext d r c diffw diffh weight | |
|
297 |
||
298 |
insets := Insets new. |
|
299 |
||
300 |
"/ If the parent has no slaves anymore, then don't do anything |
|
301 |
"/ at all: just leave the parent's size as-is. |
|
302 |
(((subViews := self subViews) size) == 0) ifTrue:[^ self]. |
|
303 |
||
1877
3b415fa4710f
only access originChanged, extentChanged and cornerChanged
Claus Gittinger <cg@exept.de>
parents:
1519
diff
changeset
|
304 |
self extentChangedFlag ifTrue:[ |
1519 | 305 |
ext := self computeExtent. |
306 |
width := ext x. |
|
307 |
height := ext y. |
|
721 | 308 |
]. |
309 |
||
310 |
"/ Pass #1: scan all the slaves to figure out the total amount |
|
311 |
"/ of space needed. |
|
312 |
||
313 |
info := self getLayoutInfo:#PREFERRED_SIZE. |
|
314 |
d := self getMinSize:info. |
|
315 |
||
316 |
((width < (d width)) or:[(height < (d height))]) ifTrue:[ |
|
1519 | 317 |
info := self getLayoutInfo:#MINIMUM_SIZE. |
318 |
d := self getMinSize:info. |
|
721 | 319 |
]. |
320 |
||
321 |
layoutInfo := info. |
|
322 |
r := Rectangle origin:0@0 extent:((d width) @ (d height)). |
|
323 |
||
324 |
"/ If the current dimensions of the window don't match the desired |
|
325 |
"/ dimensions, then adjust the minWidth and minHeight arrays |
|
326 |
"/ according to the weights. |
|
327 |
||
328 |
diffw := width - (r width). |
|
329 |
(diffw ~~ 0) ifTrue:[ |
|
1519 | 330 |
weight := 0.0. |
331 |
0 to:((info width) - 1) do:[ :i | |
|
332 |
weight := weight + ((info weightX) at:(i + 1)). |
|
333 |
]. |
|
334 |
(weight > 0.0) ifTrue:[ |
|
335 |
0 to:((info width) - 1) do:[ :i | |
|
336 |
| dx | |
|
337 |
dx := ((diffw * ((info weightX) at:(i + 1))) / weight) asInteger. |
|
338 |
(info minWidth) at:(i + 1) put:(((info minWidth) at:(i + 1)) + dx). |
|
339 |
r width:((r width) + dx). |
|
340 |
(((info minWidth) at:(i + 1)) < 0) ifTrue:[ |
|
341 |
r width:((r width) - ((info minWidth) at:(i + 1))). |
|
342 |
(info minWidth) at:(i + 1) put:0. |
|
343 |
]. |
|
344 |
]. |
|
345 |
]. |
|
346 |
diffw := width - (r width). |
|
721 | 347 |
] ifFalse:[ |
1519 | 348 |
diffw := 0. |
721 | 349 |
]. |
350 |
||
351 |
diffh := height - (r height). |
|
352 |
(diffh ~~ 0) ifTrue:[ |
|
1519 | 353 |
weight := 0.0. |
354 |
0 to:((info height) - 1) do:[ :i | |
|
355 |
weight := weight + ((info weightY) at:(i + 1)). |
|
356 |
]. |
|
357 |
(weight > 0.0) ifTrue:[ |
|
358 |
0 to:((info height) - 1) do:[ :i | |
|
359 |
| dy | |
|
360 |
dy := ((diffh * ((info weightY) at:(i + 1))) / weight) asInteger. |
|
361 |
(info minHeight) at:(i + 1) put:(((info minHeight) at:(i + 1)) + dy). |
|
362 |
r height:((r height) + dy). |
|
363 |
(((info minHeight) at:(i + 1)) < 0) ifTrue:[ |
|
364 |
r height:((r height) - ((info minHeight) at:(i + 1))). |
|
365 |
(info minHeight) at:(i + 1) put:0. |
|
366 |
]. |
|
367 |
]. |
|
368 |
]. |
|
369 |
diffh := height - (r height). |
|
721 | 370 |
] ifFalse:[ |
1519 | 371 |
diffh := 0. |
721 | 372 |
]. |
373 |
||
374 |
"/ Now do the actual layout of the slaves using the layout information |
|
375 |
"/ that has been collected. |
|
376 |
||
377 |
info startX:(((diffw / 2) + (insets left)) asInteger). |
|
378 |
info startY:(((diffh / 2) + (insets top)) asInteger). |
|
379 |
||
380 |
subViews do:[ :child | |
|
1519 | 381 |
|rW rH| |
721 | 382 |
|
1519 | 383 |
c := self constraints:child. |
384 |
||
385 |
r setLeft:(info startX). |
|
386 |
0 to:((c tempX) - 1) do:[ :i | r setLeft:((r left) + ((info minWidth) at:(i + 1))). ]. |
|
721 | 387 |
|
1519 | 388 |
r setTop:(info startY). |
389 |
0 to:((c tempY) - 1) do:[ :i | r setTop:((r top) + ((info minHeight) at:(i + 1))). ]. |
|
721 | 390 |
|
1519 | 391 |
r width:0. |
392 |
(c tempX) to:((c tempX) + (c tempWidth) - 1) do:[ :i | |
|
393 |
r width:((r width) + ((info minWidth) at:(i + 1))). |
|
394 |
]. |
|
721 | 395 |
|
1519 | 396 |
r height:0. |
397 |
(c tempY) to:((c tempY) + (c tempHeight) - 1) do:[ :i | |
|
398 |
r height:((r height) + ((info minHeight) at:(i + 1))). |
|
399 |
]. |
|
721 | 400 |
|
1519 | 401 |
self adjustForGravity:c in:r. |
721 | 402 |
|
1519 | 403 |
"/ If the window is too small to be interesting then |
404 |
"/ unmap it. Otherwise configure it and then make sure |
|
405 |
"/ its mapped. |
|
406 |
rW := r width. |
|
407 |
rH := r height. |
|
721 | 408 |
|
1519 | 409 |
((rW <= 0) or:[rH <= 0]) ifTrue:[ |
410 |
child origin:(0@0) ; width:0 height:0. |
|
411 |
] ifFalse:[ |
|
412 |
child origin:((r left) @ (r top)). |
|
413 |
child width:rW. |
|
414 |
child height:rH. |
|
415 |
]. |
|
721 | 416 |
]. |
417 |
||
418 |
"Modified: / 1.2.1998 / 12:53:37 / av" |
|
419 |
! |
|
420 |
||
421 |
constraints:anObject |
|
422 |
" Get the GridBag constraints for an object. As a fallback create new constraints for objects |
|
423 |
which don't have any constraints yet." |
|
424 |
||
425 |
| c | |
|
426 |
||
427 |
c := anObject objectAttributeAt:#GridBagConstraints. |
|
428 |
c isNil ifTrue:[ |
|
429 |
anObject objectAttributeAt:#GridBagConstraints put:(c := GridBagConstraints new) |
|
430 |
]. |
|
431 |
^ c. |
|
432 |
||
433 |
"Created: / 21.1.1998 / 00:24:34 / av" |
|
434 |
"Modified: / 1.2.1998 / 12:55:23 / av" |
|
435 |
! |
|
436 |
||
437 |
getLayoutInfo:which |
|
438 |
"return a good extent, one that makes subviews fit" |
|
439 |
||
440 |
| |
|
441 |
MAX_INT_VAL subViews |
|
442 |
r i k px py pixelsDiff weightDiff weight nextSize c |
|
443 |
curX curY curWidth curHeight curRow curCol xMax yMax |
|
444 |
| |
|
445 |
||
446 |
MAX_INT_VAL := 9999999999. |
|
447 |
subViews := (self subViews). |
|
448 |
||
449 |
r := GridBagLayoutInfo new. |
|
450 |
r width:0. |
|
451 |
r height:0. |
|
452 |
||
453 |
xMax := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
454 |
yMax := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
455 |
curRow := curCol := -1. |
|
456 |
||
457 |
"/ Pass #1 |
|
458 |
"/ |
|
459 |
"/ Figure out the dimensions of the layout grid (use a value of 1 for |
|
460 |
"/ zero or negative widths and heights). |
|
461 |
||
462 |
subViews do:[ :child | |
|
463 |
c := self constraints:child. |
|
464 |
||
465 |
curX := c gridX. |
|
466 |
curY := c gridY. |
|
467 |
((curWidth := c gridWidth) <= 0) ifTrue:[ curWidth := 1. ]. |
|
468 |
((curHeight := c gridHeight) <= 0) ifTrue:[ curHeight := 1. ]. |
|
469 |
||
470 |
"/ If x or y is negative, then use relative positioning: |
|
471 |
((curX < 0) and:[ curY < 0 ]) |
|
472 |
ifTrue:[ |
|
473 |
(curRow >= 0) |
|
474 |
ifTrue: [ curY := curRow. ] |
|
475 |
ifFalse:[ (curCol >= 0) ifTrue:[ curX := curCol. ] ifFalse: [ curY := 0. ]]. |
|
476 |
]. |
|
477 |
||
478 |
(curX < 0) |
|
479 |
ifTrue:[ |
|
480 |
px := 0. |
|
481 |
curY to:(curY + curHeight - 1) do: [ :i | |
|
482 |
px := px max:(xMax at:(i + 1)). |
|
483 |
]. |
|
484 |
((curX := px - curX - 1) < 0) ifTrue:[ curX := 0. ]. |
|
485 |
] |
|
486 |
ifFalse:[ |
|
487 |
(curY < 0) ifTrue:[ |
|
488 |
py := 0. |
|
489 |
curX to: (curX + curWidth - 1) do: [ :i | |
|
490 |
py := py max: (yMax at:(i + 1)). |
|
491 |
]. |
|
492 |
((curY := py - curY - 1) < 0) ifTrue:[ curY := 0. ]. |
|
493 |
]. |
|
494 |
]. |
|
495 |
||
496 |
"/ Adjust the grid width and height |
|
497 |
px := curX + curWidth. [ (r width) < px ] whileTrue:[ r width:((r width) + 1). ]. |
|
498 |
py := curY + curHeight. [ (r height) < py ] whileTrue:[ r height:((r height) + 1). ]. |
|
499 |
||
500 |
"/ Adjust the xMax and yMax arrays |
|
501 |
curX to:(curX + curWidth - 1) do:[ :i | yMax at:(i + 1) put:py. ]. |
|
502 |
curY to:(curY + curHeight - 1) do:[ :i | xMax at:(i + 1) put:px. ]. |
|
503 |
||
504 |
c minWidth:(child preferredExtent x). |
|
505 |
c minHeight:(child preferredExtent y). |
|
506 |
||
507 |
"/ Zero width and height must mean that this is the last item (or |
|
508 |
"/ else something is wrong). |
|
509 |
(((c gridHeight) == 0) and:[ (c gridWidth) == 0 ]) ifTrue:[ |
|
510 |
curRow := curCol := -1. |
|
511 |
]. |
|
512 |
||
513 |
"/ Zero width starts a new row |
|
514 |
(((c gridHeight) == 0) and:[ curRow < 0 ]) |
|
515 |
ifTrue:[ |
|
516 |
curCol := curX + curWidth. |
|
517 |
] |
|
518 |
ifFalse:[ |
|
519 |
((c gridWidth) == 0 and:[ curCol < 0 ]) ifTrue:[ |
|
520 |
curRow := curY + curHeight. |
|
521 |
]. |
|
522 |
]. |
|
523 |
]. |
|
524 |
||
525 |
"/ |
|
526 |
"/ Apply minimum row/column dimensions |
|
527 |
"/ |
|
528 |
((r width) < (columnWidths size)) ifTrue:[ r width:(columnWidths size). ]. |
|
529 |
((r height) < (rowHeights size)) ifTrue:[ r height:(rowHeights size). ]. |
|
530 |
||
531 |
"/ |
|
532 |
"/ Pass #2 |
|
533 |
"/ |
|
534 |
"/ Negative values for gridX are filled in with the current x value. |
|
535 |
"/ Negative values for gridY are filled in with the current y value. |
|
536 |
"/ Negative or zero values for gridWidth and gridHeight end the current |
|
537 |
"/ row or column, respectively. |
|
538 |
"/ |
|
539 |
||
540 |
curRow := curCol := -1. |
|
541 |
xMax := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
542 |
yMax := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
543 |
||
544 |
subViews do:[ :child | |
|
545 |
c := self constraints:child. |
|
546 |
||
547 |
curX := c gridX. |
|
548 |
curY := c gridY. |
|
549 |
curWidth := c gridWidth. |
|
550 |
curHeight := c gridHeight. |
|
551 |
||
552 |
"/ If x or y is negative, then use relative positioning: |
|
553 |
((curX < 0) and:[ curY < 0 ]) |
|
554 |
ifTrue:[ |
|
555 |
(curRow >= 0) |
|
556 |
ifTrue: [ curY := curRow. ] |
|
557 |
ifFalse:[ (curCol >= 0) |
|
558 |
ifTrue: [ curX := curCol. ] |
|
559 |
ifFalse: [ curY := 0. ]. |
|
560 |
]. |
|
561 |
]. |
|
562 |
||
563 |
(curX < 0) |
|
564 |
ifTrue:[ |
|
565 |
(curHeight <= 0) ifTrue:[ |
|
566 |
curHeight := curHeight + (r height) - curY. |
|
567 |
(curHeight < 1) ifTrue:[ curHeight := 1. ]. |
|
568 |
]. |
|
569 |
px := 0. |
|
570 |
curY to:(curY + curHeight - 1) do: [ :i | px := px max:(xMax at:(i + 1)). ]. |
|
571 |
((curX := px - curX - 1) < 0) ifTrue:[ curX := 0. ]. |
|
572 |
] |
|
573 |
ifFalse:[ |
|
574 |
(curY < 0) ifTrue:[ |
|
575 |
(curWidth <= 0) ifTrue:[ |
|
576 |
curWidth := curWidth + (r width) - curX. |
|
577 |
(curWidth < 1) ifTrue:[ curWidth := 1. ]. |
|
578 |
]. |
|
579 |
py := 0. |
|
580 |
curX to:(curX + curWidth - 1) do: [ :i | py := py max:(yMax at:(i + 1)). ]. |
|
581 |
((curY := py - curY - 1) < 0) ifTrue:[ curY := 0. ]. |
|
582 |
]. |
|
583 |
]. |
|
584 |
||
585 |
(curWidth <= 0) ifTrue:[ |
|
586 |
curWidth := curWidth + (r width) - curX. |
|
587 |
(curWidth < 1) ifTrue:[ curWidth := 1. ]. |
|
588 |
]. |
|
589 |
||
590 |
(curHeight <= 0) ifTrue:[ |
|
591 |
curHeight := curHeight + (r height) - curY. |
|
592 |
(curHeight < 1) ifTrue:[ curHeight := 1. ]. |
|
593 |
]. |
|
594 |
||
595 |
px := curX + curWidth. |
|
596 |
py := curY + curHeight. |
|
597 |
||
598 |
"/ Adjust the xMax and yMax arrays |
|
599 |
curX to: (curX + curWidth - 1) do:[ :i | yMax at:(i + 1) put:py. ]. |
|
600 |
curY to: (curY + curHeight - 1) do:[ :i | xMax at:(i + 1) put:px. ]. |
|
601 |
||
602 |
"/ Zero width and height must mean that this is the last item (or |
|
603 |
"/ else something is wrong). |
|
604 |
((c gridHeight) == 0 and:[ (c gridWidth) == 0 ]) ifTrue:[ |
|
605 |
curRow := curCol := -1. |
|
606 |
]. |
|
607 |
||
608 |
"/ Zero width starts a new row |
|
609 |
((c gridHeight) == 0 and:[ curRow < 0 ]) |
|
610 |
ifTrue:[ |
|
611 |
curCol := curX + curWidth. |
|
612 |
] |
|
613 |
ifFalse:[ |
|
614 |
((c gridWidth) == 0 and:[ curCol < 0 ]) ifTrue:[ |
|
615 |
curRow := curY + curHeight. |
|
616 |
]. |
|
617 |
]. |
|
618 |
||
619 |
"/ Assign the new values to the gridbag slave */ |
|
620 |
c tempX:curX. |
|
621 |
c tempY:curY. |
|
622 |
c tempWidth:curWidth. |
|
623 |
c tempHeight:curHeight. |
|
624 |
]. |
|
625 |
||
626 |
"/ |
|
627 |
"/ Apply minimum row/column dimensions and weights |
|
628 |
"/ |
|
629 |
r minWidth: (columnWidths copy). |
|
630 |
r minHeight: (rowHeights copy). |
|
631 |
r weightX: (columnWeights copy). |
|
632 |
r weightY: (rowWeights copy). |
|
633 |
||
634 |
"/ |
|
635 |
"/ Pass #3 |
|
636 |
"/ |
|
637 |
"/ Distribute the minimun widths and weights: |
|
638 |
"/ |
|
639 |
nextSize := MAX_INT_VAL. |
|
640 |
||
641 |
i := 1. [ i ~~ MAX_INT_VAL ] whileTrue:[ |
|
642 |
subViews do:[ :child | |
|
643 |
c := self constraints:child. |
|
644 |
||
645 |
((c tempWidth) == i) ifTrue:[ |
|
646 |
px := (c tempX) + (c tempWidth). |
|
647 |
||
648 |
"/ |
|
649 |
"/ Figure out if we should use this slaves weight. If the weight |
|
650 |
"/ is less than the total weight spanned by the width of the cell, |
|
651 |
"/ then discard the weight. Otherwise split the difference |
|
652 |
"/ according to the existing weights. |
|
653 |
"/ |
|
654 |
weightDiff := (c weightX) asFloat. |
|
655 |
(c tempX) to:(px - 1) do:[ :k | weightDiff := weightDiff - ((r weightX) at:(k + 1)) ]. |
|
656 |
||
657 |
(weightDiff > 0.0) ifTrue:[ |
|
658 |
weight := 0.0. |
|
659 |
(c tempX) to:(px - 1) do:[ :k | weight := weight + ((r weightX) at:(k + 1)). ]. |
|
660 |
||
661 |
k := c tempX. |
|
662 |
[ (weight > 0.0) and:[k < px] ] whileTrue:[ |
|
663 |
| wt dx | |
|
664 |
||
665 |
wt := ((r weightX) at:(k + 1)) asFloat. |
|
666 |
dx := (wt * weightDiff) / weight. |
|
667 |
wt at:(k + 1) put:(wt + dx). |
|
668 |
weightDiff := weightDiff - dx. |
|
669 |
weight := weight - wt. |
|
670 |
]. |
|
671 |
(r weightX) at:(px - 1 + 1) put:(((r weightX) at:(px - 1 + 1)) + weightDiff). |
|
672 |
]. |
|
673 |
||
674 |
"/ Calculate the minWidth array values. |
|
675 |
"/ First, figure out how wide the current slave needs to be. |
|
676 |
"/ Then, see if it will fit within the current minWidth values. |
|
677 |
"/ If it will not fit, add the difference according to the |
|
678 |
"/ weightX array. |
|
679 |
||
680 |
pixelsDiff := (c minWidth) + (c ipadX) + (c insets left) + (c insets right). |
|
681 |
(c tempX) to:(px - 1) do:[ :k | |
|
682 |
pixelsDiff := pixelsDiff - ((r minWidth) at:(k + 1)). |
|
683 |
]. |
|
684 |
||
685 |
(pixelsDiff > 0) ifTrue:[ |
|
686 |
weight := 0.0. |
|
687 |
(c tempX) to:(px - 1) do:[ :k | weight := weight + ((r weightX) at:(k + 1)). ]. |
|
688 |
k := c tempX. |
|
689 |
[ (weight > 0.0) and:[k < px] ] whileTrue:[ |
|
690 |
| wt dx | |
|
691 |
||
692 |
wt := ((r weightX) at:(k + 1)) asFloat. |
|
693 |
dx := ((wt * pixelsDiff) / weight) asInteger. |
|
694 |
(r minWidth) at:(k + 1) put:(((r minWidth) at:(k + 1)) + dx). |
|
695 |
pixelsDiff := pixelsDiff - dx. |
|
696 |
weight := weight - wt. |
|
697 |
]. |
|
698 |
(r minWidth) at:(px - 1 + 1) put:(((r minWidth) at:(px - 1 + 1)) + pixelsDiff). |
|
699 |
]. |
|
700 |
] ifFalse:[ |
|
701 |
(((c tempWidth) > i) and:[ (c tempWidth) < nextSize ]) ifTrue:[ |
|
702 |
nextSize := c tempWidth. |
|
703 |
]. |
|
704 |
]. |
|
705 |
||
706 |
"/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
707 |
||
708 |
((c tempHeight) == i) ifTrue:[ |
|
709 |
py := (c tempY) + (c tempHeight). |
|
710 |
||
711 |
"/ |
|
712 |
"/ Figure out if we should use this slaves weight. If the weight |
|
713 |
"/ is less than the total weight spanned by the width of the cell, |
|
714 |
"/ then discard the weight. Otherwise split the difference |
|
715 |
"/ according to the existing weights. |
|
716 |
"/ |
|
717 |
weightDiff := (c weightY) asFloat. |
|
718 |
(c tempY) to:(py - 1) do:[ :k | weightDiff := weightDiff - ((r weightY) at:(k + 1)) ]. |
|
719 |
||
720 |
(weightDiff > 0.0) ifTrue:[ |
|
721 |
weight := 0.0. |
|
722 |
(c tempY) to:(py - 1) do:[ :k | weight := weight + ((r weightY) at:(k + 1)). ]. |
|
723 |
||
724 |
k := c tempY. |
|
725 |
[ (weight > 0.0) and:[k < py] ] whileTrue:[ |
|
726 |
| wt dy | |
|
727 |
||
728 |
wt := ((r weightY) at:(k + 1)) asFloat. |
|
729 |
dy := (wt * weightDiff) / weight. |
|
730 |
wt at:(k + 1) put:(wt + dy). |
|
731 |
weightDiff := weightDiff - dy. |
|
732 |
weight := weight - wt. |
|
733 |
]. |
|
734 |
(r weightY) at:(py - 1 + 1) put:(((r weightY) at:(py - 1 + 1)) + weightDiff). |
|
735 |
]. |
|
736 |
||
737 |
"/ Calculate the minWidth array values. |
|
738 |
"/ First, figure out how wide the current slave needs to be. |
|
739 |
"/ Then, see if it will fit within the current minWidth values. |
|
740 |
"/ If it will not fit, add the difference according to the |
|
741 |
"/ weightX array. |
|
742 |
||
743 |
pixelsDiff := (c minHeight) + (c ipadY) + (c insets top) + (c insets bottom). |
|
744 |
(c tempY) to:(py - 1) do:[ :k | |
|
745 |
pixelsDiff := pixelsDiff - ((r minHeight) at:(k + 1)). |
|
746 |
]. |
|
747 |
||
748 |
(pixelsDiff > 0) ifTrue:[ |
|
749 |
weight := 0.0. |
|
750 |
(c tempY) to:(py - 1) do:[ :k | |
|
751 |
weight := weight + ((r weightY) at:(k + 1)). |
|
752 |
]. |
|
753 |
k := c tempY. |
|
754 |
[ (weight > 0.0) and:[k < py] ] whileTrue:[ |
|
755 |
| wt dy | |
|
756 |
||
757 |
wt := ((r weightY) at:(k + 1)) asFloat. |
|
758 |
dy := ((wt * pixelsDiff) / weight) asInteger. |
|
759 |
(r minHeight) at:(k + 1) put:(((r minHeight) at:(k + 1)) + dy). |
|
760 |
pixelsDiff := pixelsDiff - dy. |
|
761 |
weight := weight - wt. |
|
762 |
]. |
|
763 |
(r minHeight) at:(py - 1 + 1) put:(((r minHeight) at:(py - 1 + 1)) + pixelsDiff). |
|
764 |
]. |
|
765 |
] ifFalse:[ |
|
766 |
(((c tempHeight) > i) and:[ (c tempHeight) < nextSize ]) ifTrue:[ |
|
767 |
nextSize := c tempHeight. |
|
768 |
]. |
|
769 |
]. |
|
770 |
]. |
|
771 |
i := nextSize. |
|
772 |
nextSize := MAX_INT_VAL. |
|
773 |
]. |
|
774 |
||
775 |
^ r |
|
776 |
||
777 |
"Modified: / 29.3.1997 / 11:06:45 / cg" |
|
778 |
"Created: / 19.1.1998 / 17:24:26 / av" |
|
779 |
"Modified: / 21.1.1998 / 18:40:38 / av" |
|
780 |
! |
|
781 |
||
782 |
getMinSize:info |
|
783 |
"Figure out the minimum size of the master based on the information from getLayoutInfo. The |
|
784 |
result will be returned as a rectangle with width and height set to the minimum size." |
|
785 |
||
786 |
| d insets t | |
|
787 |
||
788 |
insets := Insets new. |
|
789 |
||
790 |
d := Rectangle origin:0@0 extent:0@0. |
|
791 |
||
792 |
t := 0. |
|
793 |
1 to:(info width) do:[ :i | t := t + ((info minWidth) at:i). ]. |
|
794 |
d width:(t + (insets left) + (insets right)). |
|
795 |
||
796 |
t := 0. |
|
797 |
1 to:(info height) do:[ :i | t := t + ((info minHeight) at:i). ]. |
|
798 |
d height:(t + (insets top) + (insets bottom)). |
|
799 |
||
800 |
^ d |
|
801 |
||
802 |
"Modified: / 1.2.1998 / 12:59:00 / av" |
|
803 |
! |
|
804 |
||
805 |
minimumLayoutSize |
|
806 |
"Return our minimum layout size. The width and height of the returned rectangle gives the minimum |
|
807 |
layout size." |
|
808 |
||
809 |
| info | |
|
810 |
||
811 |
info := self getLayoutInfo:#MINIMUM_SIZE. |
|
812 |
^ self getMinSize:info |
|
813 |
||
814 |
"Created: / 19.1.1998 / 17:25:01 / av" |
|
815 |
"Modified: / 1.2.1998 / 12:59:38 / av" |
|
816 |
! |
|
817 |
||
818 |
preferredLayoutSize |
|
819 |
"Return our preffered layout size. The width and height of the returned rectangle gives the |
|
820 |
preferred layout size." |
|
821 |
||
822 |
| info | |
|
823 |
||
824 |
info := self getLayoutInfo:#PREFFERED_SIZE. |
|
825 |
^ self getMinSize:info |
|
826 |
||
827 |
"Created: / 19.1.1998 / 17:23:37 / av" |
|
828 |
"Modified: / 1.2.1998 / 13:00:04 / av" |
|
829 |
! ! |
|
830 |
||
831 |
!GridBagLayoutView methodsFor:'queries'! |
|
832 |
||
833 |
preferredExtent |
|
834 |
"Return a good extent, one that makes subviews fit. Return the the preferred extent as a point |
|
835 |
where x and y represents the width and height of the extent." |
|
836 |
||
837 |
| d | |
|
838 |
||
839 |
"/ If I have an explicit preferredExtent .. |
|
840 |
preferredExtent notNil ifTrue:[ ^ preferredExtent ]. |
|
841 |
||
842 |
(self subViews) isNil ifTrue:[ ^ super preferredExtent. ]. |
|
843 |
||
844 |
d := self preferredLayoutSize. |
|
845 |
^ (d width) @ (d height). |
|
846 |
||
847 |
"Created: / 17.1.1998 / 00:11:46 / av" |
|
848 |
"Modified: / 1.2.1998 / 13:01:46 / av" |
|
849 |
! ! |
|
850 |
||
851 |
!GridBagLayoutView class methodsFor:'documentation'! |
|
852 |
||
853 |
version |
|
2840 | 854 |
^ '$Header: /cvs/stx/stx/libwidg2/GridBagLayoutView.st,v 1.5 2005-08-02 13:14:18 cg Exp $' |
721 | 855 |
! ! |