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