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 SashForm
25 class SashFormLayout extends Layout {
27 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
28 SashForm sashForm = (SashForm)composite;
29 Control[] cArray = sashForm.getControls(true);
32 if (cArray.length == 0) {
33 if (wHint != SWT.DEFAULT) width = wHint;
34 if (hHint != SWT.DEFAULT) height = hHint;
35 return new Point(width, height);
37 // determine control sizes
38 boolean vertical = sashForm.getOrientation() == SWT.VERTICAL;
41 for (int i = 0; i < cArray.length; i++) {
43 Point size = cArray[i].computeSize(wHint, SWT.DEFAULT, flushCache);
44 if (size.y > maxValue) {
48 width = Math.max(width, size.x);
50 Point size = cArray[i].computeSize(SWT.DEFAULT, hHint, flushCache);
51 if (size.x > maxValue) {
55 height = Math.max(height, size.y);
59 long[] ratios = new long[cArray.length];
61 for (int i = 0; i < cArray.length; i++) {
62 Object data = cArray[i].getLayoutData();
63 if (data != null && data instanceof SashFormData) {
64 ratios[i] = ((SashFormData)data).weight;
66 data = new SashFormData();
67 cArray[i].setLayoutData(data);
68 ((SashFormData)data).weight = ratios[i] = ((200 << 16) + 999) / 1000;
73 if (ratios[maxIndex] > 0) {
74 int sashwidth = sashForm.sashes.length > 0 ? sashForm.SASH_WIDTH + sashForm.sashes [0].getBorderWidth() * 2 : sashForm.SASH_WIDTH;
76 height += (int)(total * maxValue / ratios[maxIndex]) + (cArray.length - 1) * sashwidth;
78 width += (int)(total * maxValue / ratios[maxIndex]) + (cArray.length - 1) * sashwidth;
81 width += sashForm.getBorderWidth()*2;
82 height += sashForm.getBorderWidth()*2;
83 if (wHint != SWT.DEFAULT) width = wHint;
84 if (hHint != SWT.DEFAULT) height = hHint;
85 return new Point(width, height);
89 protected boolean flushCache(Control control) {
94 protected void layout(Composite composite, boolean flushCache) {
95 SashForm sashForm = (SashForm)composite;
96 Rectangle area = sashForm.getClientArea();
97 if (area.width <= 1 || area.height <= 1) return;
99 Control[] newControls = sashForm.getControls(true);
100 if (sashForm.controls.length == 0 && newControls.length == 0) return;
101 sashForm.controls = newControls;
103 Control[] controls = sashForm.controls;
105 if (sashForm.maxControl != null && !sashForm.maxControl.isDisposed()) {
106 for (int i= 0; i < controls.length; i++){
107 if (controls[i] != sashForm.maxControl) {
108 controls[i].setBounds(-200, -200, 0, 0);
110 controls[i].setBounds(area);
116 // keep just the right number of sashes
117 if (sashForm.sashes.length < controls.length - 1) {
118 Sash[] newSashes = new Sash[controls.length - 1];
119 System.arraycopy(sashForm.sashes, 0, newSashes, 0, sashForm.sashes.length);
120 for (int i = sashForm.sashes.length; i < newSashes.length; i++) {
121 newSashes[i] = sashForm.createSash();
123 sashForm.sashes = newSashes;
125 if (sashForm.sashes.length > controls.length - 1) {
126 if (controls.length == 0) {
127 for (int i = 0; i < sashForm.sashes.length; i++) {
128 sashForm.sashes[i].dispose();
130 sashForm.sashes = new Sash[0];
132 Sash[] newSashes = new Sash[controls.length - 1];
133 System.arraycopy(sashForm.sashes, 0, newSashes, 0, newSashes.length);
134 for (int i = controls.length - 1; i < sashForm.sashes.length; i++) {
135 sashForm.sashes[i].dispose();
137 sashForm.sashes = newSashes;
140 if (controls.length == 0) return;
141 Sash[] sashes = sashForm.sashes;
143 long[] ratios = new long[controls.length];
145 for (int i = 0; i < controls.length; i++) {
146 Object data = controls[i].getLayoutData();
147 if (data != null && data instanceof SashFormData) {
148 ratios[i] = ((SashFormData)data).weight;
150 data = new SashFormData();
151 controls[i].setLayoutData(data);
152 ((SashFormData)data).weight = ratios[i] = ((200 << 16) + 999) / 1000;
158 int sashwidth = sashes.length > 0 ? sashForm.SASH_WIDTH + sashes [0].getBorderWidth() * 2 : sashForm.SASH_WIDTH;
159 if (sashForm.getOrientation() == SWT.HORIZONTAL) {
160 int width = (int)(ratios[0] * (area.width - sashes.length * sashwidth) / total);
162 controls[0].setBounds(x, area.y, width, area.height);
164 for (int i = 1; i < controls.length - 1; i++) {
165 sashes[i - 1].setBounds(x, area.y, sashwidth, area.height);
167 width = (int)(ratios[i] * (area.width - sashes.length * sashwidth) / total);
168 controls[i].setBounds(x, area.y, width, area.height);
171 if (controls.length > 1) {
172 sashes[sashes.length - 1].setBounds(x, area.y, sashwidth, area.height);
174 width = area.width - x;
175 controls[controls.length - 1].setBounds(x, area.y, width, area.height);
178 int height = (int)(ratios[0] * (area.height - sashes.length * sashwidth) / total);
180 controls[0].setBounds(area.x, y, area.width, height);
182 for (int i = 1; i < controls.length - 1; i++) {
183 sashes[i - 1].setBounds(area.x, y, area.width, sashwidth);
185 height = (int)(ratios[i] * (area.height - sashes.length * sashwidth) / total);
186 controls[i].setBounds(area.x, y, area.width, height);
189 if (controls.length > 1) {
190 sashes[sashes.length - 1].setBounds(area.x, y, area.width, sashwidth);
192 height = area.height - y;
193 controls[controls.length - 1].setBounds(area.x, y, area.width, height);