Mercurial > projects > dwt2
comparison org.eclipse.draw2d/src/org/eclipse/draw2d/ButtonBorder.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
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 org.eclipse.draw2d.ButtonBorder; | |
14 | |
15 import java.lang.all; | |
16 | |
17 | |
18 | |
19 import org.eclipse.swt.graphics.Color; | |
20 import org.eclipse.draw2d.geometry.Insets; | |
21 import org.eclipse.draw2d.SchemeBorder; | |
22 import org.eclipse.draw2d.Border; | |
23 import org.eclipse.draw2d.IFigure; | |
24 import org.eclipse.draw2d.Graphics; | |
25 import org.eclipse.draw2d.Clickable; | |
26 import org.eclipse.draw2d.ButtonModel; | |
27 import org.eclipse.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 private static Border BUTTON_; | |
48 public static Border BUTTON(){ | |
49 if( BUTTON_ is null ){ | |
50 synchronized( ButtonScheme.classinfo ){ | |
51 if( BUTTON_ is null ){ | |
52 BUTTON_ = new ButtonBorder(SCHEMES.BUTTON); | |
53 } | |
54 } | |
55 } | |
56 return BUTTON_; | |
57 } | |
58 /** | |
59 * Inverted hightlight colors from BUTTON. | |
60 * @see SCHEMES#BUTTON_CONTRAST | |
61 */ | |
62 private static Border BUTTON_CONTRAST_; | |
63 public static Border BUTTON_CONTRAST(){ | |
64 if( BUTTON_CONTRAST_ is null ){ | |
65 synchronized( ButtonScheme.classinfo ){ | |
66 if( BUTTON_CONTRAST_ is null ){ | |
67 BUTTON_CONTRAST_ = new ButtonBorder(SCHEMES.BUTTON_CONTRAST); | |
68 } | |
69 } | |
70 } | |
71 return BUTTON_CONTRAST_; | |
72 } | |
73 /** | |
74 * Used for scrollbar buttons. | |
75 * @see SCHEMES#BUTTON_SCROLLBAR | |
76 */ | |
77 private static Border BUTTON_SCROLLBAR_; | |
78 public static Border BUTTON_SCROLLBAR(){ | |
79 if( BUTTON_SCROLLBAR_ is null ){ | |
80 synchronized( ButtonScheme.classinfo ){ | |
81 if( BUTTON_SCROLLBAR_ is null ){ | |
82 BUTTON_SCROLLBAR_ = new ButtonBorder(SCHEMES.BUTTON_SCROLLBAR); | |
83 } | |
84 } | |
85 } | |
86 return BUTTON_SCROLLBAR_; | |
87 } | |
88 /** | |
89 * Used for toolbar buttons. | |
90 * @see SCHEMES#TOOLBAR | |
91 */ | |
92 private static Border TOOLBAR_; | |
93 public static Border TOOLBAR(){ | |
94 if( TOOLBAR_ is null ){ | |
95 synchronized( ButtonScheme.classinfo ){ | |
96 if( TOOLBAR_ is null ){ | |
97 TOOLBAR_ = new ButtonBorder(SCHEMES.TOOLBAR); | |
98 } | |
99 } | |
100 } | |
101 return TOOLBAR_; | |
102 } | |
103 | |
104 /** | |
105 * Provides for a scheme to represent the borders of clickable figures like buttons. | |
106 * Though similar to the {@link SchemeBorder.Scheme Scheme} it supports an extra set of | |
107 * borders for the pressed states. | |
108 */ | |
109 public static class ButtonScheme | |
110 : Scheme | |
111 { | |
112 private Color[] | |
113 highlightPressed = null, | |
114 shadowPressed = null; | |
115 | |
116 /** | |
117 * Constructs a new button scheme where the input colors are the colors for the | |
118 * top-left and bottom-right sides of the border. These colors serve as the colors | |
119 * when the border is in a pressed state too. The width of each side is determined by | |
120 * the number of colors passed in as input. | |
121 * | |
122 * @param highlight Colors for the top-left sides of the border | |
123 * @param shadow Colors for the bottom-right sides of the border | |
124 * @since 2.0 | |
125 */ | |
126 public this(Color[] highlight, Color[] shadow) { | |
127 highlightPressed = this.highlight = highlight; | |
128 shadowPressed = this.shadow = shadow; | |
129 init(); | |
130 } | |
131 | |
132 /** | |
133 * Constructs a new button scheme where the input colors are the colors for the | |
134 * top-left and bottom-right sides of the border, for the normal and pressed states. | |
135 * The width of each side is determined by the number of colors passed in as input. | |
136 * | |
137 * @param hl Colors for the top-left sides of the border | |
138 * @param sh Colors for the bottom-right sides of the border | |
139 * @param hlp Colors for the top-left sides of the border when figure is pressed | |
140 * @param shp Colors for the bottom-right sides of the border when figure is pressed | |
141 * @since 2.0 | |
142 */ | |
143 public this(Color[] hl, Color[] sh, Color[] hlp, Color[] shp) { | |
144 highlight = hl; | |
145 shadow = sh; | |
146 highlightPressed = hlp; | |
147 shadowPressed = shp; | |
148 init(); | |
149 } | |
150 | |
151 /** | |
152 * Calculates and returns the Insets for this border. The calculations are based on | |
153 * the number of normal and pressed, highlight and shadow colors. | |
154 * | |
155 * @return The insets for this border | |
156 * @since 2.0 | |
157 */ | |
158 protected Insets calculateInsets() { | |
159 int br = 1 + Math.max(getShadow().length, getHighlightPressed().length); | |
160 int tl = Math.max(getHighlight().length, getShadowPressed().length); | |
161 return new Insets(tl, tl, br, br); | |
162 } | |
163 | |
164 /** | |
165 * Calculates and returns the opaque state of this border. | |
166 * <p> | |
167 * Returns false in the following conditions: | |
168 * <ul> | |
169 * <li> The number of highlight colors is different than the the number of | |
170 * shadow colors. | |
171 * <li> The number of pressed highlight colors is different than the number of | |
172 * pressed shadow colors. | |
173 * <li> Any of the highlight and shadow colors are set to <code>null</code> | |
174 * <li> Any of the pressed highlight and shadow colors are set to | |
175 * <code>null</code> | |
176 * </ul> | |
177 * This is done so that the entire region under the figure is properly covered. | |
178 * | |
179 * @return The opaque state of this border | |
180 * @since 2.0 | |
181 */ | |
182 protected bool calculateOpaque() { | |
183 if (!super.calculateOpaque()) | |
184 return false; | |
185 if (getHighlight().length !is getShadowPressed().length) | |
186 return false; | |
187 if (getShadow().length !is getHighlightPressed().length) | |
188 return false; | |
189 Color [] colors = getHighlightPressed(); | |
190 for (int i = 0; i < colors.length; i++) | |
191 if (colors[i] is null) | |
192 return false; | |
193 colors = getShadowPressed(); | |
194 for (int i = 0; i < colors.length; i++) | |
195 if (colors[i] is null) | |
196 return false; | |
197 return true; | |
198 } | |
199 | |
200 /** | |
201 * Returns the pressed highlight colors of this border. | |
202 * | |
203 * @return Colors as an array of Colors | |
204 * @since 2.0 | |
205 */ | |
206 protected Color[] getHighlightPressed() { | |
207 return highlightPressed; | |
208 } | |
209 | |
210 /** | |
211 * Returns the pressed shadow colors of this border. | |
212 * | |
213 * @return Colors as an array of Colors | |
214 * @since 2.0 | |
215 */ | |
216 protected Color[] getShadowPressed() { | |
217 return shadowPressed; | |
218 } | |
219 } | |
220 | |
221 /** | |
222 * Interface defining commonly used schemes for the ButtonBorder. | |
223 */ | |
224 public struct SCHEMES { | |
225 | |
226 /** | |
227 * Contrast button scheme | |
228 */ | |
229 private static ButtonScheme BUTTON_CONTRAST_; | |
230 static ButtonScheme BUTTON_CONTRAST(){ | |
231 if( BUTTON_CONTRAST_ is null ){ | |
232 synchronized( ButtonScheme.classinfo ){ | |
233 if( BUTTON_CONTRAST_ is null ){ | |
234 BUTTON_CONTRAST_ = new ButtonScheme( | |
235 [ColorConstants.button, ColorConstants.buttonLightest], | |
236 DARKEST_DARKER | |
237 ); | |
238 } | |
239 } | |
240 } | |
241 return BUTTON_CONTRAST_; | |
242 } | |
243 /** | |
244 * Regular button scheme | |
245 */ | |
246 private static ButtonScheme BUTTON_; | |
247 static ButtonScheme BUTTON(){ | |
248 if( BUTTON_ is null ){ | |
249 synchronized( ButtonScheme.classinfo ){ | |
250 if( BUTTON_ is null ){ | |
251 BUTTON_ = new ButtonScheme( | |
252 [ColorConstants.buttonLightest], | |
253 DARKEST_DARKER | |
254 ); | |
255 } | |
256 } | |
257 } | |
258 return BUTTON_; | |
259 } | |
260 /** | |
261 * Toolbar button scheme | |
262 */ | |
263 private static ButtonScheme TOOLBAR_; | |
264 static ButtonScheme TOOLBAR(){ | |
265 if( TOOLBAR_ is null ){ | |
266 synchronized( ButtonScheme.classinfo ){ | |
267 if( TOOLBAR_ is null ){ | |
268 TOOLBAR_ = new ButtonScheme( | |
269 [ColorConstants.buttonLightest], | |
270 [ColorConstants.buttonDarker] | |
271 ); | |
272 } | |
273 } | |
274 } | |
275 return TOOLBAR_; | |
276 } | |
277 /** | |
278 * Scrollbar button scheme | |
279 */ | |
280 private static ButtonScheme BUTTON_SCROLLBAR_; | |
281 static ButtonScheme BUTTON_SCROLLBAR(){ | |
282 if( BUTTON_SCROLLBAR_ is null ){ | |
283 synchronized( ButtonScheme.classinfo ){ | |
284 if( BUTTON_SCROLLBAR_ is null ){ | |
285 BUTTON_SCROLLBAR_ = new ButtonScheme( | |
286 [ColorConstants.button, ColorConstants.buttonLightest], | |
287 DARKEST_DARKER, | |
288 [ColorConstants.buttonDarker], | |
289 [ColorConstants.buttonDarker] | |
290 ); | |
291 } | |
292 } | |
293 } | |
294 return BUTTON_SCROLLBAR_; | |
295 } | |
296 } | |
297 | |
298 /** | |
299 * Constructs a ButtonBorder with a predefined button scheme set as its default. | |
300 * | |
301 * @since 2.0 | |
302 */ | |
303 public this() { | |
304 setScheme(SCHEMES.BUTTON); | |
305 } | |
306 | |
307 /** | |
308 * Constructs a ButtonBorder with the input ButtonScheme set as its Scheme. | |
309 * | |
310 * @param scheme ButtonScheme for this ButtonBorder. | |
311 * @since 2.0 | |
312 */ | |
313 public this(ButtonScheme scheme) { | |
314 setScheme(scheme); | |
315 } | |
316 | |
317 /** | |
318 * Paints this border with the help of the set scheme, the model of the clickable figure, | |
319 * and other inputs. The scheme is used in conjunction with the state of the model to get | |
320 * the appropriate colors for the border. | |
321 * | |
322 * @param figure The Clickable that this border belongs to | |
323 * @param graphics The graphics used for painting | |
324 * @param insets The insets | |
325 */ | |
326 public void paint(IFigure figure, Graphics graphics, Insets insets) { | |
327 Clickable clickable = cast(Clickable)figure; | |
328 ButtonModel model = clickable.getModel(); | |
329 ButtonScheme colorScheme = cast(ButtonScheme)getScheme(); | |
330 | |
331 if (clickable.isRolloverEnabled() && !model.isMouseOver() | |
332 && !model.isSelected()) | |
333 return; | |
334 | |
335 Color[] tl, br; | |
336 if (model.isSelected() || model.isArmed()) { | |
337 tl = colorScheme.getShadowPressed(); | |
338 br = colorScheme.getHighlightPressed(); | |
339 } else { | |
340 tl = colorScheme.getHighlight(); | |
341 br = colorScheme.getShadow(); | |
342 } | |
343 | |
344 paint(graphics, figure, insets, tl, br); | |
345 } | |
346 | |
347 } |