2009-01-23 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridView.cs
index bf522f7debcb109b17b2c3bdb668c805bc125e6a..f7df270ee7d56bc6f396fcf997856693d2c0d005 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;
@@ -190,6 +190,7 @@ namespace System.Windows.Forms {
                        columnHeadersVisible = true;
                        columns = CreateColumnsInstance();
                        columns.CollectionChanged += OnColumnCollectionChanged;
+                       currentCellAddress = new Point (-1, -1);
                        dataMember = String.Empty;
                        defaultCellStyle = new DataGridViewCellStyle();
                        defaultCellStyle.BackColor = SystemColors.Window;
@@ -265,7 +266,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 +283,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 +710,9 @@ namespace System.Windows.Forms {
                        get { return dataMember; }
                        set {
                                if (dataMember != value) {
-                                       ClearBinding ();
-                                       
                                        dataMember = value;
+                                       ReBind ();
                                        OnDataMemberChanged(EventArgs.Empty);
-                                       
-                                       DoBinding ();
                                }
                        }
                }
@@ -726,12 +732,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 +957,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 +998,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 +2256,7 @@ namespace System.Windows.Forms {
                                        return false;
                                }
                        }
-                       
+
                        DataGridViewCell cell = currentCell;
                        Type editType = cell.EditType;
                        
@@ -2267,12 +2270,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
@@ -2298,7 +2295,8 @@ namespace System.Windows.Forms {
                                cell.PositionEditingControl (true, true, this.GetCellDisplayRectangle (cell.ColumnIndex, cell.RowIndex, false), bounds, style, false, false, (columns [cell.ColumnIndex].DisplayIndex == 0), (cell.RowIndex == 0));
 
                                // Show the editing control
-                               EditingControlInternal.Visible = true;
+                               if (EditingControlInternal != null)
+                                       EditingControlInternal.Visible = true;
 
                                IDataGridViewEditingControl dgvEditingControl = (IDataGridViewEditingControl) EditingControlInternal;
                                if (dgvEditingControl != null) {
@@ -2320,21 +2318,21 @@ 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 (false, false);
+                                       MoveCurrentCell (currentCell.ColumnIndex, NewRowIndex, true, false, false, true);
+                                       OnUserDeletedRow (new DataGridViewRowEventArgs (EditingRow));
+                               }
                        }
 
                        return true;
@@ -2393,7 +2391,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 +2416,8 @@ namespace System.Windows.Forms {
                                return true;
 
                        if (!CommitEdit (context)) {
+                               if (DataManager != null)
+                                       DataManager.EndCurrentEdit ();
                                if (EditingControl != null)
                                        EditingControl.Focus ();
                                return false;
@@ -2425,9 +2425,14 @@ 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;
+                               editing_row = null; // editing row becomes a real row
+                               PrepareEditingRow (true, false); // add a new editing row
+                               MoveCurrentCell (currentCell.ColumnIndex, NewRowIndex, true, false, false, true);
+                       }
                        return true;
                }
 
@@ -3350,11 +3355,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)
@@ -4633,13 +4634,19 @@ namespace System.Windows.Forms {
                                        horizontalScrollBar.Minimum = 0;
                                        horizontalScrollBar.Maximum = gridWidth;
                                        horizontalScrollBar.SmallChange = Columns[first_col_index].Width;
-                                       horizontalScrollBar.LargeChange = ClientSize.Width - rowHeadersWidth;
+                                       int largeChange = ClientSize.Width - rowHeadersWidth;
+                                       if (largeChange <= 0)
+                                               largeChange = ClientSize.Width;
+                                       horizontalScrollBar.LargeChange = largeChange;
                                }
                                if (verticalVisible) {
                                        verticalScrollBar.Minimum = 0;
                                        verticalScrollBar.Maximum = gridHeight;
                                        verticalScrollBar.SmallChange = first_row_height + 1;
-                                       verticalScrollBar.LargeChange = ClientSize.Height - columnHeadersHeight;
+                                       int largeChange = ClientSize.Height - columnHeadersHeight;
+                                       if (largeChange <= 0)
+                                               largeChange = ClientSize.Height;
+                                       verticalScrollBar.LargeChange = largeChange;
                                }
                        }
 
@@ -4822,6 +4829,9 @@ namespace System.Windows.Forms {
 
                internal void OnRowsAddedInternal (DataGridViewRowsAddedEventArgs e)
                {
+                       // if the very first row has just been added
+                       if (Rows.Count == 1 && Columns.Count > 0)
+                               MoveCurrentCell (0, 0, true, false, false, true);
                        AutoResizeColumnsInternal ();
                        Invalidate ();
                        OnRowsAdded (e);
@@ -4854,7 +4864,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 ();
@@ -4916,11 +4927,19 @@ namespace System.Windows.Forms {
 
                protected virtual void OnUserAddedRow (DataGridViewRowEventArgs e)
                {
-                       editing_row = null;
                        PrepareEditingRow (false, false);
 
-                       e = new DataGridViewRowEventArgs (editing_row);
-                       
+                       new_row_editing = true;
+                       if (DataManager != null) {
+                               // Switch the current editing row with a real one
+                               if (editing_row != null) {
+                                       Rows.RemoveInternal (editing_row);
+                                       editing_row = null;
+                               }
+                               DataManager.AddNew (); // will raise OnListPositionChanged
+                       }
+
+                       e = new DataGridViewRowEventArgs (Rows[NewRowIndex]);
                        DataGridViewRowEventHandler eh = (DataGridViewRowEventHandler)(Events [UserAddedRowEvent]);
                        if (eh != null) eh (this, e);
                }
@@ -4929,7 +4948,6 @@ namespace System.Windows.Forms {
                {
                        DataGridViewRowEventHandler eh = (DataGridViewRowEventHandler)(Events [UserDeletedRowEvent]);
                        if (eh != null) eh (this, e);
-
                }
 
                protected virtual void OnUserDeletingRow (DataGridViewRowCancelEventArgs e)
@@ -5160,9 +5178,12 @@ namespace System.Windows.Forms {
                {
                        DataGridViewCell cell = CurrentCell;
                        
-                       if (cell != null)
+                       if (cell != null) {
                                if (cell.KeyEntersEditMode (new KeyEventArgs ((Keys)m.WParam.ToInt32 ())))
                                        BeginEdit (true);
+                               if (EditingControl != null && ((Msg)m.Msg == Msg.WM_KEYDOWN || (Msg)m.Msg == Msg.WM_CHAR))
+                                       XplatUI.SendMessage (EditingControl.Handle, (Msg)m.Msg, m.WParam, m.LParam);
+                       }
 
                        return base.ProcessKeyEventArgs (ref m);
                }
@@ -5400,8 +5421,12 @@ namespace System.Windows.Forms {
 
                        if (cell != currentCell) {
                                if (currentCell != null) {
-                                       if (currentCell.IsInEditMode && !EndEdit ())
-                                               return false;
+                                       if (currentCell.IsInEditMode) {
+                                               if (!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));
                                }
@@ -5417,8 +5442,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);
@@ -5458,7 +5492,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) {
@@ -5752,6 +5787,9 @@ namespace System.Windows.Forms {
 
                internal void PrepareEditingRow (bool cell_changed, bool column_changed)
                {
+                       if (new_row_editing)
+                               return;
+
                        bool show = false;
                        
                        show = ColumnCount > 0 && AllowUserToAddRows;
@@ -5760,15 +5798,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;
@@ -5830,8 +5865,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 ()
@@ -5858,7 +5895,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;
@@ -5880,16 +5917,23 @@ 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 ();
                }
                
                private void MoveCurrentCell (int x, int y, bool select, bool isControl, bool isShift, bool scroll)
                {
+                       if (x == -1 || y == -1)
+                               x = y = -1;
+
                        if (!SetCurrentCellAddressCore (x, y, true, false, false)) {
                                ClearSelection ();
                                return;
@@ -5983,6 +6027,8 @@ namespace System.Windows.Forms {
                
                private int ColumnIndexToDisplayIndex (int index)
                {
+                       if (index == -1)
+                               return index;
                        return Columns[index].DisplayIndex;
                }
                
@@ -5998,7 +6044,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;
@@ -6010,11 +6056,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)