Skip to content

Commit

Permalink
Multiline shape text is now supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
cliftonm committed Jan 30, 2017
1 parent e2f259a commit b4ef2b4
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 13 deletions.
66 changes: 63 additions & 3 deletions FlowSharpLib/GraphicElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class GraphicElement : IDisposable
public Font TextFont { get; set; }
public Color TextColor { get; set; }
public ContentAlignment TextAlign { get; set; }
public bool Multiline { get; set; }
// TODO: Text location - left, top, right, middle, bottom

protected bool HasCornerAnchors { get; set; }
Expand Down Expand Up @@ -244,8 +245,19 @@ public virtual GraphicElement CloneDefault(Canvas canvas, Point offset)
public virtual TextBox CreateTextBox(Point mousePosition)
{
TextBox tb = new TextBox();
tb.Location = DisplayRectangle.LeftMiddle().Move(0, -10);
tb.Size = new Size(DisplayRectangle.Width, 20);
tb.Multiline = Multiline;

if (Multiline)
{
tb.Location = DisplayRectangle.Location;
tb.Size = new Size(DisplayRectangle.Width, DisplayRectangle.Height);
}
else
{
tb.Location = DisplayRectangle.LeftMiddle().Move(0, -10);
tb.Size = new Size(DisplayRectangle.Width, 20);
}

tb.Text = Text;

return tb;
Expand Down Expand Up @@ -284,6 +296,7 @@ public virtual void Serialize(ElementPropertyBag epb, IEnumerable<GraphicElement
epb.Text = Text;
epb.TextColor = TextColor;
epb.TextAlign = TextAlign;
epb.Multiline = Multiline;
epb.TextFontFamily = TextFont.FontFamily.Name;
epb.TextFontSize = TextFont.Size;
epb.TextFontUnderline = TextFont.Underline;
Expand Down Expand Up @@ -326,6 +339,7 @@ public virtual void Deserialize(ElementPropertyBag epb)
IsBookmarked = epb.IsBookmarked;
// If missing (backwards compatibility) middle-center align.
TextAlign = epb.TextAlign == 0 ? ContentAlignment.MiddleCenter : epb.TextAlign;
Multiline = epb.Multiline;
TextFont.Dispose();
FontStyle fontStyle = (epb.TextFontUnderline ? FontStyle.Underline : FontStyle.Regular) | (epb.TextFontItalic ? FontStyle.Italic : FontStyle.Regular) | (epb.TextFontStrikeout ? FontStyle.Strikeout : FontStyle.Regular) | (epb.TextFontBold ? FontStyle.Bold : FontStyle.Regular); ;
TextFont = new Font(epb.TextFontFamily, epb.TextFontSize, fontStyle);
Expand Down Expand Up @@ -734,7 +748,53 @@ protected virtual void DrawText(Graphics gr, string text, Font textFont, Color t
break;
}

gr.DrawString(text, textFont, brush, textpos);
TextFormatFlags tff = TextFormatFlags.Default;

switch (textAlign)
{
case ContentAlignment.TopLeft:
tff |= TextFormatFlags.Left;
break;

case ContentAlignment.TopCenter:
tff |= TextFormatFlags.Top | TextFormatFlags.HorizontalCenter;
break;

case ContentAlignment.TopRight:
tff |= TextFormatFlags.Top | TextFormatFlags.Right;
break;

case ContentAlignment.MiddleLeft:
tff |= TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
break;

case ContentAlignment.MiddleCenter:
tff |= TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
break;

case ContentAlignment.MiddleRight:
tff |= TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
break;

case ContentAlignment.BottomLeft:
tff |= TextFormatFlags.Bottom | TextFormatFlags.Left;
break;

case ContentAlignment.BottomCenter:
tff |= TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter;
break;

case ContentAlignment.BottomRight:
tff |= TextFormatFlags.Bottom | TextFormatFlags.Right;
break;

default: // middle center
tff |= TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
break;
}

TextRenderer.DrawText(gr, text, textFont, DisplayRectangle, textColor, tff);
// gr.DrawString(text, textFont, brush, textpos);
brush.Dispose();
}
}
Expand Down
1 change: 1 addition & 0 deletions FlowSharpLib/Persist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public class ElementPropertyBag
public List<ChildPropertyBag> Children { get; set; }

public ContentAlignment TextAlign { get; set; }
public bool Multiline { get; set; }

[XmlIgnore]
public Color TextColor { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions FlowSharpLib/ShapeProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ public class ShapeProperties : ElementProperties
public Color TextColor { get; set; }
[Category("Text")]
public ContentAlignment TextAlign { get; set; }
[Category("Text")]
public bool Multiline { get; set; }

public ShapeProperties(GraphicElement el) : base(el)
{
Text = el.Text;
Font = el.TextFont;
TextColor = el.TextColor;
TextAlign = el.TextAlign;
Multiline = el.Multiline;
}

public override void Update(GraphicElement el, string label)
Expand All @@ -41,6 +44,7 @@ public override void Update(GraphicElement el, string label)
(label == nameof(Font)).If(() => el.TextFont = Font);
(label == nameof(TextColor)).If(() => el.TextColor = TextColor);
(label == nameof(TextAlign)).If(() => el.TextAlign = TextAlign);
(label == nameof(Multiline)).If(() => el.Multiline = Multiline);
base.Update(el, label);
}
}
Expand Down
16 changes: 10 additions & 6 deletions FlowSharpLib/Shapes/TextShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

using System.Drawing;
using System.Windows.Forms;

namespace FlowSharpLib
{
Expand All @@ -28,7 +29,7 @@ public override void GetBackground()
base.GetBackground();
}

public override void Draw(Graphics gr)
public override void Draw(Graphics gr)
{
UpdateDisplayRectangle(gr);
gr.FillRectangle(FillBrush, DisplayRectangle);
Expand All @@ -38,11 +39,13 @@ public override void Draw(Graphics gr)

protected void UpdateDisplayRectangle(Graphics gr)
{
SizeF size = gr.MeasureString(Text, TextFont);
Point center = DisplayRectangle.Center();
// Grow so selection is not right on top of text, and so that anti-aliasing has some room.
DisplayRectangle = new Rectangle(center.X - (int)(size.Width / 2), center.Y - (int)(size.Height) / 2, (int)size.Width, (int)size.Height).Grow(3);
}
SizeF size = TextRenderer.MeasureText(gr, Text, TextFont);
// SizeF size = gr.MeasureString(Text, TextFont);
// Grow so selection is not right on top of text, and so that anti-aliasing has some room.
// Point center = DisplayRectangle.Center();
// DisplayRectangle = new Rectangle(center.X - (int)(size.Width / 2), center.Y - (int)(size.Height) / 2, (int)size.Width, (int)size.Height).Grow(3);
DisplayRectangle = new Rectangle(DisplayRectangle.X+3, DisplayRectangle.Y+3, (int)size.Width, (int)size.Height).Grow(3);
}
}

/// <summary>
Expand Down Expand Up @@ -79,6 +82,7 @@ public override GraphicElement CloneDefault(Canvas canvas, Point offset)

public override void Draw(Graphics gr)
{
// Use ContentAlignment to position text.
SizeF size = gr.MeasureString(TOOLBOX_TEXT, TextFont);
Point textpos = DisplayRectangle.Center().Move((int)(-size.Width / 2), (int)(-size.Height / 2));
gr.DrawString(TOOLBOX_TEXT, TextFont, brush, textpos);
Expand Down
1 change: 0 additions & 1 deletion Plugins/FlowSharpWindowsControlShapes/TextboxShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace FlowSharpWindowsControlShapes
[ExcludeFromToolbox]
public class TextboxShape : ControlShape
{
public bool Multiline { get; set; }
public bool ReadOnly { get; set; }

public TextboxShape(Canvas canvas) : base(canvas)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace FlowSharpWindowsControlShapes
{
public class TextboxShapeProperties : ControlShapeProperties
{
[Category("Options")]
public bool Multiline { get; set; }
[Category("Options")]
public bool ReadOnly { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion Services/FlowSharpEditService/FlowSharpEditService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ protected bool CanStartEditing(Keys keyData)

protected void OnEditBoxKey(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 27 || e.KeyChar == 13)
if (e.KeyChar == 27 || (e.KeyChar == 13 && !editBox.Multiline))
{
TerminateEditing();
e.Handled = true; // Suppress beep.
Expand Down

0 comments on commit b4ef2b4

Please sign in to comment.