root/dwt/layout/GridData.d

Revision 246:fd9c62a2998e, 21.9 kB (checked in by Frank Benoit <benoit@tionex.de>, 6 months ago)

Updater SWT 3.4M7 to 3.4

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