Mercurial > projects > dwt-addons
annotate dwtx/jface/layout/AbstractColumnLayout.d @ 70:46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 01:36:46 +0200 |
parents | ea8ff534f622 |
children | 4878bef4a38e |
rev | line source |
---|---|
10 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
2 * Copyright (c) 2006, 2008 IBM Corporation and others. |
10 | 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 (original file dwtx.ui.texteditor.templates.ColumnLayout) | |
10 * Tom Schindl <tom.schindl@bestsolution.at> - refactored to be widget independent (bug 171824) | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
11 * - fix for bug 178280, 184342, 184045, 208014, 214532 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
12 * Micah Hainline <micah_hainline@yahoo.com> - fix in bug: 208335 |
10 | 13 * Port to the D programming language: |
14 * Frank Benoit <benoit@tionex.de> | |
15 *******************************************************************************/ | |
16 module dwtx.jface.layout.AbstractColumnLayout; | |
17 | |
18 | |
19 import dwt.DWT; | |
20 import dwt.graphics.Point; | |
21 import dwt.graphics.Rectangle; | |
22 import dwt.widgets.Composite; | |
23 import dwt.widgets.Event; | |
24 import dwt.widgets.Layout; | |
25 import dwt.widgets.Listener; | |
26 import dwt.widgets.Scrollable; | |
27 import dwt.widgets.Widget; | |
28 import dwtx.core.runtime.Assert; | |
29 import dwtx.jface.util.Policy; | |
30 import dwtx.jface.viewers.ColumnLayoutData; | |
31 import dwtx.jface.viewers.ColumnPixelData; | |
32 import dwtx.jface.viewers.ColumnWeightData; | |
33 import dwtx.jface.viewers.TableLayout; | |
34 | |
35 import dwt.dwthelper.utils; | |
36 | |
37 /** | |
38 * The AbstractColumnLayout is a {@link Layout} used to set the size of a table | |
39 * in a consistent way even during a resize unlike a {@link TableLayout} which | |
40 * only sets initial sizes. | |
41 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
42 * <p> |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
43 * <b>You can only add the layout to a container whose only child is the |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
44 * table/tree control you want the layouts applied to.</b> |
10 | 45 * </p> |
46 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
47 * @since 3.4 |
10 | 48 */ |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
49 public abstract class AbstractColumnLayout : Layout { |
10 | 50 private static int COLUMN_TRIM; |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
51 static { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
52 if ("win32".equals(DWT.getPlatform())) { //$NON-NLS-1$ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
53 COLUMN_TRIM = 4; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
54 } else if ("carbon".equals(DWT.getPlatform())) { //$NON-NLS-1$ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
55 COLUMN_TRIM = 24; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
56 } else { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
57 COLUMN_TRIM = 3; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
58 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
59 } |
10 | 60 |
61 static const bool IS_GTK; | |
62 | |
63 static const String LAYOUT_DATA; | |
64 | |
65 static this(){ | |
66 COLUMN_TRIM = "carbon".equals(DWT.getPlatform()) ? 24 : 3; //$NON-NLS-1$ | |
67 IS_GTK = "gtk".equals(DWT.getPlatform());//$NON-NLS-1$ | |
68 LAYOUT_DATA = Policy.JFACE ~ ".LAYOUT_DATA"; //$NON-NLS-1$ | |
69 } | |
70 | |
71 private bool inupdateMode = false; | |
72 | |
73 private bool relayout = true; | |
74 | |
75 private Listener resizeListener; | |
76 private void init_resizeListener(){ | |
77 resizeListener = new class Listener { | |
78 | |
79 public void handleEvent(Event event) { | |
80 if( ! inupdateMode ) { | |
81 updateColumnData(event.widget); | |
82 } | |
83 } | |
84 | |
85 }; | |
86 } | |
87 public this(){ | |
88 init_resizeListener(); | |
89 } | |
90 | |
91 /** | |
92 * Adds a new column of data to this table layout. | |
93 * | |
94 * @param column | |
95 * the column | |
96 * | |
97 * @param data | |
98 * the column layout data | |
99 */ | |
100 public void setColumnData(Widget column, ColumnLayoutData data) { | |
101 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
102 if (column.getData(LAYOUT_DATA) is null) { |
10 | 103 column.addListener(DWT.Resize, resizeListener); |
104 } | |
105 | |
106 column.setData(LAYOUT_DATA, data); | |
107 } | |
108 | |
109 /** | |
110 * Compute the size of the table or tree based on the ColumnLayoutData and | |
111 * the width and height hint. | |
112 * | |
113 * @param scrollable | |
114 * the widget to compute | |
115 * @param wHint | |
116 * the width hint | |
117 * @param hHint | |
118 * the height hint | |
119 * @return Point where x is the width and y is the height | |
120 */ | |
121 private Point computeTableTreeSize(Scrollable scrollable, int wHint, | |
122 int hHint) { | |
123 Point result = scrollable.computeSize(wHint, hHint); | |
124 | |
125 int width = 0; | |
126 int size = getColumnCount(scrollable); | |
127 for (int i = 0; i < size; ++i) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
128 ColumnLayoutData layoutData = getLayoutData(scrollable, i); |
10 | 129 if ( auto col = cast(ColumnPixelData)layoutData) { |
130 width += col.width; | |
131 if (col.addTrim) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
132 width += getColumnTrim(); |
10 | 133 } |
134 } else if ( auto col = cast(ColumnWeightData)layoutData ) { | |
135 width += col.minimumWidth; | |
136 } else { | |
137 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$ | |
138 } | |
139 } | |
140 if (width > result.x) | |
141 result.x = width; | |
142 | |
143 return result; | |
144 } | |
145 | |
146 /** | |
147 * Layout the scrollable based on the supplied width and area. Only increase | |
148 * the size of the scrollable if increase is <code>true</code>. | |
149 * | |
150 * @param scrollable | |
151 * @param width | |
152 * @param area | |
153 * @param increase | |
154 */ | |
155 private void layoutTableTree(Scrollable scrollable, int width, | |
156 Rectangle area, bool increase) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
157 int numberOfColumns = getColumnCount(scrollable); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
158 int[] widths = new int[numberOfColumns]; |
10 | 159 |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
160 int[] weightColumnIndices = new int[numberOfColumns]; |
10 | 161 int numberOfWeightColumns = 0; |
162 | |
163 int fixedWidth = 0; | |
164 int totalWeight = 0; | |
165 | |
166 // First calc space occupied by fixed columns | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
167 for (int i = 0; i < numberOfColumns; i++) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
168 ColumnLayoutData col = getLayoutData(scrollable, i); |
10 | 169 if ( auto cpd = cast(ColumnPixelData)col ) { |
170 int pixels = cpd.width; | |
171 if (cpd.addTrim) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
172 pixels += getColumnTrim(); |
10 | 173 } |
174 widths[i] = pixels; | |
175 fixedWidth += pixels; | |
176 } else if ( auto cw = cast(ColumnWeightData) col ) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
177 weightColumnIndices[numberOfWeightColumns] = i; |
10 | 178 numberOfWeightColumns++; |
179 totalWeight += cw.weight; | |
180 } else { | |
181 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$ | |
182 } | |
183 } | |
184 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
185 bool recalculate; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
186 do { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
187 recalculate = false; |
10 | 188 for (int i = 0; i < numberOfWeightColumns; i++) { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
189 int colIndex = weightColumnIndices[i]; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
190 ColumnWeightData cw = (ColumnWeightData) getLayoutData( |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
191 scrollable, colIndex); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
192 final int minWidth = cw.minimumWidth; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
193 final int allowedWidth = (width - fixedWidth) * cw.weight |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
194 / totalWeight; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
195 if (allowedWidth < minWidth) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
196 /* |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
197 * if the width assigned by weight is less than the minimum, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
198 * then treat this column as fixed, remove it from weight |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
199 * calculations, and recalculate other weights. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
200 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
201 numberOfWeightColumns--; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
202 totalWeight -= cw.weight; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
203 fixedWidth += minWidth; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
204 widths[colIndex] = minWidth; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
205 System.arraycopy(weightColumnIndices, i + 1, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
206 weightColumnIndices, i, numberOfWeightColumns - i); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
207 recalculate = true; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
208 break; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
209 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
210 widths[colIndex] = allowedWidth; |
10 | 211 } |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
212 } while (recalculate); |
10 | 213 |
214 if (increase) { | |
215 scrollable.setSize(area.width, area.height); | |
216 } | |
217 | |
218 inupdateMode = true; | |
219 setColumnWidths(scrollable, widths); | |
220 scrollable.update(); | |
221 inupdateMode = false; | |
222 | |
223 if (!increase) { | |
224 scrollable.setSize(area.width, area.height); | |
225 } | |
226 } | |
227 | |
228 /* | |
229 * (non-Javadoc) | |
230 * | |
231 * @see dwt.widgets.Layout#computeSize(dwt.widgets.Composite, | |
232 * int, int, bool) | |
233 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
234 protected override Point computeSize(Composite composite, int wHint, int hHint, |
10 | 235 bool flushCache) { |
236 return computeTableTreeSize(getControl(composite), wHint, hHint); | |
237 } | |
238 | |
239 /* | |
240 * (non-Javadoc) | |
241 * | |
242 * @see dwt.widgets.Layout#layout(dwt.widgets.Composite, | |
243 * bool) | |
244 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
245 protected override void layout(Composite composite, bool flushCache) { |
10 | 246 Rectangle area = composite.getClientArea(); |
247 Scrollable table = getControl(composite); | |
248 int tableWidth = table.getSize().x; | |
249 int trim = computeTrim(area, table, tableWidth); | |
250 int width = Math.max(0, area.width - trim); | |
251 | |
252 if (width > 1) | |
253 layoutTableTree(table, width, area, tableWidth < area.width); | |
254 | |
255 // For the first time we need to relayout because Scrollbars are not | |
256 // calculate appropriately | |
257 if (relayout) { | |
258 relayout = false; | |
259 composite.layout(); | |
260 } | |
261 } | |
262 | |
263 /** | |
264 * Compute the area required for trim. | |
265 * | |
266 * @param area | |
267 * @param scrollable | |
268 * @param currentWidth | |
269 * @return int | |
270 */ | |
271 private int computeTrim(Rectangle area, Scrollable scrollable, | |
272 int currentWidth) { | |
273 int trim; | |
274 | |
275 if (currentWidth > 1) { | |
276 trim = currentWidth - scrollable.getClientArea().width; | |
277 } else { | |
278 // initially, the table has no extend and no client area - use the | |
279 // border with | |
280 // plus some padding as educated guess | |
281 trim = 2 * scrollable.getBorderWidth() + 1; | |
282 } | |
283 | |
284 return trim; | |
285 } | |
286 | |
287 /** | |
288 * Get the control being laid out. | |
289 * | |
290 * @param composite | |
291 * the composite with the layout | |
292 * @return {@link Scrollable} | |
293 */ | |
294 Scrollable getControl(Composite composite) { | |
295 return cast(Scrollable) composite.getChildren()[0]; | |
296 } | |
297 | |
298 /** | |
299 * Get the number of columns for the receiver. | |
300 * | |
301 * @return the number of columns | |
302 */ | |
303 abstract int getColumnCount(Scrollable tableTree); | |
304 | |
305 /** | |
306 * Set the widths of the columns. | |
307 * | |
308 * @param widths | |
309 */ | |
310 abstract void setColumnWidths(Scrollable tableTree, int[] widths); | |
311 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
312 abstract ColumnLayoutData getLayoutData(Scrollable tableTree, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
313 int columnIndex); |
10 | 314 |
315 abstract void updateColumnData(Widget column); | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
316 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
317 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
318 * The number of extra pixels taken as horizontal trim by the table column. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
319 * To ensure there are N pixels available for the content of the column, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
320 * assign N+COLUMN_TRIM for the column width. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
321 * |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
322 * @return the trim used by the columns |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
323 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
324 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
325 protected int getColumnTrim() { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
326 return COLUMN_TRIM; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
327 } |
10 | 328 } |