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