2008-03-27 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[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.Size = new Size(16, 16);
74                         dialog_button.TabIndex = 1;
75                         dialog_button.Visible = false;
76                         dialog_button.Click += new System.EventHandler(dialog_button_Click);
77
78                         dropdown_button.Dock = DockStyle.Right;
79                         dropdown_button.Size = new Size(16, 16);
80                         dropdown_button.TabIndex = 2;
81                         dropdown_button.Visible = false;
82                         dropdown_button.Click += new System.EventHandler(dropdown_button_Click);
83
84                         textbox.AutoSize = false;
85                         // this is to explicitly set bgcolor to avoid
86                         // default ReadOnly color (which makes grid item
87                         // bgcolor inconsistent).
88                         textbox.BackColor = textbox.BackColor;
89                         textbox.BorderStyle = BorderStyle.None;
90                         textbox.Dock = DockStyle.Fill;
91                         textbox.TabIndex = 3;
92
93                         Controls.Add(textbox);
94                         Controls.Add(dropdown_button);
95                         Controls.Add(dialog_button);
96
97                         SetStyle (ControlStyles.Selectable, true);
98
99                         ResumeLayout(false);
100
101                         dropdown_button.Paint+=new PaintEventHandler(dropdown_button_Paint);
102                         dialog_button.Paint+=new PaintEventHandler(dialog_button_Paint);
103                         textbox.DoubleClick+=new EventHandler(textbox_DoubleClick);
104                 }
105
106                 
107                 #endregion Contructors
108
109                 #region Protected Instance Properties
110
111                 protected override void OnGotFocus (EventArgs args)
112                 {
113                         base.OnGotFocus (args);
114                         // force-disable selection
115                         textbox.has_been_focused = true;
116                         textbox.Focus ();
117                         textbox.SelectionLength = 0;
118                 }
119
120                 #endregion
121
122                 #region Public Instance Properties
123
124                 public bool DialogButtonVisible {
125                         get{
126                                 return dialog_button.Visible;
127                         }
128                         set {
129                                 dialog_button.Visible = value;
130                         }
131                 }
132                 public bool DropDownButtonVisible {
133                         get{
134                                 return dropdown_button.Visible;
135                         }
136                         set {
137                                 dropdown_button.Visible = value;
138                         }
139                 }
140
141                 public new Color ForeColor {
142                         get {
143                                 return base.ForeColor;
144                         }
145                         set {
146                                 textbox.ForeColor = value;
147                                 dropdown_button.ForeColor = value;
148                                 dialog_button.ForeColor = value;
149                                 base.ForeColor = value;
150                         }
151                 }
152
153                 public new Color BackColor {
154                         get {
155                                 return base.BackColor;
156                         }
157                         set {
158                                 textbox.BackColor = value;
159                                 dropdown_button.BackColor = value;
160                                 dialog_button.BackColor = value;
161                                 base.BackColor = value;
162                         }
163                 }
164                 public bool ReadOnly {
165                         get {
166                                 return textbox.ReadOnly;
167                         }
168                         set {
169                                 textbox.ReadOnly = value;
170                         }
171                 }
172
173                 public new string Text {
174                         get {
175                                 return textbox.Text;
176                         }
177                         set {
178                                 textbox.Text = value;
179                         }
180                 }
181
182                 public char PasswordChar {
183                         set { textbox.PasswordChar = value; }
184                 }
185
186                 #endregion Public Instance Properties
187                 
188                 #region Events
189                 static object DropDownButtonClickedEvent = new object ();
190                 static object DialogButtonClickedEvent = new object ();
191                 static object ToggleValueEvent = new object ();
192
193                 public event EventHandler DropDownButtonClicked {
194                         add { Events.AddHandler (DropDownButtonClickedEvent, value); }
195                         remove { Events.RemoveHandler (DropDownButtonClickedEvent, value); }
196                 }
197
198                 public event EventHandler DialogButtonClicked {
199                         add { Events.AddHandler (DialogButtonClickedEvent, value); }
200                         remove { Events.RemoveHandler (DialogButtonClickedEvent, value); }
201                 }
202
203                 public event EventHandler ToggleValue {
204                         add { Events.AddHandler (ToggleValueEvent, value); }
205                         remove { Events.RemoveHandler (ToggleValueEvent, value); }
206                 }
207                 
208                 #endregion Events
209                 
210                 #region Private Helper Methods
211
212                 private void dropdown_button_Paint(object sender, PaintEventArgs e)
213                 {
214                         ThemeEngine.Current.CPDrawComboButton(e.Graphics, dropdown_button.ClientRectangle, dropdown_button.ButtonState);
215                 }
216
217                 private void dialog_button_Paint(object sender, PaintEventArgs e) {
218                         // best way to draw the ellipse?
219                         e.Graphics.DrawString("...", new Font(Font,FontStyle.Bold), Brushes.Black, 0,0);
220                 }
221
222                 private void dropdown_button_Click(object sender, EventArgs e) {
223                         EventHandler eh = (EventHandler)(Events [DropDownButtonClickedEvent]);
224                         if (eh != null)
225                                 eh (this, e);
226                 }
227
228                 private void dialog_button_Click(object sender, EventArgs e) {
229                         EventHandler eh = (EventHandler)(Events [DialogButtonClickedEvent]);
230                         if (eh != null)
231                                 eh (this, e);
232                 }
233
234                 #endregion Private Helper Methods
235
236                 internal void SendMouseDown (Point screenLocation)
237                 {
238                         Point clientLocation = PointToClient (screenLocation);
239                         XplatUI.SendMessage (Handle, Msg.WM_LBUTTONDOWN, new IntPtr ((int) MsgButtons.MK_LBUTTON), Control.MakeParam (clientLocation.X, clientLocation.Y));
240                         textbox.FocusAt (screenLocation);
241                 }       
242
243                 private void textbox_DoubleClick(object sender, EventArgs e) {
244                         EventHandler eh = (EventHandler)(Events [ToggleValueEvent]);
245                         if (eh != null)
246                                 eh (this, e);
247                 }
248         }
249 }