comparison dwt/layout/RowLayout.d @ 40:fbe68c33eeee

Sync layout with dwt-linux
author Frank Benoit <benoit@tionex.de>
date Tue, 07 Oct 2008 14:41:31 +0200
parents e831403a80a9
children d8635bb48c7c
comparison
equal deleted inserted replaced
39:43be986a1372 40:fbe68c33eeee
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
10 *******************************************************************************/ 12 *******************************************************************************/
11 module dwt.layout.RowLayout; 13 module dwt.layout.RowLayout;
12
13 import dwt.dwthelper.utils;
14 14
15 import dwt.DWT; 15 import dwt.DWT;
16 import dwt.graphics.Point; 16 import dwt.graphics.Point;
17 import dwt.graphics.Rectangle; 17 import dwt.graphics.Rectangle;
18 import dwt.widgets.Composite;
19 import dwt.widgets.Control; 18 import dwt.widgets.Control;
20 import dwt.widgets.Layout; 19 import dwt.widgets.Layout;
20 import dwt.widgets.Composite;
21 import dwt.layout.RowData;
22 import tango.util.Convert;
23 import Math = tango.math.Math;
24 import dwt.dwthelper.utils;
25
21 26
22 /** 27 /**
23 * Instances of this class determine the size and position of the 28 * Instances of this class determine the size and position of the
24 * children of a <code>Composite</code> by placing them either in 29 * children of a <code>Composite</code> by placing them either in
25 * horizontal rows or vertical columns within the parent <code>Composite</code>. 30 * horizontal rows or vertical columns within the parent <code>Composite</code>.
26 * <p> 31 * <p>
27 * <code>RowLayout</code> aligns all controls in one row if the 32 * <code>RowLayout</code> aligns all controls in one row if the
28 * <code>type</code> is set to horizontal, and one column if it is 33 * <code>type</code> is set to horizontal, and one column if it is
29 * set to vertical. It has the ability to wrap, and provides configurable 34 * set to vertical. It has the ability to wrap, and provides configurable
30 * margins and spacing. <code>RowLayout</code> has a number of configuration 35 * margins and spacing. <code>RowLayout</code> has a number of configuration
31 * fields. In addition, the height and width of each control in a 36 * fields. In addition, the height and width of each control in a
32 * <code>RowLayout</code> can be specified by setting a <code>RowData</code> 37 * <code>RowLayout</code> can be specified by setting a <code>RowData</code>
33 * object into the control using <code>setLayoutData ()</code>. 38 * object into the control using <code>setLayoutData ()</code>.
34 * </p> 39 * </p>
35 * <p> 40 * <p>
36 * The following example code creates a <code>RowLayout</code>, sets all 41 * The following example code creates a <code>RowLayout</code>, sets all
37 * of its fields to non-default values, and then sets it into a 42 * of its fields to non-default values, and then sets it into a
38 * <code>Shell</code>. 43 * <code>Shell</code>.
39 * <pre> 44 * <pre>
40 * RowLayout rowLayout = new RowLayout(); 45 * RowLayout rowLayout = new RowLayout();
41 * rowLayout.wrap = false; 46 * rowLayout.wrap = false;
42 * rowLayout.pack = false; 47 * rowLayout.pack = false;
43 * rowLayout.justify = true; 48 * rowLayout.justify = true;
52 * If you are using the default field values, you only need one line of code: 57 * If you are using the default field values, you only need one line of code:
53 * <pre> 58 * <pre>
54 * shell.setLayout(new RowLayout()); 59 * shell.setLayout(new RowLayout());
55 * </pre> 60 * </pre>
56 * </p> 61 * </p>
57 * 62 *
58 * @see RowData 63 * @see RowData
64 * @see <a href="http://www.eclipse.org/swt/snippets/#rowlayout">RowLayout snippets</a>
65 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: LayoutExample</a>
66 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
59 */ 67 */
60 public final class RowLayout : Layout { 68 public final class RowLayout : Layout {
61 69
62 /** 70 /**
63 * type specifies whether the layout places controls in rows or 71 * type specifies whether the layout places controls in rows or
64 * columns. 72 * columns.
65 * 73 *
66 * The default value is HORIZONTAL. 74 * The default value is HORIZONTAL.
67 * 75 *
68 * Possible values are: <ul> 76 * Possible values are: <ul>
69 * <li>HORIZONTAL: Position the controls horizontally from left to right</li> 77 * <li>HORIZONTAL: Position the controls horizontally from left to right</li>
70 * <li>VERTICAL: Position the controls vertically from top to bottom</li> 78 * <li>VERTICAL: Position the controls vertically from top to bottom</li>
71 * </ul> 79 * </ul>
72 * 80 *
73 * @since 2.0 81 * @since 2.0
74 */ 82 */
75 public int type = DWT.HORIZONTAL; 83 public int type = DWT.HORIZONTAL;
76 84
77 /** 85 /**
78 * marginWidth specifies the number of pixels of horizontal margin 86 * marginWidth specifies the number of pixels of horizontal margin
79 * that will be placed along the left and right edges of the layout. 87 * that will be placed along the left and right edges of the layout.
80 * 88 *
81 * The default value is 0. 89 * The default value is 0.
82 * 90 *
83 * @since 3.0 91 * @since 3.0
84 */ 92 */
85 public int marginWidth = 0; 93 public int marginWidth = 0;
86 94
87 /** 95 /**
88 * marginHeight specifies the number of pixels of vertical margin 96 * marginHeight specifies the number of pixels of vertical margin
89 * that will be placed along the top and bottom edges of the layout. 97 * that will be placed along the top and bottom edges of the layout.
90 * 98 *
91 * The default value is 0. 99 * The default value is 0.
92 * 100 *
93 * @since 3.0 101 * @since 3.0
94 */ 102 */
95 public int marginHeight = 0; 103 public int marginHeight = 0;
96 104
97 /** 105 /**
99 * and the edge of its neighbouring cell. 107 * and the edge of its neighbouring cell.
100 * 108 *
101 * The default value is 3. 109 * The default value is 3.
102 */ 110 */
103 public int spacing = 3; 111 public int spacing = 3;
104 112
105 /** 113 /**
106 * wrap specifies whether a control will be wrapped to the next 114 * wrap specifies whether a control will be wrapped to the next
107 * row if there is insufficient space on the current row. 115 * row if there is insufficient space on the current row.
108 * 116 *
109 * The default value is true. 117 * The default value is true.
110 */ 118 */
111 public bool wrap = true; 119 public bool wrap = true;
112 120
113 /** 121 /**
114 * pack specifies whether all controls in the layout take 122 * pack specifies whether all controls in the layout take
115 * their preferred size. If pack is false, all controls will 123 * their preferred size. If pack is false, all controls will
116 * have the same size which is the size required to accommodate the 124 * have the same size which is the size required to accommodate the
117 * largest preferred height and the largest preferred width of all 125 * largest preferred height and the largest preferred width of all
118 * the controls in the layout. 126 * the controls in the layout.
119 * 127 *
120 * The default value is true. 128 * The default value is true.
121 */ 129 */
122 public bool pack = true; 130 public bool pack = true;
123 131
124 /** 132 /**
125 * fill specifies whether the controls in a row should be 133 * fill specifies whether the controls in a row should be
126 * all the same height for horizontal layouts, or the same 134 * all the same height for horizontal layouts, or the same
127 * width for vertical layouts. 135 * width for vertical layouts.
128 * 136 *
129 * The default value is false. 137 * The default value is false.
130 * 138 *
131 * @since 3.0 139 * @since 3.0
132 */ 140 */
133 public bool fill = false; 141 public bool fill = false;
134 142
135 /** 143 /**
191 199
192 /** 200 /**
193 * Constructs a new instance of this class given the type. 201 * Constructs a new instance of this class given the type.
194 * 202 *
195 * @param type the type of row layout 203 * @param type the type of row layout
196 * 204 *
197 * @since 2.0 205 * @since 2.0
198 */ 206 */
199 public this (int type) { 207 public this (int type) {
200 this.type = type; 208 this.type = type;
201 } 209 }
202 210
203 protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache) { 211 override protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache_) {
204 Point extent; 212 Point extent;
205 if (type is DWT.HORIZONTAL) { 213 if (type is DWT.HORIZONTAL) {
206 extent = layoutHorizontal (composite, false, (wHint !is DWT.DEFAULT) && wrap, wHint, flushCache); 214 extent = layoutHorizontal (composite, false, (wHint !is DWT.DEFAULT) && wrap, wHint, flushCache_);
207 } else { 215 } else {
208 extent = layoutVertical (composite, false, (hHint !is DWT.DEFAULT) && wrap, hHint, flushCache); 216 extent = layoutVertical (composite, false, (hHint !is DWT.DEFAULT) && wrap, hHint, flushCache_);
209 } 217 }
210 if (wHint !is DWT.DEFAULT) extent.x = wHint; 218 if (wHint !is DWT.DEFAULT) extent.x = wHint;
211 if (hHint !is DWT.DEFAULT) extent.y = hHint; 219 if (hHint !is DWT.DEFAULT) extent.y = hHint;
212 return extent; 220 return extent;
213 } 221 }
214 222
215 Point computeSize (Control control, bool flushCache) { 223 Point computeSize (Control control, bool flushCache_) {
216 int wHint = DWT.DEFAULT, hHint = DWT.DEFAULT; 224 int wHint = DWT.DEFAULT, hHint = DWT.DEFAULT;
217 RowData data = cast(RowData) control.getLayoutData (); 225 RowData data = cast(RowData) control.getLayoutData ();
218 if (data !is null) { 226 if (data !is null) {
219 wHint = data.width; 227 wHint = data.width;
220 hHint = data.height; 228 hHint = data.height;
221 } 229 }
222 return control.computeSize (wHint, hHint, flushCache); 230 return control.computeSize (wHint, hHint, flushCache_);
223 } 231 }
224 232
225 protected bool flushCache (Control control) { 233 override protected bool flushCache (Control control) {
226 return true; 234 return true;
227 } 235 }
228 236
229 String getName () { 237 String getName () {
230 String string = getClass ().getName (); 238 String string = this.classinfo.name;
231 int index = string.lastIndexOf ('.'); 239 int index = string.lastIndexOf('.');
232 if (index is -1) return string; 240 if (index is -1 ) return string;
233 return string.substring (index + 1, string.length ()); 241 return string[ index + 1 .. string.length ];
234 } 242 }
235 243
236 protected void layout (Composite composite, bool flushCache) { 244 override protected void layout (Composite composite, bool flushCache_) {
237 Rectangle clientArea = composite.getClientArea (); 245 Rectangle clientArea = composite.getClientArea ();
238 if (type is DWT.HORIZONTAL) { 246 if (type is DWT.HORIZONTAL) {
239 layoutHorizontal (composite, true, wrap, clientArea.width, flushCache); 247 layoutHorizontal (composite, true, wrap, clientArea.width, flushCache_);
240 } else { 248 } else {
241 layoutVertical (composite, true, wrap, clientArea.height, flushCache); 249 layoutVertical (composite, true, wrap, clientArea.height, flushCache_);
242 } 250 }
243 } 251 }
244 252
245 Point layoutHorizontal (Composite composite, bool move, bool wrap, int width, bool flushCache) { 253 Point layoutHorizontal (Composite composite, bool move, bool wrap, int width, bool flushCache_) {
246 Control [] children = composite.getChildren (); 254 Control [] children = composite.getChildren ();
247 int count = 0; 255 int count = 0;
248 for (int i=0; i<children.length; i++) { 256 for (int i=0; i<children.length; i++) {
249 Control control = children [i]; 257 Control control = children [i];
250 RowData data = cast(RowData) control.getLayoutData (); 258 RowData data = cast(RowData) control.getLayoutData ();
251 if (data is null || !data.exclude) { 259 if (data is null || !data.exclude) {
252 children [count++] = children [i]; 260 children [count++] = children [i];
253 } 261 }
254 } 262 }
255 if (count is 0) { 263 if (count is 0) {
256 return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom); 264 return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom);
257 } 265 }
258 int childWidth = 0, childHeight = 0, maxHeight = 0; 266 int childWidth = 0, childHeight = 0, maxHeight = 0;
259 if (!pack) { 267 if (!pack) {
260 for (int i=0; i<count; i++) { 268 for (int i=0; i<count; i++) {
261 Control child = children [i]; 269 Control child = children [i];
262 Point size = computeSize (child, flushCache); 270 Point size = computeSize (child, flushCache_);
263 childWidth = Math.max (childWidth, size.x); 271 childWidth = Math.max (childWidth, size.x);
264 childHeight = Math.max (childHeight, size.y); 272 childHeight = Math.max (childHeight, size.y);
265 } 273 }
266 maxHeight = childHeight; 274 maxHeight = childHeight;
267 } 275 }
280 } 288 }
281 int maxX = 0, x = marginLeft + marginWidth, y = marginTop + marginHeight; 289 int maxX = 0, x = marginLeft + marginWidth, y = marginTop + marginHeight;
282 for (int i=0; i<count; i++) { 290 for (int i=0; i<count; i++) {
283 Control child = children [i]; 291 Control child = children [i];
284 if (pack) { 292 if (pack) {
285 Point size = computeSize (child, flushCache); 293 Point size = computeSize (child, flushCache_);
286 childWidth = size.x; 294 childWidth = size.x;
287 childHeight = size.y; 295 childHeight = size.y;
288 } 296 }
289 if (wrap && (i !is 0) && (x + childWidth > width)) { 297 if (wrap && (i !is 0) && (x + childWidth > width)) {
290 wrapped = true; 298 wrapped = true;
359 } 367 }
360 } 368 }
361 return new Point (maxX, y + maxHeight + marginBottom + marginHeight); 369 return new Point (maxX, y + maxHeight + marginBottom + marginHeight);
362 } 370 }
363 371
364 Point layoutVertical (Composite composite, bool move, bool wrap, int height, bool flushCache) { 372 Point layoutVertical (Composite composite, bool move, bool wrap, int height, bool flushCache_) {
365 Control [] children = composite.getChildren (); 373 Control [] children = composite.getChildren ();
366 int count = 0; 374 int count = 0;
367 for (int i=0; i<children.length; i++) { 375 for (int i=0; i<children.length; i++) {
368 Control control = children [i]; 376 Control control = children [i];
369 RowData data = cast(RowData) control.getLayoutData (); 377 RowData data = cast(RowData) control.getLayoutData ();
370 if (data is null || !data.exclude) { 378 if (data is null || !data.exclude) {
371 children [count++] = children [i]; 379 children [count++] = children [i];
372 } 380 }
373 } 381 }
374 if (count is 0) { 382 if (count is 0) {
375 return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom); 383 return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom);
376 } 384 }
377 int childWidth = 0, childHeight = 0, maxWidth = 0; 385 int childWidth = 0, childHeight = 0, maxWidth = 0;
378 if (!pack) { 386 if (!pack) {
379 for (int i=0; i<count; i++) { 387 for (int i=0; i<count; i++) {
380 Control child = children [i]; 388 Control child = children [i];
381 Point size = computeSize (child, flushCache); 389 Point size = computeSize (child, flushCache_);
382 childWidth = Math.max (childWidth, size.x); 390 childWidth = Math.max (childWidth, size.x);
383 childHeight = Math.max (childHeight, size.y); 391 childHeight = Math.max (childHeight, size.y);
384 } 392 }
385 maxWidth = childWidth; 393 maxWidth = childWidth;
386 } 394 }
399 } 407 }
400 int maxY = 0, x = marginLeft + marginWidth, y = marginTop + marginHeight; 408 int maxY = 0, x = marginLeft + marginWidth, y = marginTop + marginHeight;
401 for (int i=0; i<count; i++) { 409 for (int i=0; i<count; i++) {
402 Control child = children [i]; 410 Control child = children [i];
403 if (pack) { 411 if (pack) {
404 Point size = computeSize (child, flushCache); 412 Point size = computeSize (child, flushCache_);
405 childWidth = size.x; 413 childWidth = size.x;
406 childHeight = size.y; 414 childHeight = size.y;
407 } 415 }
408 if (wrap && (i !is 0) && (y + childHeight > height)) { 416 if (wrap && (i !is 0) && (y + childHeight > height)) {
409 wrapped = true; 417 wrapped = true;
485 * Returns a string containing a concise, human-readable 493 * Returns a string containing a concise, human-readable
486 * description of the receiver. 494 * description of the receiver.
487 * 495 *
488 * @return a string representation of the layout 496 * @return a string representation of the layout
489 */ 497 */
490 public String toString () { 498 override public String toString () {
491 String string = getName ()+" {"; 499 String string = getName ()~" {";
492 string += "type="+((type !is DWT.HORIZONTAL) ? "DWT.VERTICAL" : "DWT.HORIZONTAL")+" "; 500 string ~= "type="~((type !is DWT.HORIZONTAL) ? "DWT.VERTICAL" : "DWT.HORIZONTAL")~" ";
493 if (marginWidth !is 0) string += "marginWidth="+marginWidth+" "; 501 if (marginWidth !is 0) string ~= "marginWidth="~to!(String)(marginWidth)~" ";
494 if (marginHeight !is 0) string += "marginHeight="+marginHeight+" "; 502 if (marginHeight !is 0) string ~= "marginHeight="~to!(String)(marginHeight)~" ";
495 if (marginLeft !is 0) string += "marginLeft="+marginLeft+" "; 503 if (marginLeft !is 0) string ~= "marginLeft="~to!(String)(marginLeft)~" ";
496 if (marginTop !is 0) string += "marginTop="+marginTop+" "; 504 if (marginTop !is 0) string ~= "marginTop="~to!(String)(marginTop)~" ";
497 if (marginRight !is 0) string += "marginRight="+marginRight+" "; 505 if (marginRight !is 0) string ~= "marginRight="~to!(String)(marginRight)~" ";
498 if (marginBottom !is 0) string += "marginBottom="+marginBottom+" "; 506 if (marginBottom !is 0) string ~= "marginBottom="~to!(String)(marginBottom)~" ";
499 if (spacing !is 0) string += "spacing="+spacing+" "; 507 if (spacing !is 0) string ~= "spacing="~to!(String)(spacing)~" ";
500 string += "wrap="+wrap+" "; 508 string ~= "wrap="~to!(String)(wrap)~" ";
501 string += "pack="+pack+" "; 509 string ~= "pack="~to!(String)(pack)~" ";
502 string += "fill="+fill+" "; 510 string ~= "fill="~to!(String)(fill)~" ";
503 string += "justify="+justify+" "; 511 string ~= "justify="~to!(String)(justify)~" ";
504 string = string.trim(); 512 string = string.trim();
505 string += "}"; 513 string ~= "}";
506 return string; 514 return string;
507 } 515 }
508 } 516 }