using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Netron.Diagramming.Core {
// ----------------------------------------------------------------------
///
/// Abstract base class for simple shapes.
/// A simple shape does not contain sub-elements (aka shape
/// material, see ) but this does
/// not mean you cannot paint sub-elements. In fact, this shape type
/// corresponds to the old base class in the previous Netron graph
/// library (i.e. before version 2.3).
///
///
///
// ----------------------------------------------------------------------
public abstract partial class SimpleShapeBase :
ShapeBase,
ISimpleShape,
ITextProvider {
// ------------------------------------------------------------------
///
/// The amount to inflate the text area (mTextRectangle) by.
///
// ------------------------------------------------------------------
protected const int TextRectangleInflation = 5;
// ------------------------------------------------------------------
///
/// Implementation of IVersion - the current version of
/// SimpleShapeBase.
///
// ------------------------------------------------------------------
protected const double simpleShapeBaseVersion = 1.0;
#region Fields
// ------------------------------------------------------------------
///
/// Specifies if this shape is auto-sized to fit the text.
///
// ------------------------------------------------------------------
protected bool mAutoSize = false;
// ------------------------------------------------------------------
///
/// Specifies if this shape allows in-place text editing. If set to
/// true, then the TextEditor is displayed when the shape is
/// double-clicked.
///
// ------------------------------------------------------------------
protected bool mAllowTextEditing = true;
// ------------------------------------------------------------------
///
/// Specifies the number of clicks required to launch the in-place
/// TextEditor.
///
// ------------------------------------------------------------------
protected int mEditTextClicks = 2;
// ------------------------------------------------------------------
///
/// The text for this shape.
///
// ------------------------------------------------------------------
protected string mText = string.Empty;
// ------------------------------------------------------------------
///
/// The Rectangle inside which the text is drawn.
///
// ------------------------------------------------------------------
protected Rectangle mTextArea;
// ------------------------------------------------------------------
///
/// The style of the text to draw.
///
// ------------------------------------------------------------------
protected ITextStyle mTextStyle = new TextStyle(
Color.Black,
new Font("Arial", 10),
StringAlignment.Center,
StringAlignment.Center);
#endregion
#region Properties
// ------------------------------------------------------------------
///
/// Gets the current version.
///
// ------------------------------------------------------------------
public override double Version {
get {
return simpleShapeBaseVersion;
}
}
// ------------------------------------------------------------------
///
/// Gets or sets the style of the text to use.
///
// ------------------------------------------------------------------
public virtual ITextStyle TextStyle {
get {
return mTextStyle;
}
set {
mTextStyle = value;
mTextStyle.TextStyleChanged +=
new TextStyleChangedEventHandler(HandleTextStyleChanged);
Invalidate();
}
}
// ------------------------------------------------------------------
///
/// Returns if text editing is allowed.
///
// ------------------------------------------------------------------
public virtual bool AllowTextEditing {
get {
return this.mAllowTextEditing;
}
}
// ------------------------------------------------------------------
///
/// Returns the number of mouse clicks required to launch the in-place
/// text editor.
///
// ------------------------------------------------------------------
public virtual int EditTextClicks {
get {
return mEditTextClicks;
}
set {
mEditTextClicks = value;
}
}
// ------------------------------------------------------------------
///
/// Gets or sets a value indicating whether to autosize the label in
/// function of the string content.
///
/// true if [auto size]; otherwise, false.
///
// ------------------------------------------------------------------
public virtual bool AutoSize {
get {
return mAutoSize;
}
set {
mAutoSize = value;
AutoReSize(mText);
}
}
// ------------------------------------------------------------------
///
/// Gets or sets the text to display in the TextArea.
///
// ------------------------------------------------------------------
[Browsable(true),
Description("The text shown on the shape"),
Category("Layout")]
public virtual string Text {
get {
return mText;
}
set {
if (value == null) {
throw new InconsistencyException(
"The text property cannot be 'null'");
}
mText = value;
if (mAutoSize) {
AutoReSize(value);
}
this.Invalidate();
}
}
// ------------------------------------------------------------------
///
/// Gets or sets the text rectangle.
///
/// The text rectangle.
// ------------------------------------------------------------------
public virtual Rectangle TextArea {
get {
return mTextArea;
}
set {
mTextArea = value;
}
}
#endregion
#region Constructor
// ------------------------------------------------------------------
///
///Default constructor.
///
// ------------------------------------------------------------------
public SimpleShapeBase()
: base() {
}
// ------------------------------------------------------------------
///
/// Initializes a new instance of the
/// class.
///
/// The
// ------------------------------------------------------------------
public SimpleShapeBase(IModel model)
: base(model) {
}
#endregion
#region Methods
// ------------------------------------------------------------------
///
/// Initializes this instance.
///
// ------------------------------------------------------------------
protected override void Initialize() {
base.Initialize();
mTextArea = Rectangle;
mTextArea.Inflate(
-TextRectangleInflation,
-TextRectangleInflation);
this.mTextStyle = new TextStyle(
Color.Black,
new Font("Arial", 12),
StringAlignment.Center,
StringAlignment.Center);
this.mTextStyle.TextStyleChanged +=
new TextStyleChangedEventHandler(HandleTextStyleChanged);
}
// ------------------------------------------------------------------
///
/// Called when the text style is changed. This shape is invalidated
/// so the new text style is reflected.
///
/// object
/// TextStyleChangedEventArgs
// ------------------------------------------------------------------
protected virtual void HandleTextStyleChanged(
object sender,
TextStyleChangedEventArgs e) {
Invalidate();
}
// ------------------------------------------------------------------
///
/// Moves the entity with the given shift vector
///
/// Represent a shift-vector, not the absolute
/// position!
// ------------------------------------------------------------------
public override void MoveBy(Point p) {
base.MoveBy(p);
this.mTextArea.X += p.X;
this.mTextArea.Y += p.Y;
}
// ------------------------------------------------------------------
///
/// Transforms this shape to the given new rectangle.
///
/// The x-coordinate of the new rectangle.
/// The y-coordinate of the new rectangle.
/// The width.
/// The height.
// ------------------------------------------------------------------
public override void Transform(
int x,
int y,
int width,
int height) {
base.Transform(x, y, width, height);
mTextArea = Rectangle;
mTextArea.Inflate(
-TextRectangleInflation,
-TextRectangleInflation);
}
// ------------------------------------------------------------------
///
/// Re-calculates the bounds of this shape to fit the text specified.
///
/// string: The text to fit to.
// ------------------------------------------------------------------
private void AutoReSize(string value) {
SizeF size = TextRenderer.MeasureText(
value,
mTextStyle.Font);
Rectangle rec = new Rectangle(
Rectangle.Location,
Size.Round(size));
rec.Inflate(TextRectangleInflation, TextRectangleInflation);
rec.Offset(TextRectangleInflation, TextRectangleInflation);
Transform(rec);
}
#endregion
}
}