// Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // Copyright (c) 2004-2005 Novell, Inc. // // Authors: // Jonathan Chambers (jonathan.chambers@ansys.com) // // COMPLETE using System.ComponentModel; using System.Drawing; namespace System.Windows.Forms.PropertyGridInternal { internal class PGTextBox : TextBox { protected override bool IsInputKey(Keys keyData) { // To be handled by the PropertyGridView if ((keyData & Keys.Alt) != 0 && (keyData & Keys.KeyCode) == Keys.Down) return true; return base.IsInputKey(keyData); } } internal class PropertyGridTextBox : System.Windows.Forms.UserControl { #region Private Members private PGTextBox textbox; private Button dialog_button; private Button dropdown_button; #endregion Private Members #region Contructors public PropertyGridTextBox() { dialog_button = new Button(); dropdown_button = new Button(); textbox = new PGTextBox(); SuspendLayout(); dialog_button.Dock = DockStyle.Right; dialog_button.BackColor = Drawing.SystemColors.Control; dialog_button.FlatAppearance.BorderSize = 0; dialog_button.TabIndex = 1; dialog_button.Visible = false; dialog_button.Click += (s, e) => DialogButtonClicked(this, e); dropdown_button.Dock = DockStyle.Right; dropdown_button.BackColor = Drawing.SystemColors.Control; dropdown_button.FlatAppearance.BorderSize = 0; dropdown_button.TabIndex = 2; dropdown_button.Visible = false; dropdown_button.Click += (s, e) => DropDownButtonClicked(this, e); textbox.AutoSize = false; textbox.BackColor = Color.Transparent; textbox.BorderStyle = BorderStyle.None; textbox.Dock = DockStyle.Fill; textbox.TabIndex = 3; textbox.Padding = new Padding(0, 1, 0, 0); Controls.Add(textbox); Controls.Add(dropdown_button); Controls.Add(dialog_button); SetStyle(ControlStyles.Selectable, true); ResumeLayout(false); textbox.DoubleClick += (s, e) => ToggleValue(this, e); textbox.KeyDown += (s, e) => KeyDown(this, e); textbox.control.Loaded += (s, e) => { var window = System.Windows.Window.GetWindow(control); if (window != null) window.PreviewMouseDown += Window_PreviewMouseDown; }; } #endregion Contructors #region Private Methods private void Window_PreviewMouseDown(object sender, Input.MouseButtonEventArgs e) { if (textbox.IsFocused && !textbox.IsMouseOver) { var args = new CancelEventArgs(); Validate?.Invoke(this, args); if (args.Cancel) { e.Handled = true; return; } } } #endregion #region Protected Methods protected override void OnParentRightToLeftChanged(EventArgs e) { base.OnParentRightToLeftChanged(e); if (RightToLeft == RightToLeft.Yes) { dialog_button.Dock = DockStyle.Left; dropdown_button.Dock = DockStyle.Left; } else { dialog_button.Dock = DockStyle.Right; dropdown_button.Dock = DockStyle.Right; } } protected override void OnResize(EventArgs e) { base.OnResize(e); if (Width <= 0 || Height <= 0) return; int _18 = _dpi(18); dialog_button.Size = new Drawing.Size(_18, _18); dropdown_button.Size = new Drawing.Size(_18, _18); Pen pen = ResPool.GetPen(Drawing.SystemColors.ControlText, _dpi(1f)); int add = RightToLeft == RightToLeft.Yes ? _dpi(2) : 0; var dropdownBmp = new Bitmap(Height, Height); using (Graphics g = Graphics.FromImage(dropdownBmp)) { float x = _dpi(3f) + add; float y = _dpi(4f) + 1; g.DrawLines(pen, new PointF[] { new PointF(x, y), new PointF(x + _dpi(4f), y + _dpi(4f)), new PointF(x + _dpi(8f), y) }); } dropdown_button.Image = dropdownBmp; var dialogBmp = new Bitmap(Height, Height); using (Graphics g = Graphics.FromImage(dialogBmp)) { float x = _dpi(2f) + 1 + add; float y = _dpi(6f) + 1; g.DrawRectangle(pen, x, y, 1, 1); g.DrawRectangle(pen, x + _dpi(4), y, 1, 1); g.DrawRectangle(pen, x + _dpi(8), y, 1, 1); } dialog_button.Image = dialogBmp; } protected override void OnGotFocus(EventArgs args) { base.OnGotFocus(args); // force-disable selection textbox.Focus(); textbox.SelectionLength = 0; } #endregion #region Public Methods public void SendMouseDown(int x) { var timer = new Timer(); timer.Interval = 50; timer.Tick += (s, e) => { textbox.Focus(); textbox.Select(textbox.GetCharIndexFromPosition(new Drawing.Point(x, 0)) + 1, 0); timer.Stop(); timer.Dispose(); }; timer.Start(); } public void SelectAll() { textbox.SelectAll(); } #endregion #region Public Instance Properties public bool DialogButtonVisible { get { return dialog_button.Visible; } set { dialog_button.Visible = value; } } public bool DropDownButtonVisible { get { return dropdown_button.Visible; } set { dropdown_button.Visible = value; } } public new Color ForeColor { get { return base.ForeColor; } set { textbox.ForeColor = value; dropdown_button.ForeColor = value; dialog_button.ForeColor = value; base.ForeColor = value; } } public new Color BackColor { get { return base.BackColor; } set { textbox.BackColor = value; base.BackColor = Color.Transparent; } } public bool ReadOnly { get { return textbox.ReadOnly; } set { textbox.ReadOnly = value; } } public new string Text { get { return textbox.Text; } set { textbox.Text = value; } } public char PasswordChar { set { textbox.PasswordChar = value; } } #endregion Public Instance Properties #region Events public event EventHandler DropDownButtonClicked; public event EventHandler DialogButtonClicked; public event EventHandler ToggleValue; public new event KeyEventHandler KeyDown; public event CancelEventHandler Validate; #endregion Events } }