123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- // 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
- }
- }
|