comparison dwt/layout/GridData.d @ 0:380af2bdd8e5

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