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