2007-04-02 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridTextBoxColumn.cs
index dac9b40039add1de0d6cae543681114d3d5334d0..ab31cdbb977d2c78b35770b0d19ceae1e035a973 100644 (file)
@@ -24,8 +24,6 @@
 //
 //
 
-// NOT COMPLETE
-
 using System.ComponentModel;
 using System.Drawing;
 using System.Runtime.InteropServices;
@@ -40,8 +38,8 @@ namespace System.Windows.Forms
                private IFormatProvider format_provider = null;
                private StringFormat string_format =  new StringFormat ();
                private DataGridTextBox textbox;
-               private static readonly int offset_x = 0;
-               private static readonly int offset_y = 0;
+               private static readonly int offset_x = 2;
+               private static readonly int offset_y = 2;
                #endregion      // Local Variables
 
                #region Constructors
@@ -77,6 +75,9 @@ namespace System.Windows.Forms
 
                #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; }
                        set {
@@ -128,8 +129,7 @@ namespace System.Windows.Forms
                                return true;
 
                        try {
-                               object obj = GetColumnValueAtRow (dataSource, rowNum);
-                               string existing_text = GetFormattedString (obj);
+                               string existing_text = GetFormattedValue (dataSource, rowNum);
 
                                if (existing_text != textbox.Text) {
                                        if (textbox.Text == NullText)
@@ -138,8 +138,7 @@ namespace System.Windows.Forms
                                                SetColumnValueAtRow (dataSource, rowNum, textbox.Text);
                                }
                        }
-                       catch (Exception e) {
-                               Console.WriteLine ("exception!?!?!?! {0}", e);
+                       catch {
                                return false;
                        }
                        
@@ -159,29 +158,21 @@ namespace System.Windows.Forms
 
                        textbox.TextAlign = alignment;
                        
-                       if ((ParentReadOnly == true)  || 
-                               (ParentReadOnly == false && ReadOnly == true) || 
-                               (ParentReadOnly == false && _ro == true)) {
-                               textbox.ReadOnly = true;
-                       } else {
-                               textbox.ReadOnly = false;
-                       }                       
-                       
-                       if (instantText != null && instantText != "") {
+                       bool ro = false;
+
+                       ro = (TableStyleReadOnly || ReadOnly || _ro);
+
+                       if (!ro && instantText != null) {
                                textbox.Text = instantText;
                        }
                        else {
-                               object obj = GetColumnValueAtRow (source, rowNum);
-                               if (obj == null)
-                                       textbox.Text = "";
-                               else
-                                       textbox.Text = GetFormattedString (obj);
+                               textbox.Text = GetFormattedValue (source, rowNum);
                        }
 
-                       textbox.Location = new Point (bounds.X + offset_x, bounds.Y + offset_y);
-                       textbox.Size = new Size (bounds.Width - offset_x, bounds.Height - offset_y);
-
-                       textbox.IsInEditOrNavigateMode = true;
+                       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 ();
@@ -203,16 +194,20 @@ 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]
@@ -240,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)
@@ -309,7 +301,13 @@ namespace System.Windows.Forms
                
                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
@@ -317,17 +315,19 @@ namespace System.Windows.Forms
 
                #region Private Instance Methods
 
-               private string GetFormattedString (object obj)
+               private string GetFormattedValue (CurrencyManager source, int rowNum)
                {
-                       if (obj == null)
-                               return "";
+                       object obj = GetColumnValueAtRow (source, rowNum);
+                       return GetFormattedValue (obj);
+               }
 
-                       if (obj == DBNull.Value)
+               private string GetFormattedValue (object obj)
+               {
+                       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 ();