comparison dwt/custom/SashForm.d @ 41:6337764516f1

Sync dwt/custom with dwt-linux (took copy of complete folder)
author Frank Benoit <benoit@tionex.de>
date Tue, 07 Oct 2008 16:29:55 +0200
parents a9ab4c738ed8
children
comparison
equal deleted inserted replaced
40:fbe68c33eeee 41:6337764516f1
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 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.custom; 13 module dwt.custom.SashForm;
12 14
13 15
14 import dwt.*; 16
15 import dwt.widgets.*; 17 import dwt.DWT;
16 import dwt.graphics.*; 18 import dwt.DWTException;
19 import dwt.graphics.Color;
20 import dwt.graphics.Rectangle;
21 import dwt.widgets.Composite;
22 import dwt.widgets.Control;
23 import dwt.widgets.Event;
24 import dwt.widgets.Layout;
25 import dwt.widgets.Listener;
26 import dwt.widgets.Sash;
27 import dwt.custom.SashFormLayout;
28 import dwt.custom.SashFormData;
29 import dwt.dwthelper.utils;
17 30
18 /** 31 /**
19 * The SashForm is a composite control that lays out its children in a 32 * The SashForm is a composite control that lays out its children in a
20 * row or column arrangement (as specified by the orientation) and places 33 * row or column arrangement (as specified by the orientation) and places
21 * a Sash between each child. One child may be maximized to occupy the 34 * a Sash between each child. One child may be maximized to occupy the
25 * <dl> 38 * <dl>
26 * <dt><b>Styles:</b></dt> 39 * <dt><b>Styles:</b></dt>
27 * <dd>HORIZONTAL, VERTICAL, SMOOTH</dd> 40 * <dd>HORIZONTAL, VERTICAL, SMOOTH</dd>
28 * </dl> 41 * </dl>
29 * </p> 42 * </p>
43 *
44 * @see <a href="http://www.eclipse.org/swt/snippets/#sashform">SashForm snippets</a>
45 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: CustomControlExample</a>
46 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
30 */ 47 */
31 public class SashForm : Composite { 48 public class SashForm : Composite {
32 49
50 /**
51 * The width of all sashes in the form.
52 */
33 public int SASH_WIDTH = 3; 53 public int SASH_WIDTH = 3;
34 54
35 int sashStyle; 55 int sashStyle;
36 Sash[] sashes = new Sash[0]; 56 Sash[] sashes;
37 // Remember background and foreground 57 // Remember background and foreground
38 // colors to determine whether to set 58 // colors to determine whether to set
39 // sashes to the default color (null) or 59 // sashes to the default color (null) or
40 // a specific color 60 // a specific color
41 Color background = null; 61 Color background = null;
42 Color foreground = null; 62 Color foreground = null;
43 Control[] controls = new Control[0]; 63 Control[] controls;
44 Control maxControl = null; 64 Control maxControl = null;
45 Listener sashListener; 65 Listener sashListener;
46 static final int DRAG_MINIMUM = 20; 66 static const int DRAG_MINIMUM = 20;
47 67
48 /** 68 /**
49 * Constructs a new instance of this class given its parent 69 * Constructs a new instance of this class given its parent
50 * and a style value describing its behavior and appearance. 70 * and a style value describing its behavior and appearance.
51 * <p> 71 * <p>
52 * The style value is either one of the style constants defined in 72 * The style value is either one of the style constants defined in
53 * class <code>DWT</code> which is applicable to instances of this 73 * class <code>DWT</code> which is applicable to instances of this
54 * class, or must be built by <em>bitwise OR</em>'ing together 74 * class, or must be built by <em>bitwise OR</em>'ing together
55 * (that is, using the <code>int</code> "|" operator) two or more 75 * (that is, using the <code>int</code> "|" operator) two or more
56 * of those <code>DWT</code> style constants. The class description 76 * of those <code>DWT</code> style constants. The class description
57 * lists the style constants that are applicable to the class. 77 * lists the style constants that are applicable to the class.
58 * Style bits are also inherited from superclasses. 78 * Style bits are also inherited from superclasses.
59 * </p> 79 * </p>
76 super(parent, checkStyle(style)); 96 super(parent, checkStyle(style));
77 super.setLayout(new SashFormLayout()); 97 super.setLayout(new SashFormLayout());
78 sashStyle = ((style & DWT.VERTICAL) !is 0) ? DWT.HORIZONTAL : DWT.VERTICAL; 98 sashStyle = ((style & DWT.VERTICAL) !is 0) ? DWT.HORIZONTAL : DWT.VERTICAL;
79 if ((style & DWT.BORDER) !is 0) sashStyle |= DWT.BORDER; 99 if ((style & DWT.BORDER) !is 0) sashStyle |= DWT.BORDER;
80 if ((style & DWT.SMOOTH) !is 0) sashStyle |= DWT.SMOOTH; 100 if ((style & DWT.SMOOTH) !is 0) sashStyle |= DWT.SMOOTH;
81 sashListener = new Listener() { 101 sashListener = new class() Listener {
82 public void handleEvent(Event e) { 102 public void handleEvent(Event e) {
83 onDragSash(e); 103 onDragSash(e);
84 } 104 }
85 }; 105 };
86 } 106 }
89 return style & mask; 109 return style & mask;
90 } 110 }
91 /** 111 /**
92 * Returns DWT.HORIZONTAL if the controls in the SashForm are laid out side by side 112 * Returns DWT.HORIZONTAL if the controls in the SashForm are laid out side by side
93 * or DWT.VERTICAL if the controls in the SashForm are laid out top to bottom. 113 * or DWT.VERTICAL if the controls in the SashForm are laid out top to bottom.
94 * 114 *
95 * @return DWT.HORIZONTAL or DWT.VERTICAL 115 * @return DWT.HORIZONTAL or DWT.VERTICAL
96 */ 116 */
97 public int getOrientation() { 117 public int getOrientation() {
98 //checkWidget(); 118 //checkWidget();
99 return (sashStyle & DWT.VERTICAL) !is 0 ? DWT.HORIZONTAL : DWT.VERTICAL; 119 return (sashStyle & DWT.VERTICAL) !is 0 ? DWT.HORIZONTAL : DWT.VERTICAL;
113 */ 133 */
114 public int getSashWidth() { 134 public int getSashWidth() {
115 checkWidget(); 135 checkWidget();
116 return SASH_WIDTH; 136 return SASH_WIDTH;
117 } 137 }
118 public int getStyle() { 138 public override int getStyle() {
119 int style = super.getStyle(); 139 int style = super.getStyle();
120 style |= getOrientation() is DWT.VERTICAL ? DWT.VERTICAL : DWT.HORIZONTAL; 140 style |= getOrientation() is DWT.VERTICAL ? DWT.VERTICAL : DWT.HORIZONTAL;
121 if ((sashStyle & DWT.SMOOTH) !is 0) style |= DWT.SMOOTH; 141 if ((sashStyle & DWT.SMOOTH) !is 0) style |= DWT.SMOOTH;
122 return style; 142 return style;
123 } 143 }
124 /** 144 /**
125 * Answer the control that currently is maximized in the SashForm. 145 * Answer the control that currently is maximized in the SashForm.
126 * This value may be null. 146 * This value may be null.
127 * 147 *
128 * @return the control that currently is maximized or null 148 * @return the control that currently is maximized or null
129 */ 149 */
130 public Control getMaximizedControl(){ 150 public Control getMaximizedControl(){
131 //checkWidget(); 151 //checkWidget();
132 return this.maxControl; 152 return this.maxControl;
133 } 153 }
134 /** 154 /**
135 * Answer the relative weight of each child in the SashForm. The weight represents the 155 * Answer the relative weight of each child in the SashForm. The weight represents the
136 * percent of the total width (if SashForm has Horizontal orientation) or 156 * percent of the total width (if SashForm has Horizontal orientation) or
137 * total height (if SashForm has Vertical orientation) each control occupies. 157 * total height (if SashForm has Vertical orientation) each control occupies.
138 * The weights are returned in order of the creation of the widgets (weight[0] 158 * The weights are returned in order of the creation of the widgets (weight[0]
139 * corresponds to the weight of the first child created). 159 * corresponds to the weight of the first child created).
140 * 160 *
141 * @return the relative weight of each child 161 * @return the relative weight of each child
142 * 162 *
143 * @exception DWTException <ul> 163 * @exception DWTException <ul>
144 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 164 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
145 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 165 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
146 * </ul> 166 * </ul>
147 */ 167 */
150 checkWidget(); 170 checkWidget();
151 Control[] cArray = getControls(false); 171 Control[] cArray = getControls(false);
152 int[] ratios = new int[cArray.length]; 172 int[] ratios = new int[cArray.length];
153 for (int i = 0; i < cArray.length; i++) { 173 for (int i = 0; i < cArray.length; i++) {
154 Object data = cArray[i].getLayoutData(); 174 Object data = cArray[i].getLayoutData();
155 if (data !is null && null !is cast(SashFormData)data ) { 175 if ( auto sfd = cast(SashFormData)data ) {
156 ratios[i] = cast(int)((cast(SashFormData)data).weight * 1000 >> 16); 176 ratios[i] = cast(int)(sfd.weight * 1000 >> 16);
157 } else { 177 } else {
158 ratios[i] = 200; 178 ratios[i] = 200;
159 } 179 }
160 } 180 }
161 return ratios; 181 return ratios;
162 } 182 }
163 Control[] getControls(bool onlyVisible) { 183 Control[] getControls(bool onlyVisible) {
164 Control[] children = getChildren(); 184 Control[] children = getChildren();
165 Control[] result = new Control[0]; 185 Control[] result = new Control[0];
166 for (int i = 0; i < children.length; i++) { 186 for (int i = 0; i < children.length; i++) {
167 if (children[i] instanceof Sash) continue; 187 if ( null !is cast(Sash)children[i]) continue;
168 if (onlyVisible && !children[i].getVisible()) continue; 188 if (onlyVisible && !children[i].getVisible()) continue;
169 189
170 Control[] newResult = new Control[result.length + 1]; 190 Control[] newResult = new Control[result.length + 1];
171 System.arraycopy(result, 0, newResult, 0, result.length); 191 System.arraycopy(result, 0, newResult, 0, result.length);
172 newResult[result.length] = children[i]; 192 newResult[result.length] = children[i];
187 207
188 Control c1 = controls[sashIndex]; 208 Control c1 = controls[sashIndex];
189 Control c2 = controls[sashIndex + 1]; 209 Control c2 = controls[sashIndex + 1];
190 Rectangle b1 = c1.getBounds(); 210 Rectangle b1 = c1.getBounds();
191 Rectangle b2 = c2.getBounds(); 211 Rectangle b2 = c2.getBounds();
192 212
193 Rectangle sashBounds = sash.getBounds(); 213 Rectangle sashBounds = sash.getBounds();
194 Rectangle area = getClientArea(); 214 Rectangle area = getClientArea();
195 bool correction = false; 215 bool correction = false;
196 if (getOrientation() is DWT.HORIZONTAL) { 216 if (getOrientation() is DWT.HORIZONTAL) {
197 correction = b1.width < DRAG_MINIMUM || b2.width < DRAG_MINIMUM; 217 correction = b1.width < DRAG_MINIMUM || b2.width < DRAG_MINIMUM;
198 int totalWidth = b2.x + b2.width - b1.x; 218 int totalWidth = b2.x + b2.width - b1.x;
199 int shift = event.x - sashBounds.x; 219 int shift = event.x - sashBounds.x;
200 b1.width += shift; 220 b1.width += shift;
201 b2.x += shift; 221 b2.x += shift;
202 b2.width -= shift; 222 b2.width -= shift;
203 if (b1.width < DRAG_MINIMUM) { 223 if (b1.width < DRAG_MINIMUM) {
251 if (data1 is null || !( null !is cast(SashFormData)data1 )) { 271 if (data1 is null || !( null !is cast(SashFormData)data1 )) {
252 data1 = new SashFormData(); 272 data1 = new SashFormData();
253 c1.setLayoutData(data1); 273 c1.setLayoutData(data1);
254 } 274 }
255 Object data2 = c2.getLayoutData(); 275 Object data2 = c2.getLayoutData();
256 if (data2 is null || !( null !is cast(SashFormData)data2 )) { 276 if (data2 is null || !(null !is cast(SashFormData)data2 )) {
257 data2 = new SashFormData(); 277 data2 = new SashFormData();
258 c2.setLayoutData(data2); 278 c2.setLayoutData(data2);
259 } 279 }
260 (cast(SashFormData)data1).weight = ((cast(long)b1.height << 16) + area.height - 1) / area.height; 280 (cast(SashFormData)data1).weight = ((cast(long)b1.height << 16) + area.height - 1) / area.height;
261 (cast(SashFormData)data2).weight = ((cast(long)b2.height << 16) + area.height - 1) / area.height; 281 (cast(SashFormData)data2).weight = ((cast(long)b2.height << 16) + area.height - 1) / area.height;
265 sash.setBounds(event.x, event.y, event.width, event.height); 285 sash.setBounds(event.x, event.y, event.width, event.height);
266 c2.setBounds(b2); 286 c2.setBounds(b2);
267 } 287 }
268 } 288 }
269 /** 289 /**
270 * If orientation is DWT.HORIZONTAL, lay the controls in the SashForm 290 * If orientation is DWT.HORIZONTAL, lay the controls in the SashForm
271 * out side by side. If orientation is DWT.VERTICAL, lay the 291 * out side by side. If orientation is DWT.VERTICAL, lay the
272 * controls in the SashForm out top to bottom. 292 * controls in the SashForm out top to bottom.
273 * 293 *
274 * @param orientation DWT.HORIZONTAL or DWT.VERTICAL 294 * @param orientation DWT.HORIZONTAL or DWT.VERTICAL
275 * 295 *
276 * @exception DWTException <ul> 296 * @exception DWTException <ul>
277 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 297 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
278 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 298 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
279 * <li>ERROR_INVALID_ARGUMENT - if the value of orientation is not DWT.HORIZONTAL or DWT.VERTICAL 299 * <li>ERROR_INVALID_ARGUMENT - if the value of orientation is not DWT.HORIZONTAL or DWT.VERTICAL
280 * </ul> 300 * </ul>
294 sashes[i].setForeground(foreground); 314 sashes[i].setForeground(foreground);
295 sashes[i].addListener(DWT.Selection, sashListener); 315 sashes[i].addListener(DWT.Selection, sashListener);
296 } 316 }
297 layout(false); 317 layout(false);
298 } 318 }
299 public void setBackground (Color color) { 319 public override void setBackground (Color color) {
300 super.setBackground(color); 320 super.setBackground(color);
301 background = color; 321 background = color;
302 for (int i = 0; i < sashes.length; i++) { 322 for (int i = 0; i < sashes.length; i++) {
303 sashes[i].setBackground(background); 323 sashes[i].setBackground(background);
304 } 324 }
305 } 325 }
306 public void setForeground (Color color) { 326 public override void setForeground (Color color) {
307 super.setForeground(color); 327 super.setForeground(color);
308 foreground = color; 328 foreground = color;
309 for (int i = 0; i < sashes.length; i++) { 329 for (int i = 0; i < sashes.length; i++) {
310 sashes[i].setForeground(foreground); 330 sashes[i].setForeground(foreground);
311 } 331 }
323 * @exception DWTException <ul> 343 * @exception DWTException <ul>
324 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 344 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
325 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 345 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
326 * </ul> 346 * </ul>
327 */ 347 */
328 public void setLayout (Layout layout) { 348 public override void setLayout (Layout layout) {
329 checkWidget(); 349 checkWidget();
330 return; 350 return;
331 } 351 }
332 /** 352 /**
333 * Specify the control that should take up the entire client area of the SashForm. 353 * Specify the control that should take up the entire client area of the SashForm.
334 * If one control has been maximized, and this method is called with a different control, 354 * If one control has been maximized, and this method is called with a different control,
335 * the previous control will be minimized and the new control will be maximized. 355 * the previous control will be minimized and the new control will be maximized.
336 * If the value of control is null, the SashForm will minimize all controls and return to 356 * If the value of control is null, the SashForm will minimize all controls and return to
337 * the default layout where all controls are laid out separated by sashes. 357 * the default layout where all controls are laid out separated by sashes.
338 * 358 *
339 * @param control the control to be maximized or null 359 * @param control the control to be maximized or null
340 * 360 *
341 * @exception DWTException <ul> 361 * @exception DWTException <ul>
342 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 362 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
343 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 363 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
344 * </ul> 364 * </ul>
345 */ 365 */
353 sashes[i].setVisible(true); 373 sashes[i].setVisible(true);
354 } 374 }
355 } 375 }
356 return; 376 return;
357 } 377 }
358 378
359 for (int i= 0; i < sashes.length; i++){ 379 for (int i= 0; i < sashes.length; i++){
360 sashes[i].setVisible(false); 380 sashes[i].setVisible(false);
361 } 381 }
362 maxControl = control; 382 maxControl = control;
363 layout(false); 383 layout(false);
382 SASH_WIDTH = width; 402 SASH_WIDTH = width;
383 layout(false); 403 layout(false);
384 } 404 }
385 /** 405 /**
386 * Specify the relative weight of each child in the SashForm. This will determine 406 * Specify the relative weight of each child in the SashForm. This will determine
387 * what percent of the total width (if SashForm has Horizontal orientation) or 407 * what percent of the total width (if SashForm has Horizontal orientation) or
388 * total height (if SashForm has Vertical orientation) each control will occupy. 408 * total height (if SashForm has Vertical orientation) each control will occupy.
389 * The weights must be positive values and there must be an entry for each 409 * The weights must be positive values and there must be an entry for each
390 * non-sash child of the SashForm. 410 * non-sash child of the SashForm.
391 * 411 *
392 * @param weights the relative weight of each child 412 * @param weights the relative weight of each child
393 * 413 *
394 * @exception DWTException <ul> 414 * @exception DWTException <ul>
395 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 415 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
396 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> 416 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
397 * <li>ERROR_INVALID_ARGUMENT - if the weights value is null or of incorrect length (must match the number of children)</li> 417 * <li>ERROR_INVALID_ARGUMENT - if the weights value is null or of incorrect length (must match the number of children)</li>
398 * </ul> 418 * </ul>
401 checkWidget(); 421 checkWidget();
402 Control[] cArray = getControls(false); 422 Control[] cArray = getControls(false);
403 if (weights is null || weights.length !is cArray.length) { 423 if (weights is null || weights.length !is cArray.length) {
404 DWT.error(DWT.ERROR_INVALID_ARGUMENT); 424 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
405 } 425 }
406 426
407 int total = 0; 427 int total = 0;
408 for (int i = 0; i < weights.length; i++) { 428 for (int i = 0; i < weights.length; i++) {
409 if (weights[i] < 0) { 429 if (weights[i] < 0) {
410 DWT.error(DWT.ERROR_INVALID_ARGUMENT); 430 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
411 } 431 }