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

Sync layout with dwt-linux
author Frank Benoit <benoit@tionex.de>
date Tue, 07 Oct 2008 14:41:31 +0200
parents a9ab4c738ed8
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.FillLayout; 13 module dwt.layout.FillLayout;
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.widgets.Scrollable; 21 import dwt.widgets.Scrollable;
22 22 import dwt.layout.FillData;
23 /** 23
24 * <code>FillLayout</code> is the simplest layout class. It lays out 24 import tango.util.Convert;
25 * controls in a single row or column, forcing them to be the same size. 25 import dwt.dwthelper.utils;
26
27 /**
28 * <code>FillLayout</code> is the simplest layout class. It lays out
29 * controls in a single row or column, forcing them to be the same size.
26 * <p> 30 * <p>
27 * Initially, the controls will all be as tall as the tallest control, 31 * Initially, the controls will all be as tall as the tallest control,
28 * and as wide as the widest. <code>FillLayout</code> does not wrap, 32 * and as wide as the widest. <code>FillLayout</code> does not wrap,
29 * but you can specify margins and spacing. You might use it to 33 * but you can specify margins and spacing. You might use it to
30 * lay out buttons in a task bar or tool bar, or to stack checkboxes 34 * lay out buttons in a task bar or tool bar, or to stack checkboxes
31 * in a <code>Group</code>. <code>FillLayout</code> can also be used 35 * in a <code>Group</code>. <code>FillLayout</code> can also be used
32 * when a <code>Composite</code> only has one child. For example, 36 * when a <code>Composite</code> only has one child. For example,
33 * if a <code>Shell</code> has a single <code>Group</code> child, 37 * if a <code>Shell</code> has a single <code>Group</code> child,
34 * <code>FillLayout</code> will cause the <code>Group</code> to 38 * <code>FillLayout</code> will cause the <code>Group</code> to
35 * completely fill the <code>Shell</code> (if margins are 0). 39 * completely fill the <code>Shell</code> (if margins are 0).
36 * </p> 40 * </p>
37 * <p> 41 * <p>
38 * Example code: first a <code>FillLayout</code> is created and 42 * Example code: first a <code>FillLayout</code> is created and
39 * its type field is set, and then the layout is set into the 43 * its type field is set, and then the layout is set into the
40 * <code>Composite</code>. Note that in a <code>FillLayout</code>, 44 * <code>Composite</code>. Note that in a <code>FillLayout</code>,
41 * children are always the same size, and they fill all available space. 45 * children are always the same size, and they fill all available space.
42 * <pre> 46 * <pre>
43 * FillLayout fillLayout = new FillLayout(); 47 * FillLayout fillLayout = new FillLayout();
44 * fillLayout.type = DWT.VERTICAL; 48 * fillLayout.type = DWT.VERTICAL;
45 * shell.setLayout(fillLayout); 49 * shell.setLayout(fillLayout);
46 * </pre> 50 * </pre>
47 * </p> 51 * </p>
52 *
53 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: LayoutExample</a>
54 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
48 */ 55 */
49 public final class FillLayout : Layout { 56 public final class FillLayout : Layout {
50 /** 57 /**
51 * type specifies how controls will be positioned 58 * type specifies how controls will be positioned
52 * within the layout. 59 * within the layout.
53 * 60 *
54 * The default value is HORIZONTAL. 61 * The default value is HORIZONTAL.
55 * 62 *
56 * Possible values are: <ul> 63 * Possible values are: <ul>
57 * <li>HORIZONTAL: Position the controls horizontally from left to right</li> 64 * <li>HORIZONTAL: Position the controls horizontally from left to right</li>
58 * <li>VERTICAL: Position the controls vertically from top to bottom</li> 65 * <li>VERTICAL: Position the controls vertically from top to bottom</li>
59 * </ul> 66 * </ul>
60 */ 67 */
61 public int type = DWT.HORIZONTAL; 68 public int type = DWT.HORIZONTAL;
62 69
63 /** 70 /**
64 * marginWidth specifies the number of pixels of horizontal margin 71 * marginWidth specifies the number of pixels of horizontal margin
65 * that will be placed along the left and right edges of the layout. 72 * that will be placed along the left and right edges of the layout.
66 * 73 *
67 * The default value is 0. 74 * The default value is 0.
68 * 75 *
69 * @since 3.0 76 * @since 3.0
70 */ 77 */
71 public int marginWidth = 0; 78 public int marginWidth = 0;
72 79
73 /** 80 /**
74 * marginHeight specifies the number of pixels of vertical margin 81 * marginHeight specifies the number of pixels of vertical margin
75 * that will be placed along the top and bottom edges of the layout. 82 * that will be placed along the top and bottom edges of the layout.
76 * 83 *
77 * The default value is 0. 84 * The default value is 0.
78 * 85 *
79 * @since 3.0 86 * @since 3.0
80 */ 87 */
81 public int marginHeight = 0; 88 public int marginHeight = 0;
82 89
83 /** 90 /**
84 * spacing specifies the number of pixels between the edge of one cell 91 * spacing specifies the number of pixels between the edge of one cell
85 * and the edge of its neighbouring cell. 92 * and the edge of its neighbouring cell.
86 * 93 *
87 * The default value is 0. 94 * The default value is 0.
88 * 95 *
89 * @since 3.0 96 * @since 3.0
90 */ 97 */
91 public int spacing = 0; 98 public int spacing = 0;
92 99
93 /** 100 /**
94 * Constructs a new instance of this class. 101 * Constructs a new instance of this class.
95 */ 102 */
96 public this () { 103 public this () {
97 } 104 }
98 105
99 /** 106 /**
100 * Constructs a new instance of this class given the type. 107 * Constructs a new instance of this class given the type.
101 * 108 *
102 * @param type the type of fill layout 109 * @param type the type of fill layout
103 * 110 *
104 * @since 2.0 111 * @since 2.0
105 */ 112 */
106 public this (int type) { 113 public this (int type) {
107 this.type = type; 114 this.type = type;
108 } 115 }
109 116
110 protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache) { 117 override protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache) {
111 Control [] children = composite.getChildren (); 118 Control [] children = composite.getChildren ();
112 int count = children.length; 119 int count = children.length;
113 int maxWidth = 0, maxHeight = 0; 120 int maxWidth = 0, maxHeight = 0;
114 for (int i=0; i<count; i++) { 121 for (int i=0; i<count; i++) {
115 Control child = children [i]; 122 Control child = children [i];
153 if (wHint is DWT.DEFAULT && hHint is DWT.DEFAULT) { 160 if (wHint is DWT.DEFAULT && hHint is DWT.DEFAULT) {
154 size = data.computeSize (control, wHint, hHint, flushCache); 161 size = data.computeSize (control, wHint, hHint, flushCache);
155 } else { 162 } else {
156 // TEMPORARY CODE 163 // TEMPORARY CODE
157 int trimX, trimY; 164 int trimX, trimY;
158 if ( null !is cast(Scrollable)control ) { 165 if ( auto sa = cast(Scrollable)control ) {
159 Rectangle rect = (cast(Scrollable) control).computeTrim (0, 0, 0, 0); 166 Rectangle rect = sa.computeTrim (0, 0, 0, 0);
160 trimX = rect.width; 167 trimX = rect.width;
161 trimY = rect.height; 168 trimY = rect.height;
162 } else { 169 } else {
163 trimX = trimY = control.getBorderWidth () * 2; 170 trimX = trimY = control.getBorderWidth () * 2;
164 } 171 }
167 size = data.computeSize (control, w, h, flushCache); 174 size = data.computeSize (control, w, h, flushCache);
168 } 175 }
169 return size; 176 return size;
170 } 177 }
171 178
172 protected bool flushCache (Control control) { 179 override protected bool flushCache (Control control) {
173 Object data = control.getLayoutData(); 180 Object data = control.getLayoutData();
174 if (data !is null) (cast(FillData)data).flushCache(); 181 if (data !is null) (cast(FillData)data).flushCache();
175 return true; 182 return true;
176 } 183 }
177 184
178 String getName () { 185 String getName () {
179 String string = getClass ().getName (); 186 String string = this.classinfo.name;
180 int index = string.lastIndexOf ('.'); 187 int index = string.lastIndexOf( '.');
181 if (index is -1) return string; 188 if (index is -1 ) return string;
182 return string.substring (index + 1, string.length ()); 189 return string[ index + 1 .. string.length ];
183 } 190 }
184 191
185 protected void layout (Composite composite, bool flushCache) { 192 override protected void layout (Composite composite, bool flushCache) {
186 Rectangle rect = composite.getClientArea (); 193 Rectangle rect = composite.getClientArea ();
187 Control [] children = composite.getChildren (); 194 Control [] children = composite.getChildren ();
188 int count = children.length; 195 int count = children.length;
189 if (count is 0) return; 196 if (count is 0) return;
190 int width = rect.width - marginWidth * 2; 197 int width = rect.width - marginWidth * 2;
226 * Returns a string containing a concise, human-readable 233 * Returns a string containing a concise, human-readable
227 * description of the receiver. 234 * description of the receiver.
228 * 235 *
229 * @return a string representation of the layout 236 * @return a string representation of the layout
230 */ 237 */
231 public String toString () { 238 override public String toString () {
232 String string = getName ()+" {"; 239 String string = getName () ~ " {";
233 string += "type="+((type is DWT.VERTICAL) ? "DWT.VERTICAL" : "DWT.HORIZONTAL")+" "; 240 string ~= "type="~((type is DWT.VERTICAL) ? "DWT.VERTICAL" : "DWT.HORIZONTAL")~" ";
234 if (marginWidth !is 0) string += "marginWidth="+marginWidth+" "; 241 if (marginWidth !is 0) string ~= "marginWidth="~to!(String)(marginWidth)~" ";
235 if (marginHeight !is 0) string += "marginHeight="+marginHeight+" "; 242 if (marginHeight !is 0) string ~= "marginHeight="~to!(String)(marginHeight)~" ";
236 if (spacing !is 0) string += "spacing="+spacing+" "; 243 if (spacing !is 0) string ~= "spacing="~to!(String)(spacing)~" ";
237 string = string.trim(); 244 string = string.trim();
238 string += "}"; 245 string ~= "}";
239 return string; 246 return string;
240 } 247 }
241 } 248 }