comparison dwtx/draw2d/ButtonBorder.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 0de61c6f08ca
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.ButtonBorder;
14
15 import dwt.dwthelper.utils;
16
17
18
19 import dwt.graphics.Color;
20 import dwtx.draw2d.geometry.Insets;
21 import dwtx.draw2d.SchemeBorder;
22 import dwtx.draw2d.Border;
23 import dwtx.draw2d.IFigure;
24 import dwtx.draw2d.Graphics;
25 import dwtx.draw2d.Clickable;
26 import dwtx.draw2d.ButtonModel;
27 import dwtx.draw2d.ColorConstants;
28
29 /**
30 * Creates a border for a clickable type of figure, which works in conjunction with the
31 * Figure and its model. This border adjusts itself to the various states the model of the
32 * figure could be. This border uses an extended {@link SchemeBorder.Scheme Scheme}
33 * called {@link ButtonScheme} which provides more information required by border to
34 * handle the the states of the model.
35 *
36 * @see Scheme
37 * @see ButtonScheme
38 */
39 public class ButtonBorder
40 : SchemeBorder
41 {
42 alias SchemeBorder.paint paint;
43 /**
44 * Default button border.
45 * @see SCHEMES#BUTTON
46 */
47 public static const Border BUTTON;
48 /**
49 * Inverted hightlight colors from BUTTON.
50 * @see SCHEMES#BUTTON_CONTRAST
51 */
52 public static const Border BUTTON_CONTRAST;
53 /**
54 * Used for scrollbar buttons.
55 * @see SCHEMES#BUTTON_SCROLLBAR
56 */
57 public static const Border BUTTON_SCROLLBAR;
58 /**
59 * Used for toolbar buttons.
60 * @see SCHEMES#TOOLBAR
61 */
62 public static const Border TOOLBAR;
63
64 static this(){
65 BUTTON = new ButtonBorder(SCHEMES.BUTTON);
66 BUTTON_CONTRAST = new ButtonBorder(SCHEMES.BUTTON_CONTRAST);
67 BUTTON_SCROLLBAR = new ButtonBorder(SCHEMES.BUTTON_SCROLLBAR);
68 TOOLBAR = new ButtonBorder(SCHEMES.TOOLBAR);
69 }
70
71 /**
72 * Provides for a scheme to represent the borders of clickable figures like buttons.
73 * Though similar to the {@link SchemeBorder.Scheme Scheme} it supports an extra set of
74 * borders for the pressed states.
75 */
76 public static class ButtonScheme
77 : Scheme
78 {
79 private Color[]
80 highlightPressed = null,
81 shadowPressed = null;
82
83 /**
84 * Constructs a new button scheme where the input colors are the colors for the
85 * top-left and bottom-right sides of the border. These colors serve as the colors
86 * when the border is in a pressed state too. The width of each side is determined by
87 * the number of colors passed in as input.
88 *
89 * @param highlight Colors for the top-left sides of the border
90 * @param shadow Colors for the bottom-right sides of the border
91 * @since 2.0
92 */
93 public this(Color[] highlight, Color[] shadow) {
94 highlightPressed = this.highlight = highlight;
95 shadowPressed = this.shadow = shadow;
96 init();
97 }
98
99 /**
100 * Constructs a new button scheme where the input colors are the colors for the
101 * top-left and bottom-right sides of the border, for the normal and pressed states.
102 * The width of each side is determined by the number of colors passed in as input.
103 *
104 * @param hl Colors for the top-left sides of the border
105 * @param sh Colors for the bottom-right sides of the border
106 * @param hlp Colors for the top-left sides of the border when figure is pressed
107 * @param shp Colors for the bottom-right sides of the border when figure is pressed
108 * @since 2.0
109 */
110 public this(Color[] hl, Color[] sh, Color[] hlp, Color[] shp) {
111 highlight = hl;
112 shadow = sh;
113 highlightPressed = hlp;
114 shadowPressed = shp;
115 init();
116 }
117
118 /**
119 * Calculates and returns the Insets for this border. The calculations are based on
120 * the number of normal and pressed, highlight and shadow colors.
121 *
122 * @return The insets for this border
123 * @since 2.0
124 */
125 protected Insets calculateInsets() {
126 int br = 1 + Math.max(getShadow().length, getHighlightPressed().length);
127 int tl = Math.max(getHighlight().length, getShadowPressed().length);
128 return new Insets(tl, tl, br, br);
129 }
130
131 /**
132 * Calculates and returns the opaque state of this border.
133 * <p>
134 * Returns false in the following conditions:
135 * <ul>
136 * <li> The number of highlight colors is different than the the number of
137 * shadow colors.
138 * <li> The number of pressed highlight colors is different than the number of
139 * pressed shadow colors.
140 * <li> Any of the highlight and shadow colors are set to <code>null</code>
141 * <li> Any of the pressed highlight and shadow colors are set to
142 * <code>null</code>
143 * </ul>
144 * This is done so that the entire region under the figure is properly covered.
145 *
146 * @return The opaque state of this border
147 * @since 2.0
148 */
149 protected bool calculateOpaque() {
150 if (!super.calculateOpaque())
151 return false;
152 if (getHighlight().length !is getShadowPressed().length)
153 return false;
154 if (getShadow().length !is getHighlightPressed().length)
155 return false;
156 Color [] colors = getHighlightPressed();
157 for (int i = 0; i < colors.length; i++)
158 if (colors[i] is null)
159 return false;
160 colors = getShadowPressed();
161 for (int i = 0; i < colors.length; i++)
162 if (colors[i] is null)
163 return false;
164 return true;
165 }
166
167 /**
168 * Returns the pressed highlight colors of this border.
169 *
170 * @return Colors as an array of Colors
171 * @since 2.0
172 */
173 protected Color[] getHighlightPressed() {
174 return highlightPressed;
175 }
176
177 /**
178 * Returns the pressed shadow colors of this border.
179 *
180 * @return Colors as an array of Colors
181 * @since 2.0
182 */
183 protected Color[] getShadowPressed() {
184 return shadowPressed;
185 }
186 }
187
188 /**
189 * Interface defining commonly used schemes for the ButtonBorder.
190 */
191 public struct SCHEMES {
192
193 /**
194 * Contrast button scheme
195 */
196 static const ButtonScheme BUTTON_CONTRAST;
197 /**
198 * Regular button scheme
199 */
200 static const ButtonScheme BUTTON;
201 /**
202 * Toolbar button scheme
203 */
204 static const ButtonScheme TOOLBAR;
205 /**
206 * Scrollbar button scheme
207 */
208 static const ButtonScheme BUTTON_SCROLLBAR;
209 static this(){
210 BUTTON_CONTRAST = new ButtonScheme(
211 [ColorConstants.button, ColorConstants.buttonLightest],
212 DARKEST_DARKER
213 );
214 BUTTON = new ButtonScheme(
215 [ColorConstants.buttonLightest],
216 DARKEST_DARKER
217 );
218 TOOLBAR = new ButtonScheme(
219 [ColorConstants.buttonLightest],
220 [ColorConstants.buttonDarker]
221 );
222 BUTTON_SCROLLBAR = new ButtonScheme(
223 [ColorConstants.button, ColorConstants.buttonLightest],
224 DARKEST_DARKER,
225 [ColorConstants.buttonDarker],
226 [ColorConstants.buttonDarker]
227 );
228 }
229 }
230
231 /**
232 * Constructs a ButtonBorder with a predefined button scheme set as its default.
233 *
234 * @since 2.0
235 */
236 public this() {
237 setScheme(SCHEMES.BUTTON);
238 }
239
240 /**
241 * Constructs a ButtonBorder with the input ButtonScheme set as its Scheme.
242 *
243 * @param scheme ButtonScheme for this ButtonBorder.
244 * @since 2.0
245 */
246 public this(ButtonScheme scheme) {
247 setScheme(scheme);
248 }
249
250 /**
251 * Paints this border with the help of the set scheme, the model of the clickable figure,
252 * and other inputs. The scheme is used in conjunction with the state of the model to get
253 * the appropriate colors for the border.
254 *
255 * @param figure The Clickable that this border belongs to
256 * @param graphics The graphics used for painting
257 * @param insets The insets
258 */
259 public void paint(IFigure figure, Graphics graphics, Insets insets) {
260 Clickable clickable = cast(Clickable)figure;
261 ButtonModel model = clickable.getModel();
262 ButtonScheme colorScheme = cast(ButtonScheme)getScheme();
263
264 if (clickable.isRolloverEnabled() && !model.isMouseOver()
265 && !model.isSelected())
266 return;
267
268 Color[] tl, br;
269 if (model.isSelected() || model.isArmed()) {
270 tl = colorScheme.getShadowPressed();
271 br = colorScheme.getHighlightPressed();
272 } else {
273 tl = colorScheme.getHighlight();
274 br = colorScheme.getShadow();
275 }
276
277 paint(graphics, figure, insets, tl, br);
278 }
279
280 }