1 /*******************************************************************************
2 * Copyright (c) 2000, 2009 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;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.widgets.*;
21 * This class provides the layout for ViewForm
25 class ViewFormLayout extends Layout {
28 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
29 ViewForm form = (ViewForm)composite;
30 Control left = form.topLeft;
31 Control center = form.topCenter;
32 Control right = form.topRight;
33 Control content = form.content;
35 Point leftSize = new Point(0, 0);
37 leftSize = computeChildSize(left, SWT.DEFAULT, SWT.DEFAULT, flushCache);
39 Point centerSize = new Point(0, 0);
41 centerSize = computeChildSize(center, SWT.DEFAULT, SWT.DEFAULT, flushCache);
43 Point rightSize = new Point(0, 0);
45 rightSize = computeChildSize(right, SWT.DEFAULT, SWT.DEFAULT, flushCache);
47 Point size = new Point(0, 0);
48 // calculate width of title bar
49 if (form.separateTopCenter ||
50 (wHint != SWT.DEFAULT && leftSize.x + centerSize.x + rightSize.x > wHint)) {
51 size.x = leftSize.x + rightSize.x;
52 if (leftSize.x > 0 && rightSize.x > 0) size.x += form.horizontalSpacing;
53 size.x = Math.max(centerSize.x, size.x);
54 size.y = Math.max(leftSize.y, rightSize.y);
56 size.y += centerSize.y;
57 if (left != null ||right != null)size.y += form.verticalSpacing;
60 size.x = leftSize.x + centerSize.x + rightSize.x;
62 if (leftSize.x > 0) count++;
63 if (centerSize.x > 0) count++;
64 if (rightSize.x > 0) count++;
65 if (count > 0) size.x += count * form.horizontalSpacing;
66 size.y = Math.max(leftSize.y, Math.max(centerSize.y, rightSize.y));
69 if (content != null) {
70 if (left != null || right != null || center != null) size.y += 1; // allow space for a vertical separator
71 Point contentSize = new Point(0, 0);
72 contentSize = computeChildSize(content, SWT.DEFAULT, SWT.DEFAULT, flushCache);
73 size.x = Math.max (size.x, contentSize.x);
74 size.y += contentSize.y;
75 if (size.y > contentSize.y) size.y += form.verticalSpacing;
78 size.x += 2*form.marginWidth;
79 size.y += 2*form.marginHeight;
81 if (wHint != SWT.DEFAULT) size.x = wHint;
82 if (hHint != SWT.DEFAULT) size.y = hHint;
87 Point computeChildSize(Control control, int wHint, int hHint, boolean flushCache) {
88 Object data = control.getLayoutData();
89 if (data == null || !(data instanceof CLayoutData)) {
90 data = new CLayoutData();
91 control.setLayoutData(data);
93 return ((CLayoutData)data).computeSize(control, wHint, hHint, flushCache);
96 int computeTrim(Control c) {
97 if (c instanceof Scrollable) {
98 Rectangle rect = ((Scrollable) c).computeTrim (0, 0, 0, 0);
101 return c.getBorderWidth () * 2;
105 protected boolean flushCache(Control control) {
106 Object data = control.getLayoutData();
107 if (data != null && data instanceof CLayoutData) ((CLayoutData)data).flushCache();
112 protected void layout(Composite composite, boolean flushCache) {
113 ViewForm form = (ViewForm)composite;
114 Control left = form.topLeft;
115 Control center = form.topCenter;
116 Control right = form.topRight;
117 Control content = form.content;
119 Rectangle rect = composite.getClientArea();
121 Point leftSize = new Point(0, 0);
122 if (left != null && !left.isDisposed()) {
123 leftSize = computeChildSize(left, SWT.DEFAULT, SWT.DEFAULT, flushCache);
125 Point centerSize = new Point(0, 0);
126 if (center != null && !center.isDisposed()) {
127 centerSize = computeChildSize(center, SWT.DEFAULT, SWT.DEFAULT, flushCache);
129 Point rightSize = new Point(0, 0);
130 if (right != null && !right.isDisposed()) {
131 rightSize = computeChildSize(right, SWT.DEFAULT, SWT.DEFAULT, flushCache);
134 int minTopWidth = leftSize.x + centerSize.x + rightSize.x + 2*form.marginWidth + 2*form.highlight;
136 if (leftSize.x > 0) count++;
137 if (centerSize.x > 0) count++;
138 if (rightSize.x > 0) count++;
139 if (count > 0) minTopWidth += count * form.horizontalSpacing;
141 int x = rect.x + rect.width - form.marginWidth - form.highlight;
142 int y = rect.y + form.marginHeight + form.highlight;
145 if (form.separateTopCenter || minTopWidth > rect.width) {
146 int topHeight = Math.max(rightSize.y, leftSize.y);
147 if (right != null && !right.isDisposed()) {
150 right.setBounds(x, y, rightSize.x, topHeight);
151 x -= form.horizontalSpacing;
153 if (left != null && !left.isDisposed()) {
155 int trim = computeTrim(left);
156 int leftW = x - rect.x - form.marginWidth - form.highlight - trim;
157 leftSize = computeChildSize(left, leftW, SWT.DEFAULT, false);
158 left.setBounds(rect.x + form.marginWidth + form.highlight, y, leftSize.x, topHeight);
160 if (top) y += topHeight + form.verticalSpacing;
161 if (center != null && !center.isDisposed()) {
163 int trim = computeTrim(center);
164 int w = rect.width - 2*form.marginWidth - 2*form.highlight - trim;
165 Point size = computeChildSize(center, w, SWT.DEFAULT, false);
166 if (size.x < centerSize.x) {
169 center.setBounds(rect.x + rect.width - form.marginWidth - form.highlight - centerSize.x, y, centerSize.x, centerSize.y);
170 y += centerSize.y + form.verticalSpacing;
173 int topHeight = Math.max(rightSize.y, Math.max(centerSize.y, leftSize.y));
174 if (right != null && !right.isDisposed()) {
177 right.setBounds(x, y, rightSize.x, topHeight);
178 x -= form.horizontalSpacing;
180 if (center != null && !center.isDisposed()) {
183 center.setBounds(x, y, centerSize.x, topHeight);
184 x -= form.horizontalSpacing;
186 if (left != null && !left.isDisposed()) {
188 Rectangle trim = left instanceof Composite ? ((Composite)left).computeTrim(0, 0, 0, 0) : new Rectangle(0, 0, 0, 0);
189 int w = x - rect.x - form.marginWidth - form.highlight - trim.width;
190 int h = topHeight - trim.height;
191 leftSize = computeChildSize(left, w, h, false);
192 left.setBounds(rect.x + form.marginWidth + form.highlight, y, leftSize.x, topHeight);
194 if (top)y += topHeight + form.verticalSpacing;
196 int oldSeperator = form.separator;
198 if (content != null && !content.isDisposed()) {
199 if (left != null || right!= null || center != null){
203 content.setBounds(rect.x + form.marginWidth + form.highlight, y, rect.width - 2 * form.marginWidth - 2*form.highlight, rect.y + rect.height - y - form.marginHeight - form.highlight);
205 if (oldSeperator != form.separator) {
207 if (oldSeperator == -1) {
209 b = form.separator + 1;
210 } else if (form.separator == -1) {
212 b = oldSeperator + 1;
214 t = Math.min(form.separator, oldSeperator);
215 b = Math.max(form.separator, oldSeperator);
217 form.redraw(form.borderLeft, t, form.getSize().x - form.borderLeft - form.borderRight, b - t, false);