1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
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/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.widgets.*;
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
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.
31 * <dt><b>Styles:</b></dt>
32 * <dd>BORDER, FLAT</dd>
33 * <dt><b>Events:</b></dt>
37 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
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.
44 public class ViewForm extends Composite {
47 * marginWidth specifies the number of points of horizontal margin
48 * that will be placed along the left and right edges of the form.
50 * The default value is 0.
52 public int marginWidth = 0;
54 * marginHeight specifies the number of points of vertical margin
55 * that will be placed along the top and bottom edges of the form.
57 * The default value is 0.
59 public int marginHeight = 0;
61 * horizontalSpacing specifies the number of points between the right
62 * edge of one cell and the left edge of its neighbouring cell to
65 * The default value is 1.
67 public int horizontalSpacing = 1;
69 * verticalSpacing specifies the number of points between the bottom
70 * edge of one cell and the top edge of its neighbouring cell underneath.
72 * The default value is 1.
74 public int verticalSpacing = 1;
77 * Color of innermost line of drop shadow border.
79 * NOTE This field is badly named and can not be fixed for backwards compatibility.
80 * It should be capitalized.
85 public static RGB borderInsideRGB = new RGB (132, 130, 132);
87 * Color of middle line of drop shadow border.
89 * NOTE This field is badly named and can not be fixed for backwards compatibility.
90 * It should be capitalized.
95 public static RGB borderMiddleRGB = new RGB (143, 141, 138);
97 * Color of outermost line of drop shadow border.
99 * NOTE This field is badly named and can not be fixed for backwards compatibility.
100 * It should be capitalized.
105 public static RGB borderOutsideRGB = new RGB (171, 168, 165);
113 // Configuration and state info
114 boolean separateTopCenter = false;
115 boolean showBorder = false;
119 int borderBottom = 0;
125 Color selectionBackground;
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;
132 * Constructs a new instance of this class given its parent
133 * and a style value describing its behavior and appearance.
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.
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
147 * @exception IllegalArgumentException <ul>
148 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
150 * @exception SWTException <ul>
151 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
158 public ViewForm(Composite parent, int style) {
159 super(parent, checkStyle(style));
160 super.setLayout(new ViewFormLayout());
162 setBorderVisible((style & SWT.BORDER) != 0);
166 case SWT.Dispose: onDispose(e); break;
167 case SWT.Paint: onPaint(e.gc); break;
168 case SWT.Resize: onResize(); break;
172 int[] events = new int[] {SWT.Dispose, SWT.Paint, SWT.Resize};
174 for (int i = 0; i < events.length; i++) {
175 addListener(events[i], listener);
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;
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);
193 public Rectangle computeTrim (int x, int y, int width, int height) {
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);
202 public Rectangle getClientArea() {
204 Rectangle clientArea = super.getClientArea();
205 clientArea.x += borderLeft;
206 clientArea.y += borderTop;
207 clientArea.width -= borderLeft + borderRight;
208 clientArea.height -= borderTop + borderBottom;
212 * Returns the content area.
214 * @return the control in the content area of the pane or null
216 public Control getContent() {
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.
225 * Returns Control that appears in the top center of the pane.
226 * Typically this is a toolbar.
228 * @return the control in the top center of the pane or null
230 public Control getTopCenter() {
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.
239 * Returns the Control that appears in the top left corner of the pane.
240 * Typically this is a label such as CLabel.
242 * @return the control in the top left corner of the pane or null
244 public Control getTopLeft() {
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.
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.
256 * @return the control in the top right corner of the pane or null
258 public Control getTopRight() {
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.
266 void onDispose(Event event) {
267 removeListener(SWT.Dispose, listener);
268 notifyListeners(SWT.Dispose, event);
269 event.type = SWT.None;
276 selectionBackground = null;
278 void onPaint(GC gc) {
279 Color gcForeground = gc.getForeground();
280 Point size = getSize();
281 Color border = getDisplay().getSystemColor(BORDER1_COLOR);
283 gc.setForeground(border);
284 gc.drawRectangle(0, 0, size.x - 1, 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);
298 if (separator > -1) {
299 gc.setForeground(border);
300 gc.drawLine(borderLeft + highlight, separator, size.x - borderLeft - borderRight - highlight, separator);
302 gc.setForeground(gcForeground);
305 Point size = getSize();
306 if (oldSize == null || oldSize.x == 0 || oldSize.y == 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;
315 redraw(size.x - width, 0, width, size.y, false);
318 if (oldSize.y < size.y) {
319 height = size.y - oldSize.y + borderBottom + highlight;
321 if (oldSize.y > size.y) {
322 height = borderBottom + highlight;
324 redraw(0, size.y - height, size.x, height, false);
330 * Setting the content to null will remove it from
331 * the pane - however, the creator of the content must dispose of the content.
333 * @param content the control to be displayed in the content area or null
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>
341 public void setContent(Control content) {
343 if (content != null && content.getParent() != this) {
344 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
346 if (this.content != null && !this.content.isDisposed()) {
347 this.content.setBounds(OFFSCREEN, OFFSCREEN, 0, 0);
349 this.content = content;
353 * Sets the layout which is associated with the receiver to be
354 * the argument which may be null.
356 * Note: No Layout can be set on this Control because it already
357 * manages the size and position of its children.
360 * @param layout the receiver's new layout or null
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>
368 public void setLayout (Layout layout) {
372 void setSelectionBackground (Color color) {
374 if (selectionBackground == color) return;
375 if (color == null) color = getDisplay().getSystemColor(SELECTION_BACKGROUND);
376 selectionBackground = color;
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.
385 * @param topCenter the control to be displayed in the top center or null
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>
393 public void setTopCenter(Control topCenter) {
395 if (topCenter != null && topCenter.getParent() != this) {
396 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
398 if (this.topCenter != null && !this.topCenter.isDisposed()) {
399 Point size = this.topCenter.getSize();
400 this.topCenter.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y);
402 this.topCenter = topCenter;
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.
411 * @param c the control to be displayed in the top left corner or null
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>
419 public void setTopLeft(Control c) {
421 if (c != null && c.getParent() != this) {
422 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
424 if (this.topLeft != null && !this.topLeft.isDisposed()) {
425 Point size = this.topLeft.getSize();
426 this.topLeft.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y);
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.
437 * @param c the control to be displayed in the top right corner or null
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>
445 public void setTopRight(Control c) {
447 if (c != null && c.getParent() != this) {
448 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
450 if (this.topRight != null && !this.topRight.isDisposed()) {
451 Point size = this.topRight.getSize();
452 this.topRight.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y);
458 * Specify whether the border should be displayed or not.
460 * @param show true if the border should be displayed
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>
467 public void setBorderVisible(boolean show) {
469 if (showBorder == show) return;
473 borderLeft = borderTop = borderRight = borderBottom = 1;
474 if ((getStyle() & SWT.FLAT)== 0) highlight = 2;
476 borderBottom = borderTop = borderLeft = borderRight = 0;
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
487 * @param show true if the topCenter will always appear on a separate line by itself
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>
494 public void setTopCenterSeparate(boolean show) {
496 separateTopCenter = show;