comparison dwtx/draw2d/AbstractHintLayout.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, 2005 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.draw2d.AbstractHintLayout;
14
15 import dwt.dwthelper.utils;
16
17 import dwtx.draw2d.geometry.Dimension;
18 import dwtx.draw2d.AbstractLayout;
19 import dwtx.draw2d.IFigure;
20
21
22 /**
23 * The foundation for layout managers which are sensitive to width and/or height hints.
24 * This class will cache preferred and minimum sizes for a given set of hints. If the
25 * hints change in a meaningful way, the cached size is thrown out and redetermined.
26 * <P>
27 * Subclasses may be sensitive to one or both hints. By default, this class assumes both
28 * hints are important. Subclasses may override this behavior in {@link
29 * #isSensitiveHorizontally(IFigure)} and {@link #isSensitiveVertically(IFigure)}. At
30 * least one of these method should return <code>true</code>.
31 *
32 * @author hudsonr
33 * @since 2.0
34 */
35 public abstract class AbstractHintLayout
36 : AbstractLayout
37 {
38
39 private Dimension minimumSize = null;
40 private Dimension cachedPreferredHint;
41 private Dimension cachedMinimumHint;
42
43 this(){
44 cachedPreferredHint = new Dimension(-1, -1);
45 cachedMinimumHint = new Dimension(-1, -1);
46 }
47 /**
48 * Calculates the minimum size using the given width and height hints. This method is
49 * called from {@link #getMinimumSize(IFigure, int, int)} whenever the cached minimum size
50 * has been flushed.
51 * <P>
52 * By default, this method just calls {@link #getPreferredSize(IFigure, int, int)},
53 * meaning minimum and preferres sizes will be the same unless this method is overridden.
54 *
55 * @param container the Figure on which this layout is installed
56 * @param wHint the width hint
57 * @param hHint the height hint
58 *
59 * @return the layout's minimum size
60 */
61 protected Dimension calculateMinimumSize(IFigure container, int wHint, int hHint) {
62 return getPreferredSize(container, wHint, hHint);
63 }
64
65 /**
66 * @see dwtx.draw2d.LayoutManager#getMinimumSize(IFigure, int, int)
67 */
68 public Dimension getMinimumSize(IFigure container, int w, int h) {
69 bool flush = cachedMinimumHint.width !is w
70 && isSensitiveHorizontally(container);
71 flush |= cachedMinimumHint.height !is h
72 && isSensitiveVertically(container);
73 if (flush) {
74 minimumSize = null;
75 cachedMinimumHint.width = w;
76 cachedMinimumHint.height = h;
77 }
78 if (minimumSize is null)
79 minimumSize = calculateMinimumSize(container, w, h);
80 return minimumSize;
81 }
82
83 /**
84 * @see dwtx.draw2d.LayoutManager#getPreferredSize(IFigure, int, int)
85 */
86 public final Dimension getPreferredSize(IFigure container, int w, int h) {
87 bool flush = cachedPreferredHint.width !is w
88 && isSensitiveHorizontally(container);
89 flush |= cachedPreferredHint.height !is h
90 && isSensitiveVertically(container);
91 if (flush) {
92 preferredSize = null;
93 cachedPreferredHint.width = w;
94 cachedPreferredHint.height = h;
95 }
96 return super.getPreferredSize(container, w, h);
97 }
98
99 /**
100 * Extends the superclass implementation to flush the cached minimum size.
101 * @see dwtx.draw2d.LayoutManager#invalidate()
102 */
103 public void invalidate() {
104 minimumSize = null;
105 super.invalidate();
106 }
107
108 /**
109 * Returns whether this layout manager is sensitive to changes in the horizontal hint. By
110 * default, this method returns <code>true</code>.
111 * @param container the layout's container
112 * @return <code>true</code> if this layout is sensite to horizontal hint changes
113 */
114 protected bool isSensitiveHorizontally(IFigure container) {
115 return true;
116 }
117
118 /**
119 * Returns whether this layout manager is sensitive to changes in the vertical hint. By
120 * default, this method returns <code>true</code>.
121 * @param container the layout's container
122 * @return <code>true</code> if this layout is sensite to vertical hint changes
123 */
124 protected bool isSensitiveVertically(IFigure container) {
125 return true;
126 }
127
128 }