Mercurial > projects > dwt-linux
annotate dwt/custom/StyleRange.d @ 259:c0d810de7093
Update SWT 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 29 Jun 2008 14:33:38 +0200 |
parents | ce446666f5a2 |
children |
rev | line source |
---|---|
115
52b32f5cb1e0
many file checked for switch default
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
1 /******************************************************************************* |
259 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
95 | 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 | |
108 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
95 | 12 *******************************************************************************/ |
13 module dwt.custom.StyleRange; | |
14 | |
238 | 15 import dwt.dwthelper.utils; |
16 | |
95 | 17 |
18 import dwt.DWT; | |
19 import dwt.graphics.Color; | |
20 import dwt.graphics.TextStyle; | |
21 import dwt.internal.CloneableCompatibility; | |
102
12e3e0e477cd
package custom, module and imports ok
Frank Benoit <benoit@tionex.de>
parents:
95
diff
changeset
|
22 import dwt.custom.StyleRange; |
12e3e0e477cd
package custom, module and imports ok
Frank Benoit <benoit@tionex.de>
parents:
95
diff
changeset
|
23 import dwt.custom.TextChangedEvent; |
12e3e0e477cd
package custom, module and imports ok
Frank Benoit <benoit@tionex.de>
parents:
95
diff
changeset
|
24 import dwt.custom.TextChangingEvent; |
95 | 25 |
26 static import tango.text.Text; | |
27 alias tango.text.Text.Text!(char) StringBuffer; | |
28 | |
259 | 29 /** |
30 * <code>StyleRange</code> defines a set of styles for a specified | |
31 * range of text. | |
32 * <p> | |
33 * The hashCode() method in this class uses the values of the public | |
34 * fields to compute the hash value. When storing instances of the | |
35 * class in hashed collections, do not modify these fields after the | |
36 * object has been inserted. | |
37 * </p> | |
38 * | |
39 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
40 */ | |
95 | 41 public class StyleRange : TextStyle, CloneableCompatibility { |
42 | |
43 /** | |
44 * the start offset of the range, zero-based from the document start | |
45 */ | |
46 public int start; | |
47 | |
48 /** | |
49 * the length of the range | |
50 */ | |
51 public int length; | |
52 | |
53 /** | |
54 * the font style of the range. It may be a combination of | |
55 * DWT.NORMAL, DWT.ITALIC or DWT.BOLD | |
56 * | |
57 * Note: the font style is not used if the <code>font</code> attribute | |
58 * is set | |
59 */ | |
60 public int fontStyle = DWT.NORMAL; | |
61 | |
240 | 62 /** |
63 * Create a new style range with no styles | |
64 * | |
65 * @since 3.2 | |
66 */ | |
67 public this() { | |
68 } | |
187
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
69 /++ |
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
70 + DWT extension for clone implementation |
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
71 +/ |
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
72 protected this( StyleRange other ){ |
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
73 super( other ); |
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
74 start = other.start; |
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
75 length = other.length; |
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
76 fontStyle = other.fontStyle; |
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
77 } |
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
78 |
95 | 79 /** |
240 | 80 * Create a new style range from an existing text style. |
81 * | |
259 | 82 * @param style the text style to copy |
95 | 83 * |
259 | 84 * @since 3.4 |
95 | 85 */ |
240 | 86 public this(TextStyle style) { |
87 super(style); | |
95 | 88 } |
89 | |
90 /** | |
91 * Create a new style range. | |
92 * | |
93 * @param start start offset of the style | |
94 * @param length length of the style | |
95 * @param foreground foreground color of the style, null if none | |
96 * @param background background color of the style, null if none | |
97 */ | |
98 public this(int start, int length, Color foreground, Color background) { | |
99 super(null, foreground, background); | |
100 this.start = start; | |
101 this.length = length; | |
102 } | |
103 | |
104 /** | |
105 * Create a new style range. | |
106 * | |
107 * @param start start offset of the style | |
108 * @param length length of the style | |
109 * @param foreground foreground color of the style, null if none | |
110 * @param background background color of the style, null if none | |
111 * @param fontStyle font style of the style, may be DWT.NORMAL, DWT.ITALIC or DWT.BOLD | |
112 */ | |
113 public this(int start, int length, Color foreground, Color background, int fontStyle) { | |
114 this(start, length, foreground, background); | |
115 this.fontStyle = fontStyle; | |
116 } | |
117 | |
118 /** | |
119 * Compares the argument to the receiver, and returns true | |
120 * if they represent the <em>same</em> object using a class | |
121 * specific comparison. | |
122 * | |
123 * @param object the object to compare with this object | |
124 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise | |
125 * | |
126 * @see #hashCode() | |
127 */ | |
128 public override int opEquals(Object object) { | |
129 if (object is this) return true; | |
130 if (auto style = cast(StyleRange) object ) { | |
131 if (start !is style.start) return false; | |
132 if (length !is style.length) return false; | |
133 return similarTo(style); | |
134 } | |
135 return false; | |
136 } | |
137 | |
138 /** | |
139 * Returns an integer hash code for the receiver. Any two | |
140 * objects that return <code>true</code> when passed to | |
141 * <code>equals</code> must return the same value for this | |
142 * method. | |
143 * | |
144 * @return the receiver's hash | |
145 * | |
146 * @see #equals(Object) | |
147 */ | |
148 public override hash_t toHash() { | |
149 return super.toHash() ^ fontStyle; | |
150 } | |
151 bool isVariableHeight() { | |
152 return font !is null || metrics !is null || rise !is 0; | |
153 } | |
154 /** | |
155 * Returns whether or not the receiver is unstyled (i.e., does not have any | |
156 * style attributes specified). | |
157 * | |
158 * @return true if the receiver is unstyled, false otherwise. | |
159 */ | |
160 public bool isUnstyled() { | |
161 if (font !is null) return false; | |
162 if (rise !is 0) return false; | |
163 if (metrics !is null) return false; | |
164 if (foreground !is null) return false; | |
165 if (background !is null) return false; | |
166 if (fontStyle !is DWT.NORMAL) return false; | |
167 if (underline) return false; | |
168 if (strikeout) return false; | |
240 | 169 if (borderStyle !is DWT.NONE) return false; |
95 | 170 return true; |
171 } | |
172 | |
173 /** | |
174 * Compares the specified object to this StyleRange and answer if the two | |
175 * are similar. The object must be an instance of StyleRange and have the | |
176 * same field values for except for start and length. | |
177 * | |
178 * @param style the object to compare with this object | |
179 * @return true if the objects are similar, false otherwise | |
180 */ | |
181 public bool similarTo(StyleRange style) { | |
182 if (!super.opEquals(style)) return false; | |
183 if (fontStyle !is style.fontStyle) return false; | |
184 return true; | |
185 } | |
186 | |
187 /** | |
188 * Returns a new StyleRange with the same values as this StyleRange. | |
189 * | |
190 * @return a shallow copy of this StyleRange | |
191 */ | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
115
diff
changeset
|
192 public /+override+/ Object clone() { |
187
cd8dc3f19679
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
193 return new StyleRange( this ); |
95 | 194 } |
195 | |
196 /** | |
197 * Returns a string containing a concise, human-readable | |
198 * description of the receiver. | |
199 * | |
200 * @return a string representation of the StyleRange | |
201 */ | |
238 | 202 public override String toString() { |
95 | 203 StringBuffer buffer = new StringBuffer(); |
204 buffer.append("StyleRange {"); | |
205 buffer.append(start); | |
206 buffer.append(", "); | |
207 buffer.append(length); | |
208 buffer.append(", fontStyle="); | |
209 switch (fontStyle) { | |
210 case DWT.BOLD: | |
211 buffer.append("bold"); | |
212 break; | |
213 case DWT.ITALIC: | |
214 buffer.append("italic"); | |
215 break; | |
216 case DWT.BOLD | DWT.ITALIC: | |
217 buffer.append("bold-italic"); | |
218 break; | |
219 default: | |
220 buffer.append("normal"); | |
221 } | |
238 | 222 String str = super.toString(); |
95 | 223 int index = tango.text.Util.locate( str, '{'); |
224 if( index is str.length ) index = -1; | |
225 str = str[ index + 1 .. $ ]; | |
226 if (str.length > 1) buffer.append(", "); | |
227 buffer.append(str); | |
228 return buffer.toString(); | |
229 } | |
230 } |