#region Imports using DPumpHydr.WinFrmUI.RLT.Design.Poison; using DPumpHydr.WinFrmUI.RLT.Drawing.Poison; using DPumpHydr.WinFrmUI.RLT.Enum.Poison; using DPumpHydr.WinFrmUI.RLT.Extension.Poison; using DPumpHydr.WinFrmUI.RLT.Interface.Poison; using DPumpHydr.WinFrmUI.RLT.Localization.Poison; using DPumpHydr.WinFrmUI.RLT.Manager; using DPumpHydr.WinFrmUI.RLT.Util; using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; #endregion namespace DPumpHydr.WinFrmUI.RLT.Controls { #region PoisonToggle [Designer(typeof(PoisonToggleDesigner))] [ToolboxBitmap(typeof(System.Windows.Forms.CheckBox))] public class PoisonToggle : System.Windows.Forms.CheckBox, IPoisonControl { #region Interface [Category(PoisonDefaults.PropertyCategory.Appearance)] public event EventHandler CustomPaintBackground; protected virtual void OnCustomPaintBackground(PoisonPaintEventArgs e) { if (GetStyle(ControlStyles.UserPaint) && CustomPaintBackground != null) { CustomPaintBackground(this, e); } } [Category(PoisonDefaults.PropertyCategory.Appearance)] public event EventHandler CustomPaint; protected virtual void OnCustomPaint(PoisonPaintEventArgs e) { if (GetStyle(ControlStyles.UserPaint) && CustomPaint != null) { CustomPaint(this, e); } } [Category(PoisonDefaults.PropertyCategory.Appearance)] public event EventHandler CustomPaintForeground; protected virtual void OnCustomPaintForeground(PoisonPaintEventArgs e) { if (GetStyle(ControlStyles.UserPaint) && CustomPaintForeground != null) { CustomPaintForeground(this, e); } } private ColorStyle poisonStyle = ColorStyle.Default; [Category(PoisonDefaults.PropertyCategory.Appearance)] [DefaultValue(ColorStyle.Default)] public ColorStyle Style { get { if (DesignMode || poisonStyle != ColorStyle.Default) { return poisonStyle; } if (StyleManager != null && poisonStyle == ColorStyle.Default) { return StyleManager.Style; } if (StyleManager == null && poisonStyle == ColorStyle.Default) { return PoisonDefaults.Style; } return poisonStyle; } set => poisonStyle = value; } private ThemeStyle poisonTheme = ThemeStyle.Default; [Category(PoisonDefaults.PropertyCategory.Appearance)] [DefaultValue(ThemeStyle.Default)] public ThemeStyle Theme { get { if (DesignMode || poisonTheme != ThemeStyle.Default) { return poisonTheme; } if (StyleManager != null && poisonTheme == ThemeStyle.Default) { return StyleManager.Theme; } if (StyleManager == null && poisonTheme == ThemeStyle.Default) { return PoisonDefaults.Theme; } return poisonTheme; } set => poisonTheme = value; } [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public PoisonStyleManager StyleManager { get; set; } = null; [DefaultValue(false)] [Category(PoisonDefaults.PropertyCategory.Appearance)] public bool UseCustomBackColor { get; set; } = false; [DefaultValue(false)] [Category(PoisonDefaults.PropertyCategory.Appearance)] public bool UseCustomForeColor { get; set; } = false; [DefaultValue(false)] [Category(PoisonDefaults.PropertyCategory.Appearance)] public bool UseStyleColors { get; set; } = false; [Browsable(false)] [Category(PoisonDefaults.PropertyCategory.Behaviour)] [DefaultValue(false)] public bool UseSelectable { get => GetStyle(ControlStyles.Selectable); set => SetStyle(ControlStyles.Selectable, value); } #endregion #region Fields [DefaultValue(false)] [Category(PoisonDefaults.PropertyCategory.Appearance)] public bool DisplayFocus { get; set; } = false; private readonly PoisonLocalize poisonLocalize = null; [DefaultValue(PoisonLinkLabelSize.Small)] [Category(PoisonDefaults.PropertyCategory.Appearance)] public PoisonLinkLabelSize FontSize { get; set; } = PoisonLinkLabelSize.Small; [DefaultValue(PoisonLinkLabelWeight.Regular)] [Category(PoisonDefaults.PropertyCategory.Appearance)] public PoisonLinkLabelWeight FontWeight { get; set; } = PoisonLinkLabelWeight.Regular; [DefaultValue(true)] [Category(PoisonDefaults.PropertyCategory.Appearance)] public bool DisplayStatus { get; set; } = true; [Browsable(false)] public override Font Font { get => base.Font; set => base.Font = value; } [Browsable(false)] public override Color ForeColor { get => base.ForeColor; set => base.ForeColor = value; } [Browsable(false)] public override string Text { get { if (Checked) { return poisonLocalize.Translate("StatusOn"); } return poisonLocalize.Translate("StatusOff"); } } private bool isHovered = false; private bool isPressed = false; private bool isFocused = false; #endregion #region Constructor public PoisonToggle() { SetStyle ( ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true ); Name = "PoisonToggle"; poisonLocalize = new PoisonLocalize(this); } #endregion #region Paint Methods protected override void OnPaintBackground(PaintEventArgs e) { try { Color backColor = BackColor; if (!UseCustomBackColor) { backColor = PoisonPaint.BackColor.Form(Theme); } if (backColor.A == 255) { e.Graphics.Clear(backColor); return; } base.OnPaintBackground(e); OnCustomPaintBackground(new PoisonPaintEventArgs(backColor, Color.Empty, e.Graphics)); } catch { Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { try { if (GetStyle(ControlStyles.AllPaintingInWmPaint)) { OnPaintBackground(e); } OnCustomPaint(new PoisonPaintEventArgs(Color.Empty, Color.Empty, e.Graphics)); OnPaintForeground(e); } catch { Invalidate(); } } protected virtual void OnPaintForeground(PaintEventArgs e) { Color borderColor, foreColor; if (isHovered && !isPressed && Enabled) { foreColor = PoisonPaint.ForeColor.CheckBox.Hover(Theme); borderColor = PoisonPaint.BorderColor.CheckBox.Hover(Theme); } else if (isHovered && isPressed && Enabled) { foreColor = PoisonPaint.ForeColor.CheckBox.Press(Theme); borderColor = PoisonPaint.BorderColor.CheckBox.Press(Theme); } else if (!Enabled) { foreColor = PoisonPaint.ForeColor.CheckBox.Disabled(Theme); borderColor = PoisonPaint.BorderColor.CheckBox.Disabled(Theme); } else { foreColor = !UseStyleColors ? PoisonPaint.ForeColor.CheckBox.Normal(Theme) : PoisonPaint.GetStyleColor(Style); borderColor = PoisonPaint.BorderColor.CheckBox.Normal(Theme); } using (Pen p = new(borderColor)) { Rectangle boxRect = new(DisplayStatus ? 30 : 0, 0, ClientRectangle.Width - (DisplayStatus ? 31 : 1), ClientRectangle.Height - 1); e.Graphics.DrawRectangle(p, boxRect); } Color fillColor = Checked ? PoisonPaint.GetStyleColor(Style) : PoisonPaint.BorderColor.CheckBox.Normal(Theme); using (SolidBrush b = new(fillColor)) { Rectangle boxRect = new(DisplayStatus ? 32 : 2, 2, ClientRectangle.Width - (DisplayStatus ? 34 : 4), ClientRectangle.Height - 4); e.Graphics.FillRectangle(b, boxRect); } Color backColor = BackColor; if (!UseCustomBackColor) { backColor = PoisonPaint.BackColor.Form(Theme); } using (SolidBrush b = new(backColor)) { int left = Checked ? Width - 11 : (DisplayStatus ? 30 : 0); Rectangle boxRect = new(left, 0, 11, ClientRectangle.Height); e.Graphics.FillRectangle(b, boxRect); } using (SolidBrush b = new(PoisonPaint.BorderColor.CheckBox.Hover(Theme))) { int left = Checked ? Width - 10 : (DisplayStatus ? 30 : 0); Rectangle boxRect = new(left, 0, 10, ClientRectangle.Height); e.Graphics.FillRectangle(b, boxRect); } if (DisplayStatus) { Rectangle textRect = new(0, 0, 30, ClientRectangle.Height); TextRenderer.DrawText(e.Graphics, Text, PoisonFonts.LinkLabel(FontSize, FontWeight), textRect, foreColor, PoisonPaint.GetTextFormatFlags(TextAlign)); } if (DisplayFocus && isFocused) { ControlPaint.DrawFocusRectangle(e.Graphics, ClientRectangle); } } #endregion #region Focus Methods protected override void OnGotFocus(EventArgs e) { isFocused = true; isHovered = true; Invalidate(); base.OnGotFocus(e); } protected override void OnLostFocus(EventArgs e) { isFocused = false; isHovered = false; isPressed = false; Invalidate(); base.OnLostFocus(e); } protected override void OnEnter(EventArgs e) { isFocused = true; isHovered = true; Invalidate(); base.OnEnter(e); } protected override void OnLeave(EventArgs e) { isFocused = false; isHovered = false; isPressed = false; Invalidate(); base.OnLeave(e); } #endregion #region Keyboard Methods protected override void OnKeyDown(KeyEventArgs e) { if (e.KeyCode == Keys.Space) { isHovered = true; isPressed = true; Invalidate(); } base.OnKeyDown(e); } protected override void OnKeyUp(KeyEventArgs e) { //Remove this code cause this prevents the focus color //isHovered = false; //isPressed = false; Invalidate(); base.OnKeyUp(e); } #endregion #region Mouse Methods protected override void OnMouseEnter(EventArgs e) { isHovered = true; Invalidate(); base.OnMouseEnter(e); } protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isPressed = true; Invalidate(); } base.OnMouseDown(e); } protected override void OnMouseUp(MouseEventArgs e) { isPressed = false; Invalidate(); base.OnMouseUp(e); } protected override void OnMouseLeave(EventArgs e) { if (!isFocused) { isHovered = false; } Invalidate(); base.OnMouseLeave(e); } #endregion #region Overridden Methods protected override void OnEnabledChanged(EventArgs e) { base.OnEnabledChanged(e); Invalidate(); } protected override void OnCheckedChanged(EventArgs e) { base.OnCheckedChanged(e); Invalidate(); } public override Size GetPreferredSize(Size proposedSize) { Size preferredSize = base.GetPreferredSize(proposedSize); preferredSize.Width = DisplayStatus ? 80 : 50; return preferredSize; } #endregion } #endregion }