comparison 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
comparison
equal deleted inserted replaced
69:07b9d96fd764 70:46a6e0e6ccd4
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2006, 2007 IBM Corporation and others. 2 * Copyright (c) 2006, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation (original file dwtx.ui.texteditor.templates.ColumnLayout) 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) 10 * Tom Schindl <tom.schindl@bestsolution.at> - refactored to be widget independent (bug 171824)
11 * - fix for bug 178280, 184342, 184045 11 * - fix for bug 178280, 184342, 184045, 208014, 214532
12 * Micah Hainline <micah_hainline@yahoo.com> - fix in bug: 208335
12 * Port to the D programming language: 13 * Port to the D programming language:
13 * Frank Benoit <benoit@tionex.de> 14 * Frank Benoit <benoit@tionex.de>
14 *******************************************************************************/ 15 *******************************************************************************/
15 module dwtx.jface.layout.AbstractColumnLayout; 16 module dwtx.jface.layout.AbstractColumnLayout;
16
17 17
18 18
19 import dwt.DWT; 19 import dwt.DWT;
20 import dwt.graphics.Point; 20 import dwt.graphics.Point;
21 import dwt.graphics.Rectangle; 21 import dwt.graphics.Rectangle;
37 /** 37 /**
38 * The AbstractColumnLayout is a {@link Layout} used to set the size of a table 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 39 * in a consistent way even during a resize unlike a {@link TableLayout} which
40 * only sets initial sizes. 40 * only sets initial sizes.
41 * 41 *
42 * <p><b>You can only add the layout to a container whose 42 * <p>
43 * only child is the table/tree control you want the layouts applied to.</b> 43 * <b>You can only add the layout to a container whose only child is the
44 * table/tree control you want the layouts applied to.</b>
44 * </p> 45 * </p>
45 * 46 *
46 * @since 3.3 47 * @since 3.4
47 */ 48 */
48 abstract class AbstractColumnLayout : Layout { 49 public abstract class AbstractColumnLayout : Layout {
49 /**
50 * The number of extra pixels taken as horizontal trim by the table column.
51 * To ensure there are N pixels available for the content of the column,
52 * assign N+COLUMN_TRIM for the column width.
53 *
54 * @since 3.1
55 */
56 private static int COLUMN_TRIM; 50 private static int COLUMN_TRIM;
51 static {
52 if ("win32".equals(DWT.getPlatform())) { //$NON-NLS-1$
53 COLUMN_TRIM = 4;
54 } else if ("carbon".equals(DWT.getPlatform())) { //$NON-NLS-1$
55 COLUMN_TRIM = 24;
56 } else {
57 COLUMN_TRIM = 3;
58 }
59 }
57 60
58 static const bool IS_GTK; 61 static const bool IS_GTK;
59 62
60 static const String LAYOUT_DATA; 63 static const String LAYOUT_DATA;
61 64
94 * @param data 97 * @param data
95 * the column layout data 98 * the column layout data
96 */ 99 */
97 public void setColumnData(Widget column, ColumnLayoutData data) { 100 public void setColumnData(Widget column, ColumnLayoutData data) {
98 101
99 if( column.getData(LAYOUT_DATA) is null ) { 102 if (column.getData(LAYOUT_DATA) is null) {
100 column.addListener(DWT.Resize, resizeListener); 103 column.addListener(DWT.Resize, resizeListener);
101 } 104 }
102 105
103 column.setData(LAYOUT_DATA, data); 106 column.setData(LAYOUT_DATA, data);
104 } 107 }
120 Point result = scrollable.computeSize(wHint, hHint); 123 Point result = scrollable.computeSize(wHint, hHint);
121 124
122 int width = 0; 125 int width = 0;
123 int size = getColumnCount(scrollable); 126 int size = getColumnCount(scrollable);
124 for (int i = 0; i < size; ++i) { 127 for (int i = 0; i < size; ++i) {
125 ColumnLayoutData layoutData = getLayoutData(scrollable,i); 128 ColumnLayoutData layoutData = getLayoutData(scrollable, i);
126 if ( auto col = cast(ColumnPixelData)layoutData) { 129 if ( auto col = cast(ColumnPixelData)layoutData) {
127 width += col.width; 130 width += col.width;
128 if (col.addTrim) { 131 if (col.addTrim) {
129 width += COLUMN_TRIM; 132 width += getColumnTrim();
130 } 133 }
131 } else if ( auto col = cast(ColumnWeightData)layoutData ) { 134 } else if ( auto col = cast(ColumnWeightData)layoutData ) {
132 width += col.minimumWidth; 135 width += col.minimumWidth;
133 } else { 136 } else {
134 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$ 137 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
149 * @param area 152 * @param area
150 * @param increase 153 * @param increase
151 */ 154 */
152 private void layoutTableTree(Scrollable scrollable, int width, 155 private void layoutTableTree(Scrollable scrollable, int width,
153 Rectangle area, bool increase) { 156 Rectangle area, bool increase) {
154 int size = getColumnCount(scrollable); 157 int numberOfColumns = getColumnCount(scrollable);
155 int[] widths = new int[size]; 158 int[] widths = new int[numberOfColumns];
156 159
157 int[] weightIteration = new int[size]; 160 int[] weightColumnIndices = new int[numberOfColumns];
158 int numberOfWeightColumns = 0; 161 int numberOfWeightColumns = 0;
159 162
160 int fixedWidth = 0; 163 int fixedWidth = 0;
161 int minWeightWidth = 0;
162 int totalWeight = 0; 164 int totalWeight = 0;
163 165
164 // First calc space occupied by fixed columns 166 // First calc space occupied by fixed columns
165 for (int i = 0; i < size; i++) { 167 for (int i = 0; i < numberOfColumns; i++) {
166 ColumnLayoutData col = getLayoutData(scrollable,i); 168 ColumnLayoutData col = getLayoutData(scrollable, i);
167 if ( auto cpd = cast(ColumnPixelData)col ) { 169 if ( auto cpd = cast(ColumnPixelData)col ) {
168 int pixels = cpd.width; 170 int pixels = cpd.width;
169 if (cpd.addTrim) { 171 if (cpd.addTrim) {
170 pixels += COLUMN_TRIM; 172 pixels += getColumnTrim();
171 } 173 }
172 widths[i] = pixels; 174 widths[i] = pixels;
173 fixedWidth += pixels; 175 fixedWidth += pixels;
174 } else if ( auto cw = cast(ColumnWeightData) col ) { 176 } else if ( auto cw = cast(ColumnWeightData) col ) {
175 weightIteration[numberOfWeightColumns] = i; 177 weightColumnIndices[numberOfWeightColumns] = i;
176 numberOfWeightColumns++; 178 numberOfWeightColumns++;
177 totalWeight += cw.weight; 179 totalWeight += cw.weight;
178 minWeightWidth += cw.minimumWidth;
179 widths[i] = cw.minimumWidth;
180 } else { 180 } else {
181 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$ 181 Assert.isTrue(false, "Unknown column layout data"); //$NON-NLS-1$
182 } 182 }
183 } 183 }
184 184
185 // Do we have columns that have a weight? 185 bool recalculate;
186 int restIncludingMinWidths = width - fixedWidth; 186 do {
187 int rest = restIncludingMinWidths - minWeightWidth; 187 recalculate = false;
188 if (numberOfWeightColumns > 0 && rest > 0) {
189
190 // Modify the weights to reflect what each column already
191 // has due to its minimum. Otherwise, columns with low
192 // minimums get discriminated.
193 int totalWantedPixels = 0;
194 int[] wantedPixels = new int[numberOfWeightColumns];
195 for (int i = 0; i < numberOfWeightColumns; i++) { 188 for (int i = 0; i < numberOfWeightColumns; i++) {
196 ColumnWeightData cw = cast(ColumnWeightData) getLayoutData(scrollable,weightIteration[i]); 189 int colIndex = weightColumnIndices[i];
197 wantedPixels[i] = totalWeight is 0 ? 0 : cw.weight 190 ColumnWeightData cw = (ColumnWeightData) getLayoutData(
198 * restIncludingMinWidths / totalWeight; 191 scrollable, colIndex);
199 totalWantedPixels += wantedPixels[i]; 192 final int minWidth = cw.minimumWidth;
193 final int allowedWidth = (width - fixedWidth) * cw.weight
194 / totalWeight;
195 if (allowedWidth < minWidth) {
196 /*
197 * if the width assigned by weight is less than the minimum,
198 * then treat this column as fixed, remove it from weight
199 * calculations, and recalculate other weights.
200 */
201 numberOfWeightColumns--;
202 totalWeight -= cw.weight;
203 fixedWidth += minWidth;
204 widths[colIndex] = minWidth;
205 System.arraycopy(weightColumnIndices, i + 1,
206 weightColumnIndices, i, numberOfWeightColumns - i);
207 recalculate = true;
208 break;
209 }
210 widths[colIndex] = allowedWidth;
200 } 211 }
201 212 } while (recalculate);
202 // Now distribute the rest to the columns with weight.
203 int totalDistributed = 0;
204 for (int i = 0; i < numberOfWeightColumns; ++i) {
205 int pixels = totalWantedPixels is 0 ? 0 : wantedPixels[i]
206 * rest / totalWantedPixels;
207 totalDistributed += pixels;
208 widths[weightIteration[i]] += pixels;
209 }
210
211 // Distribute any remaining pixels to columns with weight.
212 int diff = rest - totalDistributed;
213 for (int i = 0; diff > 0; i = ((i + 1) % numberOfWeightColumns)) {
214 ++widths[weightIteration[i]];
215 --diff;
216 }
217 }
218 213
219 if (increase) { 214 if (increase) {
220 scrollable.setSize(area.width, area.height); 215 scrollable.setSize(area.width, area.height);
221 } 216 }
222 217
312 * 307 *
313 * @param widths 308 * @param widths
314 */ 309 */
315 abstract void setColumnWidths(Scrollable tableTree, int[] widths); 310 abstract void setColumnWidths(Scrollable tableTree, int[] widths);
316 311
317 abstract ColumnLayoutData getLayoutData(Scrollable tableTree, int columnIndex); 312 abstract ColumnLayoutData getLayoutData(Scrollable tableTree,
313 int columnIndex);
318 314
319 abstract void updateColumnData(Widget column); 315 abstract void updateColumnData(Widget column);
316
317 /**
318 * The number of extra pixels taken as horizontal trim by the table column.
319 * To ensure there are N pixels available for the content of the column,
320 * assign N+COLUMN_TRIM for the column width.
321 *
322 * @return the trim used by the columns
323 * @since 3.4
324 */
325 protected int getColumnTrim() {
326 return COLUMN_TRIM;
327 }
320 } 328 }