2008-04-05 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / PropertyGridTextBox.cs
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
26 // COMPLETE
27
28 using System;
29 using System.Drawing;
30
31 namespace System.Windows.Forms.PropertyGridInternal 
32 {
33         internal class PGTextBox : TextBox 
34         {
35                 private bool _focusing = false;
36
37                 public void FocusAt (Point location)
38                 {
39                         _focusing = true;
40                         Point pnt = PointToClient (location);
41                         XplatUI.SendMessage (Handle, Msg.WM_LBUTTONDOWN, new IntPtr ((int)MsgButtons.MK_LBUTTON), Control.MakeParam (pnt.X, pnt.Y));
42                 }
43
44                 protected override void WndProc (ref Message m)
45                 {
46                         // Swallow the first MOUSEMOVE after the focusing WM_LBUTTONDOWN
47                         if (_focusing && m.Msg == (int)Msg.WM_MOUSEMOVE) {
48                                 _focusing = false;
49                                 return;
50                         }
51                         base.WndProc (ref m);
52                 }
53         }
54         
55         internal class PropertyGridTextBox : System.Windows.Forms.UserControl {
56                 #region Private Members
57
58                 private PGTextBox textbox;
59                 private Button dialog_button;
60                 private Button dropdown_button;
61
62                 #endregion Private Members
63
64                 #region Contructors
65                 public PropertyGridTextBox() {
66                         dialog_button = new Button();
67                         dropdown_button = new Button();
68                         textbox = new PGTextBox ();
69
70                         SuspendLayout();
71
72                         dialog_button.Dock = DockStyle.Right;
73                         dialog_button.BackColor = SystemColors.Control;
74                         dialog_button.Size = new Size(16, 16);
75                         dialog_button.TabIndex = 1;
76                         dialog_button.Visible = false;
77                         dialog_button.Click += new System.EventHandler(dialog_button_Click);
78
79                         dropdown_button.Dock = DockStyle.Right;
80                         dropdown_button.BackColor = SystemColors.Control;
81                         dropdown_button.Size = new Size(16, 16);
82                         dropdown_button.TabIndex = 2;
83                         dropdown_button.Visible = false;
84                         dropdown_button.Click += new System.EventHandler(dropdown_button_Click);
85
86                         textbox.AutoSize = false;
87                         textbox.BorderStyle = BorderStyle.None;
88                         textbox.Dock = DockStyle.Fill;
89                         textbox.TabIndex = 3;
90
91                         Controls.Add(textbox);
92                         Controls.Add(dropdown_button);
93                         Controls.Add(dialog_button);
94
95                         SetStyle (ControlStyles.Selectable, true);
96
97                         ResumeLayout(false);
98
99                         dropdown_button.Paint+=new PaintEventHandler(dropdown_button_Paint);
100                         dialog_button.Paint+=new PaintEventHandler(dialog_button_Paint);
101                         textbox.DoubleClick+=new EventHandler(textbox_DoubleClick);
102                 }
103
104                 
105                 #endregion Contructors
106
107                 #region Protected Instance Properties
108
109                 protected override void OnGotFocus (EventArgs args)
110                 {
111                         base.OnGotFocus (args);
112                         // force-disable selection
113                         textbox.has_been_focused = true;
114                         textbox.Focus ();
115                         textbox.SelectionLength = 0;
116                 }
117
118                 #endregion
119
120                 #region Public Instance Properties
121
122                 public bool DialogButtonVisible {
123                         get{
124                                 return dialog_button.Visible;
125                         }
126                         set {
127                                 dialog_button.Visible = value;
128                         }
129                 }
130                 public bool DropDownButtonVisible {
131                         get{
132                                 return dropdown_button.Visible;
133                         }
134                         set {
135                                 dropdown_button.Visible = value;
136                         }
137                 }
138
139                 public new Color ForeColor {
140                         get {
141                                 return base.ForeColor;
142                         }
143                         set {
144                                 textbox.ForeColor = value;
145                                 dropdown_button.ForeColor = value;
146                                 dialog_button.ForeColor = value;
147                                 base.ForeColor = value;
148                         }
149                 }
150
151                 public new Color BackColor {
152                         get {
153                                 return base.BackColor;
154                         }
155                         set {
156                                 textbox.BackColor = value;
157                                 base.BackColor = value;
158                         }
159                 }
160                 public bool ReadOnly {
161                         get {
162                                 return textbox.ReadOnly;
163                         }
164                         set {
165                                 textbox.ReadOnly = value;
166                         }
167                 }
168
169                 public new string Text {
170                         get {
171                                 return textbox.Text;
172                         }
173                         set {
174                                 textbox.Text = value;
175                         }
176                 }
177
178                 public char PasswordChar {
179                         set { textbox.PasswordChar = value; }
180                 }
181
182                 #endregion Public Instance Properties
183                 
184                 #region Events
185                 static object DropDownButtonClickedEvent = new object ();
186                 static object DialogButtonClickedEvent = new object ();
187                 static object ToggleValueEvent = new object ();
188
189                 public event EventHandler DropDownButtonClicked {
190                         add { Events.AddHandler (DropDownButtonClickedEvent, value); }
191                         remove { Events.RemoveHandler (DropDownButtonClickedEvent, value); }
192                 }
193
194                 public event EventHandler DialogButtonClicked {
195                         add { Events.AddHandler (DialogButtonClickedEvent, value); }
196                         remove { Events.RemoveHandler (DialogButtonClickedEvent, value); }
197                 }
198
199                 public event EventHandler ToggleValue {
200                         add { Events.AddHandler (ToggleValueEvent, value); }
201                         remove { Events.RemoveHandler (ToggleValueEvent, value); }
202                 }
203                 
204                 #endregion Events
205                 
206                 #region Private Helper Methods
207
208                 private void dropdown_button_Paint(object sender, PaintEventArgs e)
209                 {
210                         ThemeEngine.Current.CPDrawComboButton(e.Graphics, dropdown_button.ClientRectangle, dropdown_button.ButtonState);
211                 }
212
213                 private void dialog_button_Paint(object sender, PaintEventArgs e) {
214                         // best way to draw the ellipse?
215                         e.Graphics.DrawString("...", new Font(Font,FontStyle.Bold), Brushes.Black, 0,0);
216                 }
217
218                 private void dropdown_button_Click(object sender, EventArgs e) {
219                         EventHandler eh = (EventHandler)(Events [DropDownButtonClickedEvent]);
220                         if (eh != null)
221                                 eh (this, e);
222                 }
223
224                 private void dialog_button_Click(object sender, EventArgs e) {
225                         EventHandler eh = (EventHandler)(Events [DialogButtonClickedEvent]);
226                         if (eh != null)
227                                 eh (this, e);
228                 }
229
230                 #endregion Private Helper Methods
231
232                 internal void SendMouseDown (Point screenLocation)
233                 {
234                         Point clientLocation = PointToClient (screenLocation);
235                         XplatUI.SendMessage (Handle, Msg.WM_LBUTTONDOWN, new IntPtr ((int) MsgButtons.MK_LBUTTON), Control.MakeParam (clientLocation.X, clientLocation.Y));
236                         textbox.FocusAt (screenLocation);
237                 }       
238
239                 private void textbox_DoubleClick(object sender, EventArgs e) {
240                         EventHandler eh = (EventHandler)(Events [ToggleValueEvent]);
241                         if (eh != null)
242                                 eh (this, e);
243                 }
244         }
245 }