comparison dwt/layout/GridData.d @ 52:47febc12f0a7

GridData, GridLayout
author Frank Benoit <benoit@tionex.de>
date Fri, 11 Jan 2008 11:14:25 +0100
parents
children 8cec8f536af3
comparison
equal deleted inserted replaced
51:e300eb95bec4 52:47febc12f0a7
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 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 *******************************************************************************/
11 module dwt.layout.GridData;
12
13 import dwt.SWT;
14 import dwt.graphics.Point;
15 import dwt.widgets.Control;
16
17 import tango.text.Util;
18 import tango.util.Convert;
19
20 /**
21 * <code>GridData</code> is the layout data object associated with
22 * <code>GridLayout</code>. To set a <code>GridData</code> object into a
23 * control, you use the <code>Control.setLayoutData(Object)</code> method.
24 * <p>
25 * There are two ways to create a <code>GridData</code> object with certain
26 * fields set. The first is to set the fields directly, like this:
27 * <pre>
28 * GridData gridData = new GridData();
29 * gridData.horizontalAlignment = GridData.FILL;
30 * gridData.grabExcessHorizontalSpace = true;
31 * button1.setLayoutData(gridData);
32 * </pre>
33 * The second is to take advantage of convenience style bits defined
34 * by <code>GridData</code>:
35 * <pre>
36 * button1.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
37 * </pre>
38 * </p>
39 * <p>
40 * NOTE: Do not reuse <code>GridData</code> objects. Every control in a
41 * <code>Composite</code> that is managed by a <code>GridLayout</code>
42 * must have a unique <code>GridData</code> object. If the layout data
43 * for a control in a <code>GridLayout</code> is null at layout time,
44 * a unique <code>GridData</code> object is created for it.
45 * </p>
46 *
47 * @see GridLayout
48 * @see Control#setLayoutData
49 */
50 public final class GridData {
51 /**
52 * verticalAlignment specifies how controls will be positioned
53 * vertically within a cell.
54 *
55 * The default value is CENTER.
56 *
57 * Possible values are: <ul>
58 * <li>SWT.BEGINNING (or SWT.TOP): Position the control at the top of the cell</li>
59 * <li>SWT.CENTER: Position the control in the vertical center of the cell</li>
60 * <li>SWT.END (or SWT.BOTTOM): Position the control at the bottom of the cell</li>
61 * <li>SWT.FILL: Resize the control to fill the cell vertically</li>
62 * </ul>
63 */
64 public int verticalAlignment = CENTER;
65
66 /**
67 * horizontalAlignment specifies how controls will be positioned
68 * horizontally within a cell.
69 *
70 * The default value is BEGINNING.
71 *
72 * Possible values are: <ul>
73 * <li>SWT.BEGINNING (or SWT.LEFT): Position the control at the left of the cell</li>
74 * <li>SWT.CENTER: Position the control in the horizontal center of the cell</li>
75 * <li>SWT.END (or SWT.RIGHT): Position the control at the right of the cell</li>
76 * <li>SWT.FILL: Resize the control to fill the cell horizontally</li>
77 * </ul>
78 */
79 public int horizontalAlignment = BEGINNING;
80
81 /**
82 * widthHint specifies the preferred width in pixels. This value
83 * is the wHint passed into Control.computeSize(int, int, bool)
84 * to determine the preferred size of the control.
85 *
86 * The default value is SWT.DEFAULT.
87 *
88 * @see Control#computeSize(int, int, bool)
89 */
90 public int widthHint = SWT.DEFAULT;
91
92 /**
93 * heightHint specifies the preferred height in pixels. This value
94 * is the hHint passed into Control.computeSize(int, int, bool)
95 * to determine the preferred size of the control.
96 *
97 * The default value is SWT.DEFAULT.
98 *
99 * @see Control#computeSize(int, int, bool)
100 */
101 public int heightHint = SWT.DEFAULT;
102
103 /**
104 * horizontalIndent specifies the number of pixels of indentation
105 * that will be placed along the left side of the cell.
106 *
107 * The default value is 0.
108 */
109 public int horizontalIndent = 0;
110
111 /**
112 * verticalIndent specifies the number of pixels of indentation
113 * that will be placed along the top side of the cell.
114 *
115 * The default value is 0.
116 *
117 * @since 3.1
118 */
119 public int verticalIndent = 0;
120
121 /**
122 * horizontalSpan specifies the number of column cells that the control
123 * will take up.
124 *
125 * The default value is 1.
126 */
127 public int horizontalSpan = 1;
128
129 /**
130 * verticalSpan specifies the number of row cells that the control
131 * will take up.
132 *
133 * The default value is 1.
134 */
135 public int verticalSpan = 1;
136
137 /**
138 * <p>grabExcessHorizontalSpace specifies whether the width of the cell
139 * changes depending on the size of the parent Composite. If
140 * grabExcessHorizontalSpace is <code>true</code>, the following rules
141 * apply to the width of the cell:</p>
142 * <ul>
143 * <li>If extra horizontal space is available in the parent, the cell will
144 * grow to be wider than its preferred width. The new width
145 * will be "preferred width + delta" where delta is the extra
146 * horizontal space divided by the number of grabbing columns.</li>
147 * <li>If there is not enough horizontal space available in the parent, the
148 * cell will shrink until it reaches its minimum width as specified by
149 * GridData.minimumWidth. The new width will be the maximum of
150 * "minimumWidth" and "preferred width - delta", where delta is
151 * the amount of space missing divided by the number of grabbing columns.</li>
152 * <li>If the parent is packed, the cell will be its preferred width
153 * as specified by GridData.widthHint.</li>
154 * <li>If the control spans multiple columns and there are no other grabbing
155 * controls in any of the spanned columns, the last column in the span will
156 * grab the extra space. If there is at least one other grabbing control
157 * in the span, the grabbing will be spread over the columns already
158 * marked as grabExcessHorizontalSpace.</li>
159 * </ul>
160 *
161 * <p>The default value is false.</p>
162 *
163 * @see GridData#minimumWidth
164 * @see GridData#widthHint
165 */
166 public bool grabExcessHorizontalSpace = false;
167
168 /**
169 * <p>grabExcessVerticalSpace specifies whether the height of the cell
170 * changes depending on the size of the parent Composite. If
171 * grabExcessVerticalSpace is <code>true</code>, the following rules
172 * apply to the height of the cell:</p>
173 * <ul>
174 * <li>If extra vertical space is available in the parent, the cell will
175 * grow to be taller than its preferred height. The new height
176 * will be "preferred height + delta" where delta is the extra
177 * vertical space divided by the number of grabbing rows.</li>
178 * <li>If there is not enough vertical space available in the parent, the
179 * cell will shrink until it reaches its minimum height as specified by
180 * GridData.minimumHeight. The new height will be the maximum of
181 * "minimumHeight" and "preferred height - delta", where delta is
182 * the amount of space missing divided by the number of grabbing rows.</li>
183 * <li>If the parent is packed, the cell will be its preferred height
184 * as specified by GridData.heightHint.</li>
185 * <li>If the control spans multiple rows and there are no other grabbing
186 * controls in any of the spanned rows, the last row in the span will
187 * grab the extra space. If there is at least one other grabbing control
188 * in the span, the grabbing will be spread over the rows already
189 * marked as grabExcessVerticalSpace.</li>
190 * </ul>
191 *
192 * <p>The default value is false.</p>
193 *
194 * @see GridData#minimumHeight
195 * @see GridData#heightHint
196 */
197 public bool grabExcessVerticalSpace = false;
198
199 /**
200 * minimumWidth specifies the minimum width in pixels. This value
201 * applies only if grabExcessHorizontalSpace is true. A value of
202 * SWT.DEFAULT means that the minimum width will be the result
203 * of Control.computeSize(int, int, bool) where wHint is
204 * determined by GridData.widthHint.
205 *
206 * The default value is 0.
207 *
208 * @since 3.1
209 * @see Control#computeSize(int, int, bool)
210 * @see GridData#widthHint
211 */
212 public int minimumWidth = 0;
213
214 /**
215 * minimumHeight specifies the minimum height in pixels. This value
216 * applies only if grabExcessVerticalSpace is true. A value of
217 * SWT.DEFAULT means that the minimum height will be the result
218 * of Control.computeSize(int, int, bool) where hHint is
219 * determined by GridData.heightHint.
220 *
221 * The default value is 0.
222 *
223 * @since 3.1
224 * @see Control#computeSize(int, int, bool)
225 * @see GridData#heightHint
226 */
227 public int minimumHeight = 0;
228
229 /**
230 * exclude informs the layout to ignore this control when sizing
231 * and positioning controls. If this value is <code>true</code>,
232 * the size and position of the control will not be managed by the
233 * layout. If this value is <code>false</code>, the size and
234 * position of the control will be computed and assigned.
235 *
236 * The default value is <code>false</code>.
237 *
238 * @since 3.1
239 */
240 public bool exclude = false;
241
242 /**
243 * Value for horizontalAlignment or verticalAlignment.
244 * Position the control at the top or left of the cell.
245 * Not recommended. Use SWT.BEGINNING, SWT.TOP or SWT.LEFT instead.
246 */
247 public static const int BEGINNING = SWT.BEGINNING;
248
249 /**
250 * Value for horizontalAlignment or verticalAlignment.
251 * Position the control in the vertical or horizontal center of the cell
252 * Not recommended. Use SWT.CENTER instead.
253 */
254 public static const int CENTER = 2;
255
256 /**
257 * Value for horizontalAlignment or verticalAlignment.
258 * Position the control at the bottom or right of the cell
259 * Not recommended. Use SWT.END, SWT.BOTTOM or SWT.RIGHT instead.
260 */
261 public static const int END = 3;
262
263 /**
264 * Value for horizontalAlignment or verticalAlignment.
265 * Resize the control to fill the cell horizontally or vertically.
266 * Not recommended. Use SWT.FILL instead.
267 */
268 public static const int FILL = SWT.FILL;
269
270 /**
271 * Style bit for <code>new GridData(int)</code>.
272 * Position the control at the top of the cell.
273 * Not recommended. Use
274 * <code>new GridData(int, SWT.BEGINNING, bool, bool)</code>
275 * instead.
276 */
277 public static const int VERTICAL_ALIGN_BEGINNING = 1 << 1;
278
279 /**
280 * Style bit for <code>new GridData(int)</code> to position the
281 * control in the vertical center of the cell.
282 * Not recommended. Use
283 * <code>new GridData(int, SWT.CENTER, bool, bool)</code>
284 * instead.
285 */
286 public static const int VERTICAL_ALIGN_CENTER = 1 << 2;
287
288 /**
289 * Style bit for <code>new GridData(int)</code> to position the
290 * control at the bottom of the cell.
291 * Not recommended. Use
292 * <code>new GridData(int, SWT.END, bool, bool)</code>
293 * instead.
294 */
295 public static const int VERTICAL_ALIGN_END = 1 << 3;
296
297 /**
298 * Style bit for <code>new GridData(int)</code> to resize the
299 * control to fill the cell vertically.
300 * Not recommended. Use
301 * <code>new GridData(int, SWT.FILL, bool, bool)</code>
302 * instead
303 */
304 public static const int VERTICAL_ALIGN_FILL = 1 << 4;
305
306 /**
307 * Style bit for <code>new GridData(int)</code> to position the
308 * control at the left of the cell.
309 * Not recommended. Use
310 * <code>new GridData(SWT.BEGINNING, int, bool, bool)</code>
311 * instead.
312 */
313 public static const int HORIZONTAL_ALIGN_BEGINNING = 1 << 5;
314
315 /**
316 * Style bit for <code>new GridData(int)</code> to position the
317 * control in the horizontal center of the cell.
318 * Not recommended. Use
319 * <code>new GridData(SWT.CENTER, int, bool, bool)</code>
320 * instead.
321 */
322 public static const int HORIZONTAL_ALIGN_CENTER = 1 << 6;
323
324 /**
325 * Style bit for <code>new GridData(int)</code> to position the
326 * control at the right of the cell.
327 * Not recommended. Use
328 * <code>new GridData(SWT.END, int, bool, bool)</code>
329 * instead.
330 */
331 public static const int HORIZONTAL_ALIGN_END = 1 << 7;
332
333 /**
334 * Style bit for <code>new GridData(int)</code> to resize the
335 * control to fill the cell horizontally.
336 * Not recommended. Use
337 * <code>new GridData(SWT.FILL, int, bool, bool)</code>
338 * instead.
339 */
340 public static const int HORIZONTAL_ALIGN_FILL = 1 << 8;
341
342 /**
343 * Style bit for <code>new GridData(int)</code> to resize the
344 * control to fit the remaining horizontal space.
345 * Not recommended. Use
346 * <code>new GridData(int, int, true, bool)</code>
347 * instead.
348 */
349 public static const int GRAB_HORIZONTAL = 1 << 9;
350
351 /**
352 * Style bit for <code>new GridData(int)</code> to resize the
353 * control to fit the remaining vertical space.
354 * Not recommended. Use
355 * <code>new GridData(int, int, bool, true)</code>
356 * instead.
357 */
358 public static const int GRAB_VERTICAL = 1 << 10;
359
360 /**
361 * Style bit for <code>new GridData(int)</code> to resize the
362 * control to fill the cell vertically and to fit the remaining
363 * vertical space.
364 * FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL
365 * Not recommended. Use
366 * <code>new GridData(int, SWT.FILL, bool, true)</code>
367 * instead.
368 */
369 public static const int FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL;
370
371 /**
372 * Style bit for <code>new GridData(int)</code> to resize the
373 * control to fill the cell horizontally and to fit the remaining
374 * horizontal space.
375 * FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL
376 * Not recommended. Use
377 * <code>new GridData(SWT.FILL, int, true, bool)</code>
378 * instead.
379 */
380 public static const int FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL;
381
382 /**
383 * Style bit for <code>new GridData(int)</code> to resize the
384 * control to fill the cell horizontally and vertically and
385 * to fit the remaining horizontal and vertical space.
386 * FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL
387 * Not recommended. Use
388 * <code>new GridData(SWT.FILL, SWT.FILL, true, true)</code>
389 * instead.
390 */
391 public static const int FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL;
392
393 int cacheWidth = -1, cacheHeight = -1;
394 int defaultWhint, defaultHhint, defaultWidth = -1, defaultHeight = -1;
395 int currentWhint, currentHhint, currentWidth = -1, currentHeight = -1;
396
397 /**
398 * Constructs a new instance of GridData using
399 * default values.
400 */
401 public this () {
402 }
403
404 /**
405 * Constructs a new instance based on the GridData style.
406 * This constructor is not recommended.
407 *
408 * @param style the GridData style
409 */
410 public this (int style) {
411 if ((style & VERTICAL_ALIGN_BEGINNING) !is 0) verticalAlignment = BEGINNING;
412 if ((style & VERTICAL_ALIGN_CENTER) !is 0) verticalAlignment = CENTER;
413 if ((style & VERTICAL_ALIGN_FILL) !is 0) verticalAlignment = FILL;
414 if ((style & VERTICAL_ALIGN_END) !is 0) verticalAlignment = END;
415 if ((style & HORIZONTAL_ALIGN_BEGINNING) !is 0) horizontalAlignment = BEGINNING;
416 if ((style & HORIZONTAL_ALIGN_CENTER) !is 0) horizontalAlignment = CENTER;
417 if ((style & HORIZONTAL_ALIGN_FILL) !is 0) horizontalAlignment = FILL;
418 if ((style & HORIZONTAL_ALIGN_END) !is 0) horizontalAlignment = END;
419 grabExcessHorizontalSpace = (style & GRAB_HORIZONTAL) !is 0;
420 grabExcessVerticalSpace = (style & GRAB_VERTICAL) !is 0;
421 }
422
423 /**
424 * Constructs a new instance of GridData according to the parameters.
425 *
426 * @param horizontalAlignment how control will be positioned horizontally within a cell
427 * @param verticalAlignment how control will be positioned vertically within a cell
428 * @param grabExcessHorizontalSpace whether cell will be made wide enough to fit the remaining horizontal space
429 * @param grabExcessVerticalSpace whether cell will be made high enough to fit the remaining vertical space
430 *
431 * @since 3.0
432 */
433 public this (int horizontalAlignment, int verticalAlignment, bool grabExcessHorizontalSpace, bool grabExcessVerticalSpace) {
434 this (horizontalAlignment, verticalAlignment, grabExcessHorizontalSpace, grabExcessVerticalSpace, 1, 1);
435 }
436
437 /**
438 * Constructs a new instance of GridData according to the parameters.
439 *
440 * @param horizontalAlignment how control will be positioned horizontally within a cell
441 * @param verticalAlignment how control will be positioned vertically within a cell
442 * @param grabExcessHorizontalSpace whether cell will be made wide enough to fit the remaining horizontal space
443 * @param grabExcessVerticalSpace whether cell will be made high enough to fit the remaining vertical space
444 * @param horizontalSpan the number of column cells that the control will take up
445 * @param verticalSpan the number of row cells that the control will take up
446 *
447 * @since 3.0
448 */
449 public this (int horizontalAlignment, int verticalAlignment, bool grabExcessHorizontalSpace, bool grabExcessVerticalSpace, int horizontalSpan, int verticalSpan) {
450 this.horizontalAlignment = horizontalAlignment;
451 this.verticalAlignment = verticalAlignment;
452 this.grabExcessHorizontalSpace = grabExcessHorizontalSpace;
453 this.grabExcessVerticalSpace = grabExcessVerticalSpace;
454 this.horizontalSpan = horizontalSpan;
455 this.verticalSpan = verticalSpan;
456 }
457
458 /**
459 * Constructs a new instance of GridData according to the parameters.
460 * A value of SWT.DEFAULT indicates that no minimum width or
461 * no minimum height is specified.
462 *
463 * @param width a minimum width for the column
464 * @param height a minimum height for the row
465 *
466 * @since 3.0
467 */
468 public this (int width, int height) {
469 this.widthHint = width;
470 this.heightHint = height;
471 }
472
473 void computeSize (Control control, int wHint, int hHint, bool flushCache) {
474 if (cacheWidth !is -1 && cacheHeight !is -1) return;
475 if (wHint is this.widthHint && hHint is this.heightHint) {
476 if (defaultWidth is -1 || defaultHeight is -1 || wHint !is defaultWhint || hHint !is defaultHhint) {
477 Point size = control.computeSize (wHint, hHint, flushCache);
478 defaultWhint = wHint;
479 defaultHhint = hHint;
480 defaultWidth = size.x;
481 defaultHeight = size.y;
482 }
483 cacheWidth = defaultWidth;
484 cacheHeight = defaultHeight;
485 return;
486 }
487 if (currentWidth is -1 || currentHeight is -1 || wHint !is currentWhint || hHint !is currentHhint) {
488 Point size = control.computeSize (wHint, hHint, flushCache);
489 currentWhint = wHint;
490 currentHhint = hHint;
491 currentWidth = size.x;
492 currentHeight = size.y;
493 }
494 cacheWidth = currentWidth;
495 cacheHeight = currentHeight;
496 }
497
498 void flushCache () {
499 cacheWidth = cacheHeight = -1;
500 defaultWidth = defaultHeight = -1;
501 currentWidth = currentHeight = -1;
502 }
503
504 char[] getName () {
505 char[] string = this.classinfo.name;
506 int index = locatePrior( string, '.');
507 if (index is string.length ) return string;
508 return string[ index + 1 .. string.length ];
509 }
510
511 /**
512 * Returns a string containing a concise, human-readable
513 * description of the receiver.
514 *
515 * @return a string representation of the GridData object
516 */
517 public char[] toString () {
518 char[] hAlign = "";
519 switch (horizontalAlignment) {
520 case SWT.FILL: hAlign = "SWT.FILL"; break;
521 case SWT.BEGINNING: hAlign = "SWT.BEGINNING"; break;
522 case SWT.LEFT: hAlign = "SWT.LEFT"; break;
523 case SWT.END: hAlign = "SWT.END"; break;
524 case END: hAlign = "GridData.END"; break;
525 case SWT.RIGHT: hAlign = "SWT.RIGHT"; break;
526 case SWT.CENTER: hAlign = "SWT.CENTER"; break;
527 case CENTER: hAlign = "GridData.CENTER"; break;
528 default: hAlign = "Undefined "~to!(char[])(horizontalAlignment); break;
529 }
530 char[] vAlign = "";
531 switch (verticalAlignment) {
532 case SWT.FILL: vAlign = "SWT.FILL"; break;
533 case SWT.BEGINNING: vAlign = "SWT.BEGINNING"; break;
534 case SWT.TOP: vAlign = "SWT.TOP"; break;
535 case SWT.END: vAlign = "SWT.END"; break;
536 case END: vAlign = "GridData.END"; break;
537 case SWT.BOTTOM: vAlign = "SWT.BOTTOM"; break;
538 case SWT.CENTER: vAlign = "SWT.CENTER"; break;
539 case CENTER: vAlign = "GridData.CENTER"; break;
540 default: vAlign = "Undefined "~to!(char[])(verticalAlignment); break;
541 }
542 char[] string = getName()~" {";
543 string ~= "horizontalAlignment="~to!(char[])(hAlign)~" ";
544 if (horizontalIndent !is 0) string ~= "horizontalIndent="~to!(char[])(horizontalIndent)~" ";
545 if (horizontalSpan !is 1) string ~= "horizontalSpan="~to!(char[])(horizontalSpan)~" ";
546 if (grabExcessHorizontalSpace) string ~= "grabExcessHorizontalSpace="~to!(char[])(grabExcessHorizontalSpace)~" ";
547 if (widthHint !is SWT.DEFAULT) string ~= "widthHint="~to!(char[])(widthHint)~" ";
548 if (minimumWidth !is 0) string ~= "minimumWidth="~to!(char[])(minimumWidth)~" ";
549 string ~= "verticalAlignment="~vAlign~" ";
550 if (verticalIndent !is 0) string ~= "verticalIndent="~to!(char[])(verticalIndent)~" ";
551 if (verticalSpan !is 1) string ~= "verticalSpan="~to!(char[])(verticalSpan)~" ";
552 if (grabExcessVerticalSpace) string ~= "grabExcessVerticalSpace="~to!(char[])(grabExcessVerticalSpace)~" ";
553 if (heightHint !is SWT.DEFAULT) string ~= "heightHint="~to!(char[])(heightHint)~" ";
554 if (minimumHeight !is 0) string ~= "minimumHeight="~to!(char[])(minimumHeight)~" ";
555 if (exclude) string ~= "exclude="~to!(char[])(exclude)~" ";
556 string = string.trim();
557 string ~= "}";
558 return string;
559 }
560 }