comparison dwtx/draw2d/GridData.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
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 * Asim Ullah - Ported for use in draw2d (c.f Bugzilla 71684).[Sep 10, 2004]
11 * Port to the D programming language:
12 * Frank Benoit <benoit@tionex.de>
13 *******************************************************************************/
14 module dwtx.draw2d.GridData;
15
16 import dwt.dwthelper.utils;
17
18
19 import dwt.DWT;
20 import dwtx.draw2d.geometry.Dimension;
21 import dwtx.draw2d.IFigure;
22
23 /**
24 * <code>GridData</code> is the layout data object associated with
25 * <code>GridLayout</code>. To set a <code>GridData</code> object into a
26 * <code>Figure</code>, you use the <code>setConstraint()</code> method of
27 * <code>GridLayout</code> to map the <code>Figure</code> to its layout
28 * <code>GridData</code>.
29 * <p>
30 * There are two ways to create a <code>GridData</code> object with certain
31 * fields set. The first is to set the fields directly, like this:
32 *
33 * <pre>
34 * GridData gridData = new GridData();
35 * gridData.horizontalAlignment = GridData.FILL;
36 * gridData.grabExcessHorizontalSpace = true;
37 *
38 * // associate the figure to the GridData object
39 * myGridlayout.setConstraint(myFigure, gridData);
40 * </pre>
41 *
42 * The second is to take advantage of convenience style bits defined by
43 * <code>GridData</code>:
44 *
45 * <pre>
46 * GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
47 * | GridData.GRAB_HORIZONTAL);
48 * </pre>
49 *
50 * </p>
51 * <p>
52 * NOTE: Do not reuse <code>GridData</code> objects. Every child in the parent
53 * <code>Figure</code> that is managed by the <code>GridLayout</code> must
54 * have a unique <code>GridData</code> object. If the layout data for a Grid
55 * member in a <code>GridLayout</code> is null at layout time, a unique
56 * <code>GridData</code> object is created for it.
57 * </p>
58 *
59 * @see GridLayout
60 */
61 public final class GridData {
62 /**
63 * verticalAlignment specifies how figures will be positioned vertically
64 * within a cell.
65 *
66 * The default value is CENTER.
67 *
68 * Possible values are:
69 *
70 * DWT.BEGINNING (or DWT.TOP): Position the figure at the top of the cell
71 * DWT.CENTER: Position the figure in the vertical center of the cell
72 * DWT.END (or DWT.BOTTOM): Position the figure at the bottom of the cell
73 * DWT.FILL: Resize the figure to fill the cell vertically
74 */
75 public int verticalAlignment = CENTER;
76
77 /**
78 * horizontalAlignment specifies how figures will be positioned horizontally
79 * within a cell.
80 *
81 * The default value is BEGINNING.
82 *
83 * Possible values are:
84 *
85 * DWT.BEGINNING (or DWT.LEFT): Position the figure at the left of the cell
86 * DWT.CENTER: Position the figure in the horizontal center of the cell
87 * DWT.END (or DWT.RIGHT): Position the figure at the right of the cell
88 * DWT.FILL: Resize the figure to fill the cell horizontally
89 */
90 public int horizontalAlignment = BEGINNING;
91
92 /**
93 * widthHint specifies a minimum width for the column. A value of
94 * DWT.DEFAULT indicates that no minimum width is specified.
95 *
96 * The default value is DWT.DEFAULT.
97 */
98 public int widthHint = DWT.DEFAULT;
99
100 /**
101 * heightHint specifies a minimum height for the row. A value of DWT.DEFAULT
102 * indicates that no minimum height is specified.
103 *
104 * The default value is DWT.DEFAULT.
105 */
106 public int heightHint = DWT.DEFAULT;
107
108 /**
109 * horizontalIndent specifies the number of pixels of indentation that will
110 * be placed along the left side of the cell.
111 *
112 * The default value is 0.
113 */
114 public int horizontalIndent = 0;
115
116 /**
117 * horizontalSpan specifies the number of column cells that the figure will
118 * take up.
119 *
120 * The default value is 1.
121 */
122 public int horizontalSpan = 1;
123
124 /**
125 * verticalSpan specifies the number of row cells that the figure will take
126 * up.
127 *
128 * The default value is 1.
129 */
130 public int verticalSpan = 1;
131
132 /**
133 * grabExcessHorizontalSpace specifies whether the cell will be made wide
134 * enough to fit the remaining horizontal space.
135 *
136 * The default value is false.
137 */
138 public bool grabExcessHorizontalSpace = false;
139
140 /**
141 * grabExcessVerticalSpace specifies whether the cell will be made tall
142 * enough to fit the remaining vertical space.
143 *
144 * The default value is false.
145 */
146 public bool grabExcessVerticalSpace = false;
147
148 /**
149 * Value for horizontalAlignment or verticalAlignment. Position the figure
150 * at the top or left of the cell. Not recommended. Use DWT.BEGINNING,
151 * DWT.TOP or DWT.LEFT instead.
152 */
153 public static const int BEGINNING = DWT.BEGINNING;
154
155 /**
156 * Value for horizontalAlignment or verticalAlignment. Position the figure
157 * in the vertical or horizontal center of the cell Not recommended. Use
158 * DWT.CENTER instead.
159 */
160 public static const int CENTER = 2;
161
162 /**
163 * Value for horizontalAlignment or verticalAlignment. Position the figure
164 * at the bottom or right of the cell Not recommended. Use DWT.END,
165 * DWT.BOTTOM or DWT.RIGHT instead.
166 */
167 public static const int END = 3;
168
169 /**
170 * Value for horizontalAlignment or verticalAlignment. Resize the figure to
171 * fill the cell horizontally or vertically. Not recommended. Use DWT.FILL
172 * instead.
173 */
174 public static const int FILL = DWT.FILL;
175
176 /**
177 * Style bit for <code>new GridData(int)</code>. Position the figure at
178 * the top of the cell. Not recommended. Use
179 * <code>new GridData(int, DWT.BEGINNING, bool, bool)</code>
180 * instead.
181 */
182 public static const int VERTICAL_ALIGN_BEGINNING = 1 << 1;
183
184 /**
185 * Style bit for <code>new GridData(int)</code> to position the figure in
186 * the vertical center of the cell. Not recommended. Use
187 * <code>new GridData(int, DWT.CENTER, bool, bool)</code> instead.
188 */
189 public static const int VERTICAL_ALIGN_CENTER = 1 << 2;
190
191 /**
192 * Style bit for <code>new GridData(int)</code> to position the figure at
193 * the bottom of the cell. Not recommended. Use
194 * <code>new GridData(int, DWT.END, bool, bool)</code> instead.
195 */
196 public static const int VERTICAL_ALIGN_END = 1 << 3;
197
198 /**
199 * Style bit for <code>new GridData(int)</code> to resize the figure to
200 * fill the cell vertically. Not recommended. Use
201 * <code>new GridData(int, DWT.FILL, bool, bool)</code> instead
202 */
203 public static const int VERTICAL_ALIGN_FILL = 1 << 4;
204
205 /**
206 * Style bit for <code>new GridData(int)</code> to position the figure at
207 * the left of the cell. Not recommended. Use
208 * <code>new GridData(DWT.BEGINNING, int, bool, bool)</code>
209 * instead.
210 */
211 public static const int HORIZONTAL_ALIGN_BEGINNING = 1 << 5;
212
213 /**
214 * Style bit for <code>new GridData(int)</code> to position the figure in
215 * the horizontal center of the cell. Not recommended. Use
216 * <code>new GridData(DWT.CENTER, int, bool, bool)</code> instead.
217 */
218 public static const int HORIZONTAL_ALIGN_CENTER = 1 << 6;
219
220 /**
221 * Style bit for <code>new GridData(int)</code> to position the figure at
222 * the right of the cell. Not recommended. Use
223 * <code>new GridData(DWT.END, int, bool, bool)</code> instead.
224 */
225 public static const int HORIZONTAL_ALIGN_END = 1 << 7;
226
227 /**
228 * Style bit for <code>new GridData(int)</code> to resize the figure to
229 * fill the cell horizontally. Not recommended. Use
230 * <code>new GridData(DWT.FILL, int, bool, bool)</code> instead.
231 */
232 public static const int HORIZONTAL_ALIGN_FILL = 1 << 8;
233
234 /**
235 * Style bit for <code>new GridData(int)</code> to resize the figure to
236 * fit the remaining horizontal space. Not recommended. Use
237 * <code>new GridData(int, int, true, bool)</code> instead.
238 */
239 public static const int GRAB_HORIZONTAL = 1 << 9;
240
241 /**
242 * Style bit for <code>new GridData(int)</code> to resize the figure to
243 * fit the remaining vertical space. Not recommended. Use
244 * <code>new GridData(int, int, bool, true)</code> instead.
245 */
246 public static const int GRAB_VERTICAL = 1 << 10;
247
248 /**
249 * Style bit for <code>new GridData(int)</code> to resize the figure to
250 * fill the cell vertically and to fit the remaining vertical space.
251 * FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL Not recommended. Use
252 * <code>new GridData(int, DWT.FILL, bool, true)</code> instead.
253 */
254 public static const int FILL_VERTICAL = VERTICAL_ALIGN_FILL | GRAB_VERTICAL;
255
256 /**
257 * Style bit for <code>new GridData(int)</code> to resize the figure to
258 * fill the cell horizontally and to fit the remaining horizontal space.
259 * FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL Not
260 * recommended. Use <code>new GridData(DWT.FILL, int, true, bool)</code>
261 * instead.
262 */
263 public static const int FILL_HORIZONTAL = HORIZONTAL_ALIGN_FILL
264 | GRAB_HORIZONTAL;
265
266 /**
267 * Style bit for <code>new GridData(int)</code> to resize the figure to
268 * fill the cell horizontally and vertically and to fit the remaining
269 * horizontal and vertical space. FILL_BOTH = FILL_VERTICAL |
270 * FILL_HORIZONTAL Not recommended. Use
271 * <code>new GridData(DWT.FILL, DWT.FILL, true, true)</code> instead.
272 */
273 public static const int FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL;
274
275 int cacheWidth = -1, cacheHeight = -1;
276 int[][] cache;
277 int cacheIndex = -1;
278
279 /**
280 * Constructs a new instance of GridData using default values.
281 */
282 public this() {
283 cache = new int[][](2,4);
284 }
285
286 /**
287 * Constructs a new instance based on the GridData style. This constructor
288 * is not recommended.
289 *
290 * @param style
291 * the GridData style
292 */
293 public this(int style) {
294 this();
295 if ((style & VERTICAL_ALIGN_BEGINNING) !is 0)
296 verticalAlignment = BEGINNING;
297 if ((style & VERTICAL_ALIGN_CENTER) !is 0)
298 verticalAlignment = CENTER;
299 if ((style & VERTICAL_ALIGN_FILL) !is 0)
300 verticalAlignment = FILL;
301 if ((style & VERTICAL_ALIGN_END) !is 0)
302 verticalAlignment = END;
303 if ((style & HORIZONTAL_ALIGN_BEGINNING) !is 0)
304 horizontalAlignment = BEGINNING;
305 if ((style & HORIZONTAL_ALIGN_CENTER) !is 0)
306 horizontalAlignment = CENTER;
307 if ((style & HORIZONTAL_ALIGN_FILL) !is 0)
308 horizontalAlignment = FILL;
309 if ((style & HORIZONTAL_ALIGN_END) !is 0)
310 horizontalAlignment = END;
311 grabExcessHorizontalSpace = (style & GRAB_HORIZONTAL) !is 0;
312 grabExcessVerticalSpace = (style & GRAB_VERTICAL) !is 0;
313 }
314
315 /**
316 * Constructs a new instance of GridData according to the parameters.
317 *
318 * @param horizontalAlignment
319 * how figure will be positioned horizontally within a cell
320 * @param verticalAlignment
321 * how figure will be positioned vertically within a cell
322 * @param grabExcessHorizontalSpace
323 * whether cell will be made wide enough to fit the remaining
324 * horizontal space
325 * @param grabExcessVerticalSpace
326 * whether cell will be made high enough to fit the remaining
327 * vertical space
328 *
329 */
330 public this(int horizontalAlignment, int verticalAlignment,
331 bool grabExcessHorizontalSpace, bool grabExcessVerticalSpace) {
332 this(horizontalAlignment, verticalAlignment, grabExcessHorizontalSpace,
333 grabExcessVerticalSpace, 1, 1);
334 }
335
336 /**
337 * Constructs a new instance of GridData according to the parameters.
338 *
339 * @param horizontalAlignment
340 * how figure will be positioned horizontally within a cell
341 * @param verticalAlignment
342 * how figure will be positioned vertically within a cell
343 * @param grabExcessHorizontalSpace
344 * whether cell will be made wide enough to fit the remaining
345 * horizontal space
346 * @param grabExcessVerticalSpace
347 * whether cell will be made high enough to fit the remaining
348 * vertical space
349 * @param horizontalSpan
350 * the number of column cells that the figure will take up
351 * @param verticalSpan
352 * the number of row cells that the figure will take up
353 *
354 */
355 public this(int horizontalAlignment, int verticalAlignment,
356 bool grabExcessHorizontalSpace, bool grabExcessVerticalSpace,
357 int horizontalSpan, int verticalSpan) {
358 this();
359 this.horizontalAlignment = horizontalAlignment;
360 this.verticalAlignment = verticalAlignment;
361 this.grabExcessHorizontalSpace = grabExcessHorizontalSpace;
362 this.grabExcessVerticalSpace = grabExcessVerticalSpace;
363 this.horizontalSpan = horizontalSpan;
364 this.verticalSpan = verticalSpan;
365 }
366
367 /**
368 * Constructs a new instance of GridData according to the parameters. A
369 * value of DWT.DEFAULT indicates that no minimum width or no minumum height
370 * is specified.
371 *
372 * @param width
373 * a minimum width for the column
374 * @param height
375 * a minimum height for the row
376 *
377 */
378 public this(int width, int height) {
379 this();
380 this.widthHint = width;
381 this.heightHint = height;
382 }
383
384 Dimension computeSize(IFigure figure, bool flushCache) {
385 if (cacheWidth !is -1 && cacheHeight !is -1) {
386 return new Dimension(cacheWidth, cacheHeight);
387 }
388 for (int i = 0; i < cacheIndex + 1; i++) {
389 if (cache[i][0] is widthHint && cache[i][1] is heightHint) {
390 cacheWidth = cache[i][2];
391 cacheHeight = cache[i][3];
392 return new Dimension(cacheWidth, cacheHeight);
393 }
394 }
395
396 Dimension size = figure.getPreferredSize(widthHint, heightHint);
397 if (widthHint !is -1)
398 size.width = widthHint;
399 if (heightHint !is -1)
400 size.height = heightHint;
401
402 if (cacheIndex < cache.length - 1)
403 cacheIndex++;
404 cache[cacheIndex][0] = widthHint;
405 cache[cacheIndex][1] = heightHint;
406 cacheWidth = cache[cacheIndex][2] = size.width;
407 cacheHeight = cache[cacheIndex][3] = size.height;
408 return size;
409 }
410
411 void flushCache() {
412 cacheWidth = cacheHeight = -1;
413 cacheIndex = -1;
414 }
415
416 String getName() {
417 String string = this.classinfo.name;
418 int index = string.lastIndexOf('.');
419 if (index is -1)
420 return string;
421 return string.substring(index + 1, string.length);
422 }
423
424 public String toString() {
425
426 String hAlign = ""; //$NON-NLS-1$
427 switch (horizontalAlignment) {
428 case DWT.FILL:
429 hAlign = "DWT.FILL"; //$NON-NLS-1$
430 break;
431 case DWT.BEGINNING:
432 hAlign = "DWT.BEGINNING"; //$NON-NLS-1$
433 break;
434 case DWT.LEFT:
435 hAlign = "DWT.LEFT"; //$NON-NLS-1$
436 break;
437 case DWT.END:
438 hAlign = "DWT.END"; //$NON-NLS-1$
439 break;
440 case END:
441 hAlign = "GridData.END"; //$NON-NLS-1$
442 break;
443 case DWT.RIGHT:
444 hAlign = "DWT.RIGHT"; //$NON-NLS-1$
445 break;
446 case DWT.CENTER:
447 hAlign = "DWT.CENTER"; //$NON-NLS-1$
448 break;
449 case CENTER:
450 hAlign = "GridData.CENTER"; //$NON-NLS-1$
451 break;
452 default:
453 hAlign = "Undefined " ~ Integer.toString(horizontalAlignment); //$NON-NLS-1$
454 break;
455 }
456 String vAlign = ""; //$NON-NLS-1$
457 switch (verticalAlignment) {
458 case DWT.FILL:
459 vAlign = "DWT.FILL"; //$NON-NLS-1$
460 break;
461 case DWT.BEGINNING:
462 vAlign = "DWT.BEGINNING"; //$NON-NLS-1$
463 break;
464 case DWT.TOP:
465 vAlign = "DWT.TOP"; //$NON-NLS-1$
466 break;
467 case DWT.END:
468 vAlign = "DWT.END"; //$NON-NLS-1$
469 break;
470 case END:
471 vAlign = "GridData.END"; //$NON-NLS-1$
472 break;
473 case DWT.BOTTOM:
474 vAlign = "DWT.BOTTOM"; //$NON-NLS-1$
475 break;
476 case DWT.CENTER:
477 vAlign = "DWT.CENTER"; //$NON-NLS-1$
478 break;
479 case CENTER:
480 vAlign = "GridData.CENTER"; //$NON-NLS-1$
481 break;
482 default:
483 vAlign = "Undefined " ~ Integer.toString(verticalAlignment); //$NON-NLS-1$
484 break;
485 }
486 String string = getName() ~ " {"; //$NON-NLS-1$
487 string ~= "horizontalAlignment=" ~ hAlign ~ " "; //$NON-NLS-1$ //$NON-NLS-2$
488 if (horizontalIndent !is 0)
489 string ~= "horizontalIndent=" ~ Integer.toString(horizontalIndent) ~ " "; //$NON-NLS-1$ //$NON-NLS-2$
490 if (horizontalSpan !is 1)
491 string ~= "horizontalSpan=" ~ Integer.toString(horizontalSpan) ~ " "; //$NON-NLS-1$//$NON-NLS-2$
492 if (grabExcessHorizontalSpace)
493 string ~= "grabExcessHorizontalSpace=" ~ Integer.toString(grabExcessHorizontalSpace) //$NON-NLS-1$
494 ~ " "; //$NON-NLS-1$
495 if (widthHint !is DWT.DEFAULT)
496 string ~= "widthHint=" ~ Integer.toString(widthHint) ~ " "; //$NON-NLS-1$ //$NON-NLS-2$
497 string ~= "verticalAlignment=" ~ vAlign ~ " "; //$NON-NLS-1$ //$NON-NLS-2$
498 if (verticalSpan !is 1)
499 string ~= "verticalSpan=" ~ Integer.toString(verticalSpan) ~ " "; //$NON-NLS-1$ //$NON-NLS-2$
500 if (grabExcessVerticalSpace)
501 string ~= "grabExcessVerticalSpace=" ~ Integer.toString(grabExcessVerticalSpace) //$NON-NLS-1$
502 ~ " "; //$NON-NLS-1$
503 if (heightHint !is DWT.DEFAULT)
504 string ~= "heightHint=" ~ Integer.toString(heightHint) ~ " "; //$NON-NLS-1$ //$NON-NLS-2$
505 string = string.trim();
506 string ~= "}"; //$NON-NLS-1$
507 return string;
508
509 }
510
511 }