2007-06-06 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridTextBoxColumn.cs
index 0ede05c1625d67779feff296f69caa0c2e3a847a..b1c853ab1b39ac76e14cdff149ab8160808093b1 100644 (file)
 //
 //
 
-// NOT COMPLETE
-
 using System.ComponentModel;
 using System.Drawing;
+using System.Globalization;
 using System.Runtime.InteropServices;
 using System.Diagnostics;
 
@@ -39,50 +38,53 @@ namespace System.Windows.Forms
                private string format;
                private IFormatProvider format_provider = null;
                private StringFormat string_format =  new StringFormat ();
-               private DataGridTextBox textbox = null;
+               private DataGridTextBox textbox;
                private static readonly int offset_x = 2;
                private static readonly int offset_y = 2;
                #endregion      // Local Variables
 
                #region Constructors
-               public DataGridTextBoxColumn ()
+               public DataGridTextBoxColumn () : this (null, String.Empty, false)
                {
-                       format = string.Empty;
                }
 
-               public DataGridTextBoxColumn (PropertyDescriptor prop) : base (prop)
+               public DataGridTextBoxColumn (PropertyDescriptor prop) : this (prop, String.Empty, false)
                {
-                       format = string.Empty;
                }
                
-               public DataGridTextBoxColumn (PropertyDescriptor prop,  bool isDefault) : base (prop)
+               public DataGridTextBoxColumn (PropertyDescriptor prop,  bool isDefault) : this (prop, String.Empty, isDefault)
                {
-                       format = string.Empty;
-                       is_default = isDefault;
                }
 
-               public DataGridTextBoxColumn (PropertyDescriptor prop,  string format) : base (prop)
+               public DataGridTextBoxColumn (PropertyDescriptor prop,  string format) : this (prop, format, false)
                {
-                       this.format = format;
                }
                
                public DataGridTextBoxColumn (PropertyDescriptor prop,  string format, bool isDefault) : base (prop)
                {
-                       this.format = format;
+                       Format = format;
                        is_default = isDefault;
+
+                       textbox = new DataGridTextBox ();
+                       textbox.Multiline = true;
+                       textbox.WordWrap = false;
+                       textbox.BorderStyle = BorderStyle.None;
+                       textbox.Visible = false;
                }
 
                #endregion
 
                #region Public Instance Properties
                [Editor("System.Windows.Forms.Design.DataGridColumnStyleFormatEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
+#if NET_2_0
+               [DefaultValue (null)]
+#endif
                public string Format {
-                       get {
-                               return format;
-                       }
+                       get { return format; }
                        set {
                                if (value != format) {
                                        format = value;
+                                       Invalidate ();
                                }
                        }
                }
@@ -90,9 +92,7 @@ namespace System.Windows.Forms
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Advanced)]
                public IFormatProvider FormatInfo {
-                       get {
-                               return format_provider;
-                       }
+                       get { return format_provider; }
                        set {
                                if (value != format_provider) {
                                        format_provider = value;
@@ -102,114 +102,116 @@ namespace System.Windows.Forms
 
                [DefaultValue(null)]
                public override PropertyDescriptor PropertyDescriptor {
-                       set {
-                               base.PropertyDescriptor = value;
-                       }
+                       set { base.PropertyDescriptor = value; }
                }
 
                public override bool ReadOnly {
-                       get {
-                               return base.ReadOnly;
-                       }
-                       set {
-                               base.ReadOnly = value;
-                       }
+                       get { return base.ReadOnly; }
+                       set { base.ReadOnly = value; }
                }
                
                [Browsable(false)]
                public virtual TextBox TextBox {
-                       get {
-                               return textbox;
-                       }
+                       get { return textbox; }
                }
                #endregion      // Public Instance Properties
 
                #region Public Instance Methods
 
-
                protected internal override void Abort (int rowNum)
                {
-                       EndEdit ();                     
+                       EndEdit ();
                }
                
                protected internal override bool Commit (CurrencyManager dataSource, int rowNum)
                {
-                       string text;
-                       
-                       if (textbox.Text == NullText) {
-                               text = string.Empty;
-                       } else {
-                               text = textbox.Text;
-                       }
-                       
+                       textbox.Bounds = Rectangle.Empty;
+
+                       /* Do not write data if not editing. */
+                       if (textbox.IsInEditOrNavigateMode)
+                               return true;
+
                        try {
-                               SetColumnValueAtRow (dataSource, rowNum, text);
-                       }
-                       
-                       catch (Exception e) {
-                               string message = "The data entered in column ["+ MappingName +"] has an invalid format.";
-                               MessageBox.Show( message);
+                               string existing_text = GetFormattedValue (dataSource, rowNum);
+
+                               if (existing_text != textbox.Text) {
+                                       if (textbox.Text == NullText) {
+                                               SetColumnValueAtRow (dataSource, rowNum, DBNull.Value);
+                                       } else {
+                                               object newValue = textbox.Text;
+
+                                               TypeConverter converter = TypeDescriptor.GetConverter (
+                                                       PropertyDescriptor.PropertyType);
+                                               if (converter != null && converter.CanConvertFrom (typeof (string))) {
+                                                       newValue = converter.ConvertFrom (null, CultureInfo.CurrentCulture,
+                                                               textbox.Text);
+                                                       if (converter.CanConvertTo (typeof (string)))
+                                                               textbox.Text = (string) converter.ConvertTo (null, 
+                                                                       CultureInfo.CurrentCulture, newValue, typeof (string));
+                                               }
+
+                                               SetColumnValueAtRow (dataSource, rowNum, newValue);
+                                       }
+                               }
+                       } catch {
+                               return false;
                        }
                        
-                       
-                       EndEdit ();                     
+                       EndEdit ();
                        return true;
                }
 
                [MonoTODO]
                protected internal override void ConcedeFocus ()
                {
-
+                       HideEditBox ();
                }
 
                protected internal override void Edit (CurrencyManager source, int rowNum,  Rectangle bounds,  bool _ro, string instantText, bool cellIsVisible)
                {
-                       object obj;
-                       bool newctrl = false;
-                       
-                       if (textbox == null) {
-                               textbox = new DataGridTextBox ();
-                               textbox.SetDataGrid (DataGridTableStyle.DataGrid);
-                               newctrl = true;                         
-                               textbox.Multiline = true;
-                               textbox.BorderStyle = BorderStyle.None;
-                       }                       
-                       
+                       grid.SuspendLayout ();
+
+                       textbox.TextChanged -= new EventHandler (textbox_TextChanged);
+
                        textbox.TextAlign = alignment;
-                       textbox.Visible = cellIsVisible;
                        
-                       if ((ParentReadOnly == true)  || 
-                               (ParentReadOnly == false && ReadOnly == true) || 
-                               (ParentReadOnly == false && _ro == true)) {
-                               textbox.ReadOnly = true;
-                       } else {
-                               textbox.ReadOnly = false;
-                       }                       
-                       
-                       textbox.Location = new Point (bounds.X + offset_x, bounds.Y + offset_y);
-                       textbox.Size = new Size (bounds.Width - offset_x, bounds.Height - offset_y);
+                       bool ro = false;
 
-                       obj = GetColumnValueAtRow (source, rowNum);
-                       textbox.Text = GetFormattedString (obj);
+                       ro = (TableStyleReadOnly || ReadOnly || _ro);
+
+                       if (!ro && instantText != null) {
+                               textbox.Text = instantText;
+                               textbox.IsInEditOrNavigateMode = false;
+                       } else {
+                               textbox.Text = GetFormattedValue (source, rowNum);
+                       }
 
-                       if (newctrl == true)
-                               DataGridTableStyle.DataGrid.Controls.Add (textbox);
+                       textbox.TextChanged += new EventHandler (textbox_TextChanged);
 
+                       textbox.ReadOnly = ro;
+                       textbox.Bounds = new Rectangle (new Point (bounds.X + offset_x, bounds.Y + offset_y),
+                                                       new Size (bounds.Width - offset_x, bounds.Height - offset_y));
+                       textbox.Visible = cellIsVisible;
+                       textbox.SelectAll ();
                        textbox.Focus ();
-                       textbox.SelectAll ();                   
+                       grid.ResumeLayout (false);
+
+               }
+
+               void textbox_TextChanged (object o, EventArgs e)
+               {
+                       textbox.IsInEditOrNavigateMode = false;
                }
 
                protected void EndEdit ()
                {
-                       ReleaseHostedControl ();
-                       grid.Invalidate (grid.GetCurrentCellBounds ());
+                       textbox.TextChanged -= new EventHandler (textbox_TextChanged);
+                       HideEditBox ();
                }
 
                protected internal override void EnterNullValue ()
                {
-                       if (textbox != null) {
-                               textbox.Text = NullText;
-                       }
+                       textbox.Text = NullText;
                }
 
                protected internal override int GetMinimumHeight ()
@@ -217,22 +219,33 @@ namespace System.Windows.Forms
                        return FontHeight + 3;
                }
 
-               [MonoTODO]
                protected internal override int GetPreferredHeight (Graphics g, object value)
                {
-                       throw new NotImplementedException ();
+                       string text = GetFormattedValue (value);
+                       System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("/\r\n/");
+                       int lines = r.Matches (text).Count;
+                       return this.DataGridTableStyle.DataGrid.Font.Height * (lines+1) + 1;
                }
 
-               [MonoTODO]
                protected internal override Size GetPreferredSize (Graphics g, object value)
                {
-                       throw new NotImplementedException ();
+                       string text = GetFormattedValue (value);
+                       Size s = Size.Ceiling (g.MeasureString (text, this.DataGridTableStyle.DataGrid.Font));
+                       s.Width += 4;
+                       return s;
                }
 
                [MonoTODO]
                protected void HideEditBox ()
                {
-
+                       if (!textbox.Visible)
+                               return;
+
+                       grid.SuspendLayout ();
+                       textbox.Bounds = Rectangle.Empty;
+                       textbox.Visible = false;
+                       textbox.IsInEditOrNavigateMode = true;
+                       grid.ResumeLayout (false);
                }
 
                protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
@@ -248,10 +261,7 @@ namespace System.Windows.Forms
 
                protected internal override void Paint (Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
                {
-                       object obj;
-                       obj = GetColumnValueAtRow (source, rowNum);
-
-                       PaintText (g, bounds, GetFormattedString (obj),  backBrush, foreBrush, alignToRight);
+                       PaintText (g, bounds, GetFormattedValue (source, rowNum),  backBrush, foreBrush, alignToRight);
                }
 
                protected void PaintText (Graphics g, Rectangle bounds, string text, bool alignToRight)
@@ -274,62 +284,85 @@ namespace System.Windows.Forms
                                break;
                        case HorizontalAlignment.Right:
                                string_format.Alignment = StringAlignment.Far;
-                               break;                  
+                               break;
                        default:
                                string_format.Alignment = StringAlignment.Near;
                                break;
-                       }                       
-                                       
+                       }
+                       
                        g.FillRectangle (backBrush, textBounds);
                        PaintGridLine (g, textBounds);
-                       
+
+                       textBounds.X += offset_x;
+                       textBounds.Width -= offset_x;
+
                        textBounds.Y += offset_y;
                        textBounds.Height -= offset_y;
-                       
+
                        string_format.FormatFlags |= StringFormatFlags.NoWrap;
                        g.DrawString (text, DataGridTableStyle.DataGrid.Font, foreBrush, textBounds, string_format);
                        
                }
                
                protected internal override void ReleaseHostedControl ()
-               {                       
-                       if (textbox != null) {
-                               DataGridTableStyle.DataGrid.Controls.Remove (textbox);
-                               textbox.Dispose ();
-                               textbox = null;
-                       }
+               {
+                       if (textbox == null)
+                               return;
+
+                       grid.SuspendLayout ();
+                       grid.Controls.Remove (textbox);
+                       grid.Invalidate (new Rectangle (textbox.Location, textbox.Size));
+                       textbox.Dispose ();
+                       textbox = null;
+                       grid.ResumeLayout (false);
                }
 
                protected override void SetDataGridInColumn (DataGrid value)
                {
                        base.SetDataGridInColumn (value);
+
+                       if (value == null)
+                               return;
+
+                       textbox.SetDataGrid (grid);
+                       grid.SuspendLayout ();
+                       grid.Controls.Add (textbox);
+                       grid.ResumeLayout (false);
                }
                
                protected internal override void UpdateUI (CurrencyManager source, int rowNum, string instantText)
                {
-
+                       if (textbox.Visible // I don't really like this, but it gets DataGridTextBoxColumnTest.TestUpdateUI passing
+                           && textbox.IsInEditOrNavigateMode) {
+                               textbox.Text = GetFormattedValue (source, rowNum);
+                       } else {
+                               textbox.Text = instantText;
+                       }
                }
 
                #endregion      // Public Instance Methods
 
-
                #region Private Instance Methods
 
-               // We use DataGridTextBox to render everything that DataGridBoolColumn does not
-               internal static bool CanRenderType (Type type)
+               private string GetFormattedValue (CurrencyManager source, int rowNum)
                {
-                       return (type != typeof (Boolean));
+                       object obj = GetColumnValueAtRow (source, rowNum);
+                       return GetFormattedValue (obj);
                }
 
-               private string GetFormattedString (object obj)
+               private string GetFormattedValue (object obj)
                {
-                       if (obj == DBNull.Value) {
+                       if (DBNull.Value.Equals(obj) || obj == null)
                                return NullText;
-                       }
-                       
-                       if (format != null && obj as IFormattable != null) {
-                               return ((IFormattable)obj).ToString (format, format_provider);
-                       }
+
+                       if (format != null && format != String.Empty && obj as IFormattable != null)
+                               return ((IFormattable) obj).ToString (format, format_provider);
+
+                       TypeConverter converter = TypeDescriptor.GetConverter (
+                               PropertyDescriptor.PropertyType);
+                       if (converter != null && converter.CanConvertTo (typeof (string)))
+                               return (string) converter.ConvertTo (null, CultureInfo.CurrentCulture,
+                                       obj, typeof (string));
 
                        return obj.ToString ();