comparison dwtx/draw2d/ScrollBarLayout.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.ScrollBarLayout;
14
15 import dwt.dwthelper.utils;
16
17 import dwtx.draw2d.geometry.Dimension;
18 import dwtx.draw2d.geometry.Insets;
19 import dwtx.draw2d.geometry.Rectangle;
20 import dwtx.draw2d.geometry.Transposer;
21
22 import dwtx.draw2d.AbstractLayout;
23 import dwtx.draw2d.IFigure;
24 import dwtx.draw2d.ScrollBar;
25
26 /**
27 * Lays out the Figures that make up a ScrollBar.
28 */
29 public class ScrollBarLayout
30 : AbstractLayout
31 {
32
33 /** Used as a constraint for the up arrow figure. */
34 public static final String UP_ARROW = "up arrow"; //$NON-NLS-1$
35 /** Used as a constraint for the down arrow figure. */
36 public static final String DOWN_ARROW = "down arrow"; //$NON-NLS-1$
37 /** Used as a constraint for the thumb figure. */
38 public static final String THUMB = "thumb"; //$NON-NLS-1$
39 /** Used as a constraint for the page up figure. */
40 public static final String PAGE_UP = "page_up"; //$NON-NLS-1$
41 /** Used as a constraint for the page down figure. */
42 public static final String PAGE_DOWN = "page_down"; //$NON-NLS-1$
43
44 IFigure up, down, thumb, pageUp, pageDown;
45
46 /**
47 * Transposes values if the ScrollBar is horizontally oriented. When used properly, the
48 * layout manager just needs to code for one case: vertical orientation.
49 */
50 protected final Transposer transposer;
51
52 /**
53 * Constructs a ScrollBarLayout. If the given Transposer is enabled, the Scrollbar will
54 * be horizontally oriented. Otherwise, the ScrollBar will be vertically oriented.
55 *
56 * @param t the Transposer
57 * @since 2.0
58 */
59 public this(Transposer t) {
60 transposer = t;
61 }
62
63 /**
64 * @see AbstractLayout#setConstraint(IFigure, Object)
65 */
66 public void setConstraint(IFigure figure, Object constraint_) {
67 String constraint = stringcast(constraint_);
68 if (constraint.equals(UP_ARROW))
69 up = figure;
70 else if (constraint.equals(DOWN_ARROW))
71 down = figure;
72 else if (constraint.equals(THUMB))
73 thumb = figure;
74 else if (constraint.equals(PAGE_UP))
75 pageUp = figure;
76 else if (constraint.equals(PAGE_DOWN))
77 pageDown = figure;
78 }
79
80 /**
81 * @see AbstractLayout#calculatePreferredSize(IFigure, int, int)
82 */
83 protected Dimension calculatePreferredSize(IFigure parent, int w, int h) {
84 Insets insets = transposer.t(parent.getInsets());
85 Dimension d = new Dimension(16, 16 * 4);
86 d.expand(insets.getWidth(), insets.getHeight());
87 return transposer.t(d);
88 }
89
90 /**
91 * @see LayoutManager#layout(IFigure)
92 */
93 public void layout(IFigure parent) {
94 ScrollBar scrollBar = cast(ScrollBar) parent;
95
96 Rectangle trackBounds = layoutButtons(scrollBar);
97
98 int extent = scrollBar.getExtent();
99 int max = scrollBar.getMaximum();
100 int min = scrollBar.getMinimum();
101 int totalRange = max - min;
102 int valueRange = totalRange - extent;
103 if ((valueRange < 1) || (!scrollBar.isEnabled())) {
104 Rectangle boundsUpper = new Rectangle(trackBounds),
105 boundsLower = new Rectangle(trackBounds);
106 boundsUpper.height /= 2;
107 boundsLower.y += boundsUpper.height;
108 boundsLower.height = trackBounds.height - boundsUpper.height;
109 if (pageUp !is null)
110 pageUp.setBounds(transposer.t(boundsUpper));
111 if (pageDown !is null)
112 pageDown.setBounds(transposer.t(boundsLower));
113 return;
114 }
115
116 if (totalRange is 0)
117 return;
118 int thumbHeight = Math.max(thumb is null ? 0 : thumb.getMinimumSize().height,
119 trackBounds.height * extent / totalRange);
120
121 if (thumb !is null)
122 thumb.setVisible(trackBounds.height > thumbHeight);
123
124 int thumbY = trackBounds.y + (trackBounds.height - thumbHeight)
125 * (scrollBar.getValue() - min) / valueRange;
126
127 Rectangle thumbBounds = new Rectangle(
128 trackBounds.x,
129 thumbY,
130 trackBounds.width,
131 thumbHeight);
132
133 if (thumb !is null)
134 thumb.setBounds(transposer.t(thumbBounds));
135
136 if (pageUp !is null)
137 pageUp.setBounds(transposer.t(new Rectangle(
138 trackBounds.x,
139 trackBounds.y,
140 trackBounds.width,
141 thumbBounds.y - trackBounds.y)));
142
143 if (pageDown !is null)
144 pageDown.setBounds(transposer.t(new Rectangle(
145 trackBounds.x ,
146 thumbBounds.y + thumbHeight,
147 trackBounds.width,
148 trackBounds.bottom() - thumbBounds.bottom())));
149 }
150
151 /**
152 * Places the buttons and returns the Rectangle into which the track should be placed.
153 * The track consists of the pageup, pagedown, and thumb figures. The Rectangle returned
154 * should be transposed correctly, that is, it should be vertically oriented. Users of
155 * the rectangle will re-transpose it for horizontal use.
156 *
157 * @param scrollBar the scrollbar whose buttons are being layed out
158 * @return the Rectangle into which the track should be placed
159 * @since 2.0
160 */
161 protected Rectangle layoutButtons(ScrollBar scrollBar) {
162 Rectangle bounds = transposer.t(scrollBar.getClientArea());
163 Dimension buttonSize = new Dimension(
164 bounds.width,
165 Math.min(bounds.width, bounds.height / 2));
166
167 if (up !is null)
168 up.setBounds(transposer.t(
169 new Rectangle(bounds.getTopLeft(), buttonSize)));
170 if (down !is null) {
171 Rectangle r = new Rectangle (
172 bounds.x, bounds.bottom() - buttonSize.height,
173 buttonSize.width, buttonSize.height);
174 down.setBounds(transposer.t(r));
175 }
176
177 Rectangle trackBounds = bounds.getCropped(
178 new Insets(
179 (up is null) ? 0 : buttonSize.height, 0,
180 (down is null) ? 0 : buttonSize.height, 0));
181
182 return trackBounds;
183 }
184
185 /**
186 * @see LayoutManager#remove(IFigure)
187 */
188 public void remove(IFigure child) {
189 if (child is up) {
190 up = null;
191 } else if (child is down) {
192 down = null;
193 } else if (child is thumb) {
194 thumb = null;
195 } else if (child is pageUp) {
196 pageUp = null;
197 } else if (child is pageDown) {
198 pageDown = null;
199 }
200 }
201 }