From efb4807cd1406cb094b53940d06689a84e5999da Mon Sep 17 00:00:00 2001 From: RKrom Date: Wed, 11 Jun 2014 17:55:51 +0200 Subject: [PATCH] Small fixes for serialization and the enumerator, still didn't fix the counting --- .../Drawing/Fields/AbstractFieldHolder.cs | 6 +- Greenshot/Drawing/SpeechbubbleContainer.cs | 11 ++- Greenshot/Drawing/StepLabelContainer.cs | 22 +++-- Greenshot/Drawing/TextContainer.cs | 98 ++++++++++++------- 4 files changed, 86 insertions(+), 51 deletions(-) diff --git a/Greenshot/Drawing/Fields/AbstractFieldHolder.cs b/Greenshot/Drawing/Fields/AbstractFieldHolder.cs index 3beecac78..d289370f7 100644 --- a/Greenshot/Drawing/Fields/AbstractFieldHolder.cs +++ b/Greenshot/Drawing/Fields/AbstractFieldHolder.cs @@ -31,7 +31,7 @@ namespace Greenshot.Drawing.Fields { /// /// Basic IFieldHolder implementation, providing access to a set of fields /// - [Serializable()] + [Serializable] public abstract class AbstractFieldHolder : IFieldHolder { private static readonly ILog LOG = LogManager.GetLogger(typeof(AbstractFieldHolder)); private static EditorConfiguration editorConfiguration = IniConfig.GetIniSection(); @@ -54,9 +54,9 @@ namespace Greenshot.Drawing.Fields { public AbstractFieldHolder() {} - [OnDeserialized()] + [OnDeserialized] private void OnDeserialized(StreamingContext context) { - fieldsByType = new Dictionary(); + fieldsByType = new Dictionary(); // listen to changing properties foreach(Field field in fields) { field.PropertyChanged += delegate { diff --git a/Greenshot/Drawing/SpeechbubbleContainer.cs b/Greenshot/Drawing/SpeechbubbleContainer.cs index 197c93223..d21eae804 100644 --- a/Greenshot/Drawing/SpeechbubbleContainer.cs +++ b/Greenshot/Drawing/SpeechbubbleContainer.cs @@ -33,10 +33,9 @@ namespace Greenshot.Drawing { /// /// Description of SpeechbubbleContainer. /// - [Serializable()] + [Serializable] public class SpeechbubbleContainer : TextContainer { - public SpeechbubbleContainer(Surface parent) - : base(parent) { + public SpeechbubbleContainer(Surface parent) : base(parent) { } /// @@ -73,9 +72,11 @@ namespace Greenshot.Drawing { public override Rectangle DrawingBounds { get { + // TODO: Use the normal bounds and extend with the TargetGripper return new Rectangle(0, 0, _parent.Width, _parent.Height); } } + public override void Draw(Graphics graphics, RenderMode renderMode) { if (TargetGripper == null) { return; @@ -93,6 +94,10 @@ namespace Greenshot.Drawing { bool lineVisible = (lineThickness > 0 && Colors.IsVisible(lineColor)); Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); + if (Selected && renderMode == RenderMode.EDIT) { + DrawSelectionBorder(graphics, rect); + } + int tailAngle = 90 + (int)GeometryHelper.Angle2D(rect.Left + (rect.Width / 2), rect.Top + (rect.Height / 2), TargetGripper.Left, TargetGripper.Top); int tailLength = GeometryHelper.Distance2D(rect.Left + (rect.Width / 2), rect.Top + (rect.Height / 2), TargetGripper.Left, TargetGripper.Top); int tailWidth = (Math.Abs(rect.Width) + Math.Abs(rect.Height)) / 20; diff --git a/Greenshot/Drawing/StepLabelContainer.cs b/Greenshot/Drawing/StepLabelContainer.cs index a75faf1d1..bfb0ce235 100644 --- a/Greenshot/Drawing/StepLabelContainer.cs +++ b/Greenshot/Drawing/StepLabelContainer.cs @@ -28,6 +28,7 @@ using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; +using System.Runtime.Serialization; namespace Greenshot.Drawing { /// @@ -39,15 +40,21 @@ namespace Greenshot.Drawing { public class StepLabelContainer : DrawableContainer { [NonSerialized] private StringFormat _stringFormat = new StringFormat(); - [NonSerialized] - private Font _font; - private bool drawAsRectangle = false; + + private readonly bool _drawAsRectangle = false; public StepLabelContainer(Surface parent) : base(parent) { parent.AddStepLabel(this); InitContent(); } + [OnDeserialized] + private void OnDeserialized(StreamingContext context) { + _stringFormat = new StringFormat(); + _stringFormat.Alignment = StringAlignment.Center; + _stringFormat.LineAlignment = StringAlignment.Center; + } + public override Size DefaultSize { get { return new Size(30, 30); @@ -112,22 +119,23 @@ namespace Greenshot.Drawing { Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); Color fillColor = GetFieldValueAsColor(FieldType.FILL_COLOR); Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR); - if (drawAsRectangle) { + if (_drawAsRectangle) { RectangleContainer.DrawRectangle(rect, graphics, rm, 0, Color.Transparent, fillColor, false); } else { EllipseContainer.DrawEllipse(rect, graphics, rm, 0, Color.Transparent, fillColor, false); } using (FontFamily fam = new FontFamily(FontFamily.GenericSansSerif.Name)) { float factor = (((float)rect.Width / DefaultSize.Width) + ((float)rect.Height / DefaultSize.Height)) / 2; - _font = new Font(fam, 16 * factor, FontStyle.Bold, GraphicsUnit.Pixel); - TextContainer.DrawText(graphics, rect, 0, lineColor, false, _stringFormat, text, _font); + using (Font _font = new Font(fam, 16 * factor, FontStyle.Bold, GraphicsUnit.Pixel)) { + TextContainer.DrawText(graphics, rect, 0, lineColor, false, _stringFormat, text, _font); + } } } public override bool ClickableAt(int x, int y) { Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); Color fillColor = GetFieldValueAsColor(FieldType.FILL_COLOR); - if (drawAsRectangle) { + if (_drawAsRectangle) { return RectangleContainer.RectangleClickableAt(rect, 0, fillColor, x, y); } else { return EllipseContainer.EllipseClickableAt(rect, 0, fillColor, x, y); diff --git a/Greenshot/Drawing/TextContainer.cs b/Greenshot/Drawing/TextContainer.cs index 26bbb5a39..53d732e11 100644 --- a/Greenshot/Drawing/TextContainer.cs +++ b/Greenshot/Drawing/TextContainer.cs @@ -38,16 +38,22 @@ namespace Greenshot.Drawing { /// [Serializable] public class TextContainer : RectangleContainer, ITextContainer { + [NonSerialized] private bool fontInvalidated = true; // If makeUndoable is true the next text-change will make the change undoable. // This is set to true AFTER the first change is made, as there is already a "add element" on the undo stack private bool makeUndoable; + [NonSerialized] private Font _font; public Font Font { get { return _font; } } + + [NonSerialized] + private TextBox _textBox; + /// /// The StringFormat object is not serializable!! /// @@ -77,14 +83,9 @@ namespace Greenshot.Drawing { OnPropertyChanged("Text"); } } - - [NonSerialized] - private TextBox textBox; public TextContainer(Surface parent) : base(parent) { Init(); - _stringFormat = new StringFormat(); - _stringFormat.Trimming = StringTrimming.EllipsisWord; } protected override void InitializeFields() { @@ -102,7 +103,6 @@ namespace Greenshot.Drawing { [OnDeserialized] private void OnDeserialized(StreamingContext context) { - _stringFormat = new StringFormat(); Init(); UpdateFormat(); } @@ -117,15 +117,18 @@ namespace Greenshot.Drawing { _stringFormat.Dispose(); _stringFormat = null; } - if (textBox != null) { - textBox.Dispose(); - textBox = null; + if (_textBox != null) { + _textBox.Dispose(); + _textBox = null; } } base.Dispose(disposing); } private void Init() { + _stringFormat = new StringFormat(); + _stringFormat.Trimming = StringTrimming.EllipsisWord; + fontInvalidated = true; CreateTextBox(); PropertyChanged += TextContainer_PropertyChanged; FieldChanged += TextContainer_FieldChanged; @@ -141,23 +144,23 @@ namespace Greenshot.Drawing { void TextContainer_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName.Equals("Selected")) { - if (!Selected && textBox.Visible) { + if (!Selected && _textBox.Visible) { HideTextBox(); } else if (Selected && Status==EditStatus.DRAWING) { ShowTextBox(); } } - if (textBox.Visible) { + if (_textBox.Visible) { UpdateTextBoxPosition(); UpdateTextBoxFormat(); - textBox.Invalidate(); + _textBox.Invalidate(); } } void TextContainer_FieldChanged(object sender, FieldChangedEventArgs e) { - if (textBox.Visible) { + if (_textBox.Visible) { UpdateTextBoxFormat(); - textBox.Invalidate(); + _textBox.Invalidate(); } else { UpdateFormat(); //Invalidate(); @@ -169,29 +172,28 @@ namespace Greenshot.Drawing { public override void OnDoubleClick() { ShowTextBox(); - textBox.Focus(); } private void CreateTextBox() { - textBox = new TextBox(); + _textBox = new TextBox(); - textBox.ImeMode = ImeMode.On; - textBox.Multiline = true; - textBox.AcceptsTab = true; - textBox.AcceptsReturn = true; - textBox.DataBindings.Add("Text", this, "Text", false, DataSourceUpdateMode.OnPropertyChanged); - textBox.LostFocus += textBox_LostFocus; - textBox.KeyDown += textBox_KeyDown; - textBox.BorderStyle = BorderStyle.FixedSingle; - textBox.Visible = false; + _textBox.ImeMode = ImeMode.On; + _textBox.Multiline = true; + _textBox.AcceptsTab = true; + _textBox.AcceptsReturn = true; + _textBox.DataBindings.Add("Text", this, "Text", false, DataSourceUpdateMode.OnPropertyChanged); + _textBox.LostFocus += textBox_LostFocus; + _textBox.KeyDown += textBox_KeyDown; + _textBox.BorderStyle = BorderStyle.FixedSingle; + _textBox.Visible = false; } private void ShowTextBox() { _parent.KeysLocked = true; - _parent.Controls.Add(textBox); + _parent.Controls.Add(_textBox); EnsureTextBoxContrast(); - textBox.Show(); - textBox.Focus(); + _textBox.Show(); + _textBox.Focus(); } /// @@ -200,17 +202,17 @@ namespace Greenshot.Drawing { private void EnsureTextBoxContrast() { Color lc = GetFieldValueAsColor(FieldType.LINE_COLOR); if (lc.R > 203 && lc.G > 203 && lc.B > 203) { - textBox.BackColor = Color.FromArgb(51, 51, 51); + _textBox.BackColor = Color.FromArgb(51, 51, 51); } else { - textBox.BackColor = Color.White; + _textBox.BackColor = Color.White; } } private void HideTextBox() { _parent.Focus(); - textBox.Hide(); + _textBox.Hide(); _parent.KeysLocked = false; - _parent.Controls.Remove(textBox); + _parent.Controls.Remove(_textBox); } protected void UpdateFormat() { @@ -265,10 +267,10 @@ namespace Greenshot.Drawing { } private void UpdateTextBoxPosition() { - textBox.Left = Left; - textBox.Top = Top; - textBox.Width = Width; - textBox.Height = Height; + _textBox.Left = Left; + _textBox.Top = Top; + _textBox.Width = Width; + _textBox.Height = Height; } public override void ApplyBounds(RectangleF newBounds) { @@ -279,8 +281,28 @@ namespace Greenshot.Drawing { private void UpdateTextBoxFormat() { UpdateFormat(); Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR); - textBox.ForeColor = lineColor; - textBox.Font = _font; + _textBox.ForeColor = lineColor; + _textBox.Font = _font; + StringAlignment horizontalAlignment = (StringAlignment)GetFieldValue(FieldType.TEXT_HORIZONTAL_ALIGNMENT); + switch (horizontalAlignment) { + case StringAlignment.Center: + _textBox.TextAlign = HorizontalAlignment.Center; + break; + case StringAlignment.Far: + if (_textBox.RightToLeft != RightToLeft.Yes) { + _textBox.TextAlign = HorizontalAlignment.Right; + } else { + _textBox.TextAlign = HorizontalAlignment.Left; + } + break; + case StringAlignment.Near: + if (_textBox.RightToLeft != RightToLeft.Yes) { + _textBox.TextAlign = HorizontalAlignment.Left; + } else { + _textBox.TextAlign = HorizontalAlignment.Right; + } + break; + } } void textBox_KeyDown(object sender, KeyEventArgs e) {