comparison dwtx/draw2d/SchemeBorder.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 2d6540440fe6
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.SchemeBorder;
14
15 import dwt.dwthelper.utils;
16
17
18
19 import dwt.graphics.Color;
20 import dwtx.draw2d.geometry.Insets;
21 import dwtx.draw2d.geometry.Rectangle;
22 import dwtx.draw2d.IFigure;
23 import dwtx.draw2d.Border;
24 import dwtx.draw2d.Graphics;
25 import dwtx.draw2d.AbstractBorder;
26 import dwtx.draw2d.ColorConstants;
27
28 /**
29 * SchemeBorder allows the creation of borders based on
30 * {@link SchemeBorder.Scheme Schemes}. A <i>Scheme</i> is a class whose only purpose is
31 * to carry border specific information. SchemeBorder renders the border based on the
32 * information given by the <i>Scheme</i> set to it.
33 */
34 public class SchemeBorder
35 : AbstractBorder
36 //, ColorConstants
37 {
38
39 /** The {@link SchemeBorder.Scheme} associated with this SchemeBorder **/
40 protected Scheme scheme = null;
41
42 /** Arrays of Colors, used for shadow or highlight effects **/
43 protected static const Color[]
44 DARKEST_DARKER,
45 LIGHTER_DARKER,
46 DARKER_LIGHTER;
47
48 static this(){
49 DARKEST_DARKER = [ColorConstants.buttonDarkest, ColorConstants.buttonDarker];
50 LIGHTER_DARKER = [ColorConstants.buttonLightest, ColorConstants.buttonDarker];
51 DARKER_LIGHTER = [ColorConstants.buttonDarker, ColorConstants.buttonLightest];
52 }
53
54 /**
55 * Holds a set of information about a border, which can be changed to create a wide range
56 * of schemes. Provides support for border opacity, size, highlight side and shadow side
57 * colors.
58 */
59 public static class Scheme {
60 private Insets insets;
61 private bool isOpaque_ = false;
62
63 /** Arrays of Colors, used for highlight and shadow effecsts */
64 protected Color[]
65 highlight,
66 shadow;
67
68 /**
69 * Constructs a default border Scheme with no border sides.
70 * @since 2.0
71 */
72 protected this() { }
73
74 /**
75 * Constructs a border Scheme with the specified highlight and shadow colors. The size
76 * of the border depends on the number of colors passed in for each parameter.
77 * Hightlight colors are used in the top and left sides of the border, and Shadow
78 * colors are used in the bottom and right sides of the border.
79 *
80 * @param highlight the hightlight colors
81 * @param shadow the shadow colors
82 * @since 2.0
83 */
84 public this(Color[] highlight, Color[] shadow) {
85 this.highlight = highlight;
86 this.shadow = shadow;
87 init();
88 }
89
90 /**
91 * Constructs a border scheme with the specified colors. The input colors serve as
92 * both highlight and shadow colors. The size of the border is the number of colors
93 * passed in as input. Hightlight colors are used in the top and left sides of the
94 * border, and Shadow colors are used in the bottom and right sides of the border.
95 *
96 * @param colors the colors to be used for the border
97 * @since 2.0
98 */
99 public this(Color[] colors) {
100 highlight = shadow = colors;
101 init();
102 }
103
104 /**
105 * Calculates and returns the Insets for this border Scheme. The calculations depend
106 * on the number of colors passed in as input.
107 *
108 * @return the Insets used by this border
109 * @since 2.0
110 */
111 protected Insets calculateInsets() {
112 int tl = getHighlight().length;
113 int br = getShadow().length;
114 return new Insets(tl, tl, br, br);
115 }
116
117 /**
118 * Calculates and retuns the opaque state of this border scheme. Returns
119 * <code>false</code> if any of the border colors are <code>null</code>. This is done
120 * to prevent the appearance of underlying pixels since the border color is
121 * <code>null</code>.
122 *
123 * @return <code>true</code> if this border is opaque
124 * @since 2.0
125 */
126 protected bool calculateOpaque() {
127 Color colors [] = getHighlight();
128 for (int i = 0; i < colors.length; i++)
129 if (colors[i] is null)
130 return false;
131 colors = getShadow();
132 for (int i = 0; i < colors.length; i++)
133 if (colors[i] is null)
134 return false;
135 return true;
136 }
137
138 /**
139 * Returns the highlight colors of this border scheme as an array of Colors.
140 *
141 * @return the highlight colors
142 * @since 2.0
143 */
144 protected Color[] getHighlight() {
145 return highlight;
146 }
147
148 /**
149 * Returns the Insets required by this Scheme.
150 *
151 * @return the Insets
152 * @since 2.0
153 */
154 protected Insets getInsets() {
155 return insets;
156 }
157
158 /**
159 * Returns the shadow colors of this border scheme as an array of Colors.
160 *
161 * @return the shadow colors
162 * @since 2.0
163 */
164 protected Color[] getShadow() {
165 return shadow;
166 }
167
168 /**
169 * Calculates and initializes the properties of this border scheme.
170 *
171 * @since 2.0
172 */
173 protected void init() {
174 insets = calculateInsets();
175 isOpaque_ = calculateOpaque();
176 }
177
178 /**
179 * Returns whether this border should be opaque or not.
180 *
181 * @return <code>true</code> if this border is opaque
182 * @since 2.0
183 */
184 protected bool isOpaque() {
185 return isOpaque_;
186 }
187 }
188
189 /**
190 * Interface which defines some commonly used schemes for the border. These schemes can be
191 * given as input to the {@link SchemeBorder SchemeBorder} to generate appropriate borders.
192 */
193 public struct SCHEMES {
194
195 /** Schemes used for shadow and highlight effects **/
196 public static Scheme
197 BUTTON_CONTRAST,
198 BUTTON_RAISED,
199 BUTTON_PRESSED,
200 RAISED,
201 LOWERED,
202 RIDGED,
203 ETCHED;
204
205 static this(){
206 BUTTON_CONTRAST = new Scheme(
207 [ColorConstants.button, ColorConstants.buttonLightest],
208 DARKEST_DARKER
209 );
210 BUTTON_RAISED = new Scheme(
211 [ColorConstants.buttonLightest],
212 DARKEST_DARKER
213 );
214 BUTTON_PRESSED = new Scheme(
215 DARKEST_DARKER,
216 [ColorConstants.buttonLightest]
217 );
218 RAISED = new Scheme(
219 [ColorConstants.buttonLightest],
220 [ColorConstants.buttonDarkest]
221 );
222 LOWERED = new Scheme(
223 [ColorConstants.buttonDarkest],
224 [ColorConstants.buttonLightest]
225 );
226 RIDGED = new Scheme(LIGHTER_DARKER, DARKER_LIGHTER);
227 ETCHED = new Scheme(DARKER_LIGHTER, LIGHTER_DARKER);
228 }
229 }
230
231 /**
232 * Constructs a default SchemeBorder with no scheme defined.
233 * @since 2.0
234 */
235 protected this() { }
236
237 /**
238 * Constructs a SchemeBorder with the Scheme given as input.
239 *
240 * @param scheme the Scheme to be used by this border
241 * @since 2.0
242 */
243 public this(Scheme scheme) {
244 setScheme(scheme);
245 }
246
247 /**
248 * @see Border#getInsets(IFigure)
249 */
250 public Insets getInsets(IFigure figure) {
251 return getScheme().getInsets();
252 }
253
254 /**
255 * Returns the scheme used by this border.
256 *
257 * @return the Scheme used by this border
258 * @since 2.0
259 */
260 protected Scheme getScheme() {
261 return scheme;
262 }
263
264 /**
265 * Returns the opaque state of this border. Returns <code>true</code> indicating that this
266 * will fill in the area enclosed by the border.
267 *
268 * @see Border#isOpaque()
269 */
270 public bool isOpaque() {
271 return true;
272 }
273
274 /**
275 * Sets the Scheme for this border to the Scheme given as input.
276 *
277 * @param scheme the Scheme for this border
278 * @since 2.0
279 */
280 protected void setScheme(Scheme scheme) {
281 this.scheme = scheme;
282 }
283
284 /**
285 * @see Border#paint(IFigure, Graphics, Insets)
286 */
287 public void paint(IFigure figure, Graphics g, Insets insets) {
288 Color [] tl = scheme.getHighlight();
289 Color [] br = scheme.getShadow();
290
291 paint (g, figure, insets, tl, br);
292 }
293
294 /**
295 * Paints the border using the information in the set Scheme and the inputs given. Side
296 * widths are determined by the number of colors in the Scheme for each side.
297 *
298 * @param graphics the graphics object
299 * @param fig the figure this border belongs to
300 * @param insets the insets
301 * @param tl the highlight (top/left) colors
302 * @param br the shadow (bottom/right) colors
303 */
304 protected void paint(Graphics graphics, IFigure fig,
305 Insets insets, Color[] tl, Color[] br) {
306 graphics.setLineWidth(1);
307 graphics.setLineStyle(Graphics.LINE_SOLID);
308 graphics.setXORMode(false);
309
310 Rectangle rect = getPaintRectangle(fig, insets);
311
312 int top = rect.y;
313 int left = rect.x;
314 int bottom = rect.bottom() - 1;
315 int right = rect.right() - 1;
316 Color color;
317
318 for (int i = 0; i < br.length; i++) {
319 color = br[i];
320 graphics.setForegroundColor(color);
321 graphics.drawLine(right - i, bottom - i, right - i, top + i);
322 graphics.drawLine(right - i, bottom - i, left + i, bottom - i);
323 }
324
325 right--;
326 bottom--;
327
328 for (int i = 0; i < tl.length; i++) {
329 color = tl[i];
330 graphics.setForegroundColor(color);
331 graphics.drawLine(left + i, top + i, right - i, top + i);
332 graphics.drawLine(left + i, top + i, left + i, bottom - i);
333 }
334 }
335
336 }