Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Utils/Styling/TextStyle.cs
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Utils/Styling/TextStyle.cs
r2768 r4068 1 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 2 using System.ComponentModel; 4 3 using System.Drawing; 5 using System.ComponentModel;6 7 4 using ToolBox.Formatting; 8 5 9 namespace Netron.Diagramming.Core 10 { 11 // ---------------------------------------------------------------------- 12 /// <summary> 13 /// Specifies formatting for text to be drawn (such as color, alignment, 14 /// font size, etc.). The event 'TextStyleChanged' is raised when a 15 /// property is changed. 16 /// </summary> 17 // ---------------------------------------------------------------------- 18 public partial class TextStyle : ITextStyle 19 { 20 // ------------------------------------------------------------------ 21 /// <summary> 22 /// Event raised when this TextStyle is changed. 23 /// </summary> 24 // ------------------------------------------------------------------ 25 [field : NonSerialized()] 26 public event TextStyleChangedEventHandler TextStyleChanged; 27 28 // ------------------------------------------------------------------ 29 /// <summary> 30 /// Implementation of IVersion - the current version of 31 /// TextStyle. 32 /// </summary> 33 // ------------------------------------------------------------------ 34 protected const double textStyleVersion = 1.0; 35 36 // ------------------------------------------------------------------ 37 /// <summary> 38 /// Specifies the number of decimal places to use when a TextFormat 39 /// other than 'String' is specified. 40 /// </summary> 41 // ------------------------------------------------------------------ 42 protected int mDecimalPlaces = 2; 43 44 // ------------------------------------------------------------------ 45 /// <summary> 46 /// The formatting to apply to the text. 47 /// </summary> 48 // ------------------------------------------------------------------ 49 protected TextFormat mTextFormat = TextFormat.String; 50 51 // ------------------------------------------------------------------ 52 /// <summary> 53 /// The font. 54 /// </summary> 55 // ------------------------------------------------------------------ 56 protected Font mFont = new Font("Arial", 10); 57 58 // ------------------------------------------------------------------ 59 /// <summary> 60 /// The fontstyle to use (regular, bold, italics, etc.). 61 /// </summary> 62 // ------------------------------------------------------------------ 63 protected FontStyle mFontStyle = FontStyle.Regular; 64 65 // ------------------------------------------------------------------ 66 /// <summary> 67 /// The color for the text; 68 /// </summary> 69 // ------------------------------------------------------------------ 70 protected Color mFontColor = Color.Black; 71 72 // ------------------------------------------------------------------ 73 /// <summary> 74 /// The horizontal alignment of the text. 75 /// </summary> 76 // ------------------------------------------------------------------ 77 protected StringAlignment mHorizontalAlignment = 78 StringAlignment.Center; 79 80 // ------------------------------------------------------------------ 81 /// <summary> 82 /// The vertical alignment of the text. 83 /// </summary> 84 // ------------------------------------------------------------------ 85 protected StringAlignment mVerticalAlignment = 86 StringAlignment.Center; 87 88 // ------------------------------------------------------------------ 89 /// <summary> 90 /// Specifies if the font is bold. 91 /// </summary> 92 // ------------------------------------------------------------------ 93 protected bool mIsBold = false; 94 95 // ------------------------------------------------------------------ 96 /// <summary> 97 /// Specifies if the font is italic. 98 /// </summary> 99 // ------------------------------------------------------------------ 100 protected bool mIsItalic = false; 101 102 // ------------------------------------------------------------------ 103 /// <summary> 104 /// Specifies if the font is underlined. 105 /// </summary> 106 // ------------------------------------------------------------------ 107 protected bool mIsUnderline = false; 108 109 #region ITextStyle Members 110 111 // ------------------------------------------------------------------ 112 /// <summary> 113 /// Gets the current version. 114 /// </summary> 115 // ------------------------------------------------------------------ 116 public virtual double Version 117 { 118 get 119 { 120 return textStyleVersion; 121 } 122 } 123 124 // ------------------------------------------------------------------ 125 /// <summary> 126 /// Gets or sets the number of decimal places to use when a TextFormat 127 /// other than 'String' is specified. 128 /// </summary> 129 // ------------------------------------------------------------------ 130 public virtual int DecimalPlaces 131 { 132 get 133 { 134 return mDecimalPlaces; 135 } 136 set 137 { 138 mDecimalPlaces = value; 139 RaiseTextStyleChanged(); 140 } 141 } 142 143 // ------------------------------------------------------------------ 144 /// <summary> 145 /// Gets or sets the formatting to apply to the text. 146 /// </summary> 147 // ------------------------------------------------------------------ 148 public virtual TextFormat TextFormat 149 { 150 get 151 { 152 return mTextFormat; 153 } 154 set 155 { 156 mTextFormat = value; 157 RaiseTextStyleChanged(); 158 } 159 } 160 161 // ------------------------------------------------------------------ 162 /// <summary> 163 /// Gets or sets the text's font. 164 /// </summary> 165 // ------------------------------------------------------------------ 166 public virtual Font Font 167 { 168 get 169 { 170 return this.mFont; 171 } 172 set 173 { 174 this.mFont = value; 175 RaiseTextStyleChanged(); 176 } 177 } 178 179 // ------------------------------------------------------------------ 180 /// <summary> 181 /// Gets or sets the text's font size. 182 /// </summary> 183 // ------------------------------------------------------------------ 184 public virtual float FontSize 185 { 186 get 187 { 188 return this.mFont.Size; 189 } 190 set 191 { 192 this.mFont = new Font( 193 mFont.FontFamily, 194 value, 195 mFont.Style); 196 RaiseTextStyleChanged(); 197 } 198 } 199 200 // ------------------------------------------------------------------ 201 /// <summary> 202 /// Gets or sets the text's color. 203 /// </summary> 204 // ------------------------------------------------------------------ 205 public virtual Color FontColor 206 { 207 get 208 { 209 return mFontColor; 210 } 211 set 212 { 213 mFontColor = value; 214 RaiseTextStyleChanged(); 215 } 216 } 217 218 // ------------------------------------------------------------------ 219 /// <summary> 220 /// Gets or sets if the text is underlined. 221 /// </summary> 222 // ------------------------------------------------------------------ 223 public virtual bool IsUnderlined 224 { 225 get 226 { 227 return mFont.Underline; 228 } 229 set 230 { 231 mIsUnderline = value; 232 SetFontStyle(); 233 RaiseTextStyleChanged(); 234 } 235 } 236 237 // ------------------------------------------------------------------ 238 /// <summary> 239 /// Gets or sets if the text is bold. 240 /// </summary> 241 // ------------------------------------------------------------------ 242 public virtual bool IsBold 243 { 244 get 245 { 246 return mFont.Bold; 247 } 248 set 249 { 250 mIsBold = value; 251 SetFontStyle(); 252 RaiseTextStyleChanged(); 253 } 254 } 255 256 // ------------------------------------------------------------------ 257 /// <summary> 258 /// Gets or sets if the text is italic. 259 /// </summary> 260 // ------------------------------------------------------------------ 261 public virtual bool IsItalic 262 { 263 get 264 { 265 return mFont.Italic; 266 } 267 set 268 { 269 mIsItalic = value; 270 SetFontStyle(); 271 RaiseTextStyleChanged(); 272 } 273 } 274 275 // ------------------------------------------------------------------ 276 /// <summary> 277 /// Gets or sets the horizontal alignment of the text. 278 /// </summary> 279 // ------------------------------------------------------------------ 280 [Browsable(true), 281 Description("The horizontal alignment of the text."), 282 Category("Layout")] 283 public virtual StringAlignment HorizontalAlignment 284 { 285 get 286 { 287 return mHorizontalAlignment; 288 } 289 set 290 { 291 mHorizontalAlignment = value; 292 RaiseTextStyleChanged(); 293 } 294 } 295 296 // ------------------------------------------------------------------ 297 /// <summary> 298 /// Gets or sets the vertical alignment of the text. 299 /// </summary> 300 // ------------------------------------------------------------------ 301 [Browsable(true), 302 Description("The vertical alignment of the text."), 303 Category("Layout")] 304 public virtual StringAlignment VerticalAlignment 305 { 306 get 307 { 308 return mVerticalAlignment; 309 } 310 set 311 { 312 mVerticalAlignment = value; 313 RaiseTextStyleChanged(); 314 } 315 } 316 317 // ------------------------------------------------------------------ 318 /// <summary> 319 /// Gets the format of the string. 320 /// </summary> 321 // ------------------------------------------------------------------ 322 public virtual StringFormat StringFormat 323 { 324 get 325 { 326 StringFormat format = new StringFormat(); 327 format.Alignment = this.VerticalAlignment; 328 format.LineAlignment = this.HorizontalAlignment; 329 return format; 330 } 331 } 332 333 #endregion 334 335 #region Constructors 336 337 // ------------------------------------------------------------------ 338 /// <summary> 339 /// Default constructor. 340 /// </summary> 341 // ------------------------------------------------------------------ 342 public TextStyle() 343 { 344 } 345 346 // ------------------------------------------------------------------ 347 /// <summary> 348 /// Constructor that receives the color of the font. 349 /// </summary> 350 /// <param name="color">Color</param> 351 // ------------------------------------------------------------------ 352 public TextStyle(Color color) 353 { 354 mFontColor = color; 355 } 356 357 // ------------------------------------------------------------------ 358 /// <summary> 359 /// Constructor that receives the font color, font, horizontal 360 /// alignment, and vertical alignment. 361 /// </summary> 362 /// <param name="color">Color</param> 363 /// <param name="font">Font</param> 364 /// <param name="horizontalAlignment">StringAlignment</param> 365 /// <param name="verticalAlignment">StringAlignment</param> 366 // ------------------------------------------------------------------ 367 public TextStyle( 368 Color color, 369 Font font, 370 StringAlignment horizontalAlignment, 371 StringAlignment verticalAlignment) 372 { 373 mFontColor = color; 374 mFont = font; 375 mHorizontalAlignment = horizontalAlignment; 376 mVerticalAlignment = verticalAlignment; 377 } 378 379 #endregion 380 381 // ------------------------------------------------------------------ 382 /// <summary> 383 /// Creates a new font style using the current IsBold, IsItalic, 384 /// and IsUnderline properties. 385 /// </summary> 386 // ------------------------------------------------------------------ 387 protected virtual void SetFontStyle() 388 { 389 FontStyle style = FontStyle.Regular; 390 391 if (mIsBold) 392 { 393 style = style | FontStyle.Bold; 394 } 395 396 if (mIsItalic) 397 { 398 style = style | FontStyle.Italic; 399 } 400 401 if (mIsUnderline) 402 { 403 style = style | FontStyle.Underline; 404 } 405 406 this.mFont = new Font(mFont, style); 407 } 408 409 // ------------------------------------------------------------------ 410 /// <summary> 411 /// Gets the brush that's used to draw our text to a GDI+ graphics 412 /// surface. 413 /// </summary> 414 /// <returns>Brush</returns> 415 // ------------------------------------------------------------------ 416 public virtual Brush GetBrush() 417 { 418 return new SolidBrush(mFontColor); 419 } 420 421 // ------------------------------------------------------------------ 422 /// <summary> 423 /// Applies the formatting specified by TextFormatting to the text 424 /// specified. 425 /// </summary> 426 /// <param name="text">string: The un-formatted text.</param> 427 /// <returns>string: The formatted text.</returns> 428 // ------------------------------------------------------------------ 429 public virtual string GetFormattedText(string text) 430 { 431 return TextFormatter.Format( 432 mTextFormat, 433 text, 434 mDecimalPlaces); 435 } 436 437 // ------------------------------------------------------------------ 438 /// <summary> 439 /// Raises the TextStyleChanged event. 440 /// </summary> 441 // ------------------------------------------------------------------ 442 protected virtual void RaiseTextStyleChanged() 443 { 444 if (this.TextStyleChanged != null) 445 { 446 // Raise the event 447 this.TextStyleChanged( 448 this, 449 new TextStyleChangedEventArgs(this)); 450 } 451 } 452 } 6 namespace Netron.Diagramming.Core { 7 // ---------------------------------------------------------------------- 8 /// <summary> 9 /// Specifies formatting for text to be drawn (such as color, alignment, 10 /// font size, etc.). The event 'TextStyleChanged' is raised when a 11 /// property is changed. 12 /// </summary> 13 // ---------------------------------------------------------------------- 14 public partial class TextStyle : ITextStyle { 15 // ------------------------------------------------------------------ 16 /// <summary> 17 /// Event raised when this TextStyle is changed. 18 /// </summary> 19 // ------------------------------------------------------------------ 20 [field: NonSerialized()] 21 public event TextStyleChangedEventHandler TextStyleChanged; 22 23 // ------------------------------------------------------------------ 24 /// <summary> 25 /// Implementation of IVersion - the current version of 26 /// TextStyle. 27 /// </summary> 28 // ------------------------------------------------------------------ 29 protected const double textStyleVersion = 1.0; 30 31 // ------------------------------------------------------------------ 32 /// <summary> 33 /// Specifies the number of decimal places to use when a TextFormat 34 /// other than 'String' is specified. 35 /// </summary> 36 // ------------------------------------------------------------------ 37 protected int mDecimalPlaces = 2; 38 39 // ------------------------------------------------------------------ 40 /// <summary> 41 /// The formatting to apply to the text. 42 /// </summary> 43 // ------------------------------------------------------------------ 44 protected TextFormat mTextFormat = TextFormat.String; 45 46 // ------------------------------------------------------------------ 47 /// <summary> 48 /// The font. 49 /// </summary> 50 // ------------------------------------------------------------------ 51 protected Font mFont = new Font("Arial", 10); 52 53 // ------------------------------------------------------------------ 54 /// <summary> 55 /// The fontstyle to use (regular, bold, italics, etc.). 56 /// </summary> 57 // ------------------------------------------------------------------ 58 protected FontStyle mFontStyle = FontStyle.Regular; 59 60 // ------------------------------------------------------------------ 61 /// <summary> 62 /// The color for the text; 63 /// </summary> 64 // ------------------------------------------------------------------ 65 protected Color mFontColor = Color.Black; 66 67 // ------------------------------------------------------------------ 68 /// <summary> 69 /// The horizontal alignment of the text. 70 /// </summary> 71 // ------------------------------------------------------------------ 72 protected StringAlignment mHorizontalAlignment = 73 StringAlignment.Center; 74 75 // ------------------------------------------------------------------ 76 /// <summary> 77 /// The vertical alignment of the text. 78 /// </summary> 79 // ------------------------------------------------------------------ 80 protected StringAlignment mVerticalAlignment = 81 StringAlignment.Center; 82 83 // ------------------------------------------------------------------ 84 /// <summary> 85 /// Specifies if the font is bold. 86 /// </summary> 87 // ------------------------------------------------------------------ 88 protected bool mIsBold = false; 89 90 // ------------------------------------------------------------------ 91 /// <summary> 92 /// Specifies if the font is italic. 93 /// </summary> 94 // ------------------------------------------------------------------ 95 protected bool mIsItalic = false; 96 97 // ------------------------------------------------------------------ 98 /// <summary> 99 /// Specifies if the font is underlined. 100 /// </summary> 101 // ------------------------------------------------------------------ 102 protected bool mIsUnderline = false; 103 104 #region ITextStyle Members 105 106 // ------------------------------------------------------------------ 107 /// <summary> 108 /// Gets the current version. 109 /// </summary> 110 // ------------------------------------------------------------------ 111 public virtual double Version { 112 get { 113 return textStyleVersion; 114 } 115 } 116 117 // ------------------------------------------------------------------ 118 /// <summary> 119 /// Gets or sets the number of decimal places to use when a TextFormat 120 /// other than 'String' is specified. 121 /// </summary> 122 // ------------------------------------------------------------------ 123 public virtual int DecimalPlaces { 124 get { 125 return mDecimalPlaces; 126 } 127 set { 128 mDecimalPlaces = value; 129 RaiseTextStyleChanged(); 130 } 131 } 132 133 // ------------------------------------------------------------------ 134 /// <summary> 135 /// Gets or sets the formatting to apply to the text. 136 /// </summary> 137 // ------------------------------------------------------------------ 138 public virtual TextFormat TextFormat { 139 get { 140 return mTextFormat; 141 } 142 set { 143 mTextFormat = value; 144 RaiseTextStyleChanged(); 145 } 146 } 147 148 // ------------------------------------------------------------------ 149 /// <summary> 150 /// Gets or sets the text's font. 151 /// </summary> 152 // ------------------------------------------------------------------ 153 public virtual Font Font { 154 get { 155 return this.mFont; 156 } 157 set { 158 this.mFont = value; 159 RaiseTextStyleChanged(); 160 } 161 } 162 163 // ------------------------------------------------------------------ 164 /// <summary> 165 /// Gets or sets the text's font size. 166 /// </summary> 167 // ------------------------------------------------------------------ 168 public virtual float FontSize { 169 get { 170 return this.mFont.Size; 171 } 172 set { 173 this.mFont = new Font( 174 mFont.FontFamily, 175 value, 176 mFont.Style); 177 RaiseTextStyleChanged(); 178 } 179 } 180 181 // ------------------------------------------------------------------ 182 /// <summary> 183 /// Gets or sets the text's color. 184 /// </summary> 185 // ------------------------------------------------------------------ 186 public virtual Color FontColor { 187 get { 188 return mFontColor; 189 } 190 set { 191 mFontColor = value; 192 RaiseTextStyleChanged(); 193 } 194 } 195 196 // ------------------------------------------------------------------ 197 /// <summary> 198 /// Gets or sets if the text is underlined. 199 /// </summary> 200 // ------------------------------------------------------------------ 201 public virtual bool IsUnderlined { 202 get { 203 return mFont.Underline; 204 } 205 set { 206 mIsUnderline = value; 207 SetFontStyle(); 208 RaiseTextStyleChanged(); 209 } 210 } 211 212 // ------------------------------------------------------------------ 213 /// <summary> 214 /// Gets or sets if the text is bold. 215 /// </summary> 216 // ------------------------------------------------------------------ 217 public virtual bool IsBold { 218 get { 219 return mFont.Bold; 220 } 221 set { 222 mIsBold = value; 223 SetFontStyle(); 224 RaiseTextStyleChanged(); 225 } 226 } 227 228 // ------------------------------------------------------------------ 229 /// <summary> 230 /// Gets or sets if the text is italic. 231 /// </summary> 232 // ------------------------------------------------------------------ 233 public virtual bool IsItalic { 234 get { 235 return mFont.Italic; 236 } 237 set { 238 mIsItalic = value; 239 SetFontStyle(); 240 RaiseTextStyleChanged(); 241 } 242 } 243 244 // ------------------------------------------------------------------ 245 /// <summary> 246 /// Gets or sets the horizontal alignment of the text. 247 /// </summary> 248 // ------------------------------------------------------------------ 249 [Browsable(true), 250 Description("The horizontal alignment of the text."), 251 Category("Layout")] 252 public virtual StringAlignment HorizontalAlignment { 253 get { 254 return mHorizontalAlignment; 255 } 256 set { 257 mHorizontalAlignment = value; 258 RaiseTextStyleChanged(); 259 } 260 } 261 262 // ------------------------------------------------------------------ 263 /// <summary> 264 /// Gets or sets the vertical alignment of the text. 265 /// </summary> 266 // ------------------------------------------------------------------ 267 [Browsable(true), 268 Description("The vertical alignment of the text."), 269 Category("Layout")] 270 public virtual StringAlignment VerticalAlignment { 271 get { 272 return mVerticalAlignment; 273 } 274 set { 275 mVerticalAlignment = value; 276 RaiseTextStyleChanged(); 277 } 278 } 279 280 // ------------------------------------------------------------------ 281 /// <summary> 282 /// Gets the format of the string. 283 /// </summary> 284 // ------------------------------------------------------------------ 285 public virtual StringFormat StringFormat { 286 get { 287 StringFormat format = new StringFormat(); 288 format.Alignment = this.VerticalAlignment; 289 format.LineAlignment = this.HorizontalAlignment; 290 return format; 291 } 292 } 293 294 #endregion 295 296 #region Constructors 297 298 // ------------------------------------------------------------------ 299 /// <summary> 300 /// Default constructor. 301 /// </summary> 302 // ------------------------------------------------------------------ 303 public TextStyle() { 304 } 305 306 // ------------------------------------------------------------------ 307 /// <summary> 308 /// Constructor that receives the color of the font. 309 /// </summary> 310 /// <param name="color">Color</param> 311 // ------------------------------------------------------------------ 312 public TextStyle(Color color) { 313 mFontColor = color; 314 } 315 316 // ------------------------------------------------------------------ 317 /// <summary> 318 /// Constructor that receives the font color, font, horizontal 319 /// alignment, and vertical alignment. 320 /// </summary> 321 /// <param name="color">Color</param> 322 /// <param name="font">Font</param> 323 /// <param name="horizontalAlignment">StringAlignment</param> 324 /// <param name="verticalAlignment">StringAlignment</param> 325 // ------------------------------------------------------------------ 326 public TextStyle( 327 Color color, 328 Font font, 329 StringAlignment horizontalAlignment, 330 StringAlignment verticalAlignment) { 331 mFontColor = color; 332 mFont = font; 333 mHorizontalAlignment = horizontalAlignment; 334 mVerticalAlignment = verticalAlignment; 335 } 336 337 #endregion 338 339 // ------------------------------------------------------------------ 340 /// <summary> 341 /// Creates a new font style using the current IsBold, IsItalic, 342 /// and IsUnderline properties. 343 /// </summary> 344 // ------------------------------------------------------------------ 345 protected virtual void SetFontStyle() { 346 FontStyle style = FontStyle.Regular; 347 348 if (mIsBold) { 349 style = style | FontStyle.Bold; 350 } 351 352 if (mIsItalic) { 353 style = style | FontStyle.Italic; 354 } 355 356 if (mIsUnderline) { 357 style = style | FontStyle.Underline; 358 } 359 360 this.mFont = new Font(mFont, style); 361 } 362 363 // ------------------------------------------------------------------ 364 /// <summary> 365 /// Gets the brush that's used to draw our text to a GDI+ graphics 366 /// surface. 367 /// </summary> 368 /// <returns>Brush</returns> 369 // ------------------------------------------------------------------ 370 public virtual Brush GetBrush() { 371 return new SolidBrush(mFontColor); 372 } 373 374 // ------------------------------------------------------------------ 375 /// <summary> 376 /// Applies the formatting specified by TextFormatting to the text 377 /// specified. 378 /// </summary> 379 /// <param name="text">string: The un-formatted text.</param> 380 /// <returns>string: The formatted text.</returns> 381 // ------------------------------------------------------------------ 382 public virtual string GetFormattedText(string text) { 383 return TextFormatter.Format( 384 mTextFormat, 385 text, 386 mDecimalPlaces); 387 } 388 389 // ------------------------------------------------------------------ 390 /// <summary> 391 /// Raises the TextStyleChanged event. 392 /// </summary> 393 // ------------------------------------------------------------------ 394 protected virtual void RaiseTextStyleChanged() { 395 if (this.TextStyleChanged != null) { 396 // Raise the event 397 this.TextStyleChanged( 398 this, 399 new TextStyleChangedEventArgs(this)); 400 } 401 } 402 } 453 403 }
Note: See TracChangeset
for help on using the changeset viewer.