2009-01-11 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridView.cs
index 09e7f96572d0a542ab81e7354bf72a63151bb484..aa1390c5901244600da835d25518de411d99df98 100644 (file)
@@ -118,9 +118,9 @@ namespace System.Windows.Forms {
                private HScrollBar horizontalScrollBar;
                private VScrollBar verticalScrollBar;
                private Control editingControl;
-               private bool new_row_commited = true;
                private bool is_autogenerating_columns = false;
-               private bool in_binding_context_changed = false;
+               private bool is_binding = false;
+               private bool new_row_editing = false;
                
                // These are used to implement selection behaviour with SHIFT pressed.
                private int selected_row = -1;
@@ -265,7 +265,11 @@ namespace System.Windows.Forms {
 
                [DefaultValue (true)]
                public bool AllowUserToAddRows {
-                       get { return allowUserToAddRows; }
+                       get { 
+                               if (allowUserToAddRows && DataManager != null)
+                                       return DataManager.AllowNew;
+                               return allowUserToAddRows;
+                       }
                        set {
                                if (allowUserToAddRows != value) {
                                        allowUserToAddRows = value;
@@ -278,7 +282,11 @@ namespace System.Windows.Forms {
 
                [DefaultValue (true)]
                public bool AllowUserToDeleteRows {
-                       get { return allowUserToDeleteRows; }
+                       get {
+                               if (allowUserToDeleteRows && DataManager != null)
+                                       return DataManager.AllowRemove;
+                               return allowUserToDeleteRows;
+                       }
                        set {
                                if (allowUserToDeleteRows != value) {
                                        allowUserToDeleteRows = value;
@@ -701,12 +709,9 @@ namespace System.Windows.Forms {
                        get { return dataMember; }
                        set {
                                if (dataMember != value) {
-                                       ClearBinding ();
-                                       
                                        dataMember = value;
+                                       ReBind ();
                                        OnDataMemberChanged(EventArgs.Empty);
-                                       
-                                       DoBinding ();
                                }
                        }
                }
@@ -726,12 +731,9 @@ namespace System.Windows.Forms {
                                if (!(value == null || value is IList || value is IListSource || value is IBindingList || value is IBindingListView))
                                        throw new NotSupportedException ("Type cannot be bound.");
                                        
-                               ClearBinding ();
-                               
                                dataSource = value;
+                               ReBind ();
                                OnDataSourceChanged (EventArgs.Empty);
-                               
-                               DoBinding ();
                        }
                }
 
@@ -954,7 +956,7 @@ namespace System.Windows.Forms {
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public int NewRowIndex {
                        get {
-                               if (!allowUserToAddRows || ColumnCount == 0) {
+                               if (!AllowUserToAddRows || ColumnCount == 0) {
                                        return -1;
                                }
                                return rows.Count - 1;
@@ -995,7 +997,7 @@ namespace System.Windows.Forms {
                                if (value < 0) {
                                        throw new ArgumentException("RowCount must be >= 0.");
                                }
-                               if (value < 1 && allowUserToAddRows) {
+                               if (value < 1 && AllowUserToAddRows) {
                                        throw new ArgumentException("RowCount must be >= 1 if AllowUserToAddRows is true.");
                                }
                                if (dataSource != null) {
@@ -2253,7 +2255,7 @@ namespace System.Windows.Forms {
                                        return false;
                                }
                        }
-                       
+
                        DataGridViewCell cell = currentCell;
                        Type editType = cell.EditType;
                        
@@ -2267,12 +2269,6 @@ namespace System.Windows.Forms {
                        if (e.Cancel)
                                return false;
 
-                       // If the user begins an edit in the NewRow, add a new row
-                       if (CurrentCell.RowIndex == NewRowIndex) {
-                               new_row_commited = false;
-                               OnUserAddedRow (new DataGridViewRowEventArgs (Rows[NewRowIndex]));
-                       }
-               
                        cell.SetIsInEditMode (true);
                        
                        // The cell has an editing control we need to setup
@@ -2320,21 +2316,20 @@ namespace System.Windows.Forms {
 
                public bool CancelEdit ()
                {
-                       if (currentCell != null && currentCell.IsInEditMode) {
-                               // The user's typing caused a new row to be created, but
-                               // now they are canceling that typing, we have to remove
-                               // the new row we added.
-                               if (!new_row_commited) {
-                                       DataGridViewRow delete_row = EditingRow;
-                                       Rows.RemoveInternal (delete_row);
-                                       editing_row = Rows[currentCell.RowIndex];
-                                       OnUserDeletedRow (new DataGridViewRowEventArgs (delete_row));
-                                       new_row_commited = true;
+                       if (currentCell != null) {
+                               if (currentCell.IsInEditMode) {
+                                       currentCell.SetIsInEditMode (false);
+                                       currentCell.DetachEditingControl ();
                                }
 
-                               currentCell.SetIsInEditMode (false);
-                               currentCell.DetachEditingControl ();
-                               OnCellEndEdit (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
+                               if (currentCell.RowIndex == NewRowIndex) {
+                                       if (DataManager != null)
+                                               DataManager.CancelCurrentEdit ();
+
+                                       new_row_editing = false;
+                                       PrepareEditingRow (true, false);
+                                       OnUserDeletedRow (new DataGridViewRowEventArgs (EditingRow));
+                               }
                        }
 
                        return true;
@@ -2393,7 +2388,7 @@ namespace System.Windows.Forms {
 
                        for (int index = first_row_index; index < Rows.Count; index++) {
                                DataGridViewRow row = GetRowInternal (index);
-                               if (rowTop + row.Height < ClientSize.Height) {
+                               if (rowTop + row.Height <= ClientSize.Height) {
                                        result++;
                                        rowTop += row.Height;
                                } else {
@@ -2418,6 +2413,8 @@ namespace System.Windows.Forms {
                                return true;
 
                        if (!CommitEdit (context)) {
+                               if (DataManager != null)
+                                       DataManager.EndCurrentEdit ();
                                if (EditingControl != null)
                                        EditingControl.Focus ();
                                return false;
@@ -2425,9 +2422,12 @@ namespace System.Windows.Forms {
 
                        currentCell.SetIsInEditMode (false);
                        currentCell.DetachEditingControl ();
-                       new_row_commited = true;
                        OnCellEndEdit (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
                        Focus ();
+                       if (currentCell.RowIndex == NewRowIndex) {
+                               new_row_editing = false;
+                               PrepareEditingRow (false, false);
+                       }
                        return true;
                }
 
@@ -3350,11 +3350,7 @@ namespace System.Windows.Forms {
                protected override void OnBindingContextChanged (EventArgs e)
                {
                        base.OnBindingContextChanged(e);
-                       if (!in_binding_context_changed) {
-                               in_binding_context_changed = true;
-                               ReBind();
-                               in_binding_context_changed = true;
-                       }
+                       ReBind();
                }
 
                protected virtual void OnBorderStyleChanged (EventArgs e)
@@ -4860,7 +4856,8 @@ namespace System.Windows.Forms {
                                int nextRowIndex = e.RowIndex;
                                if (nextRowIndex >= Rows.Count)
                                        nextRowIndex = Rows.Count - 1;
-                               MoveCurrentCell (0, nextRowIndex, true, false, false, true);
+                               MoveCurrentCell (currentCell != null ? currentCell.ColumnIndex : 0, nextRowIndex, 
+                                                true, false, false, true);
                        }
 
                        Invalidate ();
@@ -4922,11 +4919,17 @@ namespace System.Windows.Forms {
 
                protected virtual void OnUserAddedRow (DataGridViewRowEventArgs e)
                {
-                       editing_row = null;
+                       // Switch the current editing row with a real
+                       int newRowIndex = NewRowIndex;
+                       int currentColumnIndex = currentCell != null ? currentCell.ColumnIndex : 0;
+                       new_row_editing = true;
                        PrepareEditingRow (false, false);
+                       if (DataManager != null)
+                               DataManager.AddNew (); // will raise OnListPositionChanged
+                       else
+                               MoveCurrentCell (currentColumnIndex, NewRowIndex, true, false, false, true);
 
-                       e = new DataGridViewRowEventArgs (editing_row);
-                       
+                       e = new DataGridViewRowEventArgs (Rows[NewRowIndex]);
                        DataGridViewRowEventHandler eh = (DataGridViewRowEventHandler)(Events [UserAddedRowEvent]);
                        if (eh != null) eh (this, e);
                }
@@ -4935,7 +4938,6 @@ namespace System.Windows.Forms {
                {
                        DataGridViewRowEventHandler eh = (DataGridViewRowEventHandler)(Events [UserDeletedRowEvent]);
                        if (eh != null) eh (this, e);
-
                }
 
                protected virtual void OnUserDeletingRow (DataGridViewRowCancelEventArgs e)
@@ -5408,6 +5410,8 @@ namespace System.Windows.Forms {
                                if (currentCell != null) {
                                        if (currentCell.IsInEditMode && !EndEdit ())
                                                return false;
+                                       else if (currentCell.RowIndex == NewRowIndex && new_row_editing)
+                                               CancelEdit ();
                                        OnCellLeave (new DataGridViewCellEventArgs(currentCell.ColumnIndex, currentCell.RowIndex));
                                        OnRowLeave (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
                                }
@@ -5423,8 +5427,17 @@ namespace System.Windows.Forms {
                                        OnCellEnter (new DataGridViewCellEventArgs(cell.ColumnIndex, cell.RowIndex));
                                }
                                OnCurrentCellChanged (EventArgs.Empty);
-                               if (cell != null && editMode == DataGridViewEditMode.EditOnEnter)
-                                       BeginEdit (true);
+
+                               if (cell != null) {
+                                       // If the user begins an edit in the NewRow, add a new row
+                                       if (AllowUserToAddRows && cell.RowIndex == NewRowIndex && !is_binding && !new_row_editing) {
+                                               // OnUserAddedRow will add a real row and reset the current cell
+                                               OnUserAddedRow (new DataGridViewRowEventArgs (Rows[NewRowIndex]));
+                                       } else {
+                                               if (editMode == DataGridViewEditMode.EditOnEnter)
+                                                       BeginEdit (true);
+                                       }
+                               }
                        } else {
                                if (cell != null && throughMouseClick)
                                        BeginEdit (true);
@@ -5464,7 +5477,8 @@ namespace System.Windows.Forms {
                }
 
                internal void SetSelectedRowCoreInternal (int rowIndex, bool selected) {
-                       SetSelectedRowCore (rowIndex, selected);
+                       if (rowIndex >= 0 && rowIndex < Rows.Count)
+                               SetSelectedRowCore (rowIndex, selected);
                }       
 
                protected virtual void SetSelectedRowCore (int rowIndex, bool selected) {
@@ -5758,6 +5772,14 @@ namespace System.Windows.Forms {
 
                internal void PrepareEditingRow (bool cell_changed, bool column_changed)
                {
+                       if (new_row_editing) {
+                               if (editing_row != null) {
+                                       Rows.RemoveInternal (editing_row);
+                                       editing_row = null;
+                               }
+                               return;
+                       }
+
                        bool show = false;
                        
                        show = ColumnCount > 0 && AllowUserToAddRows;
@@ -5766,15 +5788,12 @@ namespace System.Windows.Forms {
                                Rows.RemoveInternal (editing_row);
                                editing_row = null;
                        } else if (show) {
-                               if (editing_row != null) {
-                                       if (cell_changed) {
-                                               // The row changed, it's no longer an editing row.
-                                               editing_row = null;
-                                       } else if (column_changed) {
-                                               // The number of columns has changed, we need a new editing row.
-                                               Rows.RemoveInternal (editing_row);
-                                               editing_row = null;
-                                       }
+                               if (editing_row != null && (cell_changed || column_changed)) {
+                                       // The row changed, it's no longer an editing row.
+                                       //    or
+                                       // The number of columns has changed, we need a new editing row.
+                                       Rows.RemoveInternal (editing_row);
+                                       editing_row = null;
                                }
                                if (editing_row == null) {
                                        editing_row = RowTemplateFull;
@@ -5836,8 +5855,10 @@ namespace System.Windows.Forms {
                        columns.ClearAutoGeneratedColumns ();
                        rows.Clear ();
                        PrepareEditingRow (false, true);
-                       if (DataManager != null)
+                       if (DataManager != null) {
                                DataManager.ListChanged -= OnListChanged;
+                               DataManager.PositionChanged -= OnListPositionChanged;
+                       }
                }
                
                private void DoBinding ()
@@ -5864,7 +5885,7 @@ namespace System.Windows.Forms {
                                                DataGridViewColumn col = CreateColumnByType (property.PropertyType);
                                                col.Name = property.DisplayName;
                                                col.DataPropertyName = property.DisplayName;
-                                               col.ReadOnly = property.IsReadOnly;
+                                               col.ReadOnly = !DataManager.AllowEdit || property.IsReadOnly;
                                                col.SetIsDataBound (true);
                                                col.ValueType = property.PropertyType;
                                                col.AutoGenerated = true;
@@ -5886,10 +5907,14 @@ namespace System.Windows.Forms {
                                        AddBoundRow (element);
 
                                DataManager.ListChanged += OnListChanged;
+                               DataManager.PositionChanged += OnListPositionChanged;
                                OnDataBindingComplete (new DataGridViewBindingCompleteEventArgs (ListChangedType.Reset));
+                               OnListPositionChanged (this, EventArgs.Empty);
+                       } else {
+                               if (Rows.Count > 0 && Columns.Count > 0)
+                                       MoveCurrentCell (0, 0, true, false, false, false);
                        }
-                       if (Rows.Count > 0 && Columns.Count > 0)
-                               MoveCurrentCell (0, 0, true, false, false, false);
+
                        PerformLayout();
                        Invalidate ();
                }
@@ -6004,7 +6029,7 @@ namespace System.Windows.Forms {
                                        AddBoundRow (DataManager[args.NewIndex]);
                                        break;
                                case ListChangedType.ItemDeleted:
-                                       Rows.RemoveAt (args.NewIndex);
+                                       Rows.RemoveAtInternal (args.NewIndex);
                                        break;
                                case ListChangedType.ItemChanged:
                                        break;
@@ -6016,11 +6041,23 @@ namespace System.Windows.Forms {
                        Invalidate ();
                }
                
+               private void OnListPositionChanged (object sender, EventArgs args)
+               {
+                       if (Rows.Count > 0 && Columns.Count > 0 && DataManager.Position != -1)
+                               MoveCurrentCell (currentCell != null ? currentCell.ColumnIndex : 0, DataManager.Position, 
+                                                true, false, false, true);
+                       else
+                               MoveCurrentCell (-1, -1, true, false, false, true);
+               }
 
                private void ReBind ()
                {
-                       ClearBinding ();
-                       DoBinding ();
+                       if (!is_binding) {
+                               is_binding = true;
+                               ClearBinding ();
+                               DoBinding ();
+                               is_binding = false;
+                       }
                }
                
                private bool MouseOverColumnResize (int col, int mousex)