PropertyGridTextBox.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jonathan Chambers (jonathan.chambers@ansys.com)
  24. //
  25. // COMPLETE
  26. using System.ComponentModel;
  27. using System.Drawing;
  28. namespace System.Windows.Forms.PropertyGridInternal
  29. {
  30. internal class PGTextBox : TextBox
  31. {
  32. protected override bool IsInputKey(Keys keyData)
  33. {
  34. // To be handled by the PropertyGridView
  35. if ((keyData & Keys.Alt) != 0 &&
  36. (keyData & Keys.KeyCode) == Keys.Down)
  37. return true;
  38. return base.IsInputKey(keyData);
  39. }
  40. }
  41. internal class PropertyGridTextBox : System.Windows.Forms.UserControl
  42. {
  43. #region Private Members
  44. private PGTextBox textbox;
  45. private Button dialog_button;
  46. private Button dropdown_button;
  47. #endregion Private Members
  48. #region Contructors
  49. public PropertyGridTextBox()
  50. {
  51. dialog_button = new Button();
  52. dropdown_button = new Button();
  53. textbox = new PGTextBox();
  54. SuspendLayout();
  55. dialog_button.Dock = DockStyle.Right;
  56. dialog_button.BackColor = Drawing.SystemColors.Control;
  57. dialog_button.FlatAppearance.BorderSize = 0;
  58. dialog_button.TabIndex = 1;
  59. dialog_button.Visible = false;
  60. dialog_button.Click += (s, e) => DialogButtonClicked(this, e);
  61. dropdown_button.Dock = DockStyle.Right;
  62. dropdown_button.BackColor = Drawing.SystemColors.Control;
  63. dropdown_button.FlatAppearance.BorderSize = 0;
  64. dropdown_button.TabIndex = 2;
  65. dropdown_button.Visible = false;
  66. dropdown_button.Click += (s, e) => DropDownButtonClicked(this, e);
  67. textbox.AutoSize = false;
  68. textbox.BackColor = Color.Transparent;
  69. textbox.BorderStyle = BorderStyle.None;
  70. textbox.Dock = DockStyle.Fill;
  71. textbox.TabIndex = 3;
  72. textbox.Padding = new Padding(0, 1, 0, 0);
  73. Controls.Add(textbox);
  74. Controls.Add(dropdown_button);
  75. Controls.Add(dialog_button);
  76. SetStyle(ControlStyles.Selectable, true);
  77. ResumeLayout(false);
  78. textbox.DoubleClick += (s, e) => ToggleValue(this, e);
  79. textbox.KeyDown += (s, e) => KeyDown(this, e);
  80. textbox.control.Loaded += (s, e) =>
  81. {
  82. var window = System.Windows.Window.GetWindow(control);
  83. if (window != null)
  84. window.PreviewMouseDown += Window_PreviewMouseDown;
  85. };
  86. }
  87. #endregion Contructors
  88. #region Private Methods
  89. private void Window_PreviewMouseDown(object sender, Input.MouseButtonEventArgs e)
  90. {
  91. if (textbox.IsFocused && !textbox.IsMouseOver)
  92. {
  93. var args = new CancelEventArgs();
  94. Validate?.Invoke(this, args);
  95. if (args.Cancel)
  96. {
  97. e.Handled = true;
  98. return;
  99. }
  100. }
  101. }
  102. #endregion
  103. #region Protected Methods
  104. protected override void OnParentRightToLeftChanged(EventArgs e)
  105. {
  106. base.OnParentRightToLeftChanged(e);
  107. if (RightToLeft == RightToLeft.Yes)
  108. {
  109. dialog_button.Dock = DockStyle.Left;
  110. dropdown_button.Dock = DockStyle.Left;
  111. }
  112. else
  113. {
  114. dialog_button.Dock = DockStyle.Right;
  115. dropdown_button.Dock = DockStyle.Right;
  116. }
  117. }
  118. protected override void OnResize(EventArgs e)
  119. {
  120. base.OnResize(e);
  121. if (Width <= 0 || Height <= 0)
  122. return;
  123. int _18 = _dpi(18);
  124. dialog_button.Size = new Drawing.Size(_18, _18);
  125. dropdown_button.Size = new Drawing.Size(_18, _18);
  126. Pen pen = ResPool.GetPen(Drawing.SystemColors.ControlText, _dpi(1f));
  127. int add = RightToLeft == RightToLeft.Yes ? _dpi(2) : 0;
  128. var dropdownBmp = new Bitmap(Height, Height);
  129. using (Graphics g = Graphics.FromImage(dropdownBmp))
  130. {
  131. float x = _dpi(3f) + add;
  132. float y = _dpi(4f) + 1;
  133. g.DrawLines(pen, new PointF[]
  134. {
  135. new PointF(x, y),
  136. new PointF(x + _dpi(4f), y + _dpi(4f)),
  137. new PointF(x + _dpi(8f), y)
  138. });
  139. }
  140. dropdown_button.Image = dropdownBmp;
  141. var dialogBmp = new Bitmap(Height, Height);
  142. using (Graphics g = Graphics.FromImage(dialogBmp))
  143. {
  144. float x = _dpi(2f) + 1 + add;
  145. float y = _dpi(6f) + 1;
  146. g.DrawRectangle(pen, x, y, 1, 1);
  147. g.DrawRectangle(pen, x + _dpi(4), y, 1, 1);
  148. g.DrawRectangle(pen, x + _dpi(8), y, 1, 1);
  149. }
  150. dialog_button.Image = dialogBmp;
  151. }
  152. protected override void OnGotFocus(EventArgs args)
  153. {
  154. base.OnGotFocus(args);
  155. // force-disable selection
  156. textbox.Focus();
  157. textbox.SelectionLength = 0;
  158. }
  159. #endregion
  160. #region Public Methods
  161. public void SendMouseDown(int x)
  162. {
  163. var timer = new Timer();
  164. timer.Interval = 50;
  165. timer.Tick += (s, e) =>
  166. {
  167. textbox.Focus();
  168. textbox.Select(textbox.GetCharIndexFromPosition(new Drawing.Point(x, 0)) + 1, 0);
  169. timer.Stop();
  170. timer.Dispose();
  171. };
  172. timer.Start();
  173. }
  174. public void SelectAll()
  175. {
  176. textbox.SelectAll();
  177. }
  178. #endregion
  179. #region Public Instance Properties
  180. public bool DialogButtonVisible
  181. {
  182. get
  183. {
  184. return dialog_button.Visible;
  185. }
  186. set
  187. {
  188. dialog_button.Visible = value;
  189. }
  190. }
  191. public bool DropDownButtonVisible
  192. {
  193. get
  194. {
  195. return dropdown_button.Visible;
  196. }
  197. set
  198. {
  199. dropdown_button.Visible = value;
  200. }
  201. }
  202. public new Color ForeColor
  203. {
  204. get
  205. {
  206. return base.ForeColor;
  207. }
  208. set
  209. {
  210. textbox.ForeColor = value;
  211. dropdown_button.ForeColor = value;
  212. dialog_button.ForeColor = value;
  213. base.ForeColor = value;
  214. }
  215. }
  216. public new Color BackColor
  217. {
  218. get
  219. {
  220. return base.BackColor;
  221. }
  222. set
  223. {
  224. textbox.BackColor = value;
  225. base.BackColor = Color.Transparent;
  226. }
  227. }
  228. public bool ReadOnly
  229. {
  230. get
  231. {
  232. return textbox.ReadOnly;
  233. }
  234. set
  235. {
  236. textbox.ReadOnly = value;
  237. }
  238. }
  239. public new string Text
  240. {
  241. get
  242. {
  243. return textbox.Text;
  244. }
  245. set
  246. {
  247. textbox.Text = value;
  248. }
  249. }
  250. public char PasswordChar
  251. {
  252. set { textbox.PasswordChar = value; }
  253. }
  254. #endregion Public Instance Properties
  255. #region Events
  256. public event EventHandler DropDownButtonClicked;
  257. public event EventHandler DialogButtonClicked;
  258. public event EventHandler ToggleValue;
  259. public new event KeyEventHandler KeyDown;
  260. public event CancelEventHandler Validate;
  261. #endregion Events
  262. }
  263. }