Merge pull request #268 from pcc/menudeactivate
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewCheckBoxCell.cs
index 3ee310891acb4f3e07ba61295ffc5ec21338361d..6ef788eff1fed7bba862908fbea86617aec9ff32 100644 (file)
 //
 
 
-#if NET_2_0
-
 using System;
 using System.ComponentModel;
 using System.Drawing;
+using System.Windows.Forms.VisualStyles;
 
 namespace System.Windows.Forms {
 
@@ -41,19 +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 () {
+               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() {
+               public DataGridViewCheckBoxCell (bool threeState) : this()
+               {
+                       this.threeState = threeState;
+                       editingCellFormattedValue = CheckState.Unchecked;
                }
 
                public virtual object EditingCellFormattedValue {
@@ -75,11 +81,13 @@ namespace System.Windows.Forms {
                        get { return null; }
                }
 
+               [DefaultValue (null)]
                public object FalseValue {
                        get { return falseValue; }
                        set { falseValue = value; }
                }
 
+               [DefaultValue (FlatStyle.Standard)]
                public FlatStyle FlatStyle {
                        get { return flatStyle; }
                        set {
@@ -101,16 +109,19 @@ namespace System.Windows.Forms {
                        }
                }
 
+               [DefaultValue (null)]
                public object IndeterminateValue {
                        get { return indeterminateValue; }
                        set { indeterminateValue = value; }
                }
 
+               [DefaultValue (false)]
                public bool ThreeState {
                        get { return threeState; }
                        set { threeState = value; }
                }
 
+               [DefaultValue (null)]
                public object TrueValue {
                        get { return trueValue; }
                        set { trueValue = value; }
@@ -118,8 +129,8 @@ namespace System.Windows.Forms {
 
                public override Type ValueType {
                        get {
-                               if (valueType == null) {
-                                       if (OwningColumn != null) {
+                               if (base.ValueType == null) {
+                                       if (OwningColumn != null && OwningColumn.ValueType != null) {
                                                return OwningColumn.ValueType;
                                        }
                                        if (ThreeState) {
@@ -127,37 +138,45 @@ namespace System.Windows.Forms {
                                        }
                                        return typeof(Boolean);
                                }
-                               return valueType;
+                               return base.ValueType;
                        }
-                       set { valueType = value; }
+                       set { base.ValueType = value; }
                }
 
-               public override object Clone () {
+               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;
                }
 
-               public virtual object GetEditingCellFormattedValue (DataGridViewDataErrorContexts context) {
+               public virtual object GetEditingCellFormattedValue (DataGridViewDataErrorContexts context)
+               {
                        if (FormattedValueType == null) {
                                throw new InvalidOperationException("FormattedValueType is null.");
                        }
                        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) {
+               public override object ParseFormattedValue (object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
+               {
                        if (cellStyle == null) {
                                throw new ArgumentNullException("CellStyle is null");
                        }
@@ -167,131 +186,256 @@ namespace System.Windows.Forms {
                        if (formattedValue == null || formattedValue.GetType() != FormattedValueType) {
                                throw new ArgumentException("FormattedValue is null or is not instance of FormattedValueType.");
                        }
-                       throw new NotImplementedException();
+                       
+                       return base.ParseFormattedValue (formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
                }
 
-               public virtual void PrepareEditingCellForEdit (bool selectAll) {
+               public virtual void PrepareEditingCellForEdit (bool selectAll)
+               {
+                       editingCellFormattedValue = GetCurrentValue ();
                }
 
-               public override string ToString () {
-                       return GetType().Name + ": RowIndex: " + RowIndex.ToString() + "; ColumnIndex: " + ColumnIndex.ToString() + ";";
+               public override string ToString ()
+               {
+                       return string.Format ("DataGridViewCheckBoxCell {{ ColumnIndex={0}, RowIndex={1} }}", ColumnIndex, RowIndex);
                }
 
-               protected override bool ContentClickUnsharesRow (DataGridViewCellEventArgs e) {
+               protected override bool ContentClickUnsharesRow (DataGridViewCellEventArgs e)
+               {
                        return this.IsInEditMode;
                }
 
-               //protected override bool ContentDoubleClickUnsaresRow (DataGridViewCellEventArgs e) {
-               protected bool ContentDoubleClickUnsaresRow (DataGridViewCellEventArgs e) {
+               protected override bool ContentDoubleClickUnsharesRow (DataGridViewCellEventArgs e)
+               {
                        return this.IsInEditMode;
                }
 
-               protected override AccessibleObject CreateAccessibilityInstance () {
+               protected override AccessibleObject CreateAccessibilityInstance ()
+               {
                        return new DataGridViewCheckBoxCellAccessibleObject(this);
                }
 
-               protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex) {
-                       throw new NotImplementedException();
-               }
+               protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
+               {
+                       if (DataGridView == null)
+                               return Rectangle.Empty;
 
-               protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex) {
-                       throw new NotImplementedException();
+                       return new Rectangle ((Size.Width - 13) / 2, (Size.Height - 13) / 2, 13, 13);
                }
 
-               //protected override object GetFormttedValue (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) {
-               protected object GetFormttedValue (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) {
-                       if (DataGridView == null) {
-                               return null;
-                       }
-                       throw new NotImplementedException();
+               protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
+               {
+                       if (DataGridView == null || string.IsNullOrEmpty (ErrorText))
+                               return Rectangle.Empty;
+
+                       Size error_icon = new Size (12, 11);
+                       return new Rectangle (new Point (Size.Width - error_icon.Width - 5, (Size.Height - error_icon.Height) / 2), error_icon);
                }
 
-               protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize) {
-                       throw new NotImplementedException();
+               protected override object GetFormattedValue (object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
+               {
+                       if (DataGridView == null || value == null)
+                               if (threeState)
+                                       return CheckState.Indeterminate;
+                               else
+                                       return false;
+                                       
+                       return value;
                }
 
-               protected override object GetValue (int rowIndex) {
-                       throw new NotImplementedException();
+               protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
+               {
+                       return new Size (21, 20);
                }
 
-               protected override bool KeyDownUnsharesRow (KeyEventArgs e, int rowIndex) {
+               protected override bool KeyDownUnsharesRow (KeyEventArgs e, int rowIndex)
+               {
                        // true if the user pressed the SPACE key without modifier keys; otherwise, false
-                       throw new NotImplementedException();
+                       return e.KeyData == Keys.Space;
                }
 
-               protected override bool KeyUpUnsharesRow (KeyEventArgs e, int rowIndex) {
+               protected override bool KeyUpUnsharesRow (KeyEventArgs e, int rowIndex)
+               {
                        // true if the user released the SPACE key; otherwise false
-                       throw new NotImplementedException();
-
+                       return e.KeyData == Keys.Space;
                }
 
-               protected override bool MouseDownUnsharesRow (DataGridViewCellMouseEventArgs e) {
+               protected override bool MouseDownUnsharesRow (DataGridViewCellMouseEventArgs e)
+               {
                        return (e.Button == MouseButtons.Left);
                }
 
-               protected override bool MouseEnterUnsharesRow (int rowIndex) {
+               protected override bool MouseEnterUnsharesRow (int rowIndex)
+               {
                        // true if the cell was the last cell receiving a mouse click; otherwise, false.
-                       throw new NotImplementedException();
+                       return false;
                }
 
-               protected override bool MouseLeaveUnsharesRow (int rowIndex) {
+               protected override bool MouseLeaveUnsharesRow (int rowIndex)
+               {
                        // true if the button displayed by the cell is in the pressed state; otherwise, false.
-                       throw new NotImplementedException();
+                       return check_state == PushButtonState.Pressed;
                }
 
-               protected override bool MouseUpUnsharesRow (DataGridViewCellMouseEventArgs e) {
+               protected override bool MouseUpUnsharesRow (DataGridViewCellMouseEventArgs e)
+               {
                        // true if the mouse up was caused by the release of the left mouse button; otherwise false.
-                       throw new NotImplementedException();
-               }
+                       return e.Button == MouseButtons.Left;
+               }
+
+               protected override void OnContentClick (DataGridViewCellEventArgs e)
+               {
+                       if (ReadOnly)
+                               return;
+
+                       if (!IsInEditMode)
+                               DataGridView.BeginEdit (false);
+                       
+                       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;
+                       }
 
-               protected override void OnContentClick (DataGridViewCellEventArgs e) {
-                       throw new NotImplementedException();
+                       editingCellValueChanged = true;
+                       DataGridView.InvalidateCell (this);
                }
 
-               protected override void OnContentDoubleClick (DataGridViewCellEventArgs e) {
-                       throw new NotImplementedException();
+               protected override void OnContentDoubleClick (DataGridViewCellEventArgs e)
+               {
                }
 
-               protected override void OnKeyDown (KeyEventArgs e, int rowIndex) {
+               protected override void OnKeyDown (KeyEventArgs e, int rowIndex)
+               {
                        // when activated by the SPACE key, this method updates the cell's user interface
-                       throw new NotImplementedException();
+                       if (!ReadOnly && (e.KeyData & Keys.Space) == Keys.Space) {
+                               check_state = PushButtonState.Pressed;
+                               DataGridView.InvalidateCell (this);
+                       }
                }
 
-               protected override void OnKeyUp (KeyEventArgs e, int rowIndex) {
+               protected override void OnKeyUp (KeyEventArgs e, int rowIndex)
+               {
                        // when activated by the SPACE key, this method updates the cell's user interface
-                       throw new NotImplementedException();
+                       if (!ReadOnly && (e.KeyData & Keys.Space) == Keys.Space) {
+                               check_state = PushButtonState.Normal;
+                               if (!IsInEditMode)
+                                       DataGridView.BeginEdit (false);
+                               ToggleCheckState ();
+                       }
                }
 
-               protected override void OnLeave (int rowIndex, bool throughMouseClick) {
-                       throw new NotImplementedException();
+               protected override void OnLeave (int rowIndex, bool throughMouseClick)
+               {
+                       if (!ReadOnly && check_state != PushButtonState.Normal) {
+                               check_state = PushButtonState.Normal;
+                               DataGridView.InvalidateCell (this);
+                       }
                }
 
-               protected override void OnMouseDown (DataGridViewCellMouseEventArgs e) {
+               protected override void OnMouseDown (DataGridViewCellMouseEventArgs e)
+               {
                        // if activated by depresing the left mouse button, this method updates the cell's user interface
-                       throw new NotImplementedException();
+                       if (!ReadOnly && (e.Button & MouseButtons.Left) == MouseButtons.Left) {
+                               check_state = PushButtonState.Pressed;
+                               DataGridView.InvalidateCell (this);
+                       }
                }
 
-               protected override void OnMouseEnter (int rowIndex) {
-                       throw new NotImplementedException();
+               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 (!ReadOnly && check_state != PushButtonState.Normal) {
+                               check_state = PushButtonState.Normal;
+                               DataGridView.InvalidateCell (this);
+                       }
                }
 
-               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.
-                       throw new NotImplementedException();
+               protected override void OnMouseMove (DataGridViewCellMouseEventArgs e)
+               {
+                       if (!ReadOnly && check_state != PushButtonState.Normal && check_state != PushButtonState.Hot) {
+                               check_state = PushButtonState.Hot;
+                               DataGridView.InvalidateCell (this);
+                       }
                }
 
-               protected override void OnMouseUp (DataGridViewCellMouseEventArgs e) {
+               protected override void OnMouseUp (DataGridViewCellMouseEventArgs e)
+               {
                        // if activated by the left mouse button, this method updates the cell's user interface
-                       throw new NotImplementedException();
+                       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) {
-                       throw new NotImplementedException();
-               }
+               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, 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;
+                       CheckState value = GetCurrentValue ();
+
+                       if ((CheckState)value == CheckState.Unchecked)
+                               state = (CheckBoxState)check_state;
+                       else if ((CheckState)value == CheckState.Checked)
+                               state = (CheckBoxState)((int)check_state + 4);
+                       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) {
+                       public DataGridViewCheckBoxCellAccessibleObject (DataGridViewCell owner) : base(owner)
+                       {
                        }
 
                        public override string DefaultAction {
@@ -305,11 +449,13 @@ namespace System.Windows.Forms {
                                }
                        }
 
-                       public override void DoDefaultAction () {
+                       public override void DoDefaultAction ()
+                       {
                                // change the state of the check box
                        }
 
-                       public override int GetChildCount () {
+                       public override int GetChildCount ()
+                       {
                                return -1;
                        }
 
@@ -319,4 +465,3 @@ namespace System.Windows.Forms {
 
 }
 
-#endif