New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridTextBoxColumn.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) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Jordi Mas i Hernandez <jordi@ximian.com>
24 //
25 //
26
27 // NOT COMPLETE
28
29 using System.ComponentModel;
30 using System.Drawing;
31 using System.Runtime.InteropServices;
32 using System.Diagnostics;
33
34 namespace System.Windows.Forms
35 {
36         public class DataGridTextBoxColumn : DataGridColumnStyle
37         {
38                 #region Local Variables
39                 private string format;
40                 private IFormatProvider format_provider = null;
41                 private StringFormat string_format =  new StringFormat ();
42                 private DataGridTextBox textbox;
43                 private static readonly int offset_x = 0;
44                 private static readonly int offset_y = 0;
45                 #endregion      // Local Variables
46
47                 #region Constructors
48                 public DataGridTextBoxColumn () : this (null, String.Empty, false)
49                 {
50                 }
51
52                 public DataGridTextBoxColumn (PropertyDescriptor prop) : this (prop, String.Empty, false)
53                 {
54                 }
55                 
56                 public DataGridTextBoxColumn (PropertyDescriptor prop,  bool isDefault) : this (prop, String.Empty, isDefault)
57                 {
58                 }
59
60                 public DataGridTextBoxColumn (PropertyDescriptor prop,  string format) : this (prop, format, false)
61                 {
62                 }
63                 
64                 public DataGridTextBoxColumn (PropertyDescriptor prop,  string format, bool isDefault) : base (prop)
65                 {
66                         Format = format;
67                         is_default = isDefault;
68
69                         textbox = new DataGridTextBox ();
70                         textbox.Multiline = true;
71                         textbox.WordWrap = false;
72                         textbox.BorderStyle = BorderStyle.None;
73                         textbox.Visible = false;
74                 }
75
76                 #endregion
77
78                 #region Public Instance Properties
79                 [Editor("System.Windows.Forms.Design.DataGridColumnStyleFormatEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
80                 public string Format {
81                         get { return format; }
82                         set {
83                                 if (value != format) {
84                                         format = value;
85                                         Invalidate ();
86                                 }
87                         }
88                 }
89
90                 [Browsable(false)]
91                 [EditorBrowsable(EditorBrowsableState.Advanced)]
92                 public IFormatProvider FormatInfo {
93                         get { return format_provider; }
94                         set {
95                                 if (value != format_provider) {
96                                         format_provider = value;
97                                 }
98                         }
99                 }
100
101                 [DefaultValue(null)]
102                 public override PropertyDescriptor PropertyDescriptor {
103                         set { base.PropertyDescriptor = value; }
104                 }
105
106                 public override bool ReadOnly {
107                         get { return base.ReadOnly; }
108                         set { base.ReadOnly = value; }
109                 }
110                 
111                 [Browsable(false)]
112                 public virtual TextBox TextBox {
113                         get { return textbox; }
114                 }
115                 #endregion      // Public Instance Properties
116
117                 #region Public Instance Methods
118
119                 protected internal override void Abort (int rowNum)
120                 {
121                         EndEdit ();                     
122                 }
123                 
124                 protected internal override bool Commit (CurrencyManager dataSource, int rowNum)
125                 {
126                         /* Do not write data if not editing. */
127                         if (textbox.IsInEditOrNavigateMode)
128                                 return true;
129
130                         try {
131                                 object obj = GetColumnValueAtRow (dataSource, rowNum);
132                                 string existing_text = GetFormattedString (obj);
133
134                                 if (existing_text != textbox.Text) {
135                                         if (textbox.Text == NullText)
136                                                 SetColumnValueAtRow (dataSource, rowNum, DBNull.Value);
137                                         else
138                                                 SetColumnValueAtRow (dataSource, rowNum, textbox.Text);
139                                 }
140                         }
141                         catch (Exception e) {
142                                 Console.WriteLine ("exception!?!?!?! {0}", e);
143                                 return false;
144                         }
145                         
146                         EndEdit ();                     
147                         return true;
148                 }
149
150                 [MonoTODO]
151                 protected internal override void ConcedeFocus ()
152                 {
153                         HideEditBox ();
154                 }
155
156                 protected internal override void Edit (CurrencyManager source, int rowNum,  Rectangle bounds,  bool _ro, string instantText, bool cellIsVisible)
157                 {
158                         grid.SuspendLayout ();
159
160                         textbox.TextAlign = alignment;
161                         
162                         if ((ParentReadOnly == true)  || 
163                                 (ParentReadOnly == false && ReadOnly == true) || 
164                                 (ParentReadOnly == false && _ro == true)) {
165                                 textbox.ReadOnly = true;
166                         } else {
167                                 textbox.ReadOnly = false;
168                         }                       
169                         
170                         if (instantText != null && instantText != "") {
171                                 textbox.Text = instantText;
172                         }
173                         else {
174                                 object obj = GetColumnValueAtRow (source, rowNum);
175                                 if (obj == null)
176                                         textbox.Text = "";
177                                 else
178                                         textbox.Text = GetFormattedString (obj);
179                         }
180
181                         textbox.Location = new Point (bounds.X + offset_x, bounds.Y + offset_y);
182                         textbox.Size = new Size (bounds.Width - offset_x, bounds.Height - offset_y);
183
184                         textbox.IsInEditOrNavigateMode = true;
185                         textbox.Visible = cellIsVisible;
186                         textbox.Focus ();
187                         textbox.SelectAll ();
188                         grid.ResumeLayout (false);
189                 }
190
191                 protected void EndEdit ()
192                 {
193                         HideEditBox ();
194                 }
195
196                 protected internal override void EnterNullValue ()
197                 {
198                         textbox.Text = NullText;
199                 }
200
201                 protected internal override int GetMinimumHeight ()
202                 {
203                         return FontHeight + 3;
204                 }
205
206                 [MonoTODO]
207                 protected internal override int GetPreferredHeight (Graphics g, object value)
208                 {
209                         throw new NotImplementedException ();
210                 }
211
212                 [MonoTODO]
213                 protected internal override Size GetPreferredSize (Graphics g, object value)
214                 {
215                         throw new NotImplementedException ();
216                 }
217
218                 [MonoTODO]
219                 protected void HideEditBox ()
220                 {
221                         if (textbox.Visible) {
222                                 grid.SuspendLayout ();
223                                 textbox.Bounds = Rectangle.Empty;
224                                 textbox.Visible = false;
225                                 textbox.IsInEditOrNavigateMode = true;
226                                 grid.ResumeLayout (false);
227                         }
228                 }
229
230                 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
231                 {
232                         Paint (g, bounds, source, rowNum, false);
233                 }
234
235                 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
236                 {
237                         Paint (g, bounds, source, rowNum, ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.BackColor),
238                                 ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.ForeColor), alignToRight);
239                 }
240
241                 protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
242                 {
243                         object obj;
244                         obj = GetColumnValueAtRow (source, rowNum);
245
246                         PaintText (g, bounds, GetFormattedString (obj),  backBrush, foreBrush, alignToRight);
247                 }
248
249                 protected void PaintText (Graphics g, Rectangle bounds, string text, bool alignToRight)
250                 {
251                         PaintText (g, bounds, text,  ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.BackColor),
252                                 ThemeEngine.Current.ResPool.GetSolidBrush (DataGridTableStyle.ForeColor), alignToRight);
253                 }
254
255                 protected void PaintText (Graphics g, Rectangle textBounds, string text, Brush backBrush, Brush foreBrush, bool alignToRight)
256                 {
257                         if (alignToRight == true) {
258                                 string_format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
259                         } else {
260                                 string_format.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;
261                         }
262                         
263                         switch (alignment) {
264                         case HorizontalAlignment.Center:
265                                 string_format.Alignment = StringAlignment.Center;
266                                 break;
267                         case HorizontalAlignment.Right:
268                                 string_format.Alignment = StringAlignment.Far;
269                                 break;                  
270                         default:
271                                 string_format.Alignment = StringAlignment.Near;
272                                 break;
273                         }                       
274                                         
275                         g.FillRectangle (backBrush, textBounds);
276                         PaintGridLine (g, textBounds);
277                         
278                         textBounds.Y += offset_y;
279                         textBounds.Height -= offset_y;
280                         
281                         string_format.FormatFlags |= StringFormatFlags.NoWrap;
282                         g.DrawString (text, DataGridTableStyle.DataGrid.Font, foreBrush, textBounds, string_format);
283                         
284                 }
285                 
286                 protected internal override void ReleaseHostedControl ()
287                 {                       
288                         if (textbox != null) {
289                                 grid.SuspendLayout ();
290                                 grid.Controls.Remove (textbox);
291                                 grid.Invalidate (new Rectangle (textbox.Location, textbox.Size));
292                                 textbox.Dispose ();
293                                 textbox = null;
294                                 grid.ResumeLayout (false);
295                         }
296                 }
297
298                 protected override void SetDataGridInColumn (DataGrid value)
299                 {
300                         base.SetDataGridInColumn (value);
301
302                         if (value != null) {
303                                 textbox.SetDataGrid (grid);                     
304                                 grid.SuspendLayout ();
305                                 grid.Controls.Add (textbox);
306                                 grid.ResumeLayout (false);
307                         }
308                 }
309                 
310                 protected internal override void UpdateUI (CurrencyManager source, int rowNum, string instantText)
311                 {
312
313                 }
314
315                 #endregion      // Public Instance Methods
316
317
318                 #region Private Instance Methods
319
320                 private string GetFormattedString (object obj)
321                 {
322                         if (obj == null)
323                                 return "";
324
325                         if (obj == DBNull.Value)
326                                 return NullText;
327                         
328                         if (format != null && obj as IFormattable != null) {
329                                 return ((IFormattable)obj).ToString (format, format_provider);
330                         }
331
332                         return obj.ToString ();
333
334                 }
335                 #endregion Private Instance Methods
336         }
337 }