Mercurial > projects > dwt-win
annotate dwt/custom/ViewForm.d @ 212:ab60f3309436
reverted the char[] to String and use the an alias.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 05 May 2008 00:12:38 +0200 |
parents | a5afe31f5cdd |
children | fd9c62a2998e |
rev | line source |
---|---|
155 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 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.custom.ViewForm; | |
14 | |
15 | |
16 | |
17 import dwt.DWT; | |
18 import dwt.DWTException; | |
19 import dwt.graphics.Color; | |
20 import dwt.graphics.GC; | |
21 import dwt.graphics.Point; | |
22 import dwt.graphics.RGB; | |
23 import dwt.graphics.Rectangle; | |
24 import dwt.widgets.Composite; | |
25 import dwt.widgets.Control; | |
26 import dwt.widgets.Event; | |
27 import dwt.widgets.Layout; | |
28 import dwt.widgets.Listener; | |
29 import dwt.custom.ViewFormLayout; | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
30 import dwt.dwthelper.utils; |
155 | 31 |
32 /** | |
33 * Instances of this class implement a Composite that positions and sizes | |
34 * children and allows programmatic control of layout and border parameters. | |
35 * ViewForm is used in the workbench to lay out a view's label/menu/toolbar | |
36 * local bar. | |
37 * <p> | |
38 * Note that although this class is a subclass of <code>Composite</code>, | |
39 * it does not make sense to set a layout on it. | |
40 * </p><p> | |
41 * <dl> | |
42 * <dt><b>Styles:</b></dt> | |
43 * <dd>BORDER, FLAT</dd> | |
44 * <dt><b>Events:</b></dt> | |
45 * <dd>(None)</dd> | |
46 * </dl> | |
47 * <p> | |
48 * IMPORTANT: This class is <em>not</em> intended to be subclassed. | |
49 * </p> | |
50 */ | |
51 | |
52 public class ViewForm : Composite { | |
53 | |
54 /** | |
55 * marginWidth specifies the number of pixels of horizontal margin | |
56 * that will be placed along the left and right edges of the form. | |
57 * | |
58 * The default value is 0. | |
59 */ | |
60 public int marginWidth = 0; | |
61 /** | |
62 * marginHeight specifies the number of pixels of vertical margin | |
63 * that will be placed along the top and bottom edges of the form. | |
64 * | |
65 * The default value is 0. | |
66 */ | |
67 public int marginHeight = 0; | |
68 /** | |
69 * horizontalSpacing specifies the number of pixels between the right | |
70 * edge of one cell and the left edge of its neighbouring cell to | |
71 * the right. | |
72 * | |
73 * The default value is 1. | |
74 */ | |
75 public int horizontalSpacing = 1; | |
76 /** | |
77 * verticalSpacing specifies the number of pixels between the bottom | |
78 * edge of one cell and the top edge of its neighbouring cell underneath. | |
79 * | |
80 * The default value is 1. | |
81 */ | |
82 public int verticalSpacing = 1; | |
83 | |
84 /** | |
85 * Color of innermost line of drop shadow border. | |
86 * | |
87 * NOTE This field is badly named and can not be fixed for backwards compatibility. | |
88 * It should be capitalized. | |
89 * | |
90 * @deprecated | |
91 */ | |
92 public static RGB borderInsideRGB; | |
93 /** | |
94 * Color of middle line of drop shadow border. | |
95 * | |
96 * NOTE This field is badly named and can not be fixed for backwards compatibility. | |
97 * It should be capitalized. | |
98 * | |
99 * @deprecated | |
100 */ | |
101 public static RGB borderMiddleRGB; | |
102 /** | |
103 * Color of outermost line of drop shadow border. | |
104 * | |
105 * NOTE This field is badly named and can not be fixed for backwards compatibility. | |
106 * It should be capitalized. | |
107 * | |
108 * @deprecated | |
109 */ | |
110 public static RGB borderOutsideRGB; | |
111 | |
112 // DWT widgets | |
113 Control topLeft; | |
114 Control topCenter; | |
115 Control topRight; | |
116 Control content; | |
117 | |
118 // Configuration and state info | |
119 bool separateTopCenter = false; | |
120 bool showBorder = false; | |
121 | |
122 int separator = -1; | |
123 int borderTop = 0; | |
124 int borderBottom = 0; | |
125 int borderLeft = 0; | |
126 int borderRight = 0; | |
127 int highlight = 0; | |
128 Point oldSize; | |
129 | |
130 Color selectionBackground; | |
131 | |
132 static final int OFFSCREEN = -200; | |
133 static final int BORDER1_COLOR = DWT.COLOR_WIDGET_NORMAL_SHADOW; | |
134 static final int SELECTION_BACKGROUND = DWT.COLOR_LIST_BACKGROUND; | |
135 | |
136 | |
137 static this(){ | |
138 borderInsideRGB = new RGB (132, 130, 132); | |
139 borderMiddleRGB = new RGB (143, 141, 138); | |
140 borderOutsideRGB = new RGB (171, 168, 165); | |
141 } | |
142 /** | |
143 * Constructs a new instance of this class given its parent | |
144 * and a style value describing its behavior and appearance. | |
145 * <p> | |
146 * The style value is either one of the style constants defined in | |
147 * class <code>DWT</code> which is applicable to instances of this | |
148 * class, or must be built by <em>bitwise OR</em>'ing together | |
149 * (that is, using the <code>int</code> "|" operator) two or more | |
150 * of those <code>DWT</code> style constants. The class description | |
151 * lists the style constants that are applicable to the class. | |
152 * Style bits are also inherited from superclasses. | |
153 * </p> | |
154 * | |
155 * @param parent a widget which will be the parent of the new instance (cannot be null) | |
156 * @param style the style of widget to construct | |
157 * | |
158 * @exception IllegalArgumentException <ul> | |
159 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
160 * </ul> | |
161 * @exception DWTException <ul> | |
162 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
163 * </ul> | |
164 * | |
165 * @see DWT#BORDER | |
166 * @see DWT#FLAT | |
167 * @see #getStyle() | |
168 */ | |
169 public this(Composite parent, int style) { | |
170 super(parent, checkStyle(style)); | |
171 super.setLayout(new ViewFormLayout()); | |
172 | |
173 setBorderVisible((style & DWT.BORDER) !is 0); | |
174 | |
175 Listener listener = new class() Listener { | |
176 public void handleEvent(Event e) { | |
177 switch (e.type) { | |
178 case DWT.Dispose: onDispose(); break; | |
179 case DWT.Paint: onPaint(e.gc); break; | |
180 case DWT.Resize: onResize(); break; | |
181 default: | |
182 } | |
183 } | |
184 }; | |
185 | |
186 int[] events = [DWT.Dispose, DWT.Paint, DWT.Resize]; | |
187 | |
188 for (int i = 0; i < events.length; i++) { | |
189 addListener(events[i], listener); | |
190 } | |
191 } | |
192 | |
193 static int checkStyle (int style) { | |
194 int mask = DWT.FLAT | DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT; | |
195 return style & mask | DWT.NO_REDRAW_RESIZE; | |
196 } | |
197 | |
198 //protected void checkSubclass () { | |
199 // String name = getClass().getName (); | |
200 // String validName = ViewForm.class.getName(); | |
201 // if (!validName.equals(name)) { | |
202 // DWT.error (DWT.ERROR_INVALID_SUBCLASS); | |
203 // } | |
204 //} | |
205 | |
206 public override Rectangle computeTrim (int x, int y, int width, int height) { | |
207 checkWidget (); | |
208 int trimX = x - borderLeft - highlight; | |
209 int trimY = y - borderTop - highlight; | |
210 int trimWidth = width + borderLeft + borderRight + 2*highlight; | |
211 int trimHeight = height + borderTop + borderBottom + 2*highlight; | |
212 return new Rectangle(trimX, trimY, trimWidth, trimHeight); | |
213 } | |
214 public override Rectangle getClientArea() { | |
215 checkWidget(); | |
216 Rectangle clientArea = super.getClientArea(); | |
217 clientArea.x += borderLeft; | |
218 clientArea.y += borderTop; | |
219 clientArea.width -= borderLeft + borderRight; | |
220 clientArea.height -= borderTop + borderBottom; | |
221 return clientArea; | |
222 } | |
223 /** | |
224 * Returns the content area. | |
225 * | |
226 * @return the control in the content area of the pane or null | |
227 */ | |
228 public Control getContent() { | |
229 //checkWidget(); | |
230 return content; | |
231 } | |
232 /** | |
233 * Returns Control that appears in the top center of the pane. | |
234 * Typically this is a toolbar. | |
235 * | |
236 * @return the control in the top center of the pane or null | |
237 */ | |
238 public Control getTopCenter() { | |
239 //checkWidget(); | |
240 return topCenter; | |
241 } | |
242 /** | |
243 * Returns the Control that appears in the top left corner of the pane. | |
244 * Typically this is a label such as CLabel. | |
245 * | |
246 * @return the control in the top left corner of the pane or null | |
247 */ | |
248 public Control getTopLeft() { | |
249 //checkWidget(); | |
250 return topLeft; | |
251 } | |
252 /** | |
253 * Returns the control in the top right corner of the pane. | |
254 * Typically this is a Close button or a composite with a Menu and Close button. | |
255 * | |
256 * @return the control in the top right corner of the pane or null | |
257 */ | |
258 public Control getTopRight() { | |
259 //checkWidget(); | |
260 return topRight; | |
261 } | |
262 void onDispose() { | |
263 topLeft = null; | |
264 topCenter = null; | |
265 topRight = null; | |
266 content = null; | |
267 oldSize = null; | |
268 selectionBackground = null; | |
269 } | |
270 void onPaint(GC gc) { | |
271 Color gcForeground = gc.getForeground(); | |
272 Point size = getSize(); | |
273 Color border = getDisplay().getSystemColor(BORDER1_COLOR); | |
274 if (showBorder) { | |
275 gc.setForeground(border); | |
276 gc.drawRectangle(0, 0, size.x - 1, size.y - 1); | |
277 if (highlight > 0) { | |
278 int x1 = 1; | |
279 int y1 = 1; | |
280 int x2 = size.x - 1; | |
281 int y2 = size.y - 1; | |
282 int[] shape = [x1,y1, x2,y1, x2,y2, x1,y2, x1,y1+highlight, | |
283 x1+highlight,y1+highlight, x1+highlight,y2-highlight, | |
284 x2-highlight,y2-highlight, x2-highlight,y1+highlight, x1,y1+highlight]; | |
285 Color highlightColor = getDisplay().getSystemColor(DWT.COLOR_LIST_SELECTION); | |
286 gc.setBackground(highlightColor); | |
287 gc.fillPolygon(shape); | |
288 } | |
289 } | |
290 if (separator > -1) { | |
291 gc.setForeground(border); | |
292 gc.drawLine(borderLeft + highlight, separator, size.x - borderLeft - borderRight - highlight, separator); | |
293 } | |
294 gc.setForeground(gcForeground); | |
295 } | |
296 void onResize() { | |
297 Point size = getSize(); | |
298 if (oldSize is null || oldSize.x is 0 || oldSize.y is 0) { | |
299 redraw(); | |
300 } else { | |
301 int width = 0; | |
302 if (oldSize.x < size.x) { | |
303 width = size.x - oldSize.x + borderRight + highlight; | |
304 } else if (oldSize.x > size.x) { | |
305 width = borderRight + highlight; | |
306 } | |
307 redraw(size.x - width, 0, width, size.y, false); | |
308 | |
309 int height = 0; | |
310 if (oldSize.y < size.y) { | |
311 height = size.y - oldSize.y + borderBottom + highlight; | |
312 } | |
313 if (oldSize.y > size.y) { | |
314 height = borderBottom + highlight; | |
315 } | |
316 redraw(0, size.y - height, size.x, height, false); | |
317 } | |
318 oldSize = size; | |
319 } | |
320 /** | |
321 * Sets the content. | |
322 * Setting the content to null will remove it from | |
323 * the pane - however, the creator of the content must dispose of the content. | |
324 * | |
325 * @param content the control to be displayed in the content area or null | |
326 * | |
327 * @exception DWTException <ul> | |
328 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
329 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
330 * <li>ERROR_INVALID_ARGUMENT - if the control is not a child of this ViewForm</li> | |
331 * </ul> | |
332 */ | |
333 public void setContent(Control content) { | |
334 checkWidget(); | |
335 if (content !is null && content.getParent() !is this) { | |
336 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
337 } | |
338 if (this.content !is null && !this.content.isDisposed()) { | |
339 this.content.setBounds(OFFSCREEN, OFFSCREEN, 0, 0); | |
340 } | |
341 this.content = content; | |
342 layout(false); | |
343 } | |
344 /** | |
345 * Sets the layout which is associated with the receiver to be | |
346 * the argument which may be null. | |
347 * <p> | |
348 * Note: No Layout can be set on this Control because it already | |
349 * manages the size and position of its children. | |
350 * </p> | |
351 * | |
352 * @param layout the receiver's new layout or null | |
353 * | |
354 * @exception DWTException <ul> | |
355 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
356 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
357 * </ul> | |
358 */ | |
359 public override void setLayout (Layout layout) { | |
360 checkWidget(); | |
361 return; | |
362 } | |
363 void setSelectionBackground (Color color) { | |
364 checkWidget(); | |
365 if (selectionBackground is color) return; | |
366 if (color is null) color = getDisplay().getSystemColor(SELECTION_BACKGROUND); | |
367 selectionBackground = color; | |
368 redraw(); | |
369 } | |
370 /** | |
371 * Set the control that appears in the top center of the pane. | |
372 * Typically this is a toolbar. | |
373 * The topCenter is optional. Setting the topCenter to null will remove it from | |
374 * the pane - however, the creator of the topCenter must dispose of the topCenter. | |
375 * | |
376 * @param topCenter the control to be displayed in the top center or null | |
377 * | |
378 * @exception DWTException <ul> | |
379 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
380 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
381 * <li>ERROR_INVALID_ARGUMENT - if the control is not a child of this ViewForm</li> | |
382 * </ul> | |
383 */ | |
384 public void setTopCenter(Control topCenter) { | |
385 checkWidget(); | |
386 if (topCenter !is null && topCenter.getParent() !is this) { | |
387 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
388 } | |
389 if (this.topCenter !is null && !this.topCenter.isDisposed()) { | |
390 Point size = this.topCenter.getSize(); | |
391 this.topCenter.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y); | |
392 } | |
393 this.topCenter = topCenter; | |
394 layout(false); | |
395 } | |
396 /** | |
397 * Set the control that appears in the top left corner of the pane. | |
398 * Typically this is a label such as CLabel. | |
399 * The topLeft is optional. Setting the top left control to null will remove it from | |
400 * the pane - however, the creator of the control must dispose of the control. | |
401 * | |
402 * @param c the control to be displayed in the top left corner or null | |
403 * | |
404 * @exception DWTException <ul> | |
405 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
406 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
407 * <li>ERROR_INVALID_ARGUMENT - if the control is not a child of this ViewForm</li> | |
408 * </ul> | |
409 */ | |
410 public void setTopLeft(Control c) { | |
411 checkWidget(); | |
412 if (c !is null && c.getParent() !is this) { | |
413 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
414 } | |
415 if (this.topLeft !is null && !this.topLeft.isDisposed()) { | |
416 Point size = this.topLeft.getSize(); | |
417 this.topLeft.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y); | |
418 } | |
419 this.topLeft = c; | |
420 layout(false); | |
421 } | |
422 /** | |
423 * Set the control that appears in the top right corner of the pane. | |
424 * Typically this is a Close button or a composite with a Menu and Close button. | |
425 * The topRight is optional. Setting the top right control to null will remove it from | |
426 * the pane - however, the creator of the control must dispose of the control. | |
427 * | |
428 * @param c the control to be displayed in the top right corner or null | |
429 * | |
430 * @exception DWTException <ul> | |
431 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
432 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
433 * <li>ERROR_INVALID_ARGUMENT - if the control is not a child of this ViewForm</li> | |
434 * </ul> | |
435 */ | |
436 public void setTopRight(Control c) { | |
437 checkWidget(); | |
438 if (c !is null && c.getParent() !is this) { | |
439 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
440 } | |
441 if (this.topRight !is null && !this.topRight.isDisposed()) { | |
442 Point size = this.topRight.getSize(); | |
443 this.topRight.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y); | |
444 } | |
445 this.topRight = c; | |
446 layout(false); | |
447 } | |
448 /** | |
449 * Specify whether the border should be displayed or not. | |
450 * | |
451 * @param show true if the border should be displayed | |
452 * | |
453 * @exception DWTException <ul> | |
454 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
455 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
456 * </ul> | |
457 */ | |
458 public void setBorderVisible(bool show) { | |
459 checkWidget(); | |
460 if (showBorder is show) return; | |
461 | |
462 showBorder = show; | |
463 if (showBorder) { | |
464 borderLeft = borderTop = borderRight = borderBottom = 1; | |
465 if ((getStyle() & DWT.FLAT)is 0) highlight = 2; | |
466 } else { | |
467 borderBottom = borderTop = borderLeft = borderRight = 0; | |
468 highlight = 0; | |
469 } | |
470 layout(false); | |
471 redraw(); | |
472 } | |
473 /** | |
474 * If true, the topCenter will always appear on a separate line by itself, otherwise the | |
475 * topCenter will appear in the top row if there is room and will be moved to the second row if | |
476 * required. | |
477 * | |
478 * @param show true if the topCenter will always appear on a separate line by itself | |
479 * | |
480 * @exception DWTException <ul> | |
481 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
482 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
483 * </ul> | |
484 */ | |
485 public void setTopCenterSeparate(bool show) { | |
486 checkWidget(); | |
487 separateTopCenter = show; | |
488 layout(false); | |
489 } | |
490 } |