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