comparison dwt/graphics/TextStyle.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
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 *******************************************************************************/
11 module dwt.graphics.TextStyle;
12
13 import dwt.dwthelper.utils;
14
15 import dwt.DWT;
16
17 /**
18 * <code>TextStyle</code> defines a set of styles that can be applied
19 * to a range of text.
20 * <p>
21 * The hashCode() method in this class uses the values of the public
22 * fields to compute the hash value. When storing instances of the
23 * class in hashed collections, do not modify these fields after the
24 * object has been inserted.
25 * </p>
26 * <p>
27 * Application code does <em>not</em> need to explicitly release the
28 * resources managed by each instance when those instances are no longer
29 * required, and thus no <code>dispose()</code> method is provided.
30 * </p>
31 *
32 * @see TextLayout
33 * @see Font
34 * @see Color
35 *
36 * @since 3.0
37 */
38 public class TextStyle {
39
40 /**
41 * the font of the style
42 */
43 public Font font;
44
45 /**
46 * the foreground of the style
47 */
48 public Color foreground;
49
50 /**
51 * the background of the style
52 */
53 public Color background;
54
55 /**
56 * the underline flag of the style. The default underline
57 * style is <code>DWT.UNDERLINE_SINGLE</code>.
58 *
59 *
60 * @since 3.1
61 */
62 public bool underline;
63
64 /**
65 * the underline color of the style
66 *
67 * @since 3.4
68 */
69 public Color underlineColor;
70
71 /**
72 * the underline style. This style is ignored when
73 * <code>underline</code> is false.
74 * <p>
75 * This value should be one of <code>DWT.UNDERLINE_SINGLE</code>,
76 * <code>DWT.UNDERLINE_DOUBLE</code>, <code>DWT.UNDERLINE_ERROR</code>,
77 * or <code>DWT.UNDERLINE_SQUIGGLE</code>.
78 * </p>
79 *
80 * @see DWT#UNDERLINE_SINGLE
81 * @see DWT#UNDERLINE_DOUBLE
82 * @see DWT#UNDERLINE_ERROR
83 * @see DWT#UNDERLINE_SQUIGGLE
84 *
85 * @since 3.4
86 */
87 public int underlineStyle;
88
89 /**
90 * the strikeout flag of the style
91 *
92 * @since 3.1
93 */
94 public bool strikeout;
95
96 /**
97 * the strikeout color of the style
98 *
99 * @since 3.4
100 */
101 public Color strikeoutColor;
102
103 /**
104 * the border style. The default border style is <code>DWT.NONE</code>.
105 * <p>
106 * This value should be one of <code>DWT.BORDER_SOLID</code>,
107 * <code>DWT.BORDER_DASH</code>,<code>DWT.BORDER_DOT</code> or
108 * <code>DWT.NONE</code>.
109 * </p>
110 *
111 * @see DWT#BORDER_SOLID
112 * @see DWT#BORDER_DASH
113 * @see DWT#BORDER_DOT
114 * @see DWT#NONE
115 *
116 * @since 3.4
117 */
118 public int borderStyle;
119
120 /**
121 * the border color of the style
122 *
123 * @since 3.4
124 */
125 public Color borderColor;
126
127 /**
128 * the GlyphMetrics of the style
129 *
130 * @since 3.2
131 */
132 public GlyphMetrics metrics;
133
134 /**
135 * the baseline rise of the style.
136 *
137 * @since 3.2
138 */
139 public int rise;
140
141 /**
142 * Create an empty text style.
143 *
144 * @since 3.4
145 */
146 public TextStyle () {
147 }
148
149 /**
150 * Create a new text style with the specified font, foreground
151 * and background.
152 *
153 * @param font the font of the style, <code>null</code> if none
154 * @param foreground the foreground color of the style, <code>null</code> if none
155 * @param background the background color of the style, <code>null</code> if none
156 */
157 public TextStyle (Font font, Color foreground, Color background) {
158 if (font !is null && font.isDisposed()) DWT.error (DWT.ERROR_INVALID_ARGUMENT);
159 if (foreground !is null && foreground.isDisposed()) DWT.error (DWT.ERROR_INVALID_ARGUMENT);
160 if (background !is null && background.isDisposed()) DWT.error (DWT.ERROR_INVALID_ARGUMENT);
161 this.font = font;
162 this.foreground = foreground;
163 this.background = background;
164 }
165
166
167 /**
168 * Create a new text style from an existing text style.
169 *
170 *@param style the style to copy
171 *
172 * @since 3.4
173 */
174 public TextStyle (TextStyle style) {
175 if (style is null) DWT.error (DWT.ERROR_INVALID_ARGUMENT);
176 font = style.font;
177 foreground = style.foreground;
178 background = style.background;
179 underline = style.underline;
180 underlineColor = style.underlineColor;
181 underlineStyle = style.underlineStyle;
182 strikeout = style.strikeout;
183 strikeoutColor = style.strikeoutColor;
184 borderStyle = style.borderStyle;
185 borderColor = style.borderColor;
186 metrics = style.metrics;
187 rise = style.rise;
188 }
189
190 /**
191 * Compares the argument to the receiver, and returns true
192 * if they represent the <em>same</em> object using a class
193 * specific comparison.
194 *
195 * @param object the object to compare with this object
196 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
197 *
198 * @see #hashCode()
199 */
200 public bool equals(Object object) {
201 if (object is this) return true;
202 if (object is null) return false;
203 if (!(object instanceof TextStyle)) return false;
204 TextStyle style = (TextStyle)object;
205 if (foreground !is null) {
206 if (!foreground.equals(style.foreground)) return false;
207 } else if (style.foreground !is null) return false;
208 if (background !is null) {
209 if (!background.equals(style.background)) return false;
210 } else if (style.background !is null) return false;
211 if (font !is null) {
212 if (!font.equals(style.font)) return false;
213 } else if (style.font !is null) return false;
214 if (metrics !is null || style.metrics !is null) return false;
215 if (underline !is style.underline) return false;
216 if (underlineStyle !is style.underlineStyle) return false;
217 if (borderStyle !is style.borderStyle) return false;
218 if (strikeout !is style.strikeout) return false;
219 if (rise !is style.rise) return false;
220 if (underlineColor !is null) {
221 if (!underlineColor.equals(style.underlineColor)) return false;
222 } else if (style.underlineColor !is null) return false;
223 if (strikeoutColor !is null) {
224 if (!strikeoutColor.equals(style.strikeoutColor)) return false;
225 } else if (style.strikeoutColor !is null) return false;
226 if (underlineStyle !is style.underlineStyle) return false;
227 if (borderColor !is null) {
228 if (!borderColor.equals(style.borderColor)) return false;
229 } else if (style.borderColor !is null) return false;
230 return true;
231 }
232
233 /**
234 * Returns an integer hash code for the receiver. Any two
235 * objects that return <code>true</code> when passed to
236 * <code>equals</code> must return the same value for this
237 * method.
238 *
239 * @return the receiver's hash
240 *
241 * @see #equals(Object)
242 */
243 public int hashCode() {
244 int hash = 0;
245 if (foreground !is null) hash ^= foreground.hashCode();
246 if (background !is null) hash ^= background.hashCode();
247 if (font !is null) hash ^= font.hashCode();
248 if (metrics !is null) hash ^= metrics.hashCode();
249 if (underline) hash ^= hash;
250 if (strikeout) hash ^= hash;
251 hash ^= rise;
252 if (underlineColor !is null) hash ^= underlineColor.hashCode();
253 if (strikeoutColor !is null) hash ^= strikeoutColor.hashCode();
254 if (borderColor !is null) hash ^= borderColor.hashCode();
255 hash ^= underlineStyle;
256 return hash;
257 }
258
259 bool isAdherentBorder(TextStyle style) {
260 if (this is style) return true;
261 if (style is null) return false;
262 if (borderStyle !is style.borderStyle) return false;
263 if (borderColor !is null) {
264 if (!borderColor.equals(style.borderColor)) return false;
265 } else if (style.borderColor !is null) return false;
266 return true;
267 }
268
269 bool isAdherentUnderline(TextStyle style) {
270 if (this is style) return true;
271 if (style is null) return false;
272 if (underline !is style.underline) return false;
273 if (underlineStyle !is style.underlineStyle) return false;
274 if (underlineColor !is null) {
275 if (!underlineColor.equals(style.underlineColor)) return false;
276 } else if (style.underlineColor !is null) return false;
277 return true;
278 }
279
280 bool isAdherentStrikeout(TextStyle style) {
281 if (this is style) return true;
282 if (style is null) return false;
283 if (strikeout !is style.strikeout) return false;
284 if (strikeoutColor !is null) {
285 if (!strikeoutColor.equals(style.strikeoutColor)) return false;
286 } else if (style.strikeoutColor !is null) return false;
287 return true;
288 }
289
290 /**
291 * Returns a string containing a concise, human-readable
292 * description of the receiver.
293 *
294 * @return a string representation of the <code>TextStyle</code>
295 */
296 public String toString () {
297 StringBuffer buffer = new StringBuffer("TextStyle {");
298 int startLength = buffer.length();
299 if (font !is null) {
300 if (buffer.length() > startLength) buffer.append(", ");
301 buffer.append("font=");
302 buffer.append(font);
303 }
304 if (foreground !is null) {
305 if (buffer.length() > startLength) buffer.append(", ");
306 buffer.append("foreground=");
307 buffer.append(foreground);
308 }
309 if (background !is null) {
310 if (buffer.length() > startLength) buffer.append(", ");
311 buffer.append("background=");
312 buffer.append(background);
313 }
314 if (underline) {
315 if (buffer.length() > startLength) buffer.append(", ");
316 buffer.append("underlined");
317 }
318 if (strikeout) {
319 if (buffer.length() > startLength) buffer.append(", ");
320 buffer.append("striked out");
321 }
322 if (rise !is 0) {
323 if (buffer.length() > startLength) buffer.append(", ");
324 buffer.append("rise=");
325 buffer.append(rise);
326 }
327 if (metrics !is null) {
328 if (buffer.length() > startLength) buffer.append(", ");
329 buffer.append("metrics=");
330 buffer.append(metrics);
331 }
332 buffer.append("}");
333 return buffer.toString();
334 }
335
336 }