Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/BaseClasses/SimpleShapeBase.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/BaseClasses/SimpleShapeBase.cs
r2768 r4068 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 1 using System.ComponentModel; 4 2 using System.Drawing; 5 3 using System.Windows.Forms; 6 using System.ComponentModel; 7 namespace Netron.Diagramming.Core 8 { 9 // ---------------------------------------------------------------------- 10 /// <summary> 11 /// Abstract base class for simple shapes. 12 /// <para>A simple shape does not contain sub-elements (aka shape 13 /// material, see <see cref="IShapeMaterial"/>) but this does 14 /// not mean you cannot paint sub-elements. In fact, this shape type 15 /// corresponds to the old base class in the previous Netron graph 16 /// library (i.e. before version 2.3). 17 /// <seealso cref="ComplexShapeBase"/> 18 /// </para> 19 /// </summary> 20 // ---------------------------------------------------------------------- 21 public abstract partial class SimpleShapeBase : 22 ShapeBase, 23 ISimpleShape, 24 ITextProvider 25 { 26 // ------------------------------------------------------------------ 27 /// <summary> 28 /// The amount to inflate the text area (mTextRectangle) by. 29 /// </summary> 30 // ------------------------------------------------------------------ 31 protected const int TextRectangleInflation = 5; 32 33 // ------------------------------------------------------------------ 34 /// <summary> 35 /// Implementation of IVersion - the current version of 36 /// SimpleShapeBase. 37 /// </summary> 38 // ------------------------------------------------------------------ 39 protected const double simpleShapeBaseVersion = 1.0; 40 41 #region Fields 42 43 // ------------------------------------------------------------------ 44 /// <summary> 45 /// Specifies if this shape is auto-sized to fit the text. 46 /// </summary> 47 // ------------------------------------------------------------------ 48 protected bool mAutoSize = false; 49 50 // ------------------------------------------------------------------ 51 /// <summary> 52 /// Specifies if this shape allows in-place text editing. If set to 53 /// true, then the TextEditor is displayed when the shape is 54 /// double-clicked. 55 /// </summary> 56 // ------------------------------------------------------------------ 57 protected bool mAllowTextEditing = true; 58 59 // ------------------------------------------------------------------ 60 /// <summary> 61 /// Specifies the number of clicks required to launch the in-place 62 /// TextEditor. 63 /// </summary> 64 // ------------------------------------------------------------------ 65 protected int mEditTextClicks = 2; 66 67 // ------------------------------------------------------------------ 68 /// <summary> 69 /// The text for this shape. 70 /// </summary> 71 // ------------------------------------------------------------------ 72 protected string mText = string.Empty; 73 74 // ------------------------------------------------------------------ 75 /// <summary> 76 /// The Rectangle inside which the text is drawn. 77 /// </summary> 78 // ------------------------------------------------------------------ 79 protected Rectangle mTextArea; 80 81 // ------------------------------------------------------------------ 82 /// <summary> 83 /// The style of the text to draw. 84 /// </summary> 85 // ------------------------------------------------------------------ 86 protected ITextStyle mTextStyle = new TextStyle( 87 Color.Black, 88 new Font("Arial", 10), 89 StringAlignment.Center, 90 StringAlignment.Center); 91 92 #endregion 93 94 #region Properties 95 96 // ------------------------------------------------------------------ 97 /// <summary> 98 /// Gets the current version. 99 /// </summary> 100 // ------------------------------------------------------------------ 101 public override double Version 102 { 103 get 104 { 105 return simpleShapeBaseVersion; 106 } 4 namespace Netron.Diagramming.Core { 5 // ---------------------------------------------------------------------- 6 /// <summary> 7 /// Abstract base class for simple shapes. 8 /// <para>A simple shape does not contain sub-elements (aka shape 9 /// material, see <see cref="IShapeMaterial"/>) but this does 10 /// not mean you cannot paint sub-elements. In fact, this shape type 11 /// corresponds to the old base class in the previous Netron graph 12 /// library (i.e. before version 2.3). 13 /// <seealso cref="ComplexShapeBase"/> 14 /// </para> 15 /// </summary> 16 // ---------------------------------------------------------------------- 17 public abstract partial class SimpleShapeBase : 18 ShapeBase, 19 ISimpleShape, 20 ITextProvider { 21 // ------------------------------------------------------------------ 22 /// <summary> 23 /// The amount to inflate the text area (mTextRectangle) by. 24 /// </summary> 25 // ------------------------------------------------------------------ 26 protected const int TextRectangleInflation = 5; 27 28 // ------------------------------------------------------------------ 29 /// <summary> 30 /// Implementation of IVersion - the current version of 31 /// SimpleShapeBase. 32 /// </summary> 33 // ------------------------------------------------------------------ 34 protected const double simpleShapeBaseVersion = 1.0; 35 36 #region Fields 37 38 // ------------------------------------------------------------------ 39 /// <summary> 40 /// Specifies if this shape is auto-sized to fit the text. 41 /// </summary> 42 // ------------------------------------------------------------------ 43 protected bool mAutoSize = false; 44 45 // ------------------------------------------------------------------ 46 /// <summary> 47 /// Specifies if this shape allows in-place text editing. If set to 48 /// true, then the TextEditor is displayed when the shape is 49 /// double-clicked. 50 /// </summary> 51 // ------------------------------------------------------------------ 52 protected bool mAllowTextEditing = true; 53 54 // ------------------------------------------------------------------ 55 /// <summary> 56 /// Specifies the number of clicks required to launch the in-place 57 /// TextEditor. 58 /// </summary> 59 // ------------------------------------------------------------------ 60 protected int mEditTextClicks = 2; 61 62 // ------------------------------------------------------------------ 63 /// <summary> 64 /// The text for this shape. 65 /// </summary> 66 // ------------------------------------------------------------------ 67 protected string mText = string.Empty; 68 69 // ------------------------------------------------------------------ 70 /// <summary> 71 /// The Rectangle inside which the text is drawn. 72 /// </summary> 73 // ------------------------------------------------------------------ 74 protected Rectangle mTextArea; 75 76 // ------------------------------------------------------------------ 77 /// <summary> 78 /// The style of the text to draw. 79 /// </summary> 80 // ------------------------------------------------------------------ 81 protected ITextStyle mTextStyle = new TextStyle( 82 Color.Black, 83 new Font("Arial", 10), 84 StringAlignment.Center, 85 StringAlignment.Center); 86 87 #endregion 88 89 #region Properties 90 91 // ------------------------------------------------------------------ 92 /// <summary> 93 /// Gets the current version. 94 /// </summary> 95 // ------------------------------------------------------------------ 96 public override double Version { 97 get { 98 return simpleShapeBaseVersion; 99 } 100 } 101 102 // ------------------------------------------------------------------ 103 /// <summary> 104 /// Gets or sets the style of the text to use. 105 /// </summary> 106 // ------------------------------------------------------------------ 107 public virtual ITextStyle TextStyle { 108 get { 109 return mTextStyle; 110 } 111 set { 112 mTextStyle = value; 113 mTextStyle.TextStyleChanged += 114 new TextStyleChangedEventHandler(HandleTextStyleChanged); 115 Invalidate(); 116 } 117 } 118 119 // ------------------------------------------------------------------ 120 /// <summary> 121 /// Returns if text editing is allowed. 122 /// </summary> 123 // ------------------------------------------------------------------ 124 public virtual bool AllowTextEditing { 125 get { 126 return this.mAllowTextEditing; 127 } 128 } 129 130 // ------------------------------------------------------------------ 131 /// <summary> 132 /// Returns the number of mouse clicks required to launch the in-place 133 /// text editor. 134 /// </summary> 135 // ------------------------------------------------------------------ 136 public virtual int EditTextClicks { 137 get { 138 return mEditTextClicks; 139 } 140 set { 141 mEditTextClicks = value; 142 } 143 } 144 145 // ------------------------------------------------------------------ 146 /// <summary> 147 /// Gets or sets a value indicating whether to autosize the label in 148 /// function of the string content. 149 /// </summary> 150 /// <value><c>true</c> if [auto size]; otherwise, <c>false</c>. 151 /// </value> 152 // ------------------------------------------------------------------ 153 public virtual bool AutoSize { 154 get { 155 return mAutoSize; 156 } 157 set { 158 mAutoSize = value; 159 AutoReSize(mText); 160 } 161 } 162 163 // ------------------------------------------------------------------ 164 /// <summary> 165 /// Gets or sets the text to display in the TextArea. 166 /// </summary> 167 // ------------------------------------------------------------------ 168 [Browsable(true), 169 Description("The text shown on the shape"), 170 Category("Layout")] 171 public virtual string Text { 172 get { 173 return mText; 174 } 175 set { 176 if (value == null) { 177 throw new InconsistencyException( 178 "The text property cannot be 'null'"); 107 179 } 108 180 109 // ------------------------------------------------------------------ 110 /// <summary> 111 /// Gets or sets the style of the text to use. 112 /// </summary> 113 // ------------------------------------------------------------------ 114 public virtual ITextStyle TextStyle 115 { 116 get 117 { 118 return mTextStyle; 119 } 120 set 121 { 122 mTextStyle = value; 123 mTextStyle.TextStyleChanged += 124 new TextStyleChangedEventHandler(HandleTextStyleChanged); 125 Invalidate(); 126 } 181 mText = value; 182 if (mAutoSize) { 183 AutoReSize(value); 127 184 } 128 129 // ------------------------------------------------------------------ 130 /// <summary> 131 /// Returns if text editing is allowed. 132 /// </summary> 133 // ------------------------------------------------------------------ 134 public virtual bool AllowTextEditing 135 { 136 get 137 { 138 return this.mAllowTextEditing; 139 } 140 } 141 142 // ------------------------------------------------------------------ 143 /// <summary> 144 /// Returns the number of mouse clicks required to launch the in-place 145 /// text editor. 146 /// </summary> 147 // ------------------------------------------------------------------ 148 public virtual int EditTextClicks 149 { 150 get 151 { 152 return mEditTextClicks; 153 } 154 set 155 { 156 mEditTextClicks = value; 157 } 158 } 159 160 // ------------------------------------------------------------------ 161 /// <summary> 162 /// Gets or sets a value indicating whether to autosize the label in 163 /// function of the string content. 164 /// </summary> 165 /// <value><c>true</c> if [auto size]; otherwise, <c>false</c>. 166 /// </value> 167 // ------------------------------------------------------------------ 168 public virtual bool AutoSize 169 { 170 get 171 { 172 return mAutoSize; 173 } 174 set 175 { 176 mAutoSize = value; 177 AutoReSize(mText); 178 } 179 } 180 181 // ------------------------------------------------------------------ 182 /// <summary> 183 /// Gets or sets the text to display in the TextArea. 184 /// </summary> 185 // ------------------------------------------------------------------ 186 [Browsable(true), 187 Description("The text shown on the shape"), 188 Category("Layout")] 189 public virtual string Text 190 { 191 get 192 { 193 return mText; 194 } 195 set 196 { 197 if (value == null) 198 { 199 throw new InconsistencyException( 200 "The text property cannot be 'null'"); 201 } 202 203 mText = value; 204 if (mAutoSize) 205 { 206 AutoReSize(value); 207 } 208 this.Invalidate(); 209 } 210 } 211 212 // ------------------------------------------------------------------ 213 /// <summary> 214 /// Gets or sets the text rectangle. 215 /// </summary> 216 /// <value>The text rectangle.</value> 217 // ------------------------------------------------------------------ 218 public virtual Rectangle TextArea 219 { 220 get 221 { 222 return mTextArea; 223 } 224 set 225 { 226 mTextArea = value; 227 } 228 } 229 #endregion 230 231 #region Constructor 232 233 // ------------------------------------------------------------------ 234 ///<summary> 235 ///Default constructor. 236 ///</summary> 237 // ------------------------------------------------------------------ 238 public SimpleShapeBase() : base() 239 { 240 } 241 242 // ------------------------------------------------------------------ 243 /// <summary> 244 /// Initializes a new instance of the <see cref="T:SimpleShapeBase"/> 245 /// class. 246 /// </summary> 247 /// <param name="model">The <see cref="IModel"/></param> 248 // ------------------------------------------------------------------ 249 public SimpleShapeBase(IModel model) 250 : base(model) 251 { 252 } 253 254 #endregion 255 256 #region Methods 257 258 // ------------------------------------------------------------------ 259 /// <summary> 260 /// Initializes this instance. 261 /// </summary> 262 // ------------------------------------------------------------------ 263 protected override void Initialize() 264 { 265 base.Initialize(); 266 mTextArea = Rectangle; 267 268 mTextArea.Inflate( 269 -TextRectangleInflation, 270 -TextRectangleInflation); 271 272 this.mTextStyle = new TextStyle( 273 Color.Black, 274 new Font("Arial", 12), 275 StringAlignment.Center, 276 StringAlignment.Center); 277 278 this.mTextStyle.TextStyleChanged += 279 new TextStyleChangedEventHandler(HandleTextStyleChanged); 280 } 281 282 // ------------------------------------------------------------------ 283 /// <summary> 284 /// Called when the text style is changed. This shape is invalidated 285 /// so the new text style is reflected. 286 /// </summary> 287 /// <param name="sender">object</param> 288 /// <param name="e">TextStyleChangedEventArgs</param> 289 // ------------------------------------------------------------------ 290 protected virtual void HandleTextStyleChanged( 291 object sender, 292 TextStyleChangedEventArgs e) 293 { 294 Invalidate(); 295 } 296 297 // ------------------------------------------------------------------ 298 /// <summary> 299 /// Moves the entity with the given shift vector 300 /// </summary> 301 /// <param name="p">Represent a shift-vector, not the absolute 302 /// position!</param> 303 // ------------------------------------------------------------------ 304 public override void MoveBy(Point p) 305 { 306 base.MoveBy(p); 307 this.mTextArea.X += p.X; 308 this.mTextArea.Y += p.Y; 309 } 310 311 // ------------------------------------------------------------------ 312 /// <summary> 313 /// Transforms this shape to the given new rectangle. 314 /// </summary> 315 /// <param name="x">The x-coordinate of the new rectangle.</param> 316 /// <param name="y">The y-coordinate of the new rectangle.</param> 317 /// <param name="width">The width.</param> 318 /// <param name="height">The height.</param> 319 // ------------------------------------------------------------------ 320 public override void Transform( 321 int x, 322 int y, 323 int width, 324 int height) 325 { 326 base.Transform(x, y, width, height); 327 mTextArea = Rectangle; 328 329 mTextArea.Inflate( 330 -TextRectangleInflation, 331 -TextRectangleInflation); 332 } 333 334 // ------------------------------------------------------------------ 335 /// <summary> 336 /// Re-calculates the bounds of this shape to fit the text specified. 337 /// </summary> 338 /// <param name="value">string: The text to fit to.</param> 339 // ------------------------------------------------------------------ 340 private void AutoReSize(string value) 341 { 342 SizeF size = TextRenderer.MeasureText( 343 value, 344 mTextStyle.Font); 345 346 Rectangle rec = new Rectangle( 347 Rectangle.Location, 348 Size.Round(size)); 349 350 rec.Inflate(TextRectangleInflation, TextRectangleInflation); 351 352 rec.Offset(TextRectangleInflation, TextRectangleInflation); 353 354 Transform(rec); 355 } 356 357 #endregion 358 } 185 this.Invalidate(); 186 } 187 } 188 189 // ------------------------------------------------------------------ 190 /// <summary> 191 /// Gets or sets the text rectangle. 192 /// </summary> 193 /// <value>The text rectangle.</value> 194 // ------------------------------------------------------------------ 195 public virtual Rectangle TextArea { 196 get { 197 return mTextArea; 198 } 199 set { 200 mTextArea = value; 201 } 202 } 203 #endregion 204 205 #region Constructor 206 207 // ------------------------------------------------------------------ 208 ///<summary> 209 ///Default constructor. 210 ///</summary> 211 // ------------------------------------------------------------------ 212 public SimpleShapeBase() 213 : base() { 214 } 215 216 // ------------------------------------------------------------------ 217 /// <summary> 218 /// Initializes a new instance of the <see cref="T:SimpleShapeBase"/> 219 /// class. 220 /// </summary> 221 /// <param name="model">The <see cref="IModel"/></param> 222 // ------------------------------------------------------------------ 223 public SimpleShapeBase(IModel model) 224 : base(model) { 225 } 226 227 #endregion 228 229 #region Methods 230 231 // ------------------------------------------------------------------ 232 /// <summary> 233 /// Initializes this instance. 234 /// </summary> 235 // ------------------------------------------------------------------ 236 protected override void Initialize() { 237 base.Initialize(); 238 mTextArea = Rectangle; 239 240 mTextArea.Inflate( 241 -TextRectangleInflation, 242 -TextRectangleInflation); 243 244 this.mTextStyle = new TextStyle( 245 Color.Black, 246 new Font("Arial", 12), 247 StringAlignment.Center, 248 StringAlignment.Center); 249 250 this.mTextStyle.TextStyleChanged += 251 new TextStyleChangedEventHandler(HandleTextStyleChanged); 252 } 253 254 // ------------------------------------------------------------------ 255 /// <summary> 256 /// Called when the text style is changed. This shape is invalidated 257 /// so the new text style is reflected. 258 /// </summary> 259 /// <param name="sender">object</param> 260 /// <param name="e">TextStyleChangedEventArgs</param> 261 // ------------------------------------------------------------------ 262 protected virtual void HandleTextStyleChanged( 263 object sender, 264 TextStyleChangedEventArgs e) { 265 Invalidate(); 266 } 267 268 // ------------------------------------------------------------------ 269 /// <summary> 270 /// Moves the entity with the given shift vector 271 /// </summary> 272 /// <param name="p">Represent a shift-vector, not the absolute 273 /// position!</param> 274 // ------------------------------------------------------------------ 275 public override void MoveBy(Point p) { 276 base.MoveBy(p); 277 this.mTextArea.X += p.X; 278 this.mTextArea.Y += p.Y; 279 } 280 281 // ------------------------------------------------------------------ 282 /// <summary> 283 /// Transforms this shape to the given new rectangle. 284 /// </summary> 285 /// <param name="x">The x-coordinate of the new rectangle.</param> 286 /// <param name="y">The y-coordinate of the new rectangle.</param> 287 /// <param name="width">The width.</param> 288 /// <param name="height">The height.</param> 289 // ------------------------------------------------------------------ 290 public override void Transform( 291 int x, 292 int y, 293 int width, 294 int height) { 295 base.Transform(x, y, width, height); 296 mTextArea = Rectangle; 297 298 mTextArea.Inflate( 299 -TextRectangleInflation, 300 -TextRectangleInflation); 301 } 302 303 // ------------------------------------------------------------------ 304 /// <summary> 305 /// Re-calculates the bounds of this shape to fit the text specified. 306 /// </summary> 307 /// <param name="value">string: The text to fit to.</param> 308 // ------------------------------------------------------------------ 309 private void AutoReSize(string value) { 310 SizeF size = TextRenderer.MeasureText( 311 value, 312 mTextStyle.Font); 313 314 Rectangle rec = new Rectangle( 315 Rectangle.Location, 316 Size.Round(size)); 317 318 rec.Inflate(TextRectangleInflation, TextRectangleInflation); 319 320 rec.Offset(TextRectangleInflation, TextRectangleInflation); 321 322 Transform(rec); 323 } 324 325 #endregion 326 } 359 327 }
Note: See TracChangeset
for help on using the changeset viewer.