Merge pull request #268 from pcc/menudeactivate
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewCheckBoxCell.cs
index 4dc477c913a37e204774604b63935c360303b26a..6ef788eff1fed7bba862908fbea86617aec9ff32 100644 (file)
@@ -24,8 +24,6 @@
 //
 
 
-#if NET_2_0
-
 using System;
 using System.ComponentModel;
 using System.Drawing;
@@ -42,24 +40,26 @@ namespace System.Windows.Forms {
                private object indeterminateValue;
                private bool threeState;
                private object trueValue;
-               private Type valueType;
+//             private Type valueType;
                private PushButtonState check_state;
 
                public DataGridViewCheckBoxCell ()
                {
                        check_state = PushButtonState.Normal;
+                       editingCellFormattedValue = false;
                        editingCellValueChanged = false;
                        falseValue = null;
                        flatStyle = FlatStyle.Standard;
                        indeterminateValue = null;
                        threeState = false;
                        trueValue = null;
-                       valueType = null;
+                       ValueType = null;
                }
 
                public DataGridViewCheckBoxCell (bool threeState) : this()
                {
                        this.threeState = threeState;
+                       editingCellFormattedValue = CheckState.Unchecked;
                }
 
                public virtual object EditingCellFormattedValue {
@@ -129,7 +129,7 @@ namespace System.Windows.Forms {
 
                public override Type ValueType {
                        get {
-                               if (valueType == null) {
+                               if (base.ValueType == null) {
                                        if (OwningColumn != null && OwningColumn.ValueType != null) {
                                                return OwningColumn.ValueType;
                                        }
@@ -138,21 +138,22 @@ namespace System.Windows.Forms {
                                        }
                                        return typeof(Boolean);
                                }
-                               return valueType;
+                               return base.ValueType;
                        }
-                       set { valueType = value; }
+                       set { base.ValueType = value; }
                }
 
                public override object Clone ()
                {
                        DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell) base.Clone();
                        cell.editingCellValueChanged = this.editingCellValueChanged;
+                       cell.editingCellFormattedValue = this.editingCellFormattedValue;
                        cell.falseValue = this.falseValue;
                        cell.flatStyle = this.flatStyle;
                        cell.indeterminateValue = this.indeterminateValue;
                        cell.threeState = this.threeState;
                        cell.trueValue = this.trueValue;
-                       cell.valueType = this.valueType;
+                       cell.ValueType = this.ValueType;
                        return cell;
                }
 
@@ -164,10 +165,14 @@ namespace System.Windows.Forms {
                        if ((context & DataGridViewDataErrorContexts.ClipboardContent) != 0) {
                                return Convert.ToString(Value);
                        }
-                       if (ThreeState) {
-                               return (CheckState) Value;
-                       }
-                       return (Boolean) Value;
+
+                       if (editingCellFormattedValue == null)
+                               if (threeState)
+                                       return CheckState.Indeterminate;
+                               else
+                                       return false;
+
+                       return editingCellFormattedValue;
                }
 
                public override object ParseFormattedValue (object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
@@ -187,6 +192,7 @@ namespace System.Windows.Forms {
 
                public virtual void PrepareEditingCellForEdit (bool selectAll)
                {
+                       editingCellFormattedValue = GetCurrentValue ();
                }
 
                public override string ToString ()
@@ -228,17 +234,13 @@ namespace System.Windows.Forms {
 
                protected override object GetFormattedValue (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
                {
-                       if (DataGridView == null)
-                               return null;
-                       
-                       if (value == falseValue)
-                               return false;
-                       if (value == trueValue)
-                               return true;
-                       if (value == DBNull.Value)
-                               return false;
-                               
-                       return Convert.ToBoolean (value);
+                       if (DataGridView == null || value == null)
+                               if (threeState)
+                                       return CheckState.Indeterminate;
+                               else
+                                       return false;
+                                       
+                       return value;
                }
 
                protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
@@ -283,14 +285,35 @@ namespace System.Windows.Forms {
 
                protected override void OnContentClick (DataGridViewCellEventArgs e)
                {
-                       DataGridViewCellStyle current_style = InheritedStyle;
-                       
-                       bool current = (bool)GetFormattedValue (Value, e.RowIndex, ref current_style, null, null, DataGridViewDataErrorContexts.Parsing);
+                       if (ReadOnly)
+                               return;
+
+                       if (!IsInEditMode)
+                               DataGridView.BeginEdit (false);
                        
-                       if (current)
-                               Value = falseValue == null ? false : falseValue;
-                       else
-                               Value = trueValue == null ? true : trueValue;
+                       ToggleCheckState ();
+               }
+
+               private void ToggleCheckState ()
+               {
+                       CheckState cs = GetCurrentValue ();
+
+                       if (threeState) {
+                               if (cs == CheckState.Indeterminate)
+                                       editingCellFormattedValue = CheckState.Unchecked;
+                               else if (cs == CheckState.Checked)
+                                       editingCellFormattedValue = CheckState.Indeterminate;
+                               else
+                                       editingCellFormattedValue = CheckState.Checked;
+                       } else {
+                               if (cs == CheckState.Checked)
+                                       editingCellFormattedValue = false;
+                               else
+                                       editingCellFormattedValue = true;
+                       }
+
+                       editingCellValueChanged = true;
+                       DataGridView.InvalidateCell (this);
                }
 
                protected override void OnContentDoubleClick (DataGridViewCellEventArgs e)
@@ -300,7 +323,7 @@ namespace System.Windows.Forms {
                protected override void OnKeyDown (KeyEventArgs e, int rowIndex)
                {
                        // when activated by the SPACE key, this method updates the cell's user interface
-                       if ((e.KeyData & Keys.Space) == Keys.Space) {
+                       if (!ReadOnly && (e.KeyData & Keys.Space) == Keys.Space) {
                                check_state = PushButtonState.Pressed;
                                DataGridView.InvalidateCell (this);
                        }
@@ -309,15 +332,17 @@ namespace System.Windows.Forms {
                protected override void OnKeyUp (KeyEventArgs e, int rowIndex)
                {
                        // when activated by the SPACE key, this method updates the cell's user interface
-                       if ((e.KeyData & Keys.Space) == Keys.Space) {
+                       if (!ReadOnly && (e.KeyData & Keys.Space) == Keys.Space) {
                                check_state = PushButtonState.Normal;
-                               DataGridView.InvalidateCell (this);
+                               if (!IsInEditMode)
+                                       DataGridView.BeginEdit (false);
+                               ToggleCheckState ();
                        }
                }
 
                protected override void OnLeave (int rowIndex, bool throughMouseClick)
                {
-                       if (check_state != PushButtonState.Normal) {
+                       if (!ReadOnly && check_state != PushButtonState.Normal) {
                                check_state = PushButtonState.Normal;
                                DataGridView.InvalidateCell (this);
                        }
@@ -326,7 +351,7 @@ namespace System.Windows.Forms {
                protected override void OnMouseDown (DataGridViewCellMouseEventArgs e)
                {
                        // if activated by depresing the left mouse button, this method updates the cell's user interface
-                       if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
+                       if (!ReadOnly && (e.Button & MouseButtons.Left) == MouseButtons.Left) {
                                check_state = PushButtonState.Pressed;
                                DataGridView.InvalidateCell (this);
                        }
@@ -335,7 +360,7 @@ namespace System.Windows.Forms {
                protected override void OnMouseLeave (int rowIndex)
                {
                        // if the cell's button is not in its normal state, this method causes the cell's user interface to be updated.
-                       if (check_state != PushButtonState.Normal) {
+                       if (!ReadOnly && check_state != PushButtonState.Normal) {
                                check_state = PushButtonState.Normal;
                                DataGridView.InvalidateCell (this);
                        }
@@ -343,7 +368,7 @@ namespace System.Windows.Forms {
 
                protected override void OnMouseMove (DataGridViewCellMouseEventArgs e)
                {
-                       if (check_state != PushButtonState.Normal && check_state != PushButtonState.Hot) {
+                       if (!ReadOnly && check_state != PushButtonState.Normal && check_state != PushButtonState.Hot) {
                                check_state = PushButtonState.Hot;
                                DataGridView.InvalidateCell (this);
                        }
@@ -352,32 +377,61 @@ namespace System.Windows.Forms {
                protected override void OnMouseUp (DataGridViewCellMouseEventArgs e)
                {
                        // if activated by the left mouse button, this method updates the cell's user interface
-                       if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
+                       if (!ReadOnly && (e.Button & MouseButtons.Left) == MouseButtons.Left) {
                                check_state = PushButtonState.Normal;
                                DataGridView.InvalidateCell (this);
                        }
                }
 
-               protected override void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
+               protected override void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
                {
-                       base.Paint (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
+                       base.Paint (graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                }
 
                internal override void PaintPartContent (Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, object formattedValue)
                {
                        CheckBoxState state;
-                       
-                       if ((bool)formattedValue == false)
+                       CheckState value = GetCurrentValue ();
+
+                       if ((CheckState)value == CheckState.Unchecked)
                                state = (CheckBoxState)check_state;
-                       else if ((bool)formattedValue == true)
+                       else if ((CheckState)value == CheckState.Checked)
                                state = (CheckBoxState)((int)check_state + 4);
-                       else
+                       else if (threeState)
                                state = (CheckBoxState)((int)check_state + 8);
-                       
+                       else
+                               state = (CheckBoxState)check_state;
+                                       
                        Point p = new Point (cellBounds.X + (Size.Width - 13) / 2, cellBounds.Y + (Size.Height - 13) / 2);
                        CheckBoxRenderer.DrawCheckBox (graphics, p, state);
                }
                
+               private CheckState GetCurrentValue ()
+               {
+                       CheckState cs = CheckState.Indeterminate;
+                       
+                       object current_obj;
+                       
+                       if (editingCellValueChanged)
+                               current_obj = editingCellFormattedValue;
+                       else
+                               current_obj = Value;
+
+                       if (current_obj == null)
+                               cs = CheckState.Indeterminate;
+                       else if (current_obj is bool)
+                       {
+                               if ((bool)current_obj == true)
+                                       cs = CheckState.Checked;
+                               else if ((bool)current_obj == false)
+                                       cs = CheckState.Unchecked;
+                       }
+                       else if (current_obj is CheckState)
+                               cs = (CheckState)current_obj;
+
+                       return cs;
+               }
+               
                protected class DataGridViewCheckBoxCellAccessibleObject : DataGridViewCellAccessibleObject {
 
                        public DataGridViewCheckBoxCellAccessibleObject (DataGridViewCell owner) : base(owner)
@@ -411,4 +465,3 @@ namespace System.Windows.Forms {
 
 }
 
-#endif