Mercurial > projects > dwt-addons
annotate dwtx/jface/viewers/TableLayout.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 | 7943b8e4206d |
children | 4878bef4a38e |
rev | line source |
---|---|
10 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 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 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
68
diff
changeset
|
10 * Florian Priester - bug 106059 |
10 | 11 * Port to the D programming language: |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.jface.viewers.TableLayout; | |
15 | |
16 import dwtx.jface.viewers.ColumnLayoutData; | |
17 import dwtx.jface.viewers.ColumnPixelData; | |
18 import dwtx.jface.viewers.ColumnWeightData; | |
19 | |
20 import tango.util.collection.model.Seq; | |
21 import tango.util.collection.ArraySeq; | |
22 | |
23 import dwt.DWT; | |
24 import dwt.graphics.Point; | |
25 import dwt.widgets.Composite; | |
26 import dwt.widgets.Item; | |
27 import dwt.widgets.Layout; | |
28 import dwt.widgets.Table; | |
29 import dwt.widgets.TableColumn; | |
30 import dwt.widgets.Tree; | |
31 import dwt.widgets.TreeColumn; | |
32 import dwtx.core.runtime.Assert; | |
33 import dwtx.jface.layout.TableColumnLayout; | |
34 | |
35 import dwt.dwthelper.utils; | |
36 | |
37 /** | |
38 * A layout for a table. Call <code>addColumnData</code> to add columns. | |
39 * The TableLayout {@link ColumnLayoutData} is only valid until the table | |
40 * is resized. To keep the proportions constant when the table is resized | |
41 * see {@link TableColumnLayout} | |
42 */ | |
43 public class TableLayout : Layout { | |
44 | |
45 /** | |
46 * The number of extra pixels taken as horizontal trim by the table column. | |
47 * To ensure there are N pixels available for the content of the column, | |
48 * assign N+COLUMN_TRIM for the column width. | |
49 * | |
50 * @since 3.1 | |
51 */ | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
68
diff
changeset
|
52 private static int COLUMN_TRIM; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
68
diff
changeset
|
53 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
68
diff
changeset
|
54 static { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
68
diff
changeset
|
55 if ("win32".equals(DWT.getPlatform())) { //$NON-NLS-1$ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
68
diff
changeset
|
56 COLUMN_TRIM = 4; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
68
diff
changeset
|
57 } else if ("carbon".equals(DWT.getPlatform())) { //$NON-NLS-1$ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
68
diff
changeset
|
58 COLUMN_TRIM = 24; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
68
diff
changeset
|
59 } else { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
68
diff
changeset
|
60 COLUMN_TRIM = 3; |
68 | 61 } |
10 | 62 } |
63 | |
64 /** | |
65 * The list of column layout data (element type: | |
66 * <code>ColumnLayoutData</code>). | |
67 */ | |
68 private Seq!(ColumnLayoutData) columns; | |
69 | |
70 /** | |
71 * Indicates whether <code>layout</code> has yet to be called. | |
72 */ | |
73 private bool firstTime = true; | |
74 | |
75 /** | |
76 * Creates a new table layout. | |
77 */ | |
78 public this() { | |
79 columns = new ArraySeq!(ColumnLayoutData); | |
80 } | |
81 | |
82 /** | |
83 * Adds a new column of data to this table layout. | |
84 * | |
85 * @param data | |
86 * the column layout data | |
87 */ | |
88 public void addColumnData(ColumnLayoutData data) { | |
89 columns.append(data); | |
90 } | |
91 | |
92 /* | |
93 * (non-Javadoc) Method declared on Layout. | |
94 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
95 public override Point computeSize(Composite c, int wHint, int hHint, bool flush) { |
10 | 96 if (wHint !is DWT.DEFAULT && hHint !is DWT.DEFAULT) { |
97 return new Point(wHint, hHint); | |
98 } | |
99 | |
100 Table table = cast(Table) c; | |
101 // To avoid recursions. | |
102 table.setLayout(null); | |
103 // Use native layout algorithm | |
104 Point result = table.computeSize(wHint, hHint, flush); | |
105 table.setLayout(this); | |
106 | |
107 int width = 0; | |
108 int size = columns.size(); | |
109 foreach( layoutData; columns ){ | |
110 if ( auto col = cast(ColumnPixelData) layoutData ) { | |
111 width += col.width; | |
112 if (col.addTrim) { | |
113 width += COLUMN_TRIM; | |
114 } | |
115 } else if ( auto col = cast(ColumnWeightData) layoutData ) { | |
116 width += col.minimumWidth; | |
117 } else { | |
118 Assert.isTrue(false, "Unknown column layout data");//$NON-NLS-1$ | |
119 } | |
120 } | |
121 if (width > result.x) { | |
122 result.x = width; | |
123 } | |
124 return result; | |
125 } | |
126 | |
127 /* | |
128 * (non-Javadoc) Method declared on Layout. | |
129 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
130 public override void layout(Composite c, bool flush) { |
10 | 131 // Only do initial layout. Trying to maintain proportions when resizing |
132 // is too hard, | |
133 // causes lots of widget flicker, causes scroll bars to appear and | |
134 // occasionally stick around (on Windows), | |
135 // requires hooking column resizing as well, and may not be what the | |
136 // user wants anyway. | |
137 if (!firstTime) { | |
138 return; | |
139 } | |
140 | |
141 int width = c.getClientArea().width; | |
142 | |
143 // XXX: Layout is being called with an invalid value the first time | |
144 // it is being called on Linux. This method resets the | |
145 // Layout to null so we make sure we run it only when | |
146 // the value is OK. | |
147 if (width <= 1) { | |
148 return; | |
149 } | |
150 | |
151 Item[] tableColumns = getColumns(c); | |
152 int size = Math.min(columns.size(), tableColumns.length); | |
153 int[] widths = new int[size]; | |
154 int fixedWidth = 0; | |
155 int numberOfWeightColumns = 0; | |
156 int totalWeight = 0; | |
157 | |
158 // First calc space occupied by fixed columns | |
159 for (int i = 0; i < size; i++) { | |
160 ColumnLayoutData col = /+cast(ColumnLayoutData)+/ columns.get(i); | |
161 if ( auto cpd = cast(ColumnPixelData) col ) { | |
162 int pixels = cpd.width; | |
163 if (cpd.addTrim) { | |
164 pixels += COLUMN_TRIM; | |
165 } | |
166 widths[i] = pixels; | |
167 fixedWidth += pixels; | |
168 } else if ( auto cw = cast(ColumnWeightData) col ) { | |
169 numberOfWeightColumns++; | |
170 // first time, use the weight specified by the column data, | |
171 // otherwise use the actual width as the weight | |
172 // int weight = firstTime ? cw.weight : | |
173 // tableColumns[i].getWidth(); | |
174 int weight = cw.weight; | |
175 totalWeight += weight; | |
176 } else { | |
177 Assert.isTrue(false, "Unknown column layout data");//$NON-NLS-1$ | |
178 } | |
179 } | |
180 | |
181 // Do we have columns that have a weight | |
182 if (numberOfWeightColumns > 0) { | |
183 // Now distribute the rest to the columns with weight. | |
184 int rest = width - fixedWidth; | |
185 int totalDistributed = 0; | |
186 for (int i = 0; i < size; ++i) { | |
187 ColumnLayoutData col = /+cast(ColumnLayoutData)+/ columns.get(i); | |
188 if (auto cw = cast(ColumnWeightData) col ) { | |
189 // calculate weight as above | |
190 // int weight = firstTime ? cw.weight : | |
191 // tableColumns[i].getWidth(); | |
192 int weight = cw.weight; | |
193 int pixels = totalWeight is 0 ? 0 : weight * rest | |
194 / totalWeight; | |
195 if (pixels < cw.minimumWidth) { | |
196 pixels = cw.minimumWidth; | |
197 } | |
198 totalDistributed += pixels; | |
199 widths[i] = pixels; | |
200 } | |
201 } | |
202 | |
203 // Distribute any remaining pixels to columns with weight. | |
204 int diff = rest - totalDistributed; | |
205 for (int i = 0; diff > 0; ++i) { | |
206 if (i is size) { | |
207 i = 0; | |
208 } | |
209 ColumnLayoutData col = /+cast(ColumnLayoutData)+/ columns.get(i); | |
210 if (cast(ColumnWeightData)col ) { | |
211 ++widths[i]; | |
212 --diff; | |
213 } | |
214 } | |
215 } | |
216 | |
217 firstTime = false; | |
218 | |
219 for (int i = 0; i < size; i++) { | |
220 setWidth(tableColumns[i], widths[i]); | |
221 } | |
222 } | |
223 | |
224 /** | |
225 * Set the width of the item. | |
226 * | |
227 * @param item | |
228 * @param width | |
229 */ | |
230 private void setWidth(Item item, int width) { | |
231 if ( cast(TreeColumn)item ) { | |
232 (cast(TreeColumn) item).setWidth(width); | |
233 } else { | |
234 (cast(TableColumn) item).setWidth(width); | |
235 } | |
236 | |
237 } | |
238 | |
239 /** | |
240 * Return the columns for the receiver. | |
241 * | |
242 * @param composite | |
243 * @return Item[] | |
244 */ | |
245 private Item[] getColumns(Composite composite) { | |
246 if (cast(Tree)composite ) { | |
247 return (cast(Tree) composite).getColumns(); | |
248 } | |
249 return (cast(Table) composite).getColumns(); | |
250 } | |
251 } |