25
|
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 org.eclipse.swt.layout.RowLayout;
|
|
14
|
|
15 import org.eclipse.swt.SWT;
|
|
16 import org.eclipse.swt.graphics.Point;
|
|
17 import org.eclipse.swt.graphics.Rectangle;
|
|
18 import org.eclipse.swt.widgets.Control;
|
|
19 import org.eclipse.swt.widgets.Layout;
|
|
20 import org.eclipse.swt.widgets.Composite;
|
|
21 import org.eclipse.swt.layout.RowData;
|
48
|
22 import java.lang.all;
|
|
23 version(Tango){
|
51
|
24 import tango.util.Convert;
|
48
|
25 } else { // Phobos
|
51
|
26 import std.conv;
|
48
|
27 }
|
25
|
28
|
|
29
|
|
30 /**
|
|
31 * Instances of this class determine the size and position of the
|
|
32 * children of a <code>Composite</code> by placing them either in
|
|
33 * horizontal rows or vertical columns within the parent <code>Composite</code>.
|
|
34 * <p>
|
|
35 * <code>RowLayout</code> aligns all controls in one row if the
|
|
36 * <code>type</code> is set to horizontal, and one column if it is
|
|
37 * set to vertical. It has the ability to wrap, and provides configurable
|
|
38 * margins and spacing. <code>RowLayout</code> has a number of configuration
|
|
39 * fields. In addition, the height and width of each control in a
|
|
40 * <code>RowLayout</code> can be specified by setting a <code>RowData</code>
|
|
41 * object into the control using <code>setLayoutData ()</code>.
|
|
42 * </p>
|
|
43 * <p>
|
|
44 * The following example code creates a <code>RowLayout</code>, sets all
|
|
45 * of its fields to non-default values, and then sets it into a
|
|
46 * <code>Shell</code>.
|
|
47 * <pre>
|
|
48 * RowLayout rowLayout = new RowLayout();
|
|
49 * rowLayout.wrap = false;
|
|
50 * rowLayout.pack = false;
|
|
51 * rowLayout.justify = true;
|
|
52 * rowLayout.type = SWT.VERTICAL;
|
|
53 * rowLayout.marginLeft = 5;
|
|
54 * rowLayout.marginTop = 5;
|
|
55 * rowLayout.marginRight = 5;
|
|
56 * rowLayout.marginBottom = 5;
|
|
57 * rowLayout.spacing = 0;
|
|
58 * shell.setLayout(rowLayout);
|
|
59 * </pre>
|
|
60 * If you are using the default field values, you only need one line of code:
|
|
61 * <pre>
|
|
62 * shell.setLayout(new RowLayout());
|
|
63 * </pre>
|
|
64 * </p>
|
|
65 *
|
|
66 * @see RowData
|
|
67 * @see <a href="http://www.eclipse.org/swt/snippets/#rowlayout">RowLayout snippets</a>
|
|
68 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: LayoutExample</a>
|
|
69 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
|
70 */
|
|
71 public final class RowLayout : Layout {
|
|
72
|
|
73 /**
|
|
74 * type specifies whether the layout places controls in rows or
|
|
75 * columns.
|
|
76 *
|
|
77 * The default value is HORIZONTAL.
|
|
78 *
|
|
79 * Possible values are: <ul>
|
|
80 * <li>HORIZONTAL: Position the controls horizontally from left to right</li>
|
|
81 * <li>VERTICAL: Position the controls vertically from top to bottom</li>
|
|
82 * </ul>
|
|
83 *
|
|
84 * @since 2.0
|
|
85 */
|
|
86 public int type = SWT.HORIZONTAL;
|
|
87
|
|
88 /**
|
|
89 * marginWidth specifies the number of pixels of horizontal margin
|
|
90 * that will be placed along the left and right edges of the layout.
|
|
91 *
|
|
92 * The default value is 0.
|
|
93 *
|
|
94 * @since 3.0
|
|
95 */
|
|
96 public int marginWidth = 0;
|
|
97
|
|
98 /**
|
|
99 * marginHeight specifies the number of pixels of vertical margin
|
|
100 * that will be placed along the top and bottom edges of the layout.
|
|
101 *
|
|
102 * The default value is 0.
|
|
103 *
|
|
104 * @since 3.0
|
|
105 */
|
|
106 public int marginHeight = 0;
|
|
107
|
|
108 /**
|
|
109 * spacing specifies the number of pixels between the edge of one cell
|
|
110 * and the edge of its neighbouring cell.
|
|
111 *
|
|
112 * The default value is 3.
|
|
113 */
|
|
114 public int spacing = 3;
|
|
115
|
|
116 /**
|
|
117 * wrap specifies whether a control will be wrapped to the next
|
|
118 * row if there is insufficient space on the current row.
|
|
119 *
|
|
120 * The default value is true.
|
|
121 */
|
|
122 public bool wrap = true;
|
|
123
|
|
124 /**
|
|
125 * pack specifies whether all controls in the layout take
|
|
126 * their preferred size. If pack is false, all controls will
|
|
127 * have the same size which is the size required to accommodate the
|
|
128 * largest preferred height and the largest preferred width of all
|
|
129 * the controls in the layout.
|
|
130 *
|
|
131 * The default value is true.
|
|
132 */
|
|
133 public bool pack = true;
|
|
134
|
|
135 /**
|
|
136 * fill specifies whether the controls in a row should be
|
|
137 * all the same height for horizontal layouts, or the same
|
|
138 * width for vertical layouts.
|
|
139 *
|
|
140 * The default value is false.
|
|
141 *
|
|
142 * @since 3.0
|
|
143 */
|
|
144 public bool fill = false;
|
|
145
|
|
146 /**
|
|
147 * center specifies whether the controls in a row should be
|
|
148 * centered vertically in each cell for horizontal layouts,
|
|
149 * or centered horizontally in each cell for vertical layouts.
|
|
150 *
|
|
151 * The default value is false.
|
|
152 *
|
|
153 * @since 3.4
|
|
154 */
|
|
155 public bool center = false;
|
|
156
|
|
157 /**
|
|
158 * justify specifies whether the controls in a row should be
|
|
159 * fully justified, with any extra space placed between the controls.
|
|
160 *
|
|
161 * The default value is false.
|
|
162 */
|
|
163 public bool justify = false;
|
|
164
|
|
165 /**
|
|
166 * marginLeft specifies the number of pixels of horizontal margin
|
|
167 * that will be placed along the left edge of the layout.
|
|
168 *
|
|
169 * The default value is 3.
|
|
170 */
|
|
171 public int marginLeft = 3;
|
|
172
|
|
173 /**
|
|
174 * marginTop specifies the number of pixels of vertical margin
|
|
175 * that will be placed along the top edge of the layout.
|
|
176 *
|
|
177 * The default value is 3.
|
|
178 */
|
|
179 public int marginTop = 3;
|
|
180
|
|
181 /**
|
|
182 * marginRight specifies the number of pixels of horizontal margin
|
|
183 * that will be placed along the right edge of the layout.
|
|
184 *
|
|
185 * The default value is 3.
|
|
186 */
|
|
187 public int marginRight = 3;
|
|
188
|
|
189 /**
|
|
190 * marginBottom specifies the number of pixels of vertical margin
|
|
191 * that will be placed along the bottom edge of the layout.
|
|
192 *
|
|
193 * The default value is 3.
|
|
194 */
|
|
195 public int marginBottom = 3;
|
|
196
|
|
197 /**
|
|
198 * Constructs a new instance of this class.
|
|
199 */
|
|
200 public this () {
|
|
201 }
|
|
202
|
|
203 /**
|
|
204 * Constructs a new instance of this class given the type.
|
|
205 *
|
|
206 * @param type the type of row layout
|
|
207 *
|
|
208 * @since 2.0
|
|
209 */
|
|
210 public this (int type) {
|
|
211 this.type = type;
|
|
212 }
|
|
213
|
|
214 override protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache_) {
|
|
215 Point extent;
|
|
216 if (type is SWT.HORIZONTAL) {
|
|
217 extent = layoutHorizontal (composite, false, (wHint !is SWT.DEFAULT) && wrap, wHint, flushCache_);
|
|
218 } else {
|
|
219 extent = layoutVertical (composite, false, (hHint !is SWT.DEFAULT) && wrap, hHint, flushCache_);
|
|
220 }
|
|
221 if (wHint !is SWT.DEFAULT) extent.x = wHint;
|
|
222 if (hHint !is SWT.DEFAULT) extent.y = hHint;
|
|
223 return extent;
|
|
224 }
|
|
225
|
|
226 Point computeSize (Control control, bool flushCache_) {
|
|
227 int wHint = SWT.DEFAULT, hHint = SWT.DEFAULT;
|
|
228 RowData data = cast(RowData) control.getLayoutData ();
|
|
229 if (data !is null) {
|
|
230 wHint = data.width;
|
|
231 hHint = data.height;
|
|
232 }
|
|
233 return control.computeSize (wHint, hHint, flushCache_);
|
|
234 }
|
|
235
|
|
236 override protected bool flushCache (Control control) {
|
|
237 return true;
|
|
238 }
|
|
239
|
|
240 String getName () {
|
|
241 String string = this.classinfo.name;
|
|
242 int index = string.lastIndexOf('.');
|
|
243 if (index is -1 ) return string;
|
|
244 return string[ index + 1 .. string.length ];
|
|
245 }
|
|
246
|
|
247 override protected void layout (Composite composite, bool flushCache_) {
|
|
248 Rectangle clientArea = composite.getClientArea ();
|
|
249 if (type is SWT.HORIZONTAL) {
|
|
250 layoutHorizontal (composite, true, wrap, clientArea.width, flushCache_);
|
|
251 } else {
|
|
252 layoutVertical (composite, true, wrap, clientArea.height, flushCache_);
|
|
253 }
|
|
254 }
|
|
255
|
|
256 Point layoutHorizontal (Composite composite, bool move, bool wrap, int width, bool flushCache_) {
|
|
257 Control [] children = composite.getChildren ();
|
|
258 int count = 0;
|
|
259 for (int i=0; i<children.length; i++) {
|
|
260 Control control = children [i];
|
|
261 RowData data = cast(RowData) control.getLayoutData ();
|
|
262 if (data is null || !data.exclude) {
|
|
263 children [count++] = children [i];
|
|
264 }
|
|
265 }
|
|
266 if (count is 0) {
|
|
267 return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom);
|
|
268 }
|
|
269 int childWidth = 0, childHeight = 0, maxHeight = 0;
|
|
270 if (!pack) {
|
|
271 for (int i=0; i<count; i++) {
|
|
272 Control child = children [i];
|
|
273 Point size = computeSize (child, flushCache_);
|
|
274 childWidth = Math.max (childWidth, size.x);
|
|
275 childHeight = Math.max (childHeight, size.y);
|
|
276 }
|
|
277 maxHeight = childHeight;
|
|
278 }
|
|
279 int clientX = 0, clientY = 0;
|
|
280 if (move) {
|
|
281 Rectangle rect = composite.getClientArea ();
|
|
282 clientX = rect.x;
|
|
283 clientY = rect.y;
|
|
284 }
|
|
285 int [] wraps = null;
|
|
286 bool wrapped = false;
|
|
287 Rectangle [] bounds = null;
|
|
288 if (move && (justify || fill || center)) {
|
|
289 bounds = new Rectangle [count];
|
|
290 wraps = new int [count];
|
|
291 }
|
|
292 int maxX = 0, x = marginLeft + marginWidth, y = marginTop + marginHeight;
|
|
293 for (int i=0; i<count; i++) {
|
|
294 Control child = children [i];
|
|
295 if (pack) {
|
|
296 Point size = computeSize (child, flushCache_);
|
|
297 childWidth = size.x;
|
|
298 childHeight = size.y;
|
|
299 }
|
|
300 if (wrap && (i !is 0) && (x + childWidth > width)) {
|
|
301 wrapped = true;
|
|
302 if (move && (justify || fill || center)) wraps [i - 1] = maxHeight;
|
|
303 x = marginLeft + marginWidth;
|
|
304 y += spacing + maxHeight;
|
|
305 if (pack) maxHeight = 0;
|
|
306 }
|
|
307 if (pack || fill || center) {
|
|
308 maxHeight = Math.max (maxHeight, childHeight);
|
|
309 }
|
|
310 if (move) {
|
|
311 int childX = x + clientX, childY = y + clientY;
|
|
312 if (justify || fill || center) {
|
|
313 bounds [i] = new Rectangle (childX, childY, childWidth, childHeight);
|
|
314 } else {
|
|
315 child.setBounds (childX, childY, childWidth, childHeight);
|
|
316 }
|
|
317 }
|
|
318 x += spacing + childWidth;
|
|
319 maxX = Math.max (maxX, x);
|
|
320 }
|
|
321 maxX = Math.max (clientX + marginLeft + marginWidth, maxX - spacing);
|
|
322 if (!wrapped) maxX += marginRight + marginWidth;
|
|
323 if (move && (justify || fill || center)) {
|
|
324 int space = 0, margin = 0;
|
|
325 if (!wrapped) {
|
|
326 space = Math.max (0, (width - maxX) / (count + 1));
|
|
327 margin = Math.max (0, ((width - maxX) % (count + 1)) / 2);
|
|
328 } else {
|
|
329 if (fill || justify || center) {
|
|
330 int last = 0;
|
|
331 if (count > 0) wraps [count - 1] = maxHeight;
|
|
332 for (int i=0; i<count; i++) {
|
|
333 if (wraps [i] !is 0) {
|
|
334 int wrapCount = i - last + 1;
|
|
335 if (justify) {
|
|
336 int wrapX = 0;
|
|
337 for (int j=last; j<=i; j++) {
|
|
338 wrapX += bounds [j].width + spacing;
|
|
339 }
|
|
340 space = Math.max (0, (width - wrapX) / (wrapCount + 1));
|
|
341 margin = Math.max (0, ((width - wrapX) % (wrapCount + 1)) / 2);
|
|
342 }
|
|
343 for (int j=last; j<=i; j++) {
|
|
344 if (justify) bounds [j].x += (space * (j - last + 1)) + margin;
|
|
345 if (fill) {
|
|
346 bounds [j].height = wraps [i];
|
|
347 } else {
|
|
348 if (center) {
|
|
349 bounds [j].y += Math.max (0, (wraps [i] - bounds [j].height) / 2);
|
|
350 }
|
|
351 }
|
|
352 }
|
|
353 last = i + 1;
|
|
354 }
|
|
355 }
|
|
356 }
|
|
357 }
|
|
358 for (int i=0; i<count; i++) {
|
|
359 if (!wrapped) {
|
|
360 if (justify) bounds [i].x += (space * (i + 1)) + margin;
|
|
361 if (fill) {
|
|
362 bounds [i].height = maxHeight;
|
|
363 } else {
|
|
364 if (center) {
|
|
365 bounds [i].y += Math.max (0, (maxHeight - bounds [i].height) / 2);
|
|
366 }
|
|
367 }
|
|
368 }
|
|
369 children [i].setBounds (bounds [i]);
|
|
370 }
|
|
371 }
|
|
372 return new Point (maxX, y + maxHeight + marginBottom + marginHeight);
|
|
373 }
|
|
374
|
|
375 Point layoutVertical (Composite composite, bool move, bool wrap, int height, bool flushCache_) {
|
|
376 Control [] children = composite.getChildren ();
|
|
377 int count = 0;
|
|
378 for (int i=0; i<children.length; i++) {
|
|
379 Control control = children [i];
|
|
380 RowData data = cast(RowData) control.getLayoutData ();
|
|
381 if (data is null || !data.exclude) {
|
|
382 children [count++] = children [i];
|
|
383 }
|
|
384 }
|
|
385 if (count is 0) {
|
|
386 return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom);
|
|
387 }
|
|
388 int childWidth = 0, childHeight = 0, maxWidth = 0;
|
|
389 if (!pack) {
|
|
390 for (int i=0; i<count; i++) {
|
|
391 Control child = children [i];
|
|
392 Point size = computeSize (child, flushCache_);
|
|
393 childWidth = Math.max (childWidth, size.x);
|
|
394 childHeight = Math.max (childHeight, size.y);
|
|
395 }
|
|
396 maxWidth = childWidth;
|
|
397 }
|
|
398 int clientX = 0, clientY = 0;
|
|
399 if (move) {
|
|
400 Rectangle rect = composite.getClientArea ();
|
|
401 clientX = rect.x;
|
|
402 clientY = rect.y;
|
|
403 }
|
|
404 int [] wraps = null;
|
|
405 bool wrapped = false;
|
|
406 Rectangle [] bounds = null;
|
|
407 if (move && (justify || fill || center)) {
|
|
408 bounds = new Rectangle [count];
|
|
409 wraps = new int [count];
|
|
410 }
|
|
411 int maxY = 0, x = marginLeft + marginWidth, y = marginTop + marginHeight;
|
|
412 for (int i=0; i<count; i++) {
|
|
413 Control child = children [i];
|
|
414 if (pack) {
|
|
415 Point size = computeSize (child, flushCache_);
|
|
416 childWidth = size.x;
|
|
417 childHeight = size.y;
|
|
418 }
|
|
419 if (wrap && (i !is 0) && (y + childHeight > height)) {
|
|
420 wrapped = true;
|
|
421 if (move && (justify || fill || center)) wraps [i - 1] = maxWidth;
|
|
422 x += spacing + maxWidth;
|
|
423 y = marginTop + marginHeight;
|
|
424 if (pack) maxWidth = 0;
|
|
425 }
|
|
426 if (pack || fill || center) {
|
|
427 maxWidth = Math.max (maxWidth, childWidth);
|
|
428 }
|
|
429 if (move) {
|
|
430 int childX = x + clientX, childY = y + clientY;
|
|
431 if (justify || fill || center) {
|
|
432 bounds [i] = new Rectangle (childX, childY, childWidth, childHeight);
|
|
433 } else {
|
|
434 child.setBounds (childX, childY, childWidth, childHeight);
|
|
435 }
|
|
436 }
|
|
437 y += spacing + childHeight;
|
|
438 maxY = Math.max (maxY, y);
|
|
439 }
|
|
440 maxY = Math.max (clientY + marginTop + marginHeight, maxY - spacing);
|
|
441 if (!wrapped) maxY += marginBottom + marginHeight;
|
|
442 if (move && (justify || fill || center)) {
|
|
443 int space = 0, margin = 0;
|
|
444 if (!wrapped) {
|
|
445 space = Math.max (0, (height - maxY) / (count + 1));
|
|
446 margin = Math.max (0, ((height - maxY) % (count + 1)) / 2);
|
|
447 } else {
|
|
448 if (fill || justify || center) {
|
|
449 int last = 0;
|
|
450 if (count > 0) wraps [count - 1] = maxWidth;
|
|
451 for (int i=0; i<count; i++) {
|
|
452 if (wraps [i] !is 0) {
|
|
453 int wrapCount = i - last + 1;
|
|
454 if (justify) {
|
|
455 int wrapY = 0;
|
|
456 for (int j=last; j<=i; j++) {
|
|
457 wrapY += bounds [j].height + spacing;
|
|
458 }
|
|
459 space = Math.max (0, (height - wrapY) / (wrapCount + 1));
|
|
460 margin = Math.max (0, ((height - wrapY) % (wrapCount + 1)) / 2);
|
|
461 }
|
|
462 for (int j=last; j<=i; j++) {
|
|
463 if (justify) bounds [j].y += (space * (j - last + 1)) + margin;
|
|
464 if (fill) {
|
|
465 bounds [j].width = wraps [i];
|
|
466 } else {
|
|
467 if (center) {
|
|
468 bounds [j].x += Math.max (0, (wraps [i] - bounds [j].width) / 2);
|
|
469 }
|
|
470 }
|
|
471 }
|
|
472 last = i + 1;
|
|
473 }
|
|
474 }
|
|
475 }
|
|
476 }
|
|
477 for (int i=0; i<count; i++) {
|
|
478 if (!wrapped) {
|
|
479 if (justify) bounds [i].y += (space * (i + 1)) + margin;
|
|
480 if (fill) {
|
|
481 bounds [i].width = maxWidth;
|
|
482 } else {
|
|
483 if (center) {
|
|
484 bounds [i].x += Math.max (0, (maxWidth - bounds [i].width) / 2);
|
|
485 }
|
|
486 }
|
|
487
|
|
488 }
|
|
489 children [i].setBounds (bounds [i]);
|
|
490 }
|
|
491 }
|
|
492 return new Point (x + maxWidth + marginRight + marginWidth, maxY);
|
|
493 }
|
|
494
|
|
495 /**
|
|
496 * Returns a string containing a concise, human-readable
|
|
497 * description of the receiver.
|
|
498 *
|
|
499 * @return a string representation of the layout
|
|
500 */
|
|
501 override public String toString () {
|
|
502 String string = getName ()~" {";
|
|
503 string ~= "type="~((type !is SWT.HORIZONTAL) ? "SWT.VERTICAL" : "SWT.HORIZONTAL")~" ";
|
|
504 if (marginWidth !is 0) string ~= "marginWidth="~to!(String)(marginWidth)~" ";
|
|
505 if (marginHeight !is 0) string ~= "marginHeight="~to!(String)(marginHeight)~" ";
|
|
506 if (marginLeft !is 0) string ~= "marginLeft="~to!(String)(marginLeft)~" ";
|
|
507 if (marginTop !is 0) string ~= "marginTop="~to!(String)(marginTop)~" ";
|
|
508 if (marginRight !is 0) string ~= "marginRight="~to!(String)(marginRight)~" ";
|
|
509 if (marginBottom !is 0) string ~= "marginBottom="~to!(String)(marginBottom)~" ";
|
|
510 if (spacing !is 0) string ~= "spacing="~to!(String)(spacing)~" ";
|
|
511 string ~= "wrap="~to!(String)(wrap)~" ";
|
|
512 string ~= "pack="~to!(String)(pack)~" ";
|
|
513 string ~= "fill="~to!(String)(fill)~" ";
|
|
514 string ~= "justify="~to!(String)(justify)~" ";
|
|
515 string = string.trim();
|
|
516 string ~= "}";
|
|
517 return string;
|
|
518 }
|
|
519 }
|