Mercurial > projects > dwt-mac
comparison dwt/custom/SashFormLayout.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | e831403a80a9 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2005 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 *******************************************************************************/ | |
11 module dwt.custom; | |
12 | |
13 import dwt.*; | |
14 import dwt.graphics.*; | |
15 import dwt.widgets.*; | |
16 | |
17 /** | |
18 * This class provides the layout for SashForm | |
19 * | |
20 * @see SashForm | |
21 */ | |
22 class SashFormLayout : Layout { | |
23 protected Point computeSize(Composite composite, int wHint, int hHint, bool flushCache) { | |
24 SashForm sashForm = (SashForm)composite; | |
25 Control[] cArray = sashForm.getControls(true); | |
26 int width = 0; | |
27 int height = 0; | |
28 if (cArray.length is 0) { | |
29 if (wHint !is DWT.DEFAULT) width = wHint; | |
30 if (hHint !is DWT.DEFAULT) height = hHint; | |
31 return new Point(width, height); | |
32 } | |
33 // determine control sizes | |
34 bool vertical = sashForm.getOrientation() is DWT.VERTICAL; | |
35 int maxIndex = 0; | |
36 int maxValue = 0; | |
37 for (int i = 0; i < cArray.length; i++) { | |
38 if (vertical) { | |
39 Point size = cArray[i].computeSize(wHint, DWT.DEFAULT, flushCache); | |
40 if (size.y > maxValue) { | |
41 maxIndex = i; | |
42 maxValue = size.y; | |
43 } | |
44 width = Math.max(width, size.x); | |
45 } else { | |
46 Point size = cArray[i].computeSize(DWT.DEFAULT, hHint, flushCache); | |
47 if (size.x > maxValue) { | |
48 maxIndex = i; | |
49 maxValue = size.x; | |
50 } | |
51 height = Math.max(height, size.y); | |
52 } | |
53 } | |
54 // get the ratios | |
55 long[] ratios = new long[cArray.length]; | |
56 long total = 0; | |
57 for (int i = 0; i < cArray.length; i++) { | |
58 Object data = cArray[i].getLayoutData(); | |
59 if (data !is null && data instanceof SashFormData) { | |
60 ratios[i] = ((SashFormData)data).weight; | |
61 } else { | |
62 data = new SashFormData(); | |
63 cArray[i].setLayoutData(data); | |
64 ((SashFormData)data).weight = ratios[i] = ((200 << 16) + 999) / 1000; | |
65 | |
66 } | |
67 total += ratios[i]; | |
68 } | |
69 if (ratios[maxIndex] > 0) { | |
70 int sashwidth = sashForm.sashes.length > 0 ? sashForm.SASH_WIDTH + sashForm.sashes [0].getBorderWidth() * 2 : sashForm.SASH_WIDTH; | |
71 if (vertical) { | |
72 height += (int)(total * maxValue / ratios[maxIndex]) + (cArray.length - 1) * sashwidth; | |
73 } else { | |
74 width += (int)(total * maxValue / ratios[maxIndex]) + (cArray.length - 1) * sashwidth; | |
75 } | |
76 } | |
77 width += sashForm.getBorderWidth()*2; | |
78 height += sashForm.getBorderWidth()*2; | |
79 if (wHint !is DWT.DEFAULT) width = wHint; | |
80 if (hHint !is DWT.DEFAULT) height = hHint; | |
81 return new Point(width, height); | |
82 } | |
83 | |
84 protected bool flushCache(Control control) { | |
85 return true; | |
86 } | |
87 | |
88 protected void layout(Composite composite, bool flushCache) { | |
89 SashForm sashForm = (SashForm)composite; | |
90 Rectangle area = sashForm.getClientArea(); | |
91 if (area.width <= 1 || area.height <= 1) return; | |
92 | |
93 Control[] newControls = sashForm.getControls(true); | |
94 if (sashForm.controls.length is 0 && newControls.length is 0) return; | |
95 sashForm.controls = newControls; | |
96 | |
97 Control[] controls = sashForm.controls; | |
98 | |
99 if (sashForm.maxControl !is null && !sashForm.maxControl.isDisposed()) { | |
100 for (int i= 0; i < controls.length; i++){ | |
101 if (controls[i] !is sashForm.maxControl) { | |
102 controls[i].setBounds(-200, -200, 0, 0); | |
103 } else { | |
104 controls[i].setBounds(area); | |
105 } | |
106 } | |
107 return; | |
108 } | |
109 | |
110 // keep just the right number of sashes | |
111 if (sashForm.sashes.length < controls.length - 1) { | |
112 Sash[] newSashes = new Sash[controls.length - 1]; | |
113 System.arraycopy(sashForm.sashes, 0, newSashes, 0, sashForm.sashes.length); | |
114 for (int i = sashForm.sashes.length; i < newSashes.length; i++) { | |
115 newSashes[i] = new Sash(sashForm, sashForm.sashStyle); | |
116 newSashes[i].setBackground(sashForm.background); | |
117 newSashes[i].setForeground(sashForm.foreground); | |
118 newSashes[i].addListener(DWT.Selection, sashForm.sashListener); | |
119 } | |
120 sashForm.sashes = newSashes; | |
121 } | |
122 if (sashForm.sashes.length > controls.length - 1) { | |
123 if (controls.length is 0) { | |
124 for (int i = 0; i < sashForm.sashes.length; i++) { | |
125 sashForm.sashes[i].dispose(); | |
126 } | |
127 sashForm.sashes = new Sash[0]; | |
128 } else { | |
129 Sash[] newSashes = new Sash[controls.length - 1]; | |
130 System.arraycopy(sashForm.sashes, 0, newSashes, 0, newSashes.length); | |
131 for (int i = controls.length - 1; i < sashForm.sashes.length; i++) { | |
132 sashForm.sashes[i].dispose(); | |
133 } | |
134 sashForm.sashes = newSashes; | |
135 } | |
136 } | |
137 if (controls.length is 0) return; | |
138 Sash[] sashes = sashForm.sashes; | |
139 // get the ratios | |
140 long[] ratios = new long[controls.length]; | |
141 long total = 0; | |
142 for (int i = 0; i < controls.length; i++) { | |
143 Object data = controls[i].getLayoutData(); | |
144 if (data !is null && data instanceof SashFormData) { | |
145 ratios[i] = ((SashFormData)data).weight; | |
146 } else { | |
147 data = new SashFormData(); | |
148 controls[i].setLayoutData(data); | |
149 ((SashFormData)data).weight = ratios[i] = ((200 << 16) + 999) / 1000; | |
150 | |
151 } | |
152 total += ratios[i]; | |
153 } | |
154 | |
155 int sashwidth = sashes.length > 0 ? sashForm.SASH_WIDTH + sashes [0].getBorderWidth() * 2 : sashForm.SASH_WIDTH; | |
156 if (sashForm.getOrientation() is DWT.HORIZONTAL) { | |
157 int width = (int)(ratios[0] * (area.width - sashes.length * sashwidth) / total); | |
158 int x = area.x; | |
159 controls[0].setBounds(x, area.y, width, area.height); | |
160 x += width; | |
161 for (int i = 1; i < controls.length - 1; i++) { | |
162 sashes[i - 1].setBounds(x, area.y, sashwidth, area.height); | |
163 x += sashwidth; | |
164 width = (int)(ratios[i] * (area.width - sashes.length * sashwidth) / total); | |
165 controls[i].setBounds(x, area.y, width, area.height); | |
166 x += width; | |
167 } | |
168 if (controls.length > 1) { | |
169 sashes[sashes.length - 1].setBounds(x, area.y, sashwidth, area.height); | |
170 x += sashwidth; | |
171 width = area.width - x; | |
172 controls[controls.length - 1].setBounds(x, area.y, width, area.height); | |
173 } | |
174 } else { | |
175 int height = (int)(ratios[0] * (area.height - sashes.length * sashwidth) / total); | |
176 int y = area.y; | |
177 controls[0].setBounds(area.x, y, area.width, height); | |
178 y += height; | |
179 for (int i = 1; i < controls.length - 1; i++) { | |
180 sashes[i - 1].setBounds(area.x, y, area.width, sashwidth); | |
181 y += sashwidth; | |
182 height = (int)(ratios[i] * (area.height - sashes.length * sashwidth) / total); | |
183 controls[i].setBounds(area.x, y, area.width, height); | |
184 y += height; | |
185 } | |
186 if (controls.length > 1) { | |
187 sashes[sashes.length - 1].setBounds(area.x, y, area.width, sashwidth); | |
188 y += sashwidth; | |
189 height = area.height - y; | |
190 controls[controls.length - 1].setBounds(area.x, y, area.width, height); | |
191 } | |
192 | |
193 } | |
194 } | |
195 } |