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