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