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