comparison dwt/graphics/TextStyle.d @ 22:b5521a2c27c2

TextStyle
author Frank Benoit <benoit@tionex.de>
date Mon, 07 Jan 2008 09:25:10 +0100
parents
children 8cec8f536af3
comparison
equal deleted inserted replaced
21:4f9c0fea3440 22:b5521a2c27c2
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 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.SWT;
14 import dwt.graphics.Font;
15 import dwt.graphics.Color;
16 import dwt.graphics.GlyphMetrics;
17
18 import tango.util.Convert;
19 /**
20 * <code>TextStyle</code> defines a set of styles that can be applied
21 * to a range of text.
22 * <p>
23 * The hashCode() method in this class uses the values of the public
24 * fields to compute the hash value. When storing instances of the
25 * class in hashed collections, do not modify these fields after the
26 * object has been inserted.
27 * </p>
28 * <p>
29 * Application code does <em>not</em> need to explicitly release the
30 * resources managed by each instance when those instances are no longer
31 * required, and thus no <code>dispose()</code> method is provided.
32 * </p>
33 *
34 * @see TextLayout
35 * @see Font
36 * @see Color
37 *
38 * @since 3.0
39 */
40 public class TextStyle {
41
42 /**
43 * the font of the style
44 */
45 public Font font;
46
47 /**
48 * the foreground of the style
49 */
50 public Color foreground;
51
52 /**
53 * the background of the style
54 */
55 public Color background;
56
57 /**
58 * the underline flag of the style
59 *
60 * @since 3.1
61 */
62 public bool underline;
63
64 /**
65 * the strikeout flag of the style
66 *
67 * @since 3.1
68 */
69 public bool strikeout;
70
71 /**
72 * the GlyphMetrics of the style
73 *
74 * @since 3.2
75 */
76 public GlyphMetrics metrics;
77
78 /**
79 * the baseline rise of the style.
80 *
81 * @since 3.2
82 */
83 public int rise;
84
85 /**
86 * Create a new text style with the specified font, foreground
87 * and background.
88 *
89 * @param font the font of the style, <code>null</code> if none
90 * @param foreground the foreground color of the style, <code>null</code> if none
91 * @param background the background color of the style, <code>null</code> if none
92 */
93 public this (Font font, Color foreground, Color background) {
94 if (font !is null && font.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
95 if (foreground !is null && foreground.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
96 if (background !is null && background.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
97 this.font = font;
98 this.foreground = foreground;
99 this.background = background;
100 }
101
102 /**
103 * Compares the argument to the receiver, and returns true
104 * if they represent the <em>same</em> object using a class
105 * specific comparison.
106 *
107 * @param object the object to compare with this object
108 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
109 *
110 * @see #hashCode()
111 */
112 public override int opEquals(Object object) {
113 if (object is this) return true;
114 if (object is null) return false;
115 if (!(cast(TextStyle)object)) return false;
116 TextStyle style = cast(TextStyle)object;
117 if (foreground !is null) {
118 if ( foreground != style.foreground ) return false;
119 } else if (style.foreground !is null) return false;
120 if (background !is null) {
121 if ( background != style.background ) return false;
122 } else if (style.background !is null) return false;
123 if (font !is null) {
124 if (font != style.font) return false;
125 } else if (style.font != null) return false;
126 if (metrics != null || style.metrics !is null) return false;
127 if (underline !is style.underline) return false;
128 if (strikeout !is style.strikeout) return false;
129 if (rise !is style.rise) return false;
130 return true;
131 }
132
133 /**
134 * Returns an integer hash code for the receiver. Any two
135 * objects that return <code>true</code> when passed to
136 * <code>equals</code> must return the same value for this
137 * method.
138 *
139 * @return the receiver's hash
140 *
141 * @see #equals(Object)
142 */
143 public override hash_t toHash() {
144 int hash = 0;
145 if (foreground !is null) hash ^= foreground.toHash();
146 if (background !is null) hash ^= background.toHash();
147 if (font !is null) hash ^= font.toHash();
148 if (metrics !is null) hash ^= metrics.toHash();
149 if (underline) hash ^= hash;
150 if (strikeout) hash ^= hash;
151 hash ^= rise;
152 return hash;
153 }
154
155 /**
156 * Returns a string containing a concise, human-readable
157 * description of the receiver.
158 *
159 * @return a string representation of the <code>TextStyle</code>
160 */
161 public char[] toString () {
162 char[] buffer = "TextStyle {";
163 int startLength = buffer.length;
164 if (font != null) {
165 if (buffer.length > startLength) buffer ~= ", ";
166 buffer ~= "font=";
167 buffer ~= font.toString;
168 }
169 if (foreground != null) {
170 if (buffer.length > startLength) buffer ~= ", ";
171 buffer ~= "foreground=";
172 buffer ~= foreground.toString;
173 }
174 if (background != null) {
175 if (buffer.length > startLength) buffer ~= ", ";
176 buffer ~= "background=";
177 buffer ~= background.toString;
178 }
179 if (underline) {
180 if (buffer.length > startLength) buffer ~= ", ";
181 buffer ~= "underlined";
182 }
183 if (strikeout) {
184 if (buffer.length > startLength) buffer ~= ", ";
185 buffer ~= "striked out";
186 }
187 if (rise != 0) {
188 if (buffer.length > startLength) buffer ~= ", ";
189 buffer ~= "rise=";
190 buffer ~= to!(char[])(rise);
191 }
192 if (metrics != null) {
193 if (buffer.length > startLength) buffer ~= ", ";
194 buffer ~= "metrics=";
195 buffer ~= metrics.toString;
196 }
197 buffer ~= "}";
198 return buffer;
199 }
200
201 }