author | Claus Gittinger <cg@exept.de> |
Wed, 08 Nov 2000 10:17:18 +0100 | |
changeset 1877 | 3b415fa4710f |
parent 1519 | eeb8243a99af |
child 2840 | 93e2b635838d |
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 |
||
194 |
^ super new initialize |
|
195 |
||
196 |
"Created: / 19.1.1998 / 13:53:11 / av" |
|
197 |
"Modified: / 1.2.1998 / 13:02:59 / av" |
|
198 |
! ! |
|
199 |
||
200 |
!GridBagLayoutView methodsFor:'layout'! |
|
201 |
||
202 |
setChildPositions |
|
203 |
"(re)compute position of every child whenever childs are added or my size has changed" |
|
204 |
||
205 |
self arrangeGrid. |
|
206 |
||
207 |
"Modified: / 1.2.1998 / 12:52:22 / av" |
|
208 |
! ! |
|
209 |
||
210 |
!GridBagLayoutView methodsFor:'private'! |
|
211 |
||
212 |
adjustForGravity:c in:r |
|
213 |
||
214 |
| diffx diffy add | |
|
215 |
||
216 |
r setLeft: ((r left) + (c insets left)). |
|
217 |
r width: ((r width) - ((c insets left) + (c insets right))). |
|
218 |
r setTop: ((r top) + (c insets top)). |
|
219 |
r height: ((r height) - ((c insets top) + (c insets bottom))). |
|
220 |
||
221 |
diffx := 0. |
|
222 |
(((c fill) ~~ #HORIZONTAL and:[(c fill) ~~ #BOTH]) and:[(r width) > (add := (c minWidth) + (c ipadX))]) ifTrue:[ |
|
223 |
diffx := (r width) - add. |
|
224 |
r width:add. |
|
225 |
]. |
|
226 |
||
227 |
diffy := 0. |
|
228 |
(((c fill) ~~ #VERTICAL and:[(c fill) ~~ #BOTH]) and:[(r height) > (add := (c minHeight) + (c ipadY))]) ifTrue:[ |
|
229 |
diffy := (r height) - add. |
|
230 |
r height:add. |
|
231 |
]. |
|
232 |
||
233 |
((c anchor) == #CENTER) ifTrue:[ |
|
234 |
r setLeft:((r left) + (diffx / 2)). |
|
235 |
r setTop:((r top) + (diffy / 2)). |
|
236 |
]. |
|
237 |
((c anchor) == #NORTH) ifTrue:[ |
|
238 |
r setLeft:((r left) + (diffx / 2)). |
|
239 |
]. |
|
240 |
((c anchor) == #NORTHEAST) ifTrue:[ |
|
241 |
r setLeft:((r left) + diffx). |
|
242 |
]. |
|
243 |
((c anchor) == #EAST) ifTrue:[ |
|
244 |
r setLeft:((r left) + diffx). |
|
245 |
r setTop:((r top) + (diffy / 2)). |
|
246 |
]. |
|
247 |
((c anchor) == #SOUTHEAST) ifTrue:[ |
|
248 |
r setLeft:((r left) + diffx). |
|
249 |
r setTop:((r top) + diffy). |
|
250 |
]. |
|
251 |
((c anchor) == #SOUTH) ifTrue:[ |
|
252 |
r setLeft:((r left) + (diffx / 2)). |
|
253 |
r setTop:((r top) + diffy). |
|
254 |
]. |
|
255 |
((c anchor) == #SOUTHWEST) ifTrue:[ |
|
256 |
r setTop:((r top) + diffy). |
|
257 |
]. |
|
258 |
((c anchor) == #WEST) ifTrue:[ |
|
259 |
r setTop:((r top) + (diffy / 2)). |
|
260 |
]. |
|
261 |
((c anchor) == #NORTHWEST) ifTrue:[ |
|
262 |
]. |
|
263 |
||
264 |
r setLeft: ((r left) asInteger). |
|
265 |
r setTop: ((r top) asInteger). |
|
266 |
r width: ((r width) asInteger). |
|
267 |
r height: ((r height) asInteger). |
|
268 |
||
269 |
^ r |
|
270 |
||
271 |
"Modified: / 22.1.1998 / 09:50:01 / av" |
|
272 |
! |
|
273 |
||
274 |
arrangeGrid |
|
275 |
" Lay out the grid." |
|
276 |
||
277 |
| insets subViews info ext d r c diffw diffh weight | |
|
278 |
||
279 |
insets := Insets new. |
|
280 |
||
281 |
"/ If the parent has no slaves anymore, then don't do anything |
|
282 |
"/ at all: just leave the parent's size as-is. |
|
283 |
(((subViews := self subViews) size) == 0) ifTrue:[^ self]. |
|
284 |
||
1877
3b415fa4710f
only access originChanged, extentChanged and cornerChanged
Claus Gittinger <cg@exept.de>
parents:
1519
diff
changeset
|
285 |
self extentChangedFlag ifTrue:[ |
1519 | 286 |
ext := self computeExtent. |
287 |
width := ext x. |
|
288 |
height := ext y. |
|
721 | 289 |
]. |
290 |
||
291 |
"/ Pass #1: scan all the slaves to figure out the total amount |
|
292 |
"/ of space needed. |
|
293 |
||
294 |
info := self getLayoutInfo:#PREFERRED_SIZE. |
|
295 |
d := self getMinSize:info. |
|
296 |
||
297 |
((width < (d width)) or:[(height < (d height))]) ifTrue:[ |
|
1519 | 298 |
info := self getLayoutInfo:#MINIMUM_SIZE. |
299 |
d := self getMinSize:info. |
|
721 | 300 |
]. |
301 |
||
302 |
layoutInfo := info. |
|
303 |
r := Rectangle origin:0@0 extent:((d width) @ (d height)). |
|
304 |
||
305 |
"/ If the current dimensions of the window don't match the desired |
|
306 |
"/ dimensions, then adjust the minWidth and minHeight arrays |
|
307 |
"/ according to the weights. |
|
308 |
||
309 |
diffw := width - (r width). |
|
310 |
(diffw ~~ 0) ifTrue:[ |
|
1519 | 311 |
weight := 0.0. |
312 |
0 to:((info width) - 1) do:[ :i | |
|
313 |
weight := weight + ((info weightX) at:(i + 1)). |
|
314 |
]. |
|
315 |
(weight > 0.0) ifTrue:[ |
|
316 |
0 to:((info width) - 1) do:[ :i | |
|
317 |
| dx | |
|
318 |
dx := ((diffw * ((info weightX) at:(i + 1))) / weight) asInteger. |
|
319 |
(info minWidth) at:(i + 1) put:(((info minWidth) at:(i + 1)) + dx). |
|
320 |
r width:((r width) + dx). |
|
321 |
(((info minWidth) at:(i + 1)) < 0) ifTrue:[ |
|
322 |
r width:((r width) - ((info minWidth) at:(i + 1))). |
|
323 |
(info minWidth) at:(i + 1) put:0. |
|
324 |
]. |
|
325 |
]. |
|
326 |
]. |
|
327 |
diffw := width - (r width). |
|
721 | 328 |
] ifFalse:[ |
1519 | 329 |
diffw := 0. |
721 | 330 |
]. |
331 |
||
332 |
diffh := height - (r height). |
|
333 |
(diffh ~~ 0) ifTrue:[ |
|
1519 | 334 |
weight := 0.0. |
335 |
0 to:((info height) - 1) do:[ :i | |
|
336 |
weight := weight + ((info weightY) at:(i + 1)). |
|
337 |
]. |
|
338 |
(weight > 0.0) ifTrue:[ |
|
339 |
0 to:((info height) - 1) do:[ :i | |
|
340 |
| dy | |
|
341 |
dy := ((diffh * ((info weightY) at:(i + 1))) / weight) asInteger. |
|
342 |
(info minHeight) at:(i + 1) put:(((info minHeight) at:(i + 1)) + dy). |
|
343 |
r height:((r height) + dy). |
|
344 |
(((info minHeight) at:(i + 1)) < 0) ifTrue:[ |
|
345 |
r height:((r height) - ((info minHeight) at:(i + 1))). |
|
346 |
(info minHeight) at:(i + 1) put:0. |
|
347 |
]. |
|
348 |
]. |
|
349 |
]. |
|
350 |
diffh := height - (r height). |
|
721 | 351 |
] ifFalse:[ |
1519 | 352 |
diffh := 0. |
721 | 353 |
]. |
354 |
||
355 |
"/ Now do the actual layout of the slaves using the layout information |
|
356 |
"/ that has been collected. |
|
357 |
||
358 |
info startX:(((diffw / 2) + (insets left)) asInteger). |
|
359 |
info startY:(((diffh / 2) + (insets top)) asInteger). |
|
360 |
||
361 |
subViews do:[ :child | |
|
1519 | 362 |
|rW rH| |
721 | 363 |
|
1519 | 364 |
c := self constraints:child. |
365 |
||
366 |
r setLeft:(info startX). |
|
367 |
0 to:((c tempX) - 1) do:[ :i | r setLeft:((r left) + ((info minWidth) at:(i + 1))). ]. |
|
721 | 368 |
|
1519 | 369 |
r setTop:(info startY). |
370 |
0 to:((c tempY) - 1) do:[ :i | r setTop:((r top) + ((info minHeight) at:(i + 1))). ]. |
|
721 | 371 |
|
1519 | 372 |
r width:0. |
373 |
(c tempX) to:((c tempX) + (c tempWidth) - 1) do:[ :i | |
|
374 |
r width:((r width) + ((info minWidth) at:(i + 1))). |
|
375 |
]. |
|
721 | 376 |
|
1519 | 377 |
r height:0. |
378 |
(c tempY) to:((c tempY) + (c tempHeight) - 1) do:[ :i | |
|
379 |
r height:((r height) + ((info minHeight) at:(i + 1))). |
|
380 |
]. |
|
721 | 381 |
|
1519 | 382 |
self adjustForGravity:c in:r. |
721 | 383 |
|
1519 | 384 |
"/ If the window is too small to be interesting then |
385 |
"/ unmap it. Otherwise configure it and then make sure |
|
386 |
"/ its mapped. |
|
387 |
rW := r width. |
|
388 |
rH := r height. |
|
721 | 389 |
|
1519 | 390 |
((rW <= 0) or:[rH <= 0]) ifTrue:[ |
391 |
child origin:(0@0) ; width:0 height:0. |
|
392 |
] ifFalse:[ |
|
393 |
child origin:((r left) @ (r top)). |
|
394 |
child width:rW. |
|
395 |
child height:rH. |
|
396 |
]. |
|
721 | 397 |
]. |
398 |
||
399 |
"Modified: / 1.2.1998 / 12:53:37 / av" |
|
400 |
! |
|
401 |
||
402 |
constraints:anObject |
|
403 |
" Get the GridBag constraints for an object. As a fallback create new constraints for objects |
|
404 |
which don't have any constraints yet." |
|
405 |
||
406 |
| c | |
|
407 |
||
408 |
c := anObject objectAttributeAt:#GridBagConstraints. |
|
409 |
c isNil ifTrue:[ |
|
410 |
anObject objectAttributeAt:#GridBagConstraints put:(c := GridBagConstraints new) |
|
411 |
]. |
|
412 |
^ c. |
|
413 |
||
414 |
"Created: / 21.1.1998 / 00:24:34 / av" |
|
415 |
"Modified: / 1.2.1998 / 12:55:23 / av" |
|
416 |
! |
|
417 |
||
418 |
getLayoutInfo:which |
|
419 |
"return a good extent, one that makes subviews fit" |
|
420 |
||
421 |
| |
|
422 |
MAX_INT_VAL subViews |
|
423 |
r i k px py pixelsDiff weightDiff weight nextSize c |
|
424 |
curX curY curWidth curHeight curRow curCol xMax yMax |
|
425 |
| |
|
426 |
||
427 |
MAX_INT_VAL := 9999999999. |
|
428 |
subViews := (self subViews). |
|
429 |
||
430 |
r := GridBagLayoutInfo new. |
|
431 |
r width:0. |
|
432 |
r height:0. |
|
433 |
||
434 |
xMax := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
435 |
yMax := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
436 |
curRow := curCol := -1. |
|
437 |
||
438 |
"/ Pass #1 |
|
439 |
"/ |
|
440 |
"/ Figure out the dimensions of the layout grid (use a value of 1 for |
|
441 |
"/ zero or negative widths and heights). |
|
442 |
||
443 |
subViews do:[ :child | |
|
444 |
c := self constraints:child. |
|
445 |
||
446 |
curX := c gridX. |
|
447 |
curY := c gridY. |
|
448 |
((curWidth := c gridWidth) <= 0) ifTrue:[ curWidth := 1. ]. |
|
449 |
((curHeight := c gridHeight) <= 0) ifTrue:[ curHeight := 1. ]. |
|
450 |
||
451 |
"/ If x or y is negative, then use relative positioning: |
|
452 |
((curX < 0) and:[ curY < 0 ]) |
|
453 |
ifTrue:[ |
|
454 |
(curRow >= 0) |
|
455 |
ifTrue: [ curY := curRow. ] |
|
456 |
ifFalse:[ (curCol >= 0) ifTrue:[ curX := curCol. ] ifFalse: [ curY := 0. ]]. |
|
457 |
]. |
|
458 |
||
459 |
(curX < 0) |
|
460 |
ifTrue:[ |
|
461 |
px := 0. |
|
462 |
curY to:(curY + curHeight - 1) do: [ :i | |
|
463 |
px := px max:(xMax at:(i + 1)). |
|
464 |
]. |
|
465 |
((curX := px - curX - 1) < 0) ifTrue:[ curX := 0. ]. |
|
466 |
] |
|
467 |
ifFalse:[ |
|
468 |
(curY < 0) ifTrue:[ |
|
469 |
py := 0. |
|
470 |
curX to: (curX + curWidth - 1) do: [ :i | |
|
471 |
py := py max: (yMax at:(i + 1)). |
|
472 |
]. |
|
473 |
((curY := py - curY - 1) < 0) ifTrue:[ curY := 0. ]. |
|
474 |
]. |
|
475 |
]. |
|
476 |
||
477 |
"/ Adjust the grid width and height |
|
478 |
px := curX + curWidth. [ (r width) < px ] whileTrue:[ r width:((r width) + 1). ]. |
|
479 |
py := curY + curHeight. [ (r height) < py ] whileTrue:[ r height:((r height) + 1). ]. |
|
480 |
||
481 |
"/ Adjust the xMax and yMax arrays |
|
482 |
curX to:(curX + curWidth - 1) do:[ :i | yMax at:(i + 1) put:py. ]. |
|
483 |
curY to:(curY + curHeight - 1) do:[ :i | xMax at:(i + 1) put:px. ]. |
|
484 |
||
485 |
c minWidth:(child preferredExtent x). |
|
486 |
c minHeight:(child preferredExtent y). |
|
487 |
||
488 |
"/ Zero width and height must mean that this is the last item (or |
|
489 |
"/ else something is wrong). |
|
490 |
(((c gridHeight) == 0) and:[ (c gridWidth) == 0 ]) ifTrue:[ |
|
491 |
curRow := curCol := -1. |
|
492 |
]. |
|
493 |
||
494 |
"/ Zero width starts a new row |
|
495 |
(((c gridHeight) == 0) and:[ curRow < 0 ]) |
|
496 |
ifTrue:[ |
|
497 |
curCol := curX + curWidth. |
|
498 |
] |
|
499 |
ifFalse:[ |
|
500 |
((c gridWidth) == 0 and:[ curCol < 0 ]) ifTrue:[ |
|
501 |
curRow := curY + curHeight. |
|
502 |
]. |
|
503 |
]. |
|
504 |
]. |
|
505 |
||
506 |
"/ |
|
507 |
"/ Apply minimum row/column dimensions |
|
508 |
"/ |
|
509 |
((r width) < (columnWidths size)) ifTrue:[ r width:(columnWidths size). ]. |
|
510 |
((r height) < (rowHeights size)) ifTrue:[ r height:(rowHeights size). ]. |
|
511 |
||
512 |
"/ |
|
513 |
"/ Pass #2 |
|
514 |
"/ |
|
515 |
"/ Negative values for gridX are filled in with the current x value. |
|
516 |
"/ Negative values for gridY are filled in with the current y value. |
|
517 |
"/ Negative or zero values for gridWidth and gridHeight end the current |
|
518 |
"/ row or column, respectively. |
|
519 |
"/ |
|
520 |
||
521 |
curRow := curCol := -1. |
|
522 |
xMax := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
523 |
yMax := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
524 |
||
525 |
subViews do:[ :child | |
|
526 |
c := self constraints:child. |
|
527 |
||
528 |
curX := c gridX. |
|
529 |
curY := c gridY. |
|
530 |
curWidth := c gridWidth. |
|
531 |
curHeight := c gridHeight. |
|
532 |
||
533 |
"/ If x or y is negative, then use relative positioning: |
|
534 |
((curX < 0) and:[ curY < 0 ]) |
|
535 |
ifTrue:[ |
|
536 |
(curRow >= 0) |
|
537 |
ifTrue: [ curY := curRow. ] |
|
538 |
ifFalse:[ (curCol >= 0) |
|
539 |
ifTrue: [ curX := curCol. ] |
|
540 |
ifFalse: [ curY := 0. ]. |
|
541 |
]. |
|
542 |
]. |
|
543 |
||
544 |
(curX < 0) |
|
545 |
ifTrue:[ |
|
546 |
(curHeight <= 0) ifTrue:[ |
|
547 |
curHeight := curHeight + (r height) - curY. |
|
548 |
(curHeight < 1) ifTrue:[ curHeight := 1. ]. |
|
549 |
]. |
|
550 |
px := 0. |
|
551 |
curY to:(curY + curHeight - 1) do: [ :i | px := px max:(xMax at:(i + 1)). ]. |
|
552 |
((curX := px - curX - 1) < 0) ifTrue:[ curX := 0. ]. |
|
553 |
] |
|
554 |
ifFalse:[ |
|
555 |
(curY < 0) ifTrue:[ |
|
556 |
(curWidth <= 0) ifTrue:[ |
|
557 |
curWidth := curWidth + (r width) - curX. |
|
558 |
(curWidth < 1) ifTrue:[ curWidth := 1. ]. |
|
559 |
]. |
|
560 |
py := 0. |
|
561 |
curX to:(curX + curWidth - 1) do: [ :i | py := py max:(yMax at:(i + 1)). ]. |
|
562 |
((curY := py - curY - 1) < 0) ifTrue:[ curY := 0. ]. |
|
563 |
]. |
|
564 |
]. |
|
565 |
||
566 |
(curWidth <= 0) ifTrue:[ |
|
567 |
curWidth := curWidth + (r width) - curX. |
|
568 |
(curWidth < 1) ifTrue:[ curWidth := 1. ]. |
|
569 |
]. |
|
570 |
||
571 |
(curHeight <= 0) ifTrue:[ |
|
572 |
curHeight := curHeight + (r height) - curY. |
|
573 |
(curHeight < 1) ifTrue:[ curHeight := 1. ]. |
|
574 |
]. |
|
575 |
||
576 |
px := curX + curWidth. |
|
577 |
py := curY + curHeight. |
|
578 |
||
579 |
"/ Adjust the xMax and yMax arrays |
|
580 |
curX to: (curX + curWidth - 1) do:[ :i | yMax at:(i + 1) put:py. ]. |
|
581 |
curY to: (curY + curHeight - 1) do:[ :i | xMax at:(i + 1) put:px. ]. |
|
582 |
||
583 |
"/ Zero width and height must mean that this is the last item (or |
|
584 |
"/ else something is wrong). |
|
585 |
((c gridHeight) == 0 and:[ (c gridWidth) == 0 ]) ifTrue:[ |
|
586 |
curRow := curCol := -1. |
|
587 |
]. |
|
588 |
||
589 |
"/ Zero width starts a new row |
|
590 |
((c gridHeight) == 0 and:[ curRow < 0 ]) |
|
591 |
ifTrue:[ |
|
592 |
curCol := curX + curWidth. |
|
593 |
] |
|
594 |
ifFalse:[ |
|
595 |
((c gridWidth) == 0 and:[ curCol < 0 ]) ifTrue:[ |
|
596 |
curRow := curY + curHeight. |
|
597 |
]. |
|
598 |
]. |
|
599 |
||
600 |
"/ Assign the new values to the gridbag slave */ |
|
601 |
c tempX:curX. |
|
602 |
c tempY:curY. |
|
603 |
c tempWidth:curWidth. |
|
604 |
c tempHeight:curHeight. |
|
605 |
]. |
|
606 |
||
607 |
"/ |
|
608 |
"/ Apply minimum row/column dimensions and weights |
|
609 |
"/ |
|
610 |
r minWidth: (columnWidths copy). |
|
611 |
r minHeight: (rowHeights copy). |
|
612 |
r weightX: (columnWeights copy). |
|
613 |
r weightY: (rowWeights copy). |
|
614 |
||
615 |
"/ |
|
616 |
"/ Pass #3 |
|
617 |
"/ |
|
618 |
"/ Distribute the minimun widths and weights: |
|
619 |
"/ |
|
620 |
nextSize := MAX_INT_VAL. |
|
621 |
||
622 |
i := 1. [ i ~~ MAX_INT_VAL ] whileTrue:[ |
|
623 |
subViews do:[ :child | |
|
624 |
c := self constraints:child. |
|
625 |
||
626 |
((c tempWidth) == i) ifTrue:[ |
|
627 |
px := (c tempX) + (c tempWidth). |
|
628 |
||
629 |
"/ |
|
630 |
"/ Figure out if we should use this slaves weight. If the weight |
|
631 |
"/ is less than the total weight spanned by the width of the cell, |
|
632 |
"/ then discard the weight. Otherwise split the difference |
|
633 |
"/ according to the existing weights. |
|
634 |
"/ |
|
635 |
weightDiff := (c weightX) asFloat. |
|
636 |
(c tempX) to:(px - 1) do:[ :k | weightDiff := weightDiff - ((r weightX) at:(k + 1)) ]. |
|
637 |
||
638 |
(weightDiff > 0.0) ifTrue:[ |
|
639 |
weight := 0.0. |
|
640 |
(c tempX) to:(px - 1) do:[ :k | weight := weight + ((r weightX) at:(k + 1)). ]. |
|
641 |
||
642 |
k := c tempX. |
|
643 |
[ (weight > 0.0) and:[k < px] ] whileTrue:[ |
|
644 |
| wt dx | |
|
645 |
||
646 |
wt := ((r weightX) at:(k + 1)) asFloat. |
|
647 |
dx := (wt * weightDiff) / weight. |
|
648 |
wt at:(k + 1) put:(wt + dx). |
|
649 |
weightDiff := weightDiff - dx. |
|
650 |
weight := weight - wt. |
|
651 |
]. |
|
652 |
(r weightX) at:(px - 1 + 1) put:(((r weightX) at:(px - 1 + 1)) + weightDiff). |
|
653 |
]. |
|
654 |
||
655 |
"/ Calculate the minWidth array values. |
|
656 |
"/ First, figure out how wide the current slave needs to be. |
|
657 |
"/ Then, see if it will fit within the current minWidth values. |
|
658 |
"/ If it will not fit, add the difference according to the |
|
659 |
"/ weightX array. |
|
660 |
||
661 |
pixelsDiff := (c minWidth) + (c ipadX) + (c insets left) + (c insets right). |
|
662 |
(c tempX) to:(px - 1) do:[ :k | |
|
663 |
pixelsDiff := pixelsDiff - ((r minWidth) at:(k + 1)). |
|
664 |
]. |
|
665 |
||
666 |
(pixelsDiff > 0) ifTrue:[ |
|
667 |
weight := 0.0. |
|
668 |
(c tempX) to:(px - 1) do:[ :k | weight := weight + ((r weightX) at:(k + 1)). ]. |
|
669 |
k := c tempX. |
|
670 |
[ (weight > 0.0) and:[k < px] ] whileTrue:[ |
|
671 |
| wt dx | |
|
672 |
||
673 |
wt := ((r weightX) at:(k + 1)) asFloat. |
|
674 |
dx := ((wt * pixelsDiff) / weight) asInteger. |
|
675 |
(r minWidth) at:(k + 1) put:(((r minWidth) at:(k + 1)) + dx). |
|
676 |
pixelsDiff := pixelsDiff - dx. |
|
677 |
weight := weight - wt. |
|
678 |
]. |
|
679 |
(r minWidth) at:(px - 1 + 1) put:(((r minWidth) at:(px - 1 + 1)) + pixelsDiff). |
|
680 |
]. |
|
681 |
] ifFalse:[ |
|
682 |
(((c tempWidth) > i) and:[ (c tempWidth) < nextSize ]) ifTrue:[ |
|
683 |
nextSize := c tempWidth. |
|
684 |
]. |
|
685 |
]. |
|
686 |
||
687 |
"/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
688 |
||
689 |
((c tempHeight) == i) ifTrue:[ |
|
690 |
py := (c tempY) + (c tempHeight). |
|
691 |
||
692 |
"/ |
|
693 |
"/ Figure out if we should use this slaves weight. If the weight |
|
694 |
"/ is less than the total weight spanned by the width of the cell, |
|
695 |
"/ then discard the weight. Otherwise split the difference |
|
696 |
"/ according to the existing weights. |
|
697 |
"/ |
|
698 |
weightDiff := (c weightY) asFloat. |
|
699 |
(c tempY) to:(py - 1) do:[ :k | weightDiff := weightDiff - ((r weightY) at:(k + 1)) ]. |
|
700 |
||
701 |
(weightDiff > 0.0) ifTrue:[ |
|
702 |
weight := 0.0. |
|
703 |
(c tempY) to:(py - 1) do:[ :k | weight := weight + ((r weightY) at:(k + 1)). ]. |
|
704 |
||
705 |
k := c tempY. |
|
706 |
[ (weight > 0.0) and:[k < py] ] whileTrue:[ |
|
707 |
| wt dy | |
|
708 |
||
709 |
wt := ((r weightY) at:(k + 1)) asFloat. |
|
710 |
dy := (wt * weightDiff) / weight. |
|
711 |
wt at:(k + 1) put:(wt + dy). |
|
712 |
weightDiff := weightDiff - dy. |
|
713 |
weight := weight - wt. |
|
714 |
]. |
|
715 |
(r weightY) at:(py - 1 + 1) put:(((r weightY) at:(py - 1 + 1)) + weightDiff). |
|
716 |
]. |
|
717 |
||
718 |
"/ Calculate the minWidth array values. |
|
719 |
"/ First, figure out how wide the current slave needs to be. |
|
720 |
"/ Then, see if it will fit within the current minWidth values. |
|
721 |
"/ If it will not fit, add the difference according to the |
|
722 |
"/ weightX array. |
|
723 |
||
724 |
pixelsDiff := (c minHeight) + (c ipadY) + (c insets top) + (c insets bottom). |
|
725 |
(c tempY) to:(py - 1) do:[ :k | |
|
726 |
pixelsDiff := pixelsDiff - ((r minHeight) at:(k + 1)). |
|
727 |
]. |
|
728 |
||
729 |
(pixelsDiff > 0) ifTrue:[ |
|
730 |
weight := 0.0. |
|
731 |
(c tempY) to:(py - 1) do:[ :k | |
|
732 |
weight := weight + ((r weightY) at:(k + 1)). |
|
733 |
]. |
|
734 |
k := c tempY. |
|
735 |
[ (weight > 0.0) and:[k < py] ] whileTrue:[ |
|
736 |
| wt dy | |
|
737 |
||
738 |
wt := ((r weightY) at:(k + 1)) asFloat. |
|
739 |
dy := ((wt * pixelsDiff) / weight) asInteger. |
|
740 |
(r minHeight) at:(k + 1) put:(((r minHeight) at:(k + 1)) + dy). |
|
741 |
pixelsDiff := pixelsDiff - dy. |
|
742 |
weight := weight - wt. |
|
743 |
]. |
|
744 |
(r minHeight) at:(py - 1 + 1) put:(((r minHeight) at:(py - 1 + 1)) + pixelsDiff). |
|
745 |
]. |
|
746 |
] ifFalse:[ |
|
747 |
(((c tempHeight) > i) and:[ (c tempHeight) < nextSize ]) ifTrue:[ |
|
748 |
nextSize := c tempHeight. |
|
749 |
]. |
|
750 |
]. |
|
751 |
]. |
|
752 |
i := nextSize. |
|
753 |
nextSize := MAX_INT_VAL. |
|
754 |
]. |
|
755 |
||
756 |
^ r |
|
757 |
||
758 |
"Modified: / 29.3.1997 / 11:06:45 / cg" |
|
759 |
"Created: / 19.1.1998 / 17:24:26 / av" |
|
760 |
"Modified: / 21.1.1998 / 18:40:38 / av" |
|
761 |
! |
|
762 |
||
763 |
getMinSize:info |
|
764 |
"Figure out the minimum size of the master based on the information from getLayoutInfo. The |
|
765 |
result will be returned as a rectangle with width and height set to the minimum size." |
|
766 |
||
767 |
| d insets t | |
|
768 |
||
769 |
insets := Insets new. |
|
770 |
||
771 |
d := Rectangle origin:0@0 extent:0@0. |
|
772 |
||
773 |
t := 0. |
|
774 |
1 to:(info width) do:[ :i | t := t + ((info minWidth) at:i). ]. |
|
775 |
d width:(t + (insets left) + (insets right)). |
|
776 |
||
777 |
t := 0. |
|
778 |
1 to:(info height) do:[ :i | t := t + ((info minHeight) at:i). ]. |
|
779 |
d height:(t + (insets top) + (insets bottom)). |
|
780 |
||
781 |
^ d |
|
782 |
||
783 |
"Modified: / 1.2.1998 / 12:59:00 / av" |
|
784 |
! |
|
785 |
||
786 |
initialize |
|
787 |
"Initialize the instance. Mainly set our instance variables to required values." |
|
788 |
||
789 |
super initialize. |
|
790 |
||
791 |
columnWidths := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
792 |
rowHeights := IdentityDictionaryWithDefault newWithDefaultValue:0. |
|
793 |
||
794 |
columnWeights := IdentityDictionaryWithDefault newWithDefaultValue:0.0. |
|
795 |
rowWeights := IdentityDictionaryWithDefault newWithDefaultValue:0.0. |
|
796 |
||
797 |
layoutInfo := nil. |
|
798 |
||
799 |
"Created: / 19.1.1998 / 13:53:59 / av" |
|
800 |
"Modified: / 1.2.1998 / 12:57:05 / av" |
|
801 |
! |
|
802 |
||
803 |
minimumLayoutSize |
|
804 |
"Return our minimum layout size. The width and height of the returned rectangle gives the minimum |
|
805 |
layout size." |
|
806 |
||
807 |
| info | |
|
808 |
||
809 |
info := self getLayoutInfo:#MINIMUM_SIZE. |
|
810 |
^ self getMinSize:info |
|
811 |
||
812 |
"Created: / 19.1.1998 / 17:25:01 / av" |
|
813 |
"Modified: / 1.2.1998 / 12:59:38 / av" |
|
814 |
! |
|
815 |
||
816 |
preferredLayoutSize |
|
817 |
"Return our preffered layout size. The width and height of the returned rectangle gives the |
|
818 |
preferred layout size." |
|
819 |
||
820 |
| info | |
|
821 |
||
822 |
info := self getLayoutInfo:#PREFFERED_SIZE. |
|
823 |
^ self getMinSize:info |
|
824 |
||
825 |
"Created: / 19.1.1998 / 17:23:37 / av" |
|
826 |
"Modified: / 1.2.1998 / 13:00:04 / av" |
|
827 |
! ! |
|
828 |
||
829 |
!GridBagLayoutView methodsFor:'queries'! |
|
830 |
||
831 |
preferredExtent |
|
832 |
"Return a good extent, one that makes subviews fit. Return the the preferred extent as a point |
|
833 |
where x and y represents the width and height of the extent." |
|
834 |
||
835 |
| d | |
|
836 |
||
837 |
"/ If I have an explicit preferredExtent .. |
|
838 |
preferredExtent notNil ifTrue:[ ^ preferredExtent ]. |
|
839 |
||
840 |
(self subViews) isNil ifTrue:[ ^ super preferredExtent. ]. |
|
841 |
||
842 |
d := self preferredLayoutSize. |
|
843 |
^ (d width) @ (d height). |
|
844 |
||
845 |
"Created: / 17.1.1998 / 00:11:46 / av" |
|
846 |
"Modified: / 1.2.1998 / 13:01:46 / av" |
|
847 |
! ! |
|
848 |
||
849 |
!GridBagLayoutView class methodsFor:'documentation'! |
|
850 |
||
851 |
version |
|
1877
3b415fa4710f
only access originChanged, extentChanged and cornerChanged
Claus Gittinger <cg@exept.de>
parents:
1519
diff
changeset
|
852 |
^ '$Header: /cvs/stx/stx/libwidg2/GridBagLayoutView.st,v 1.4 2000-11-08 09:17:18 cg Exp $' |
721 | 853 |
! ! |