]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/custom/SashFormLayout.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / custom / SashFormLayout.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2009 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 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.widgets.*;
19
20 /**
21  * This class provides the layout for SashForm
22  *
23  * @see SashForm
24  */
25 class SashFormLayout extends Layout {
26 @Override
27 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
28         SashForm sashForm = (SashForm)composite;
29         Control[] cArray = sashForm.getControls(true);
30         int width = 0;
31         int height = 0;
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);
36         }
37         // determine control sizes
38         boolean vertical = sashForm.getOrientation() == SWT.VERTICAL;
39         int maxIndex = 0;
40         int maxValue = 0;
41         for (int i = 0; i < cArray.length; i++) {
42                 if (vertical) {
43                         Point size = cArray[i].computeSize(wHint, SWT.DEFAULT, flushCache);
44                         if (size.y > maxValue) {
45                                 maxIndex = i;
46                                 maxValue = size.y;
47                         }
48                         width = Math.max(width, size.x);
49                 } else {
50                         Point size = cArray[i].computeSize(SWT.DEFAULT, hHint, flushCache);
51                         if (size.x > maxValue) {
52                                 maxIndex = i;
53                                 maxValue = size.x;
54                         }
55                         height = Math.max(height, size.y);
56                 }
57         }
58         // get the ratios
59         long[] ratios = new long[cArray.length];
60         long total = 0;
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;
65                 } else {
66                         data = new SashFormData();
67                         cArray[i].setLayoutData(data);
68                         ((SashFormData)data).weight = ratios[i] = ((200 << 16) + 999) / 1000;
69
70                 }
71                 total += ratios[i];
72         }
73         if (ratios[maxIndex] > 0) {
74                 int sashwidth = sashForm.sashes.length > 0 ? sashForm.SASH_WIDTH + sashForm.sashes [0].getBorderWidth() * 2 : sashForm.SASH_WIDTH;
75                 if (vertical) {
76                         height += (int)(total * maxValue / ratios[maxIndex]) + (cArray.length - 1) * sashwidth;
77                 } else {
78                         width += (int)(total * maxValue / ratios[maxIndex]) + (cArray.length - 1) * sashwidth;
79                 }
80         }
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);
86 }
87
88 @Override
89 protected boolean flushCache(Control control) {
90         return true;
91 }
92
93 @Override
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;
98
99         Control[] newControls = sashForm.getControls(true);
100         if (sashForm.controls.length == 0 && newControls.length == 0) return;
101         sashForm.controls = newControls;
102
103         Control[] controls = sashForm.controls;
104
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);
109                         } else {
110                                 controls[i].setBounds(area);
111                         }
112                 }
113                 return;
114         }
115
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();
122                 }
123                 sashForm.sashes = newSashes;
124         }
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();
129                         }
130                         sashForm.sashes = new Sash[0];
131                 } else {
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();
136                         }
137                         sashForm.sashes = newSashes;
138                 }
139         }
140         if (controls.length == 0) return;
141         Sash[] sashes = sashForm.sashes;
142         // get the ratios
143         long[] ratios = new long[controls.length];
144         long total = 0;
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;
149                 } else {
150                         data = new SashFormData();
151                         controls[i].setLayoutData(data);
152                         ((SashFormData)data).weight = ratios[i] = ((200 << 16) + 999) / 1000;
153
154                 }
155                 total += ratios[i];
156         }
157
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);
161                 int x = area.x;
162                 controls[0].setBounds(x, area.y, width, area.height);
163                 x += width;
164                 for (int i = 1; i < controls.length - 1; i++) {
165                         sashes[i - 1].setBounds(x, area.y, sashwidth, area.height);
166                         x += sashwidth;
167                         width = (int)(ratios[i] * (area.width - sashes.length * sashwidth) / total);
168                         controls[i].setBounds(x, area.y, width, area.height);
169                         x += width;
170                 }
171                 if (controls.length > 1) {
172                         sashes[sashes.length - 1].setBounds(x, area.y, sashwidth, area.height);
173                         x += sashwidth;
174                         width = area.width - x;
175                         controls[controls.length - 1].setBounds(x, area.y, width, area.height);
176                 }
177         } else {
178                 int height = (int)(ratios[0] * (area.height - sashes.length * sashwidth) / total);
179                 int y = area.y;
180                 controls[0].setBounds(area.x, y, area.width, height);
181                 y += height;
182                 for (int i = 1; i < controls.length - 1; i++) {
183                         sashes[i - 1].setBounds(area.x, y, area.width, sashwidth);
184                         y += sashwidth;
185                         height = (int)(ratios[i] * (area.height - sashes.length * sashwidth) / total);
186                         controls[i].setBounds(area.x, y, area.width, height);
187                         y += height;
188                 }
189                 if (controls.length > 1) {
190                         sashes[sashes.length - 1].setBounds(area.x, y, area.width, sashwidth);
191                         y += sashwidth;
192                         height = area.height - y;
193                         controls[controls.length - 1].setBounds(area.x, y, area.width, height);
194                 }
195
196         }
197 }
198 }