2007-04-02 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridTextBoxColumn.cs
index c7c52b1c9b17bfcf23a9b7a6aedc6746ee4d130d..ab31cdbb977d2c78b35770b0d19ceae1e035a973 100644 (file)
@@ -24,8 +24,6 @@
 //
 //
 
-// NOT COMPLETE
-
 using System.ComponentModel;
 using System.Drawing;
 using System.Runtime.InteropServices;
@@ -39,50 +37,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 +91,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,32 +101,22 @@ 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 {
-                               EnsureTextBox();
-                               return textbox;
-                       }
+                       get { return textbox; }
                }
                #endregion      // Public Instance Properties
 
                #region Public Instance Methods
 
-
                protected internal override void Abort (int rowNum)
                {
                        EndEdit ();                     
@@ -135,24 +124,24 @@ namespace System.Windows.Forms
                
                protected internal override bool Commit (CurrencyManager dataSource, int rowNum)
                {
-                       string text;
-                       
-                       if (textbox.Text == NullText) {
-                               text = string.Empty;
-                       } else {
-                               text = textbox.Text;
-                       }
-                       
+                       /* Do not write data if not editing. */
+                       if (textbox.IsInEditOrNavigateMode)
+                               return true;
+
                        try {
-                               SetColumnValueAtRow (dataSource, rowNum, text);
+                               string existing_text = GetFormattedValue (dataSource, rowNum);
+
+                               if (existing_text != textbox.Text) {
+                                       if (textbox.Text == NullText)
+                                               SetColumnValueAtRow (dataSource, rowNum, DBNull.Value);
+                                       else
+                                               SetColumnValueAtRow (dataSource, rowNum, textbox.Text);
+                               }
                        }
-                       
-                       catch (Exception) {
-                               string message = "The data entered in column ["+ MappingName +"] has an invalid format.";
-                               MessageBox.Show( message);
+                       catch {
+                               return false;
                        }
                        
-                       
                        EndEdit ();                     
                        return true;
                }
@@ -160,47 +149,44 @@ namespace System.Windows.Forms
                [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;
-                       
-                       EnsureTextBox();
+                       grid.SuspendLayout ();
 
                        textbox.TextAlign = alignment;
                        
-                       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;
+
+                       ro = (TableStyleReadOnly || ReadOnly || _ro);
 
-                       obj = GetColumnValueAtRow (source, rowNum);
-                       textbox.Text = GetFormattedString (obj);
+                       if (!ro && instantText != null) {
+                               textbox.Text = instantText;
+                       }
+                       else {
+                               textbox.Text = GetFormattedValue (source, rowNum);
+                       }
 
+                       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.IsInEditOrNavigateMode = ro || instantText == null;
                        textbox.Visible = cellIsVisible;
                        textbox.Focus ();
-                       textbox.SelectAll ();                   
+                       textbox.SelectAll ();
+                       grid.ResumeLayout (false);
                }
 
                protected void EndEdit ()
                {
-                       ReleaseHostedControl ();
-                       grid.Invalidate (grid.GetCurrentCellBounds ());
+                       HideEditBox ();
                }
 
                protected internal override void EnterNullValue ()
                {
-                       if (textbox != null) {
-                               textbox.Text = NullText;
-                       }
+                       textbox.Text = NullText;
                }
 
                protected internal override int GetMinimumHeight ()
@@ -208,22 +194,32 @@ 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) {
+                               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)
@@ -239,10 +235,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)
@@ -285,20 +278,36 @@ namespace System.Windows.Forms
                protected internal override void ReleaseHostedControl ()
                {                       
                        if (textbox != null) {
-                               DataGridTableStyle.DataGrid.Controls.Remove (textbox);
+                               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) {
+                               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
@@ -306,37 +315,23 @@ namespace System.Windows.Forms
 
                #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) {
+                       if (format != null && format != String.Empty && obj as IFormattable != null)
                                return ((IFormattable)obj).ToString (format, format_provider);
-                       }
 
                        return obj.ToString ();
 
                }
-
-               private void EnsureTextBox() {
-                       if (textbox == null) {
-                               textbox = new DataGridTextBox ();
-                               textbox.SetDataGrid (DataGridTableStyle.DataGrid);
-                               textbox.Multiline = true;
-                               textbox.BorderStyle = BorderStyle.None;
-                               textbox.Visible = false;
-
-                               DataGridTableStyle.DataGrid.Controls.Add (textbox);
-                       }                       
-               }
                #endregion Private Instance Methods
        }
 }