comparison dwtx/draw2d/GridLayout.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children c3583c6ec027
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Asim Ullah - Ported for use in draw2d (c.f Bugzilla 71684). [Sep 10, 2004]
11 * Port to the D programming language:
12 * Frank Benoit <benoit@tionex.de>
13 *******************************************************************************/
14 module dwtx.draw2d.GridLayout;
15
16 import dwt.dwthelper.utils;
17 import dwtx.dwtxhelper.Collection;
18
19 import dwt.DWT;
20 import dwtx.draw2d.geometry.Dimension;
21 import dwtx.draw2d.geometry.Rectangle;
22 import dwtx.draw2d.AbstractLayout;
23 import dwtx.draw2d.IFigure;
24 import dwtx.draw2d.GridData;
25
26 /**
27 * Lays out children into a Grid arrangement in which overall aligment and
28 * spacing can be configured, as well as specfic layout requirements for the
29 * each individual member of the GridLayout. This layout is a Draw2D port of the
30 * swt GridLayout.
31 *
32 * <code>GridLayout</code> has a number of configuration fields, and the
33 * Figures it lays out can have an associated layout data object, called
34 * <code>GridData</code> (similar to the swt GridData object). The power of
35 * <code>GridLayout</code> lies in the ability to configure
36 * <code>GridData</code> for each Figure in the layout.
37 * <p>
38 * The following code creates a container Figure managed by a
39 * <code>GridLayout</code> with 2 columns, containing 3 RectangleFigure
40 * shapes, the last of which has been given further layout instructions. Note
41 * that it is the <code>GridLayout</code> method <code>setConstraint</code>
42 * that binds the child <code>Figure</code> to its layout
43 * <code>GridData</code> object.
44 *
45 * <pre>
46 * Figure container = new Figure();
47 * GridLayout gridLayout = new GridLayout();
48 * gridLayout.numColumns = 2;
49 * container.setLayout(gridLayout);
50 *
51 * Shape rect;
52 * rect = new RectangleFigure();
53 * container.add(rect);
54 *
55 * rect = new RectangleFigure();
56 * container.add(rect);
57 *
58 * rect = new RectangleFigure();
59 * GridData gridData = new GridData();
60 * gridData.widthHint = 150;
61 * layout.setConstraint(rect, gridData);
62 *
63 * container.add(rect);
64 * </pre>
65 *
66 * <p>
67 * The <code>numColumns</code> field is the most important field in a
68 * <code>GridLayout</code>. Widgets are laid out in columns from left to
69 * right, and a new row is created when <code>numColumns</code>+ 1 figures
70 * are added to the <code>Figure<code> parent container.
71 *
72 * @see GridData
73 *
74 */
75 public class GridLayout : AbstractLayout {
76
77 /**
78 * numColumns specifies the number of cell columns in the layout.
79 *
80 * The default value is 1.
81 */
82 public int numColumns = 1;
83
84 /**
85 * makeColumnsEqualWidth specifies whether all columns in the layout will be
86 * forced to have the same width.
87 *
88 * The default value is false.
89 */
90 public bool makeColumnsEqualWidth = false;
91
92 /**
93 * marginWidth specifies the number of pixels of horizontal margin that will
94 * be placed along the left and right edges of the layout.
95 *
96 * The default value is 5.
97 */
98 public int marginWidth = 5;
99
100 /**
101 * marginHeight specifies the number of pixels of vertical margin that will
102 * be placed along the top and bottom edges of the layout.
103 *
104 * The default value is 5.
105 */
106 public int marginHeight = 5;
107
108 /**
109 * horizontalSpacing specifies the number of pixels between the right edge
110 * of one cell and the left edge of its neighbouring cell to the right.
111 *
112 * The default value is 5.
113 */
114 public int horizontalSpacing = 5;
115
116 /**
117 * verticalSpacing specifies the number of pixels between the bottom edge of
118 * one cell and the top edge of its neighbouring cell underneath.
119 *
120 * The default value is 5.
121 */
122 public int verticalSpacing = 5;
123
124 /** The layout contraints */
125 protected Map constraints;
126
127 /**
128 * Default Constructor
129 */
130 public this() {
131 //super();
132 constraints = new HashMap();
133 }
134
135 /**
136 * Constructs a new instance of this class given the number of columns, and
137 * whether or not the columns should be forced to have the same width.
138 *
139 * @param numColumns
140 * the number of columns in the grid
141 * @param makeColumnsEqualWidth
142 * whether or not the columns will have equal width
143 *
144 */
145 public this(int numColumns, bool makeColumnsEqualWidth) {
146 this.numColumns = numColumns;
147 this.makeColumnsEqualWidth = makeColumnsEqualWidth;
148 }
149
150 /**
151 * @param child
152 * @param wHint
153 * @param hHint
154 * @return the child size.
155 */
156 protected Dimension getChildSize(IFigure child, int wHint, int hHint) {
157 return child.getPreferredSize(wHint, hHint);
158 }
159
160 GridData getData(IFigure[][] grid, int row, int column, int rowCount,
161 int columnCount, bool first) {
162 IFigure figure = grid[row][column];
163 if (figure !is null) {
164 GridData data = cast(GridData) getConstraint(figure);
165 int hSpan = Math.max(1, Math.min(data.horizontalSpan, columnCount));
166 int vSpan = Math.max(1, data.verticalSpan);
167 int i = first ? row + vSpan - 1 : row - vSpan + 1;
168 int j = first ? column + hSpan - 1 : column - hSpan + 1;
169 if (0 <= i && i < rowCount) {
170 if (0 <= j && j < columnCount) {
171 if (figure is grid[i][j])
172 return data;
173 }
174 }
175 }
176 return null;
177 }
178
179 void initChildren(IFigure container) {
180 List children = container.getChildren();
181 for (int i = 0; i < children.size(); i++) {
182 IFigure child = cast(IFigure) children.get(i);
183 if (child.getLayoutManager() is null)
184 child.setLayoutManager(this);
185 }
186 }
187
188 /*
189 * (non-Javadoc)
190 *
191 * @see dwtx.draw2d.AbstractLayout#calculatePreferredSize(dwtx.draw2d.IFigure,
192 * int, int)
193 */
194 protected Dimension calculatePreferredSize(IFigure container, int wHint,
195 int hHint) {
196 Dimension size = layout(container, false, 0, 0, wHint, hHint, /* flushCache */
197 true);
198 if (wHint !is DWT.DEFAULT)
199 size.width = wHint;
200 if (hHint !is DWT.DEFAULT)
201 size.height = hHint;
202
203 return size;
204 }
205
206 /*
207 * (non-Javadoc)
208 *
209 * @see dwtx.draw2d.LayoutManager#layout(dwtx.draw2d.IFigure)
210 */
211 public void layout(IFigure container) {
212 // initChildren( container);
213 Rectangle rect = container.getClientArea();
214 layout(container, true, rect.x, rect.y, rect.width, rect.height, /* flushCache */
215 true);
216
217 }
218
219 Dimension layout(IFigure container, bool move, int x, int y, int width,
220 int height, bool flushCache) {
221 if (numColumns < 1)
222 return new Dimension(marginWidth * 2, marginHeight * 2);
223 List children = container.getChildren();
224 for (int i = 0; i < children.size(); i++) {
225 IFigure child = cast(IFigure) children.get(i);
226
227 GridData data = cast(GridData) getConstraint(child);
228 if (data is null)
229 setConstraint(child, data = new GridData());
230 if (flushCache)
231 data.flushCache();
232 data.computeSize(child, flushCache);
233 }
234
235 /* Build the grid */
236 int row = 0, column = 0, rowCount = 0, columnCount = numColumns;
237 IFigure[][] grid = new IFigure[][](4,columnCount);
238 for (int i = 0; i < children.size(); i++) {
239 IFigure child = cast(IFigure) children.get(i);
240 GridData data = cast(GridData) getConstraint(child);
241 int hSpan = Math.max(1, Math.min(data.horizontalSpan, columnCount));
242 int vSpan = Math.max(1, data.verticalSpan);
243 while (true) {
244 int lastRow = row + vSpan;
245 if (lastRow >= grid.length) {
246 IFigure[][] newGrid = new IFigure[][](lastRow + 4, columnCount );
247 SimpleType!(IFigure[]).arraycopy(grid, 0, newGrid, 0, grid.length);
248 grid = newGrid;
249 }
250 if (grid[row] is null) {
251 grid[row] = new IFigure[columnCount];
252 }
253 while (column < columnCount && grid[row][column] !is null) {
254 column++;
255 }
256 int endCount = column + hSpan;
257 if (endCount <= columnCount) {
258 int index = column;
259 while (index < endCount && grid[row][index] is null) {
260 index++;
261 }
262 if (index is endCount)
263 break;
264 column = index;
265 }
266 if (column + hSpan >= columnCount) {
267 column = 0;
268 row++;
269 }
270 }
271 for (int j = 0; j < vSpan; j++) {
272 if (grid[row + j] is null) {
273 grid[row + j] = new IFigure[columnCount];
274 }
275 for (int k = 0; k < hSpan; k++) {
276 grid[row + j][column + k] = child;
277 }
278 }
279 rowCount = Math.max(rowCount, row + vSpan);
280 column += hSpan;
281 }
282
283 /* Column widths */
284 int availableWidth = width - horizontalSpacing * (columnCount - 1)
285 - marginWidth * 2;
286 int expandCount = 0;
287 int[] widths = new int[columnCount];
288 int[] minWidths = new int[columnCount];
289 bool[] expandColumn = new bool[columnCount];
290 for (int j = 0; j < columnCount; j++) {
291 for (int i = 0; i < rowCount; i++) {
292 GridData data = getData(grid, i, j, rowCount, columnCount, true);
293 if (data !is null) {
294 int hSpan = Math.max(1, Math.min(data.horizontalSpan,
295 columnCount));
296 if (hSpan is 1) {
297 int w = data.cacheWidth + data.horizontalIndent;
298 widths[j] = Math.max(widths[j], w);
299 if (data.grabExcessHorizontalSpace) {
300 if (!expandColumn[j])
301 expandCount++;
302 expandColumn[j] = true;
303 }
304 if (data.widthHint !is DWT.DEFAULT
305 || !data.grabExcessHorizontalSpace) {
306 minWidths[j] = Math.max(minWidths[j], w);
307 }
308 }
309 }
310 }
311 for (int i = 0; i < rowCount; i++) {
312 GridData data = getData(grid, i, j, rowCount, columnCount,
313 false);
314 if (data !is null) {
315 int hSpan = Math.max(1, Math.min(data.horizontalSpan,
316 columnCount));
317 if (hSpan > 1) {
318 int spanWidth = 0, spanMinWidth = 0, spanExpandCount = 0;
319 for (int k = 0; k < hSpan; k++) {
320 spanWidth += widths[j - k];
321 spanMinWidth += minWidths[j - k];
322 if (expandColumn[j - k])
323 spanExpandCount++;
324 }
325 if (data.grabExcessHorizontalSpace
326 && spanExpandCount is 0) {
327 expandCount++;
328 expandColumn[j] = true;
329 }
330 int w = data.cacheWidth + data.horizontalIndent
331 - spanWidth - (hSpan - 1) * horizontalSpacing;
332 if (w > 0) {
333 if (spanExpandCount is 0) {
334 widths[j] += w;
335 } else {
336 int delta = w / spanExpandCount;
337 int remainder = w % spanExpandCount, last = -1;
338 for (int k = 0; k < hSpan; k++) {
339 if (expandColumn[j - k]) {
340 widths[last = j - k] += delta;
341 }
342 }
343 if (last > -1)
344 widths[last] += remainder;
345 }
346 }
347 if (data.widthHint !is DWT.DEFAULT
348 || !data.grabExcessHorizontalSpace) {
349 w = data.cacheWidth + data.horizontalIndent
350 - spanMinWidth - (hSpan - 1)
351 * horizontalSpacing;
352 if (w > 0) {
353 if (spanExpandCount is 0) {
354 minWidths[j] += w;
355 } else {
356 int delta = w / spanExpandCount;
357 int remainder = w % spanExpandCount, last = -1;
358 for (int k = 0; k < hSpan; k++) {
359 if (expandColumn[j - k]) {
360 minWidths[last = j - k] += delta;
361 }
362 }
363 if (last > -1)
364 minWidths[last] += remainder;
365 }
366 }
367 }
368 }
369 }
370 }
371 }
372 if (makeColumnsEqualWidth) {
373 int minColumnWidth = 0;
374 int columnWidth = 0;
375 for (int i = 0; i < columnCount; i++) {
376 minColumnWidth = Math.max(minColumnWidth, minWidths[i]);
377 columnWidth = Math.max(columnWidth, widths[i]);
378 }
379 columnWidth = width is DWT.DEFAULT || expandCount is 0 ? columnWidth
380 : Math.max(minColumnWidth, availableWidth / columnCount);
381 for (int i = 0; i < columnCount; i++) {
382 expandColumn[i] = expandCount > 0;
383 widths[i] = columnWidth;
384 }
385 } else {
386 if (width !is DWT.DEFAULT && expandCount > 0) {
387 int totalWidth = 0;
388 for (int i = 0; i < columnCount; i++) {
389 totalWidth += widths[i];
390 }
391 int count = expandCount;
392 int delta = (availableWidth - totalWidth) / count;
393 int remainder = (availableWidth - totalWidth) % count;
394 int last = -1;
395 while (totalWidth !is availableWidth) {
396 for (int j = 0; j < columnCount; j++) {
397 if (expandColumn[j]) {
398 if (widths[j] + delta > minWidths[j]) {
399 widths[last = j] = widths[j] + delta;
400 } else {
401 widths[j] = minWidths[j];
402 expandColumn[j] = false;
403 count--;
404 }
405 }
406 }
407 if (last > -1)
408 widths[last] += remainder;
409
410 for (int j = 0; j < columnCount; j++) {
411 for (int i = 0; i < rowCount; i++) {
412 GridData data = getData(grid, i, j, rowCount,
413 columnCount, false);
414 if (data !is null) {
415 int hSpan = Math.max(1, Math.min(
416 data.horizontalSpan, columnCount));
417 if (hSpan > 1) {
418 if (data.widthHint !is DWT.DEFAULT
419 || !data.grabExcessHorizontalSpace) {
420 int spanWidth = 0, spanExpandCount = 0;
421 for (int k = 0; k < hSpan; k++) {
422 spanWidth += widths[j - k];
423 if (expandColumn[j - k])
424 spanExpandCount++;
425 }
426 int w = data.cacheWidth
427 + data.horizontalIndent
428 - spanWidth - (hSpan - 1)
429 * horizontalSpacing;
430 if (w > 0) {
431 if (spanExpandCount is 0) {
432 widths[j] += w;
433 } else {
434 int delta2 = w
435 / spanExpandCount;
436 int remainder2 = w
437 % spanExpandCount, last2 = -1;
438 for (int k = 0; k < hSpan; k++) {
439 if (expandColumn[j - k]) {
440 widths[last2 = j - k] += delta2;
441 }
442 }
443 if (last2 > -1)
444 widths[last2] += remainder2;
445 }
446 }
447 }
448 }
449 }
450 }
451 }
452 if (count is 0)
453 break;
454 totalWidth = 0;
455 for (int i = 0; i < columnCount; i++) {
456 totalWidth += widths[i];
457 }
458 delta = (availableWidth - totalWidth) / count;
459 remainder = (availableWidth - totalWidth) % count;
460 last = -1;
461 }
462 }
463 }
464
465 /* Wrapping */
466 GridData[] flush = null;
467 int flushLength = 0;
468 if (width !is DWT.DEFAULT) {
469 for (int j = 0; j < columnCount; j++) {
470 for (int i = 0; i < rowCount; i++) {
471 GridData data = getData(grid, i, j, rowCount, columnCount,
472 false);
473 if (data !is null) {
474 if (data.heightHint is DWT.DEFAULT) {
475 IFigure child = grid[i][j];
476 // TEMPORARY CODE
477 int hSpan = Math.max(1, Math.min(
478 data.horizontalSpan, columnCount));
479 int currentWidth = 0;
480 for (int k = 0; k < hSpan; k++) {
481 currentWidth += widths[j - k];
482 }
483 currentWidth += (hSpan - 1) * horizontalSpacing
484 - data.horizontalIndent;
485 if ((currentWidth !is data.cacheWidth && data.horizontalAlignment is DWT.FILL)
486 || (data.cacheWidth > currentWidth)) {
487 int trim = 0;
488 /*
489 * // *Note*: Left this in place from DWT //
490 * GridLayout. Not sure if Draw2D Borders or //
491 * Scrollbars 'trim' will need to be takeninto
492 * account.
493 *
494 * if (child instanceof Group) { Group g
495 * =(Group)child; trim = g.getSize ().x -
496 * g.getClientArea ().width; } else if (child
497 * instanceof Scrollable) { Rectangle rect =
498 * ((Scrollable) child).computeTrim (0, 0, 0,0);
499 * trim = rect.width; } else { trim =
500 * child.getBorderWidth () * 2; }
501 */
502 int oldWidthHint = data.widthHint;
503 data.widthHint = Math.max(0, currentWidth
504 - trim);
505 data.cacheWidth = data.cacheHeight = DWT.DEFAULT;
506 data.computeSize(child, false);
507 data.widthHint = oldWidthHint;
508 if (flush is null)
509 flush = new GridData[children.size()];
510 flush[flushLength++] = data;
511 }
512 }
513 }
514 }
515 }
516 }
517
518 /* Row heights */
519 int availableHeight = height - verticalSpacing * (rowCount - 1)
520 - marginHeight * 2;
521 expandCount = 0;
522 int[] heights = new int[rowCount];
523 int[] minHeights = new int[rowCount];
524 bool[] expandRow = new bool[rowCount];
525 for (int i = 0; i < rowCount; i++) {
526 for (int j = 0; j < columnCount; j++) {
527 GridData data = getData(grid, i, j, rowCount, columnCount, true);
528 if (data !is null) {
529 int vSpan = Math.max(1, Math.min(data.verticalSpan,
530 rowCount));
531 if (vSpan is 1) {
532 int h = data.cacheHeight; // + data.verticalIndent;
533 heights[i] = Math.max(heights[i], h);
534 if (data.grabExcessVerticalSpace) {
535 if (!expandRow[i])
536 expandCount++;
537 expandRow[i] = true;
538 }
539 if (data.heightHint !is DWT.DEFAULT
540 || !data.grabExcessVerticalSpace) {
541 minHeights[i] = Math.max(minHeights[i], h);
542 }
543 }
544 }
545 }
546 for (int j = 0; j < columnCount; j++) {
547 GridData data = getData(grid, i, j, rowCount, columnCount,
548 false);
549 if (data !is null) {
550 int vSpan = Math.max(1, Math.min(data.verticalSpan,
551 rowCount));
552 if (vSpan > 1) {
553 int spanHeight = 0, spanMinHeight = 0, spanExpandCount = 0;
554 for (int k = 0; k < vSpan; k++) {
555 spanHeight += heights[i - k];
556 spanMinHeight += minHeights[i - k];
557 if (expandRow[i - k])
558 spanExpandCount++;
559 }
560 if (data.grabExcessVerticalSpace
561 && spanExpandCount is 0) {
562 expandCount++;
563 expandRow[i] = true;
564 }
565 int h = data.cacheHeight - spanHeight - (vSpan - 1)
566 * verticalSpacing; // + data.verticalalIndent
567 if (h > 0) {
568 if (spanExpandCount is 0) {
569 heights[i] += h;
570 } else {
571 int delta = h / spanExpandCount;
572 int remainder = h % spanExpandCount, last = -1;
573 for (int k = 0; k < vSpan; k++) {
574 if (expandRow[i - k]) {
575 heights[last = i - k] += delta;
576 }
577 }
578 if (last > -1)
579 heights[last] += remainder;
580 }
581 }
582 if (data.heightHint !is DWT.DEFAULT
583 || !data.grabExcessVerticalSpace) {
584 h = data.cacheHeight - spanMinHeight - (vSpan - 1)
585 * verticalSpacing; // + data.verticalIndent
586 if (h > 0) {
587 if (spanExpandCount is 0) {
588 minHeights[i] += h;
589 } else {
590 int delta = h / spanExpandCount;
591 int remainder = h % spanExpandCount, last = -1;
592 for (int k = 0; k < vSpan; k++) {
593 if (expandRow[i - k]) {
594 minHeights[last = i - k] += delta;
595 }
596 }
597 if (last > -1)
598 minHeights[last] += remainder;
599 }
600 }
601 }
602 }
603 }
604 }
605 }
606 if (height !is DWT.DEFAULT && expandCount > 0) {
607 int totalHeight = 0;
608 for (int i = 0; i < rowCount; i++) {
609 totalHeight += heights[i];
610 }
611 int count = expandCount;
612 int delta = (availableHeight - totalHeight) / count;
613 int remainder = (availableHeight - totalHeight) % count;
614 int last = -1;
615 while (totalHeight !is availableHeight) {
616 for (int i = 0; i < rowCount; i++) {
617 if (expandRow[i]) {
618 if (heights[i] + delta > minHeights[i]) {
619 heights[last = i] = heights[i] + delta;
620 } else {
621 heights[i] = minHeights[i];
622 expandRow[i] = false;
623 count--;
624 }
625 }
626 }
627 if (last > -1)
628 heights[last] += remainder;
629
630 for (int i = 0; i < rowCount; i++) {
631 for (int j = 0; j < columnCount; j++) {
632 GridData data = getData(grid, i, j, rowCount,
633 columnCount, false);
634 if (data !is null) {
635 int vSpan = Math.max(1, Math.min(data.verticalSpan,
636 rowCount));
637 if (vSpan > 1) {
638 if (data.heightHint !is DWT.DEFAULT
639 || !data.grabExcessVerticalSpace) {
640 int spanHeight = 0, spanExpandCount = 0;
641 for (int k = 0; k < vSpan; k++) {
642 spanHeight += heights[i - k];
643 if (expandRow[i - k])
644 spanExpandCount++;
645 }
646 int h = data.cacheHeight - spanHeight
647 - (vSpan - 1) * verticalSpacing; // +
648 // data.verticalIndent
649 if (h > 0) {
650 if (spanExpandCount is 0) {
651 heights[i] += h;
652 } else {
653 int delta2 = h / spanExpandCount;
654 int remainder2 = h
655 % spanExpandCount, last2 = -1;
656 for (int k = 0; k < vSpan; k++) {
657 if (expandRow[i - k]) {
658 heights[last2 = i - k] += delta2;
659 }
660 }
661 if (last2 > -1)
662 heights[last2] += remainder2;
663 }
664 }
665 }
666 }
667 }
668 }
669 }
670 if (count is 0)
671 break;
672 totalHeight = 0;
673 for (int i = 0; i < rowCount; i++) {
674 totalHeight += heights[i];
675 }
676 delta = (availableHeight - totalHeight) / count;
677 remainder = (availableHeight - totalHeight) % count;
678 last = -1;
679 }
680 }
681
682 /* Position the IFigures */
683 if (move) {
684 int gridY = y + marginHeight;
685 for (int i = 0; i < rowCount; i++) {
686 int gridX = x + marginWidth;
687 for (int j = 0; j < columnCount; j++) {
688 GridData data = getData(grid, i, j, rowCount, columnCount,
689 true);
690 if (data !is null) {
691 int hSpan = Math.max(1, Math.min(data.horizontalSpan,
692 columnCount));
693 int vSpan = Math.max(1, data.verticalSpan);
694 int cellWidth = 0, cellHeight = 0;
695 for (int k = 0; k < hSpan; k++) {
696 cellWidth += widths[j + k];
697 }
698 for (int k = 0; k < vSpan; k++) {
699 cellHeight += heights[i + k];
700 }
701 cellWidth += horizontalSpacing * (hSpan - 1);
702 int childX = gridX + data.horizontalIndent;
703 int childWidth = Math.min(data.cacheWidth, cellWidth);
704 switch (data.horizontalAlignment) {
705 case DWT.CENTER:
706 case GridData.CENTER:
707 childX = gridX
708 + Math.max(0, (cellWidth - childWidth) / 2);
709 break;
710 case DWT.RIGHT:
711 case DWT.END:
712 case GridData.END:
713 childX = gridX
714 + Math.max(0, cellWidth - childWidth);
715 break;
716 case DWT.FILL:
717 childWidth = cellWidth - data.horizontalIndent;
718 break;
719 }
720 cellHeight += verticalSpacing * (vSpan - 1);
721 int childY = gridY; // + data.verticalIndent;
722 int childHeight = Math
723 .min(data.cacheHeight, cellHeight);
724 switch (data.verticalAlignment) {
725 case DWT.CENTER:
726 case GridData.CENTER:
727 childY = gridY
728 + Math.max(0,
729 (cellHeight - childHeight) / 2);
730 break;
731 case DWT.BOTTOM:
732 case DWT.END:
733 case GridData.END:
734 childY = gridY
735 + Math.max(0, cellHeight - childHeight);
736 break;
737 case DWT.FILL:
738 childHeight = cellHeight; // -
739 // data.verticalIndent;
740 break;
741 }
742 IFigure child = grid[i][j];
743 if (child !is null) {
744 // following param could be replaced by
745 // Rectangle.SINGLETON
746 child.setBounds(new Rectangle(childX, childY,
747 childWidth, childHeight));
748 }
749 }
750 gridX += widths[j] + horizontalSpacing;
751 }
752 gridY += heights[i] + verticalSpacing;
753 }
754 }
755
756 // clean up cache
757 for (int i = 0; i < flushLength; i++) {
758 flush[i].cacheWidth = flush[i].cacheHeight = -1;
759 }
760
761 int totalDefaultWidth = 0;
762 int totalDefaultHeight = 0;
763 for (int i = 0; i < columnCount; i++) {
764 totalDefaultWidth += widths[i];
765 }
766 for (int i = 0; i < rowCount; i++) {
767 totalDefaultHeight += heights[i];
768 }
769 totalDefaultWidth += horizontalSpacing * (columnCount - 1)
770 + marginWidth * 2;
771 totalDefaultHeight += verticalSpacing * (rowCount - 1) + marginHeight
772 * 2;
773 return new Dimension(totalDefaultWidth, totalDefaultHeight);
774 }
775
776 /*
777 * (non-Javadoc)
778 *
779 * @see dwtx.draw2d.LayoutManager#getConstraint(dwtx.draw2d.IFigure)
780 */
781 public Object getConstraint(IFigure child) {
782 return constraints.get(cast(Object)child);
783 }
784
785 /**
786 * Sets the layout constraint of the given figure. The constraints can only
787 * be of type {@link GridData}.
788 *
789 * @see LayoutManager#setConstraint(IFigure, Object)
790 */
791 public void setConstraint(IFigure figure, Object newConstraint) {
792 super.setConstraint(figure, newConstraint);
793 if (newConstraint !is null) {
794 constraints.put(cast(Object)figure, newConstraint);
795
796 }
797 }
798
799 }