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.graphics.TextStyle; 14 15 import java.lang.all; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.graphics.Font; 19 import org.eclipse.swt.graphics.Color; 20 import org.eclipse.swt.graphics.GlyphMetrics; 21 22 version(Tango){ 23 import tango.util.Convert; 24 } else { // Phobos 25 import std.conv; 26 } 27 /** 28 * <code>TextStyle</code> defines a set of styles that can be applied 29 * to a 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 * <p> 37 * Application code does <em>not</em> need to explicitly release the 38 * resources managed by each instance when those instances are no longer 39 * required, and thus no <code>dispose()</code> method is provided. 40 * </p> 41 * 42 * @see TextLayout 43 * @see Font 44 * @see Color 45 * @see <a href="http://www.eclipse.org/swt/snippets/#textlayout">TextLayout, TextStyle snippets</a> 46 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> 47 * 48 * @since 3.0 49 */ 50 public class TextStyle { 51 52 /** 53 * the font of the style 54 */ 55 public Font font; 56 57 /** 58 * the foreground of the style 59 */ 60 public Color foreground; 61 62 /** 63 * the background of the style 64 */ 65 public Color background; 66 67 /** 68 * the underline flag of the style. The default underline 69 * style is <code>SWT.UNDERLINE_SINGLE</code>. 70 * 71 * 72 * @since 3.1 73 */ 74 public bool underline; 75 76 /** 77 * the underline color of the style 78 * 79 * @since 3.4 80 */ 81 public Color underlineColor; 82 83 /** 84 * the underline style. This style is ignored when 85 * <code>underline</code> is false. 86 * <p> 87 * This value should be one of <code>SWT.UNDERLINE_SINGLE</code>, 88 * <code>SWT.UNDERLINE_DOUBLE</code>, <code>SWT.UNDERLINE_ERROR</code>, 89 * or <code>SWT.UNDERLINE_SQUIGGLE</code>. 90 * </p> 91 * 92 * @see SWT#UNDERLINE_SINGLE 93 * @see SWT#UNDERLINE_DOUBLE 94 * @see SWT#UNDERLINE_ERROR 95 * @see SWT#UNDERLINE_SQUIGGLE 96 * 97 * @since 3.4 98 */ 99 public int underlineStyle; 100 101 /** 102 * the strikeout flag of the style 103 * 104 * @since 3.1 105 */ 106 public bool strikeout; 107 108 /** 109 * the strikeout color of the style 110 * 111 * @since 3.4 112 */ 113 public Color strikeoutColor; 114 115 /** 116 * the border style. The default border style is <code>SWT.NONE</code>. 117 * <p> 118 * This value should be one of <code>SWT.BORDER_SOLID</code>, 119 * <code>SWT.BORDER_DASH</code>,<code>SWT.BORDER_DOT</code> or 120 * <code>SWT.NONE</code>. 121 * </p> 122 * 123 * @see SWT#BORDER_SOLID 124 * @see SWT#BORDER_DASH 125 * @see SWT#BORDER_DOT 126 * @see SWT#NONE 127 * 128 * @since 3.4 129 */ 130 public int borderStyle; 131 132 /** 133 * the border color of the style 134 * 135 * @since 3.4 136 */ 137 public Color borderColor; 138 139 /** 140 * the GlyphMetrics of the style 141 * 142 * @since 3.2 143 */ 144 public GlyphMetrics metrics; 145 146 /** 147 * the baseline rise of the style. 148 * 149 * @since 3.2 150 */ 151 public int rise; 152 153 /** 154 * Create an empty text style. 155 * 156 * @since 3.4 157 */ 158 public this () { 159 } 160 161 /** 162 * Create a new text style with the specified font, foreground 163 * and background. 164 * 165 * @param font the font of the style, <code>null</code> if none 166 * @param foreground the foreground color of the style, <code>null</code> if none 167 * @param background the background color of the style, <code>null</code> if none 168 */ 169 public this (Font font, Color foreground, Color background) { 170 if (font !is null && font.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT); 171 if (foreground !is null && foreground.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT); 172 if (background !is null && background.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT); 173 this.font = font; 174 this.foreground = foreground; 175 this.background = background; 176 } 177 178 179 /** 180 * Create a new text style from an existing text style. 181 * 182 *@param style the style to copy 183 * 184 * @since 3.4 185 */ 186 public this (TextStyle style) { 187 if (style is null) SWT.error (SWT.ERROR_INVALID_ARGUMENT); 188 font = style.font; 189 foreground = style.foreground; 190 background = style.background; 191 underline = style.underline; 192 underlineColor = style.underlineColor; 193 underlineStyle = style.underlineStyle; 194 strikeout = style.strikeout; 195 strikeoutColor = style.strikeoutColor; 196 borderStyle = style.borderStyle; 197 borderColor = style.borderColor; 198 metrics = style.metrics; 199 rise = style.rise; 200 } 201 202 /** 203 * Compares the argument to the receiver, and returns true 204 * if they represent the <em>same</em> object using a class 205 * specific comparison. 206 * 207 * @param object the object to compare with this object 208 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise 209 * 210 * @see #hashCode() 211 */ 212 public override equals_t opEquals(Object object) { 213 if (object is this) return true; 214 if (object is null) return false; 215 if (!(cast(TextStyle)object)) return false; 216 TextStyle style = cast(TextStyle)object; 217 if (foreground !is null) { 218 if ( foreground !is style.foreground ) return false; 219 } else if (style.foreground !is null) return false; 220 if (background !is null) { 221 if ( background !is style.background ) return false; 222 } else if (style.background !is null) return false; 223 if (font !is null) { 224 if (font !is style.font) return false; 225 } else if (style.font !is null) return false; 226 if (metrics !is null || style.metrics !is null) return false; 227 if (underline !is style.underline) return false; 228 if (underlineStyle !is style.underlineStyle) return false; 229 if (borderStyle !is style.borderStyle) return false; 230 if (strikeout !is style.strikeout) return false; 231 if (rise !is style.rise) return false; 232 if (underlineColor !is null) { 233 if (!underlineColor.opEquals(style.underlineColor)) return false; 234 } else if (style.underlineColor !is null) return false; 235 if (strikeoutColor !is null) { 236 if (!strikeoutColor.opEquals(style.strikeoutColor)) return false; 237 } else if (style.strikeoutColor !is null) return false; 238 if (underlineStyle !is style.underlineStyle) return false; 239 if (borderColor !is null) { 240 if (!borderColor.opEquals(style.borderColor)) return false; 241 } else if (style.borderColor !is null) return false; 242 return true; 243 } 244 245 /** 246 * Returns an integer hash code for the receiver. Any two 247 * objects that return <code>true</code> when passed to 248 * <code>equals</code> must return the same value for this 249 * method. 250 * 251 * @return the receiver's hash 252 * 253 * @see #equals(Object) 254 */ 255 public override hash_t toHash() { 256 int hash = 0; 257 if (foreground !is null) hash ^= foreground.toHash(); 258 if (background !is null) hash ^= background.toHash(); 259 if (font !is null) hash ^= font.toHash(); 260 if (metrics !is null) hash ^= metrics.toHash(); 261 if (underline) hash ^= hash; 262 if (strikeout) hash ^= hash; 263 hash ^= rise; 264 if (underlineColor !is null) hash ^= underlineColor.toHash(); 265 if (strikeoutColor !is null) hash ^= strikeoutColor.toHash(); 266 if (borderColor !is null) hash ^= borderColor.toHash(); 267 hash ^= underlineStyle; 268 return hash; 269 } 270 271 bool isAdherentBorder(TextStyle style) { 272 if (this is style) return true; 273 if (style is null) return false; 274 if (borderStyle !is style.borderStyle) return false; 275 if (borderColor !is null) { 276 if (!borderColor.opEquals(style.borderColor)) return false; 277 } else if (style.borderColor !is null) return false; 278 return true; 279 } 280 281 bool isAdherentUnderline(TextStyle style) { 282 if (this is style) return true; 283 if (style is null) return false; 284 if (underline !is style.underline) return false; 285 if (underlineStyle !is style.underlineStyle) return false; 286 if (underlineColor !is null) { 287 if (!underlineColor.opEquals(style.underlineColor)) return false; 288 } else if (style.underlineColor !is null) return false; 289 return true; 290 } 291 292 bool isAdherentStrikeout(TextStyle style) { 293 if (this is style) return true; 294 if (style is null) return false; 295 if (strikeout !is style.strikeout) return false; 296 if (strikeoutColor !is null) { 297 if (!strikeoutColor.opEquals(style.strikeoutColor)) return false; 298 } else if (style.strikeoutColor !is null) return false; 299 return true; 300 } 301 302 /** 303 * Returns a string containing a concise, human-readable 304 * description of the receiver. 305 * 306 * @return a string representation of the <code>TextStyle</code> 307 */ 308 override public String toString () { 309 String buffer = "TextStyle {"; 310 auto startLength = buffer.length; 311 if (font !is null) { 312 if (buffer.length > startLength) buffer ~= ", "; 313 buffer ~= "font="; 314 buffer ~= font.toString; 315 } 316 if (foreground !is null) { 317 if (buffer.length > startLength) buffer ~= ", "; 318 buffer ~= "foreground="; 319 buffer ~= foreground.toString; 320 } 321 if (background !is null) { 322 if (buffer.length > startLength) buffer ~= ", "; 323 buffer ~= "background="; 324 buffer ~= background.toString; 325 } 326 if (underline) { 327 if (buffer.length > startLength) buffer ~= ", "; 328 buffer ~= "underlined"; 329 } 330 if (strikeout) { 331 if (buffer.length > startLength) buffer ~= ", "; 332 buffer ~= "striked out"; 333 } 334 if (rise !is 0) { 335 if (buffer.length > startLength) buffer ~= ", "; 336 buffer ~= "rise="; 337 buffer ~= to!(String)(rise); 338 } 339 if (metrics !is null) { 340 if (buffer.length > startLength) buffer ~= ", "; 341 buffer ~= "metrics="; 342 buffer ~= metrics.toString; 343 } 344 buffer ~= "}"; 345 return buffer; 346 } 347 348 }