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