BUG-1712: Improving the TextBox behavior a bit: horizontal alignment was missing during the editing and making the size of the Textbox better match the size of the element

This commit is contained in:
RKrom 2014-12-03 11:12:06 +01:00
parent 74ffad41c6
commit 4c17a87055

View file

@ -130,6 +130,9 @@ namespace Greenshot.Drawing {
_stringFormat.Trimming = StringTrimming.EllipsisWord; _stringFormat.Trimming = StringTrimming.EllipsisWord;
fontInvalidated = true; fontInvalidated = true;
CreateTextBox(); CreateTextBox();
AlignTextbox();
UpdateFormat();
UpdateTextBoxFormat();
PropertyChanged += TextContainer_PropertyChanged; PropertyChanged += TextContainer_PropertyChanged;
FieldChanged += TextContainer_FieldChanged; FieldChanged += TextContainer_FieldChanged;
} }
@ -195,10 +198,28 @@ namespace Greenshot.Drawing {
_textBox.DataBindings.Add("Text", this, "Text", false, DataSourceUpdateMode.OnPropertyChanged); _textBox.DataBindings.Add("Text", this, "Text", false, DataSourceUpdateMode.OnPropertyChanged);
_textBox.LostFocus += textBox_LostFocus; _textBox.LostFocus += textBox_LostFocus;
_textBox.KeyDown += textBox_KeyDown; _textBox.KeyDown += textBox_KeyDown;
_textBox.BorderStyle = BorderStyle.FixedSingle; _textBox.BorderStyle = BorderStyle.None;
_textBox.Visible = false; _textBox.Visible = false;
} }
private void AlignTextbox() {
if (_textBox == null) {
return;
}
StringAlignment alignment = (StringAlignment)GetFieldValue(FieldType.TEXT_HORIZONTAL_ALIGNMENT);
switch (alignment) {
case StringAlignment.Near:
_textBox.TextAlign = HorizontalAlignment.Left;
break;
case StringAlignment.Far:
_textBox.TextAlign = HorizontalAlignment.Right;
break;
case StringAlignment.Center:
_textBox.TextAlign = HorizontalAlignment.Center;
break;
}
}
private void ShowTextBox() { private void ShowTextBox() {
_parent.KeysLocked = true; _parent.KeysLocked = true;
_parent.Controls.Add(_textBox); _parent.Controls.Add(_textBox);
@ -250,6 +271,9 @@ namespace Greenshot.Drawing {
fontInvalidated = true; fontInvalidated = true;
} }
/// <summary>
/// Generate the Font-Formal so we can draw correctly
/// </summary>
protected void UpdateFormat() { protected void UpdateFormat() {
string fontFamily = GetFieldValueAsString(FieldType.FONT_FAMILY); string fontFamily = GetFieldValueAsString(FieldType.FONT_FAMILY);
bool fontBold = GetFieldValueAsBool(FieldType.FONT_BOLD); bool fontBold = GetFieldValueAsBool(FieldType.FONT_BOLD);
@ -300,13 +324,28 @@ namespace Greenshot.Drawing {
_stringFormat.Alignment = (StringAlignment)GetFieldValue(FieldType.TEXT_HORIZONTAL_ALIGNMENT); _stringFormat.Alignment = (StringAlignment)GetFieldValue(FieldType.TEXT_HORIZONTAL_ALIGNMENT);
_stringFormat.LineAlignment = (StringAlignment)GetFieldValue(FieldType.TEXT_VERTICAL_ALIGNMENT); _stringFormat.LineAlignment = (StringAlignment)GetFieldValue(FieldType.TEXT_VERTICAL_ALIGNMENT);
} }
/// <summary>
/// This will create the textbox exactly to the inner size of the element
/// is a bit of a hack, but for now it seems to work...
/// </summary>
private void UpdateTextBoxPosition() { private void UpdateTextBoxPosition() {
int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS);
int lineWidth = (int)Math.Floor(lineThickness / 2d);
int correction = (lineThickness +1 ) % 2;
if (lineThickness <= 1) {
lineWidth = 1;
correction = -1;
}
Rectangle absRectangle = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); Rectangle absRectangle = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height);
_textBox.Left = absRectangle.Left; _textBox.Left = absRectangle.Left + lineWidth;
_textBox.Top = absRectangle.Top; _textBox.Top = absRectangle.Top + lineWidth;
_textBox.Width = absRectangle.Width; if (lineThickness <= 1) {
_textBox.Height = absRectangle.Height; lineWidth = 0;
}
_textBox.Width = absRectangle.Width - (2 * lineWidth) + correction;
_textBox.Height = absRectangle.Height - (2 * lineWidth) + correction;
} }
public override void ApplyBounds(RectangleF newBounds) { public override void ApplyBounds(RectangleF newBounds) {
@ -319,6 +358,7 @@ namespace Greenshot.Drawing {
Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR); Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR);
_textBox.ForeColor = lineColor; _textBox.ForeColor = lineColor;
_textBox.Font = _font; _textBox.Font = _font;
AlignTextbox();
} }
void textBox_KeyDown(object sender, KeyEventArgs e) { void textBox_KeyDown(object sender, KeyEventArgs e) {