random-file-runner/RyzStudio/Windows/ThemedForms/TextBox/TButtonTextBox.cs
2021-10-30 03:21:45 +01:00

161 lines
5.6 KiB
C#

namespace RyzStudio.Windows.ThemedForms
{
using RyzStudio.Drawing;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public partial class TButtonTextBox : RyzStudio.Windows.ThemedForms.TUserControl
{
protected readonly Padding defaultPadding = new Padding(6, 5, 6, 6);
protected readonly Padding defaultMargin = new Padding(10, 0, 0, 10);
public TButtonTextBox() : base()
{
InitializeComponent();
//this.Margin = new Padding(10, 6, 10, 6);
this.Margin = defaultMargin;
this.Font = new Font(this.Font, FontStyle.Regular);
textBox1.Font = this.Font;
textBox1.Left = this.Margin.Left;
textBox1.PreviewKeyDown += textBox_PreviewKeyDown;
imageBox1.MouseClick += imageBox1_MouseClick;
OnResize(null);
}
protected virtual void imageBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
OnButtonClick?.Invoke(sender, e);
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Margin = defaultMargin;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
int b = (styleActive.BorderWidth + 1) + styleActive.BorderPadding;
this.Height = textBox1.Height + (b + defaultPadding.Top) + ((b - 1) + defaultPadding.Bottom);
imageBox1.Width = 18;
imageBox1.Height = textBox1.Height + defaultPadding.Top + defaultPadding.Bottom;
imageBox1.Left = this.Width - (this.Margin.Right + b + imageBox1.Width);
textBox1.Left = this.Margin.Left;
textBox1.Width = imageBox1.Left - textBox1.Left - 3;
textBox1.Top = (int)Math.Ceiling(decimal.Divide((this.Height - textBox1.Height), 2));
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
textBox1.Focus();
}
protected void textBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
if (this.SubmitButton != null)
{
this.SubmitButton.PerformClick();
}
break;
case Keys.Escape:
close();
break;
default: break;
}
}
[Category("Action")]
[Browsable(true)]
public event EventHandler OnButtonClick;
[Browsable(true), EditorBrowsable(EditorBrowsableState.Advanced)]
[Category("Appearance")]
public Forms.TImageBox InnerImageBox { get => imageBox1; set => imageBox1 = value; }
[Browsable(true), EditorBrowsable(EditorBrowsableState.Advanced)]
[Category("Appearance")]
public System.Windows.Forms.TextBox InnerTextBox { get => textBox1; set => textBox1 = value; }
[Category("Appearance"), Browsable(true)]
public Image NormalImage { get => imageBox1.ImageNormal; set => imageBox1.ImageNormal = value; }
[Category("Appearance"), Browsable(true)]
public Image HighlightImage { get => imageBox1.ImageHover; set => imageBox1.ImageHover = value; }
[Browsable(true), EditorBrowsable(EditorBrowsableState.Advanced)]
[Category("Appearance")]
public new string Text
{
get => textBox1.Text;
set
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new MethodInvoker(() => {
textBox1.Text = value;
textBox1.SelectionStart = textBox1.Text.Length;
}));
}
else
{
textBox1.Text = value;
textBox1.SelectionStart = textBox1.Text.Length;
}
}
}
[Browsable(true), EditorBrowsable(EditorBrowsableState.Advanced)]
[Category("Appearance")]
public bool UseSystemPasswordChar { get => textBox1.UseSystemPasswordChar; set => textBox1.UseSystemPasswordChar = value; }
[Browsable(true), EditorBrowsable(EditorBrowsableState.Advanced)]
[Category("Appearance")]
public TButton SubmitButton { get; set; } = null;
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new Padding Margin { get => base.Margin; set => base.Margin = defaultMargin; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new Padding Padding { get => base.Padding; set => base.Padding = value; }
public void SetTooltipText(ToolTip toolTip, string text)
{
toolTip.SetToolTip(imageBox1, text);
}
protected override void updateBackground(Graphics g, ThemeStyle style)
{
int b = (styleActive.BorderWidth + 1) + styleActive.BorderPadding;
this.Padding = new Padding((b + defaultPadding.Left), (b + defaultPadding.Top), ((b - 1) + defaultPadding.Right), ((b - 1) + defaultPadding.Bottom));
Rectangoid area = new Rectangoid(this.ClientRectangle, style.BorderRadius, style.BorderWidth);
g.FillPath(new SolidBrush(style.BackColour), area.ToGraphicsPath());
g.DrawPath(new Pen(new SolidBrush(style.BorderColour), style.BorderWidth), area.ToGraphicsPath());
}
}
}