Mercurial > projects > dwt-mac
comparison dwt/custom/StyleRange.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.custom; | |
12 | |
13 import dwt.*; | |
14 import dwt.graphics.*; | |
15 import dwt.internal.CloneableCompatibility; | |
16 | |
17 public class StyleRange : TextStyle : CloneableCompatibility { | |
18 | |
19 /** | |
20 * the start offset of the range, zero-based from the document start | |
21 */ | |
22 public int start; | |
23 | |
24 /** | |
25 * the length of the range | |
26 */ | |
27 public int length; | |
28 | |
29 /** | |
30 * the font style of the range. It may be a combination of | |
31 * DWT.NORMAL, DWT.ITALIC or DWT.BOLD | |
32 * | |
33 * Note: the font style is not used if the <code>font</code> attribute | |
34 * is set | |
35 */ | |
36 public int fontStyle = DWT.NORMAL; | |
37 | |
38 /** | |
39 * Create a new style range with no styles | |
40 * | |
41 * @since 3.2 | |
42 */ | |
43 public StyleRange() { | |
44 } | |
45 | |
46 /** | |
47 * Create a new style range from an existing text style. | |
48 * | |
49 *@param style the text style to copy | |
50 * | |
51 *@since 3.4 | |
52 */ | |
53 public StyleRange(TextStyle style) { | |
54 super(style); | |
55 } | |
56 | |
57 /** | |
58 * Create a new style range. | |
59 * | |
60 * @param start start offset of the style | |
61 * @param length length of the style | |
62 * @param foreground foreground color of the style, null if none | |
63 * @param background background color of the style, null if none | |
64 */ | |
65 public StyleRange(int start, int length, Color foreground, Color background) { | |
66 super(null, foreground, background); | |
67 this.start = start; | |
68 this.length = length; | |
69 } | |
70 | |
71 /** | |
72 * Create a new style range. | |
73 * | |
74 * @param start start offset of the style | |
75 * @param length length of the style | |
76 * @param foreground foreground color of the style, null if none | |
77 * @param background background color of the style, null if none | |
78 * @param fontStyle font style of the style, may be DWT.NORMAL, DWT.ITALIC or DWT.BOLD | |
79 */ | |
80 public StyleRange(int start, int length, Color foreground, Color background, int fontStyle) { | |
81 this(start, length, foreground, background); | |
82 this.fontStyle = fontStyle; | |
83 } | |
84 | |
85 /** | |
86 * Compares the argument to the receiver, and returns true | |
87 * if they represent the <em>same</em> object using a class | |
88 * specific comparison. | |
89 * | |
90 * @param object the object to compare with this object | |
91 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise | |
92 * | |
93 * @see #toHash() | |
94 */ | |
95 public bool opEquals(Object object) { | |
96 if (object is this) return true; | |
97 if (object instanceof StyleRange) { | |
98 StyleRange style = (StyleRange)object; | |
99 if (start !is style.start) return false; | |
100 if (length !is style.length) return false; | |
101 return similarTo(style); | |
102 } | |
103 return false; | |
104 } | |
105 | |
106 /** | |
107 * Returns an integer hash code for the receiver. Any two | |
108 * objects that return <code>true</code> when passed to | |
109 * <code>opEquals</code> must return the same value for this | |
110 * method. | |
111 * | |
112 * @return the receiver's hash | |
113 * | |
114 * @see #opEquals(Object) | |
115 */ | |
116 public int toHash() { | |
117 return super.toHash() ^ fontStyle; | |
118 } | |
119 bool isVariableHeight() { | |
120 return font !is null || metrics !is null || rise !is 0; | |
121 } | |
122 /** | |
123 * Returns whether or not the receiver is unstyled (i.e., does not have any | |
124 * style attributes specified). | |
125 * | |
126 * @return true if the receiver is unstyled, false otherwise. | |
127 */ | |
128 public bool isUnstyled() { | |
129 if (font !is null) return false; | |
130 if (rise !is 0) return false; | |
131 if (metrics !is null) return false; | |
132 if (foreground !is null) return false; | |
133 if (background !is null) return false; | |
134 if (fontStyle !is DWT.NORMAL) return false; | |
135 if (underline) return false; | |
136 if (strikeout) return false; | |
137 if (borderStyle !is DWT.NONE) return false; | |
138 return true; | |
139 } | |
140 | |
141 /** | |
142 * Compares the specified object to this StyleRange and answer if the two | |
143 * are similar. The object must be an instance of StyleRange and have the | |
144 * same field values for except for start and length. | |
145 * | |
146 * @param style the object to compare with this object | |
147 * @return true if the objects are similar, false otherwise | |
148 */ | |
149 public bool similarTo(StyleRange style) { | |
150 if (!super.opEquals(style)) return false; | |
151 if (fontStyle !is style.fontStyle) return false; | |
152 return true; | |
153 } | |
154 | |
155 /** | |
156 * Returns a new StyleRange with the same values as this StyleRange. | |
157 * | |
158 * @return a shallow copy of this StyleRange | |
159 */ | |
160 public Object clone() { | |
161 try { | |
162 return super.clone(); | |
163 } catch (CloneNotSupportedException e) { | |
164 return null; | |
165 } | |
166 } | |
167 | |
168 /** | |
169 * Returns a String containing a concise, human-readable | |
170 * description of the receiver. | |
171 * | |
172 * @return a String representation of the StyleRange | |
173 */ | |
174 public String toString() { | |
175 StringBuffer buffer = new StringBuffer(); | |
176 buffer.append("StyleRange {"); | |
177 buffer.append(start); | |
178 buffer.append(", "); | |
179 buffer.append(length); | |
180 buffer.append(", fontStyle="); | |
181 switch (fontStyle) { | |
182 case DWT.BOLD: | |
183 buffer.append("bold"); | |
184 break; | |
185 case DWT.ITALIC: | |
186 buffer.append("italic"); | |
187 break; | |
188 case DWT.BOLD | DWT.ITALIC: | |
189 buffer.append("bold-italic"); | |
190 break; | |
191 default: | |
192 buffer.append("normal"); | |
193 } | |
194 String str = super.toString(); | |
195 int index = str.indexOf('{'); | |
196 str = str.substring(index + 1); | |
197 if (str.length() > 1) buffer.append(", "); | |
198 buffer.append(str); | |
199 return buffer.toString(); | |
200 } | |
201 } |