Mercurial > projects > dwt-addons
comparison dwtx/draw2d/FigureUtilities.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 | 1082a0fc2bb8 |
comparison
equal
deleted
inserted
replaced
96:b492ba44e44d | 98:95307ad235d9 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 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.FigureUtilities; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import dwtx.dwtxhelper.Collection; | |
18 | |
19 import dwt.graphics.Color; | |
20 import dwt.graphics.Font; | |
21 import dwt.graphics.FontMetrics; | |
22 import dwt.graphics.GC; | |
23 import dwt.widgets.Shell; | |
24 import dwtx.draw2d.geometry.Dimension; | |
25 import dwtx.draw2d.geometry.Rectangle; | |
26 import dwtx.draw2d.Shape; | |
27 import dwtx.draw2d.IFigure; | |
28 import dwtx.draw2d.Graphics; | |
29 static import dwtx.draw2d.geometry.Point; | |
30 /** | |
31 * Provides miscellaneous Figure operations. | |
32 */ | |
33 public class FigureUtilities { | |
34 | |
35 private static final float RGB_VALUE_MULTIPLIER = 0.6f; | |
36 private static GC gc; | |
37 private static Font appliedFont; | |
38 private static FontMetrics metrics; | |
39 private static Color ghostFillColor; | |
40 | |
41 static this(){ | |
42 ghostFillColor = new Color(null, 31, 31, 31); | |
43 } | |
44 | |
45 /** | |
46 * Returns a new Color the same as the passed color in a darker hue. | |
47 * | |
48 * @param color the color to darken | |
49 * @return the darkened color | |
50 * @since 2.0 | |
51 */ | |
52 public static Color darker(Color color) { | |
53 return new Color(null, | |
54 cast(int)(color.getRed() * RGB_VALUE_MULTIPLIER), | |
55 cast(int)(color.getGreen() * RGB_VALUE_MULTIPLIER), | |
56 cast(int)(color.getBlue() * RGB_VALUE_MULTIPLIER)); | |
57 } | |
58 | |
59 /** | |
60 * Returns the FontMetrics associated with the passed Font. | |
61 * | |
62 * @param f the font | |
63 * @return the FontMetrics for the given font | |
64 * @see GC#getFontMetrics() | |
65 * @since 2.0 | |
66 */ | |
67 public static FontMetrics getFontMetrics(Font f) { | |
68 setFont(f); | |
69 if (metrics is null) | |
70 metrics = getGC().getFontMetrics(); | |
71 return metrics; | |
72 } | |
73 | |
74 /** | |
75 * Returns the GC used for various utilities. Advanced graphics must not be switched on by | |
76 * clients using this GC. | |
77 * @deprecated do not mess with this GC | |
78 * @return the GC | |
79 */ | |
80 protected static GC getGC() { | |
81 if (gc is null) { | |
82 gc = new GC(new Shell()); | |
83 appliedFont = gc.getFont(); | |
84 } | |
85 return gc; | |
86 } | |
87 | |
88 /** | |
89 * Returns the dimensions of the String <i>s</i> using the font <i>f</i>. Tab expansion | |
90 * and carriage return processing are performed. | |
91 * @param s the string | |
92 * @param f the font | |
93 * @return the text's dimensions | |
94 * @see GC#textExtent(String) | |
95 */ | |
96 protected static dwt.graphics.Point.Point getTextDimension(String s, Font f) { | |
97 setFont(f); | |
98 return getGC().textExtent(s); | |
99 } | |
100 | |
101 /** | |
102 * Returns the highest ancestor for the given figure | |
103 * @since 3.0 | |
104 * @param figure a figure | |
105 * @return the root ancestor | |
106 */ | |
107 public static IFigure getRoot(IFigure figure) { | |
108 while (figure.getParent() !is null) | |
109 figure = figure.getParent(); | |
110 return figure; | |
111 } | |
112 | |
113 /** | |
114 * Returns the dimensions of the String <i>s</i> using the font <i>f</i>. No tab | |
115 * expansion or carriage return processing will be performed. | |
116 * @param s the string | |
117 * @param f the font | |
118 * @return the string's dimensions | |
119 * @see GC#stringExtent(java.lang.String) | |
120 */ | |
121 protected static dwt.graphics.Point.Point getStringDimension(String s, Font f) { | |
122 setFont(f); | |
123 return getGC().stringExtent(s); | |
124 } | |
125 | |
126 /** | |
127 * Returns the Dimensions of the given text, converting newlines and tabs appropriately. | |
128 * | |
129 * @param text the text | |
130 * @param f the font | |
131 * @return the dimensions of the given text | |
132 * @since 2.0 | |
133 */ | |
134 public static Dimension getTextExtents(String text, Font f) { | |
135 return new Dimension(getTextDimension(text, f)); | |
136 } | |
137 | |
138 /** | |
139 * Returns the Dimensions of <i>s</i> in Font <i>f</i>. | |
140 * | |
141 * @param s the string | |
142 * @param f the font | |
143 * @return the dimensions of the given string | |
144 * @since 2.0 | |
145 */ | |
146 public static Dimension getStringExtents(String s, Font f) { | |
147 return new Dimension(getStringDimension(s, f)); | |
148 } | |
149 | |
150 /** | |
151 * Returns the Dimensions of the given text, converting newlines and tabs appropriately. | |
152 * | |
153 * @param s the string | |
154 * @param f the font | |
155 * @param result the Dimension that will contain the result of this calculation | |
156 * @since 2.0 | |
157 */ | |
158 public static void getTextExtents(String s, Font f, Dimension result) { | |
159 dwt.graphics.Point.Point pt = getTextDimension(s, f); | |
160 result.width = pt.x; | |
161 result.height = pt.y; | |
162 } | |
163 | |
164 /** | |
165 * Returns the width of <i>s</i> in Font <i>f</i>. | |
166 * | |
167 * @param s the string | |
168 * @param f the font | |
169 * @return the width | |
170 * @since 2.0 | |
171 */ | |
172 public static int getTextWidth(String s, Font f) { | |
173 return getTextDimension(s, f).x; | |
174 } | |
175 | |
176 /** | |
177 * Returns a Color the same as the passed color in a lighter hue. | |
178 * | |
179 * @param rgb the color | |
180 * @return the lighter color | |
181 * @since 2.0 | |
182 */ | |
183 public static Color lighter(Color rgb) { | |
184 int r = rgb.getRed(), | |
185 g = rgb.getGreen(), | |
186 b = rgb.getBlue(); | |
187 | |
188 return new Color(null, | |
189 Math.max(2, Math.min(cast(int)(r / RGB_VALUE_MULTIPLIER), 255)), | |
190 Math.max(2, Math.min(cast(int)(g / RGB_VALUE_MULTIPLIER), 255)), | |
191 Math.max(2, Math.min(cast(int)(b / RGB_VALUE_MULTIPLIER), 255)) | |
192 ); | |
193 } | |
194 | |
195 /** | |
196 * Produces a ghosting effect on the shape <i>s</i>. | |
197 * | |
198 * @param s the shape | |
199 * @return the ghosted shape | |
200 * @since 2.0 | |
201 */ | |
202 public static Shape makeGhostShape(Shape s) { | |
203 s.setBackgroundColor(ghostFillColor); | |
204 s.setFillXOR(true); | |
205 s.setOutlineXOR(true); | |
206 return s; | |
207 } | |
208 | |
209 /** | |
210 * Mixes the passed Colors and returns the resulting Color. | |
211 * | |
212 * @param c1 the first color | |
213 * @param c2 the second color | |
214 * @param weight the first color's weight from 0-1 | |
215 * @return the new color | |
216 * @since 2.0 | |
217 */ | |
218 public static Color mixColors(Color c1, Color c2, double weight) { | |
219 return new Color(null, | |
220 cast(int)(c1.getRed() * weight + c2.getRed() * (1 - weight)), | |
221 cast(int)(c1.getGreen() * weight + c2.getGreen() * (1 - weight)), | |
222 cast(int)(c1.getBlue() * weight + c2.getBlue() * (1 - weight))); | |
223 } | |
224 | |
225 | |
226 /** | |
227 * Mixes the passed Colors and returns the resulting Color. | |
228 * | |
229 * @param c1 the first color | |
230 * @param c2 the second color | |
231 * @return the new color | |
232 * @since 2.0 | |
233 */ | |
234 public static Color mixColors(Color c1, Color c2) { | |
235 return new Color(null, | |
236 (c1.getRed() + c2.getRed()) / 2, | |
237 (c1.getGreen() + c2.getGreen()) / 2, | |
238 (c1.getBlue() + c2.getBlue()) / 2); | |
239 } | |
240 | |
241 /** | |
242 * Paints a border with an etching effect, having a shadow of Color <i>shadow</i> and | |
243 * highlight of Color <i>highlight</i>. | |
244 * | |
245 * @param g the graphics object | |
246 * @param r the bounds of the border | |
247 * @param shadow the shadow color | |
248 * @param highlight the highlight color | |
249 * @since 2.0 | |
250 */ | |
251 public static void paintEtchedBorder(Graphics g, Rectangle r, | |
252 Color shadow, Color highlight) { | |
253 int x = r.x, | |
254 y = r.y, | |
255 w = r.width, | |
256 h = r.height; | |
257 | |
258 g.setLineStyle(Graphics.LINE_SOLID); | |
259 g.setLineWidth(1); | |
260 g.setXORMode(false); | |
261 | |
262 w -= 2; | |
263 h -= 2; | |
264 | |
265 g.setForegroundColor(shadow); | |
266 g.drawRectangle(x, y, w, h); | |
267 | |
268 x++; | |
269 y++; | |
270 g.setForegroundColor(highlight); | |
271 g.drawRectangle(x, y, w, h); | |
272 } | |
273 | |
274 /** | |
275 * Helper method to paint a grid. Painting is optimized as it is restricted to the | |
276 * Graphics' clip. | |
277 * | |
278 * @param g The Graphics object to be used for painting | |
279 * @param f The figure in which the grid is to be painted | |
280 * @param origin Any point where the grid lines are expected to intersect | |
281 * @param distanceX Distance between vertical grid lines; if 0 or less, vertical grid | |
282 * lines will not be drawn | |
283 * @param distanceY Distance between horizontal grid lines; if 0 or less, horizontal | |
284 * grid lines will not be drawn | |
285 * | |
286 * @since 3.0 | |
287 */ | |
288 public static void paintGrid(Graphics g, IFigure f, | |
289 dwtx.draw2d.geometry.Point.Point origin, int distanceX, int distanceY) { | |
290 Rectangle clip = g.getClip(Rectangle.SINGLETON); | |
291 | |
292 if (distanceX > 0) { | |
293 if (origin.x >= clip.x) | |
294 while (origin.x - distanceX >= clip.x) | |
295 origin.x -= distanceX; | |
296 else | |
297 while (origin.x < clip.x) | |
298 origin.x += distanceX; | |
299 for (int i = origin.x; i < clip.x + clip.width; i += distanceX) | |
300 g.drawLine(i, clip.y, i, clip.y + clip.height); | |
301 } | |
302 | |
303 if (distanceY > 0) { | |
304 if (origin.y >= clip.y) | |
305 while (origin.y - distanceY >= clip.y) | |
306 origin.y -= distanceY; | |
307 else | |
308 while (origin.y < clip.y) | |
309 origin.y += distanceY; | |
310 for (int i = origin.y; i < clip.y + clip.height; i += distanceY) | |
311 g.drawLine(clip.x, i, clip.x + clip.width, i); | |
312 } | |
313 } | |
314 | |
315 /** | |
316 * Paints a border with an etching effect, having a shadow of a darker version of g's | |
317 * background color, and a highlight a lighter version of g's background color. | |
318 * | |
319 * @param g the graphics object | |
320 * @param r the bounds of the border | |
321 * @since 2.0 | |
322 */ | |
323 public static void paintEtchedBorder(Graphics g, Rectangle r) { | |
324 Color rgb = g.getBackgroundColor(), | |
325 shadow = darker(rgb), | |
326 highlight = lighter(rgb); | |
327 paintEtchedBorder(g, r, shadow, highlight); | |
328 } | |
329 | |
330 /** | |
331 * Sets Font to passed value. | |
332 * | |
333 * @param f the new font | |
334 * @since 2.0 | |
335 */ | |
336 protected static void setFont(Font f) { | |
337 if (appliedFont is f || f.opEquals(appliedFont)) | |
338 return; | |
339 getGC().setFont(f); | |
340 appliedFont = f; | |
341 metrics = null; | |
342 } | |
343 | |
344 /** | |
345 * Returns the figure which is the ancestor of both figures, or <code>null</code>. A | |
346 * figure is an ancestor if it is the parent of another figure, or if it is the ancestor | |
347 * of that figure's parent. If one figure contains the other, <code>null</code> is | |
348 * returned. | |
349 * @since 3.1 | |
350 * @param l left | |
351 * @param r right | |
352 * @return the common ancestor | |
353 */ | |
354 public static IFigure findCommonAncestor(IFigure l, IFigure r) { | |
355 if (l is r) | |
356 return l; | |
357 ArrayList left = new ArrayList(); | |
358 ArrayList right = new ArrayList(); | |
359 while (l !is null) { | |
360 left.add(cast(Object)l); | |
361 l = l.getParent(); | |
362 } | |
363 while (r !is null) { | |
364 right.add(cast(Object)r); | |
365 r = r.getParent(); | |
366 } | |
367 if (left.isEmpty() || right.isEmpty()) | |
368 return null; | |
369 | |
370 int il = left.size() - 1; | |
371 int ir = right.size() - 1; | |
372 do { | |
373 if (left.get(il) !is right.get(ir)) | |
374 break; | |
375 il--; | |
376 ir--; | |
377 } while (il >= 0 && ir >= 0); | |
378 | |
379 return cast(IFigure) left.get(il + 1); | |
380 } | |
381 | |
382 /** | |
383 * Returns <code>true</code> if the ancestor contains the descendant, or is the ancestor | |
384 * of the descendant's parent. | |
385 * @param ancestor the ancestor | |
386 * @param descendant the descendant | |
387 * @return <code>true</code> if ancestor | |
388 * @since 3.2 | |
389 */ | |
390 public static bool isAncestor(IFigure ancestor, IFigure descendant) { | |
391 while (descendant !is null) { | |
392 descendant = descendant.getParent(); | |
393 if (descendant is ancestor) | |
394 return true; | |
395 } | |
396 return false; | |
397 } | |
398 | |
399 } |