Mercurial > projects > dwt-win
annotate dwt/custom/StyleRange.d @ 213:36f5cb12e1a2
Update to SWT 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 17 May 2008 17:34:28 +0200 |
parents | ab60f3309436 |
children | fd9c62a2998e |
rev | line source |
---|---|
155 | 1 /******************************************************************************* |
213 | 2 * Copyright (c) 2000, 2007 IBM Corporation and others. |
155 | 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 | |
49 /** | |
50 * Create a new style range with no styles | |
51 * | |
52 * @since 3.2 | |
53 */ | |
54 public this() { | |
213 | 55 } |
56 | |
57 /** | |
58 * Create a new style range from an existing text style. | |
59 * | |
60 *@param style the text style to copy | |
61 * | |
62 *@since 3.4 | |
63 */ | |
64 public this(TextStyle style) { | |
65 super(style); | |
155 | 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; | |
213 | 147 if (borderStyle !is DWT.NONE) return false; |
155 | 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 */ | |
170 public /+override+/ Object clone() { | |
173
97c2675aca38
Fix: missing styles in StyleRange
Frank Benoit <benoit@tionex.de>
parents:
155
diff
changeset
|
171 return new StyleRange( this ); |
155 | 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 */ | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
173
diff
changeset
|
180 public override String toString() { |
155 | 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 } | |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
173
diff
changeset
|
200 String str = super.toString(); |
155 | 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 } |