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