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