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