public bool SelectCells(SelectionCellRangeWithItems cellRangeWithItems) { SelectionRange itemRange = cellRangeWithItems.ItemRange; if (itemRange.IsEmpty) { throw new ArgumentException("cellRangeWithItems.ItemRange can't be empty", "cellRangeWithItems"); } if (cellRangeWithItems.Length == 1) { if (!m_cellsToUnselect.Remove(cellRangeWithItems)) { if (m_owner.SelectedCellsStore.Contains(cellRangeWithItems)) { return(false); } if (m_cellsToSelect.Contains(cellRangeWithItems.CellRange)) { return(false); } this.m_cellsToSelect.Add(cellRangeWithItems); } return(true); } else { bool selectionChanged = m_cellsToUnselect.Remove(cellRangeWithItems); SelectedCellsStorage tempStorage = new SelectedCellsStorage(m_owner); tempStorage.Add(cellRangeWithItems); // Remove the currently selected item from the new range to select foreach (SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_owner.SelectedCellsStore) { tempStorage.Remove(existingSelectionCellRangeWithItems); } // Remove the pending item to be selected from the new range to select foreach (SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect) { tempStorage.Remove(existingSelectionCellRangeWithItems); } if (tempStorage.Count > 0) { selectionChanged = true; foreach (SelectionCellRangeWithItems cellRangeWithItemsToAdd in tempStorage) { m_cellsToSelect.Add(cellRangeWithItemsToAdd); } } return(selectionChanged); } }
public void Visit( DataGridContext sourceContext, int startSourceDataItemIndex, int endSourceDataItemIndex, ref bool stopVisit ) { SelectionManager selectionChangerManager = sourceContext.DataGridControl.SelectionChangerManager; if( m_selectedColumns != null ) { int columnCount = sourceContext.Columns.Count; if( columnCount == 0 ) return; SelectionRange contextColumnMaxRange = new SelectionRange( 0, columnCount - 1 ); for( int i = 0; i < m_selectedColumns.Length; i++ ) { SelectionRange selectionRange = m_selectedColumns[ i ]; SelectionRange intersectionSelectionRange = selectionRange.Intersect( contextColumnMaxRange ); if( intersectionSelectionRange.IsEmpty ) continue; #if DEBUG string action = ( m_unselect ) ? "Removing" : "Adding"; Debug.WriteLine( "Selection : " + action + " cell : (" + startSourceDataItemIndex.ToString() + " - " + endSourceDataItemIndex.ToString() + ") - (" + intersectionSelectionRange.StartIndex.ToString() + " - " + intersectionSelectionRange.EndIndex.ToString() + ")" ); #endif var cellRange = new SelectionCellRangeWithItems( new SelectionRange( startSourceDataItemIndex, endSourceDataItemIndex ), null, intersectionSelectionRange ); if( m_unselect ) { selectionChangerManager.UnselectCells( sourceContext, cellRange ); } else { selectionChangerManager.SelectCells( sourceContext, cellRange ); } } } else { #if DEBUG string action = ( m_unselect ) ? "Removing" : "Adding"; Debug.WriteLine( "Selection : " + action + " Adding item : " + startSourceDataItemIndex.ToString() + " - " + endSourceDataItemIndex.ToString() ); #endif var itemRange = new SelectionRangeWithItems( new SelectionRange( startSourceDataItemIndex, endSourceDataItemIndex ), null ); if( m_unselect ) { selectionChangerManager.UnselectItems( sourceContext, itemRange ); } else { selectionChangerManager.SelectItems( sourceContext, itemRange ); } } }
public void Visit(DataGridContext sourceContext, int startSourceDataItemIndex, int endSourceDataItemIndex, ref bool stopVisit) { SelectionManager selectionChangerManager = sourceContext.DataGridControl.SelectionChangerManager; if (m_selectedColumns != null) { int columnCount = sourceContext.Columns.Count; if (columnCount == 0) { return; } SelectionRange contextColumnMaxRange = new SelectionRange(0, columnCount - 1); for (int i = 0; i < m_selectedColumns.Length; i++) { SelectionRange selectionRange = m_selectedColumns[i]; SelectionRange intersectionSelectionRange = selectionRange.Intersect(contextColumnMaxRange); if (intersectionSelectionRange.IsEmpty) { continue; } var cellRange = new SelectionCellRangeWithItems(new SelectionRange(startSourceDataItemIndex, endSourceDataItemIndex), null, intersectionSelectionRange); if (m_unselect) { selectionChangerManager.UnselectCells(sourceContext, cellRange); } else { selectionChangerManager.SelectCells(sourceContext, cellRange); } } } else { var itemRange = new SelectionRangeWithItems(new SelectionRange(startSourceDataItemIndex, endSourceDataItemIndex), null); if (m_unselect) { selectionChangerManager.UnselectItems(sourceContext, itemRange); } else { selectionChangerManager.SelectItems(sourceContext, itemRange); } } }
public SelectionCellRangeWithItems this[int index] { get { return(m_list[index]); } set { SelectionCellRangeWithItems oldRange = m_list[index]; m_list[index] = value; m_cellsCount += value.Length - oldRange.Length; } }
public int Add(SelectionCellRangeWithItems cellRangeWithItems) { m_cellsCount += cellRangeWithItems.Length; SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems; SelectedItemsStorage.UpdateSelectionRangeWithItemsFromAdd( m_dataGridContext, itemRangeWithItems, ref itemRangeWithItems); m_list.Add(new SelectionCellRangeWithItems( itemRangeWithItems.Range, itemRangeWithItems.Items, cellRangeWithItems.ColumnRange)); return(m_list.Count - 1); }
public void Insert(int index, SelectionCellRangeWithItems cellRangeWithItems) { Debug.Assert(!this.Contains(cellRangeWithItems.CellRange)); m_cellsCount += cellRangeWithItems.Length; SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems; SelectedItemsStorage.UpdateSelectionRangeWithItemsFromAdd( m_dataGridContext, itemRangeWithItems, ref itemRangeWithItems); m_list.Insert( index, new SelectionCellRangeWithItems( itemRangeWithItems.Range, itemRangeWithItems.Items, cellRangeWithItems.ColumnRange)); }
public bool SelectJustThisCell(int itemIndex, object item, int columnIndex) { bool selectionDone = true; m_toDeferSelect.Clear(); SelectionCellRangeWithItems rangeWithItemsToSelect = new SelectionCellRangeWithItems(itemIndex, item, columnIndex); SelectionCellRange cellRange = rangeWithItemsToSelect.CellRange; if (m_cellsToSelect.Contains(cellRange)) { selectionDone = false; } m_cellsToSelect.Clear(); SelectedCellsStorage selectedCellsInChange = m_owner.SelectedCellsStore; if (selectedCellsInChange.Contains(cellRange)) { if (!m_cellsToUnselect.Contains(cellRange)) { selectionDone = false; } m_cellsToUnselect.Clear(); foreach (SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange) { m_cellsToUnselect.Add(selectedCellRangeWithItems); } m_cellsToUnselect.Remove(rangeWithItemsToSelect); } else { m_cellsToSelect.Add(rangeWithItemsToSelect); m_cellsToUnselect.Clear(); foreach (SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange) { m_cellsToUnselect.Add(selectedCellRangeWithItems); } } this.UnselectAllItems(); return(selectionDone); }
public bool Contains(SelectionCellRangeWithItems cellRangeWithItemsToCompare) { int count = m_list.Count; SelectionCellRange cellRangeToCompare = cellRangeWithItemsToCompare.CellRange; int cellRangeLength = cellRangeToCompare.Length; // If there is more than one Cell in the range, ensure // the range is completely contained within this SelectedCellsStorage if (cellRangeLength > 1) { SelectedCellsStorage cellStorage = new SelectedCellsStorage(null, cellRangeLength); cellStorage.Add(cellRangeWithItemsToCompare); // The range is completely contained when the store // is empty after removing all ranges contained within // this SelectedCellsStorage for (int i = 0; i < count; i++) { SelectionCellRangeWithItems cellRangeWithItems = m_list[i]; cellStorage.Remove(cellRangeWithItems); if (cellStorage.Count == 0) { return(true); } } } else { for (int i = 0; i < count; i++) { SelectionCellRangeWithItems rangeWithItem = m_list[i]; SelectionCellRange rangeIntersection = rangeWithItem.CellRange.Intersect(cellRangeToCompare); if (!rangeIntersection.IsEmpty) { if (cellRangeWithItemsToCompare.ItemRangeWithItems.IsItemsEqual( rangeIntersection.ItemRange, rangeWithItem.ItemRangeWithItems)) { return(true); } } } } return(false); }
public void UpdateSelectionAfterSourceDataItemRemoved(NotifyCollectionChangedEventArgs e) { int oldStartingIndex = e.OldStartingIndex; IList removedItemsList = e.OldItems; int removedItemsCount = removedItemsList.Count; m_sourceChanges.Add(new SourceChangeInfo(e.Action, oldStartingIndex, removedItemsCount, removedItemsList)); if (oldStartingIndex == -1) { foreach (object item in removedItemsList) { SelectionRangeWithItems rangeWithItemsToRemove = new SelectionRangeWithItems(SelectionRange.Empty, new object[] { item }); m_itemsToSelect.Remove(rangeWithItemsToRemove); m_itemsToUnselect.Remove(rangeWithItemsToRemove); SelectionCellRangeWithItems cellRangeWithItemsToRemove = new SelectionCellRangeWithItems( SelectionRange.Empty, new object[] { item }, new SelectionRange(0, int.MaxValue - 1)); m_cellsToSelect.Remove(cellRangeWithItemsToRemove); m_cellsToUnselect.Remove(cellRangeWithItemsToRemove); } // Seek out in a max range of removedItemsCount the new position for actually selected item. m_itemsToSelect.OffsetIndexBasedOnSourceNewIndex(-removedItemsCount); m_itemsToUnselect.OffsetIndexBasedOnSourceNewIndex(-removedItemsCount); m_cellsToSelect.OffsetIndexBasedOnSourceNewIndex(-removedItemsCount); m_cellsToUnselect.OffsetIndexBasedOnSourceNewIndex(-removedItemsCount); } else { SelectionRange itemRange = new SelectionRange(oldStartingIndex, oldStartingIndex + removedItemsCount - 1); SelectionRangeWithItems rangeWithItemsToRemove = new SelectionRangeWithItems(itemRange, null); m_itemsToSelect.Remove(rangeWithItemsToRemove); m_itemsToUnselect.Remove(rangeWithItemsToRemove); m_itemsToSelect.OffsetIndex(oldStartingIndex, -removedItemsCount); m_itemsToUnselect.OffsetIndex(oldStartingIndex, -removedItemsCount); SelectionCellRangeWithItems cellRangeWithItemsToRemove = new SelectionCellRangeWithItems( itemRange, null, new SelectionRange(0, int.MaxValue - 1)); m_cellsToSelect.Remove(cellRangeWithItemsToRemove); m_cellsToUnselect.Remove(cellRangeWithItemsToRemove); m_cellsToSelect.OffsetIndex(oldStartingIndex, -removedItemsCount); m_cellsToUnselect.OffsetIndex(oldStartingIndex, -removedItemsCount); } }
public List <SelectionRange> GetIntersectedColumnRanges(SelectionCellRange cellRange) { List <SelectionRange> intersectionRanges = new List <SelectionRange>(); int count = m_list.Count; for (int i = 0; i < count; i++) { SelectionCellRangeWithItems range = m_list[i]; SelectionCellRange cellRangeIntersection = range.CellRange.Intersect(cellRange); if (!cellRangeIntersection.IsEmpty) { intersectionRanges.Add(cellRangeIntersection.ColumnRange); } } return(intersectionRanges); }
public bool Remove(SelectionCellRangeWithItems cellRangeWithItemsToRemove) { if (cellRangeWithItemsToRemove.CellRange.IsEmpty) { return(true); } bool removed = false; SelectionCellRange cellRangeToRemove = cellRangeWithItemsToRemove.CellRange; object[] itemsToRemove = cellRangeWithItemsToRemove.ItemRangeWithItems.Items; int itemsToRemoveCount = (itemsToRemove == null) ? 0 : itemsToRemove.Length; int itemOffsetToRemove = 0; bool itemRangeToRemoveIsEmpty = cellRangeToRemove.ItemRange.IsEmpty; do { for (int i = m_list.Count - 1; i >= 0; i--) { SelectionCellRangeWithItems cellRangeWithItems = m_list[i]; SelectionRange itemRange = cellRangeWithItems.ItemRange; SelectionCellRange cellRange = cellRangeWithItems.CellRange; SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems; SelectionCellRange cellRangeIntersection; object[] oldRangeItems = itemRangeWithItems.Items; if (itemRangeToRemoveIsEmpty) { int itemOffset = Array.IndexOf(oldRangeItems, itemsToRemove[itemOffsetToRemove]); if (itemOffset == -1) { continue; } int itemIndex = itemRange.GetIndexFromItemOffset(itemOffset); cellRangeIntersection = new SelectionCellRange( new SelectionRange(itemIndex, itemIndex), cellRangeWithItems.ColumnRange.Intersect(cellRangeToRemove.ColumnRange)); } else { cellRangeIntersection = cellRange.Intersect(cellRangeToRemove); if (cellRangeIntersection.IsEmpty) { continue; } if (!itemRangeWithItems.IsItemsEqual( cellRangeIntersection.ItemRange, cellRangeWithItemsToRemove.ItemRangeWithItems)) { continue; } } removed = true; m_cellsCount -= cellRangeIntersection.Length; SelectionCellRange[] newCellRanges = cellRange.Exclude(cellRangeIntersection); if (newCellRanges.Length == 0) { m_list.RemoveAt(i); } else { SelectionCellRange newCellRange = newCellRanges[0]; m_list[i] = new SelectionCellRangeWithItems( newCellRange.ItemRange, itemRangeWithItems.GetItems(newCellRange.ItemRange), newCellRange.ColumnRange); for (int j = 1; j < newCellRanges.Length; j++) { newCellRange = newCellRanges[j]; m_list.Insert(i + j, new SelectionCellRangeWithItems( newCellRange.ItemRange, itemRangeWithItems.GetItems(newCellRange.ItemRange), newCellRange.ColumnRange)); } } } itemOffsetToRemove++; } while((itemRangeToRemoveIsEmpty) && (itemOffsetToRemove < itemsToRemoveCount)); return(removed); }
public bool SelectJustThisCell( int itemIndex, object item, int columnIndex ) { bool selectionDone = true; m_toDeferSelect.Clear(); SelectionCellRangeWithItems rangeWithItemsToSelect = new SelectionCellRangeWithItems( itemIndex, item, columnIndex ); SelectionCellRange cellRange = rangeWithItemsToSelect.CellRange; if( m_cellsToSelect.Contains( cellRange ) ) selectionDone = false; m_cellsToSelect.Clear(); SelectedCellsStorage selectedCellsInChange = m_owner.SelectedCellsStore; if( selectedCellsInChange.Contains( cellRange ) ) { if( !m_cellsToUnselect.Contains( cellRange ) ) selectionDone = false; m_cellsToUnselect.Clear(); foreach( SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange ) { m_cellsToUnselect.Add( selectedCellRangeWithItems ); } m_cellsToUnselect.Remove( rangeWithItemsToSelect ); } else { m_cellsToSelect.Add( rangeWithItemsToSelect ); m_cellsToUnselect.Clear(); foreach( SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange ) { m_cellsToUnselect.Add( selectedCellRangeWithItems ); } } this.UnselectAllItems(); return selectionDone; }
public void UpdateSelectionAfterSourceCollectionChanged( DataGridContext dataGridContext, NotifyCollectionChangedEventArgs e ) { switch( e.Action ) { case NotifyCollectionChangedAction.Replace: { // When we get a replace with the same instance, we just have nothing to do. if( ( e.NewItems.Count == 1 ) && ( e.OldItems.Count == 1 ) && ( e.OldItems[ 0 ] == e.NewItems[ 0 ] ) ) break; this.Begin(); try { // This is done to force the SelectionChangerManager to call the DataGridControl's // DataGridContext UpdatePublicSelectionProperties method wheter or not // there is any changes to the selection. This is usefull when resetting due to a Sort operation for instance. this.EnsureRootSelectedItemAndSelectedIndex(); SelectionChanger selectionChanger = this.GetSelectionChanger( dataGridContext ); selectionChanger.UpdateSelectionAfterSourceDataItemReplaced( e ); } finally { this.End( true, false, true ); } break; } case NotifyCollectionChangedAction.Add: { this.Begin(); try { // This is done to force the SelectionChangerManager to call the DataGridControl's // DataGridContext UpdatePublicSelectionProperties method wheter or not // there is any changes to the selection. This is usefull when resetting due to a Sort operation for instance. this.EnsureRootSelectedItemAndSelectedIndex(); m_rangeSelectionItemStartAnchor = -1; m_rangeSelectionColumnStartAnchor = -1; SelectionChanger selectionChanger = this.GetSelectionChanger( dataGridContext ); selectionChanger.UpdateSelectionAfterSourceDataItemAdded( e ); } finally { this.End( true, false, true ); } break; } case NotifyCollectionChangedAction.Remove: { this.Begin(); try { // This is done to force the SelectionChangerManager to call the DataGridControl's // DataGridContext UpdatePublicSelectionProperties method wheter or not // there is any changes to the selection. This is usefull when resetting due to a Sort operation for instance. this.EnsureRootSelectedItemAndSelectedIndex(); m_rangeSelectionItemStartAnchor = -1; m_rangeSelectionColumnStartAnchor = -1; SelectionChanger selectionChanger = this.GetSelectionChanger( dataGridContext ); selectionChanger.UpdateSelectionAfterSourceDataItemRemoved( e ); } finally { this.End( true, false, true ); } break; } case NotifyCollectionChangedAction.Move: case NotifyCollectionChangedAction.Reset: { this.Begin(); try { // This is done to force the SelectionChangerManager to call the DataGridControl's // DataGridContext UpdatePublicSelectionProperties method wheter or not // there is any changes to the selection. This is usefull when resetting due to a Sort operation for instance. this.EnsureRootSelectedItemAndSelectedIndex(); m_rangeSelectionItemStartAnchor = -1; m_rangeSelectionColumnStartAnchor = -1; SelectedItemsStorage selectedItemsStorage = dataGridContext.SelectedItemsStore; int selectedItemsCount = selectedItemsStorage.Count; SelectedCellsStorage selectedCellsStorage = dataGridContext.SelectedCellsStore; int selectedCellsCount = selectedCellsStorage.Count; SelectionChanger selectionChanger = this.GetSelectionChanger( dataGridContext ); int newItemCount = dataGridContext.Items.Count; SelectionRange maxItemRange; if( newItemCount == 0 ) { maxItemRange = SelectionRange.Empty; } else { maxItemRange = new SelectionRange( 0, newItemCount - 1 ); } if( selectedItemsCount > 0 ) { selectionChanger.UnselectAllItems(); for( int i = 0; i < selectedItemsCount; i++ ) { SelectionRangeWithItems rangeWithItems = selectedItemsStorage[ i ]; object[] items = rangeWithItems.Items; if( items == null ) { SelectionRange itemRange = rangeWithItems.Range; SelectionRange rangeIntersection = itemRange.Intersect( maxItemRange ); if( rangeIntersection != itemRange ) { if( rangeIntersection.IsEmpty ) continue; rangeWithItems = new SelectionRangeWithItems( rangeIntersection, null ); } selectionChanger.SelectItems( rangeWithItems ); } else { CollectionView sourceItems = dataGridContext.Items; int itemsCount = items.Length; SelectionRange range = rangeWithItems.Range; for( int j = 0; j < itemsCount; j++ ) { object item = items[ j ]; int index = sourceItems.IndexOf( item ); if( index != -1 ) { selectionChanger.SelectItems( new SelectionRangeWithItems( index, item ) ); } } } } } if( selectedCellsCount > 0 ) { selectionChanger.UnselectAllCells(); for( int i = 0; i < selectedCellsCount; i++ ) { SelectionCellRangeWithItems cellRangeWithItems = selectedCellsStorage[ i ]; SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems; object[] items = itemRangeWithItems.Items; if( items == null ) { SelectionRange itemRange = itemRangeWithItems.Range; SelectionRange itemRangeIntersection = itemRange.Intersect( maxItemRange ); if( itemRangeIntersection != itemRange ) { if( itemRangeIntersection.IsEmpty ) continue; cellRangeWithItems = new SelectionCellRangeWithItems( itemRangeIntersection, null, cellRangeWithItems.ColumnRange ); } selectionChanger.SelectCells( cellRangeWithItems ); } else { CollectionView sourceItems = dataGridContext.Items; int itemsCount = items.Length; SelectionRange itemRange = cellRangeWithItems.ItemRange; for( int j = 0; j < itemsCount; j++ ) { object item = items[ j ]; int index = sourceItems.IndexOf( item ); if( index != -1 ) { selectionChanger.SelectCells( new SelectionCellRangeWithItems( new SelectionRange( index ), new object[] { item }, cellRangeWithItems.ColumnRange ) ); } } } } } } finally { this.End( true, false, true ); } break; } } }
public bool UnselectCells( DataGridContext dataGridContext, SelectionCellRangeWithItems cellRangeWithItems ) { SelectionChanger selectionChanger = this.GetSelectionChanger( dataGridContext ); return selectionChanger.UnselectCells( cellRangeWithItems ); }
private List<SelectionCellRangeWithItems> GetUnselectedCellsFromRemove( out List<SelectionCellRangeWithItems> removedCellsRangeWithItems ) { removedCellsRangeWithItems = new List<SelectionCellRangeWithItems>( 8 ); List<SelectionCellRangeWithItems> unselectedCellsFromRemove = new List<SelectionCellRangeWithItems>( 8 ); int count = m_sourceChanges.Count; for( int i = 0; i < count; i++ ) { SourceChangeInfo sourceChangeInfo = m_sourceChanges[ i ]; if( ( sourceChangeInfo.Action != NotifyCollectionChangedAction.Remove ) || ( sourceChangeInfo.StartIndex == -1 ) ) continue; int startIndex = sourceChangeInfo.StartIndex; int removedItemCount = sourceChangeInfo.Count; object[] removedItems = new object[ removedItemCount ]; sourceChangeInfo.Items.CopyTo( removedItems, 0 ); SelectionCellRangeWithItems cellRangeWithItemsToRemove = new SelectionCellRangeWithItems( new SelectionRange( startIndex, startIndex + removedItemCount - 1 ), removedItems, new SelectionRange( 0, int.MaxValue - 1 ) ); removedCellsRangeWithItems.Add( cellRangeWithItemsToRemove ); List<SelectionCellRangeWithItems> intersectedCellRangesWithItems = m_owner.SelectedCellsStore.GetIntersectedCellRangesWithItems( cellRangeWithItemsToRemove.CellRange ); foreach( SelectionCellRangeWithItems cellRangeWithItems in intersectedCellRangesWithItems ) { unselectedCellsFromRemove.Add( cellRangeWithItems ); } } return unselectedCellsFromRemove; }
public bool UnselectCells( SelectionCellRangeWithItems cellRangeWithItems ) { SelectionRange itemRange = cellRangeWithItems.ItemRange; SelectionRange columnRange = cellRangeWithItems.ColumnRange; SelectionCellRange cellRange = cellRangeWithItems.CellRange; if( itemRange.IsEmpty ) { // We have no index we have to remove based on item bool selectionChanged = false; List<SelectionCellRangeWithItems> cellsRangeToRemove = new List<SelectionCellRangeWithItems>(); List<object> itemsToUnselect = new List<object>( cellRangeWithItems.ItemRangeWithItems.Items ); int count = itemsToUnselect.Count; for( int i = count - 1; i >= 0; i-- ) { object itemToUnselect = itemsToUnselect[ i ]; foreach( SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect ) { SelectionRange columnIntersection = columnRange.Intersect( existingSelectionCellRangeWithItems.ColumnRange ); if( columnIntersection.IsEmpty ) continue; int index = Array.IndexOf( existingSelectionCellRangeWithItems.ItemRangeWithItems.Items, itemToUnselect ); if( index > -1 ) { cellsRangeToRemove.Add( new SelectionCellRangeWithItems( new SelectionRange( existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset( index ) ), new object[] { itemToUnselect }, columnIntersection ) ); } } } // Remove the currently unselected item from the new range to select foreach( SelectionCellRangeWithItems cellRangeToRemove in cellsRangeToRemove ) { selectionChanged |= m_cellsToSelect.Remove( cellRangeToRemove ); } count = itemsToUnselect.Count; for( int i = 0; i < count; i++ ) { object itemToUnselect = itemsToUnselect[ i ]; foreach( SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_owner.SelectedCellsStore ) { SelectionRange columnIntersection = columnRange.Intersect( existingSelectionCellRangeWithItems.ColumnRange ); if( columnIntersection.IsEmpty ) continue; int index = Array.IndexOf( existingSelectionCellRangeWithItems.ItemRangeWithItems.Items, itemToUnselect ); if( index > -1 ) { index = existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset( index ); SelectionCellRange cellRangeTemp = new SelectionCellRange( new SelectionRange( existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset( index ) ), columnIntersection ); if( !m_cellsToUnselect.Contains( cellRangeTemp ) ) { selectionChanged = true; m_cellsToUnselect.Add( new SelectionCellRangeWithItems( cellRangeTemp.ItemRange, new object[] { itemToUnselect }, cellRangeTemp.ColumnRange ) ); } } } } return selectionChanged; } if( cellRangeWithItems.Length == 1 ) { if( !m_cellsToSelect.Remove( cellRangeWithItems ) ) { if( !m_owner.SelectedCellsStore.Contains( cellRange ) ) return false; if( m_cellsToUnselect.Contains( cellRange ) ) return false; m_cellsToUnselect.Add( cellRangeWithItems ); } return true; } else { SelectedCellsStorage tempStorage = new SelectedCellsStorage( m_owner, 8 ); tempStorage.Add( cellRangeWithItems ); // Remove the currently selected item from the new range to select foreach( SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect ) { tempStorage.Remove( existingSelectionCellRangeWithItems ); } bool selectionChanged = m_cellsToSelect.Remove( cellRangeWithItems ); if( tempStorage.Count > 0 ) { selectionChanged = true; foreach( SelectionCellRangeWithItems cellRangeWithItemsToAdd in tempStorage ) { Debug.Assert( !m_cellsToUnselect.Contains( cellRangeWithItemsToAdd.CellRange ) ); m_cellsToUnselect.Add( cellRangeWithItemsToAdd ); } } return selectionChanged; } }
public bool Remove( SelectionCellRangeWithItems cellRangeWithItemsToRemove ) { if( cellRangeWithItemsToRemove.CellRange.IsEmpty ) return true; bool removed = false; SelectionCellRange cellRangeToRemove = cellRangeWithItemsToRemove.CellRange; object[] itemsToRemove = cellRangeWithItemsToRemove.ItemRangeWithItems.Items; int itemsToRemoveCount = ( itemsToRemove == null ) ? 0 : itemsToRemove.Length; int itemOffsetToRemove = 0; bool itemRangeToRemoveIsEmpty = cellRangeToRemove.ItemRange.IsEmpty; do { for( int i = m_list.Count - 1; i >= 0; i-- ) { SelectionCellRangeWithItems cellRangeWithItems = m_list[ i ]; SelectionRange itemRange = cellRangeWithItems.ItemRange; SelectionCellRange cellRange = cellRangeWithItems.CellRange; SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems; SelectionCellRange cellRangeIntersection; object[] oldRangeItems = itemRangeWithItems.Items; if( itemRangeToRemoveIsEmpty ) { int itemOffset = Array.IndexOf( oldRangeItems, itemsToRemove[ itemOffsetToRemove ] ); if( itemOffset == -1 ) continue; int itemIndex = itemRange.GetIndexFromItemOffset( itemOffset ); cellRangeIntersection = new SelectionCellRange( new SelectionRange( itemIndex, itemIndex ), cellRangeWithItems.ColumnRange.Intersect( cellRangeToRemove.ColumnRange ) ); } else { cellRangeIntersection = cellRange.Intersect( cellRangeToRemove ); if( cellRangeIntersection.IsEmpty ) continue; if( !itemRangeWithItems.IsItemsEqual( cellRangeIntersection.ItemRange, cellRangeWithItemsToRemove.ItemRangeWithItems ) ) { continue; } } removed = true; m_cellsCount -= cellRangeIntersection.Length; SelectionCellRange[] newCellRanges = cellRange.Exclude( cellRangeIntersection ); if( newCellRanges.Length == 0 ) { m_list.RemoveAt( i ); } else { SelectionCellRange newCellRange = newCellRanges[ 0 ]; m_list[ i ] = new SelectionCellRangeWithItems( newCellRange.ItemRange, itemRangeWithItems.GetItems( newCellRange.ItemRange ), newCellRange.ColumnRange ); for( int j = 1; j < newCellRanges.Length; j++ ) { newCellRange = newCellRanges[ j ]; m_list.Insert( i + j, new SelectionCellRangeWithItems( newCellRange.ItemRange, itemRangeWithItems.GetItems( newCellRange.ItemRange ), newCellRange.ColumnRange ) ); } } } itemOffsetToRemove++; } while( ( itemRangeToRemoveIsEmpty ) && ( itemOffsetToRemove < itemsToRemoveCount ) ); return removed; }
public void OffsetIndex(int itemStartIndex, int offset) { // Used to offset index after an add or remove from the data source of the grid. SelectionRange offsetRange = new SelectionRange( itemStartIndex, itemStartIndex + Math.Abs(offset) - 1); for (int i = this.Count - 1; i >= 0; i--) { SelectionCellRangeWithItems cellRangeWithItems = m_list[i]; SelectionRange itemRange = cellRangeWithItems.ItemRange; SelectionRange columnRange = cellRangeWithItems.ColumnRange; if (offsetRange > itemRange) { continue; } SelectionRange itemRangeIntersection = itemRange.Intersect(offsetRange); object[] originalItems = cellRangeWithItems.ItemRangeWithItems.Items; if (!itemRangeIntersection.IsEmpty) { // Should only happen when adding since when we remove data from the source, we remove the // the range from the list. Debug.Assert(offset > 0); // Offset the index higher than the start index of the new added item SelectionRange topRange; SelectionRange bottomRange; if (itemRange.StartIndex > itemRange.EndIndex) { if (itemRangeIntersection.EndIndex == itemRange.EndIndex) { SelectionRange newItemRange = new SelectionRange(itemRange.StartIndex + offset, itemRange.EndIndex + offset); m_list[i] = new SelectionCellRangeWithItems(newItemRange, originalItems, columnRange); continue; } else { int bottomRangeEndIndex = itemStartIndex + offset; bottomRange = new SelectionRange(itemStartIndex - 1, itemRange.EndIndex); topRange = new SelectionRange(itemRange.Length - bottomRange.Length - 1 + bottomRangeEndIndex, bottomRangeEndIndex); } } else { if (itemRangeIntersection.StartIndex == itemRange.StartIndex) { SelectionRange newItemRange = new SelectionRange(itemRange.StartIndex + offset, itemRange.EndIndex + offset); m_list[i] = new SelectionCellRangeWithItems(newItemRange, originalItems, columnRange); continue; } else { int bottomRangeStartIndex = itemStartIndex + offset; topRange = new SelectionRange(itemRange.StartIndex, itemStartIndex - 1); bottomRange = new SelectionRange(bottomRangeStartIndex, itemRange.Length - topRange.Length - 1 + bottomRangeStartIndex); } } object[] topItems = null; object[] bottomItems = null; if (originalItems != null) { topItems = new object[topRange.Length]; Array.Copy(originalItems, 0, topItems, 0, topItems.Length); bottomItems = new object[bottomRange.Length]; Array.Copy(originalItems, topItems.Length, bottomItems, 0, bottomItems.Length); } m_list[i] = new SelectionCellRangeWithItems(topRange, topItems, columnRange); m_list.Insert(i + 1, new SelectionCellRangeWithItems(bottomRange, bottomItems, columnRange)); } else { // Offset the index by the count added SelectionRange newItemRange = new SelectionRange(itemRange.StartIndex + offset, itemRange.EndIndex + offset); m_list[i] = new SelectionCellRangeWithItems(newItemRange, originalItems, columnRange); } } }
public bool Contains( SelectionCellRangeWithItems cellRangeWithItemsToCompare ) { int count = m_list.Count; SelectionCellRange cellRangeToCompare = cellRangeWithItemsToCompare.CellRange; int cellRangeLength = cellRangeToCompare.Length; // If there is more than one Cell in the range, ensure // the range is completely contained within this SelectedCellsStorage if( cellRangeLength > 1 ) { SelectedCellsStorage cellStorage = new SelectedCellsStorage( null, cellRangeLength ); cellStorage.Add( cellRangeWithItemsToCompare ); // The range is completely contained when the store // is empty after removing all ranges contained within // this SelectedCellsStorage for( int i = 0; i < count; i++ ) { SelectionCellRangeWithItems cellRangeWithItems = m_list[ i ]; cellStorage.Remove( cellRangeWithItems ); if( cellStorage.Count == 0 ) return true; } } else { for( int i = 0; i < count; i++ ) { SelectionCellRangeWithItems rangeWithItem = m_list[ i ]; SelectionCellRange rangeIntersection = rangeWithItem.CellRange.Intersect( cellRangeToCompare ); if( !rangeIntersection.IsEmpty ) { if( cellRangeWithItemsToCompare.ItemRangeWithItems.IsItemsEqual( rangeIntersection.ItemRange, rangeWithItem.ItemRangeWithItems ) ) { return true; } } } } return false; }
public void Insert( int index, SelectionCellRangeWithItems cellRangeWithItems ) { Debug.Assert( !this.Contains( cellRangeWithItems.CellRange ) ); m_cellsCount += cellRangeWithItems.Length; SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems; SelectedItemsStorage.UpdateSelectionRangeWithItemsFromAdd( m_dataGridContext, itemRangeWithItems, ref itemRangeWithItems ); m_list.Insert( index, new SelectionCellRangeWithItems( itemRangeWithItems.Range, itemRangeWithItems.Items, cellRangeWithItems.ColumnRange ) ); }
internal SelectionCellRangeWithItemsWrapper(SelectionCellRangeWithItems value, int index) { m_value = value; m_index = index; }
public void UpdateSelectionAfterSourceDataItemRemoved( NotifyCollectionChangedEventArgs e ) { int oldStartingIndex = e.OldStartingIndex; IList removedItemsList = e.OldItems; int removedItemsCount = removedItemsList.Count; m_sourceChanges.Add( new SourceChangeInfo( e.Action, oldStartingIndex, removedItemsCount, removedItemsList ) ); if( oldStartingIndex == -1 ) { foreach( object item in removedItemsList ) { SelectionRangeWithItems rangeWithItemsToRemove = new SelectionRangeWithItems( SelectionRange.Empty, new object[] { item } ); m_itemsToSelect.Remove( rangeWithItemsToRemove ); m_itemsToUnselect.Remove( rangeWithItemsToRemove ); SelectionCellRangeWithItems cellRangeWithItemsToRemove = new SelectionCellRangeWithItems( SelectionRange.Empty, new object[] { item }, new SelectionRange( 0, int.MaxValue - 1 ) ); m_cellsToSelect.Remove( cellRangeWithItemsToRemove ); m_cellsToUnselect.Remove( cellRangeWithItemsToRemove ); } // Seek out in a max range of removedItemsCount the new position for actually selected item. m_itemsToSelect.OffsetIndexBasedOnSourceNewIndex( -removedItemsCount ); m_itemsToUnselect.OffsetIndexBasedOnSourceNewIndex( -removedItemsCount ); m_cellsToSelect.OffsetIndexBasedOnSourceNewIndex( -removedItemsCount ); m_cellsToUnselect.OffsetIndexBasedOnSourceNewIndex( -removedItemsCount ); } else { SelectionRange itemRange = new SelectionRange( oldStartingIndex, oldStartingIndex + removedItemsCount - 1 ); SelectionRangeWithItems rangeWithItemsToRemove = new SelectionRangeWithItems( itemRange, null ); m_itemsToSelect.Remove( rangeWithItemsToRemove ); m_itemsToUnselect.Remove( rangeWithItemsToRemove ); m_itemsToSelect.OffsetIndex( oldStartingIndex, -removedItemsCount ); m_itemsToUnselect.OffsetIndex( oldStartingIndex, -removedItemsCount ); SelectionCellRangeWithItems cellRangeWithItemsToRemove = new SelectionCellRangeWithItems( itemRange, null, new SelectionRange( 0, int.MaxValue - 1 ) ); m_cellsToSelect.Remove( cellRangeWithItemsToRemove ); m_cellsToUnselect.Remove( cellRangeWithItemsToRemove ); m_cellsToSelect.OffsetIndex( oldStartingIndex, -removedItemsCount ); m_cellsToUnselect.OffsetIndex( oldStartingIndex, -removedItemsCount ); } }
public bool SelectItemCells( int itemIndex, object item, HashedLinkedList<ColumnBase> columnsByVisiblePosition, bool preserveSelection ) { bool selectionDone = true; int columnsCount = columnsByVisiblePosition.Count; m_toDeferSelect.Clear(); SelectionCellRangeWithItems rangeWithItemsToSelect = new SelectionCellRangeWithItems( new SelectionRange( itemIndex ), new object[] { item }, new SelectionRange( 0, columnsCount - 1 ) ); SelectionCellRange cellRange = rangeWithItemsToSelect.CellRange; m_cellsToSelect.Clear(); SelectedCellsStorage selectedCellsInChange = m_owner.SelectedCellsStore; // Remove all currently selected Cells from the new selectionRange // to avoid duplicate SelectionCellRange SelectedCellsStorage tempStorage = new SelectedCellsStorage( null, 1 ); tempStorage.Add( rangeWithItemsToSelect ); for( int i = 0; i < selectedCellsInChange.Count; i++ ) { tempStorage.Remove( selectedCellsInChange[ i ] ); } foreach( ColumnBase column in columnsByVisiblePosition ) { if( !column.Visible ) tempStorage.Remove( new SelectionCellRangeWithItems( itemIndex, item, column.VisiblePosition ) ); } int tempStorageCount = tempStorage.Count; // All Cells are already selected if( tempStorageCount == 0 ) { if( !m_cellsToUnselect.Contains( cellRange ) ) selectionDone = false; m_cellsToUnselect.Clear(); if( !preserveSelection ) { foreach( SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange ) { m_cellsToUnselect.Add( selectedCellRangeWithItems ); } } m_cellsToUnselect.Remove( rangeWithItemsToSelect ); } else { // Add each range to selection for( int i = 0; i < tempStorageCount; i++ ) { m_cellsToSelect.Add( tempStorage[ i ] ); } m_cellsToUnselect.Clear(); if( !preserveSelection ) { foreach( SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange ) { tempStorage = new SelectedCellsStorage( null, 1 ); tempStorage.Add( selectedCellRangeWithItems ); tempStorage.Remove( rangeWithItemsToSelect ); tempStorageCount = tempStorage.Count; for( int i = 0; i < tempStorageCount; i++ ) { m_cellsToUnselect.Add( tempStorage[ i ] ); } } } } if( !preserveSelection ) this.UnselectAllItems(); return selectionDone; }
public void OffsetIndexBasedOnSourceNewIndex(int maxOffset) { if (m_dataGridContext == null) { throw new InvalidOperationException("We must have a DataGridContext to find the new index."); } CollectionView sourceItems = m_dataGridContext.Items; for (int i = this.Count - 1; i >= 0; i--) { SelectionCellRangeWithItems cellRangeWithItems = m_list[i]; SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems; object[] items = itemRangeWithItems.Items; if (items == null) { throw new InvalidOperationException("We should have items to find the new index."); } object item = items[0]; SelectionRange itemRange = itemRangeWithItems.Range; int startIndex = Math.Min(itemRange.StartIndex, itemRange.EndIndex); if (maxOffset < 0) { for (int j = 0; j >= maxOffset; j--) { if (object.Equals(sourceItems.GetItemAt(startIndex), item)) { if (j != 0) { SelectionRange newItemRange = new SelectionRange(itemRange.StartIndex + j, itemRange.EndIndex + j); m_list[i] = new SelectionCellRangeWithItems(newItemRange, items, cellRangeWithItems.ColumnRange); } break; } startIndex--; if (startIndex < 0) { break; } } } else { int sourceItemCount = sourceItems.Count; for (int j = 0; j <= maxOffset; j++) { if (object.Equals(sourceItems.GetItemAt(startIndex), item)) { if (j != 0) { SelectionRange newItemRange = new SelectionRange(itemRange.StartIndex + j, itemRange.EndIndex + j); m_list[i] = new SelectionCellRangeWithItems(newItemRange, items, cellRangeWithItems.ColumnRange); } break; } startIndex++; if (startIndex >= sourceItemCount) { break; } } } } }
private void SetIsSelectedOnDataCell( Dictionary<ColumnBase, int> columnsVisiblePosition, Dictionary<int, DataRow> realizedDataRows, SelectionCellRangeWithItems cellRangeWithItems, bool selected ) { object[] rangeItems = cellRangeWithItems.ItemRangeWithItems.Items; bool selectionChanged = false; SelectionRange columnRange = cellRangeWithItems.ColumnRange; if( rangeItems != null ) { int itemsCount = rangeItems.Length; for( int i = 0; i < itemsCount; i++ ) { DataRow rangeItemAsDataRow = rangeItems[ i ] as DataRow; if( rangeItemAsDataRow != null ) { selectionChanged = true; foreach( DataCell dataCell in rangeItemAsDataRow.CreatedCells ) { if( dataCell.IsContainerVirtualized ) continue; int columnPosition; if( !columnsVisiblePosition.TryGetValue( dataCell.ParentColumn, out columnPosition ) ) continue; if( !columnRange.Intersect( new SelectionRange( columnPosition ) ).IsEmpty ) { dataCell.SetIsSelected( selected ); } } } else { // We take for granted that no item will be a DataRow break; } } } if( !selectionChanged ) { SelectionRange itemRange = cellRangeWithItems.ItemRange; foreach( KeyValuePair<int, DataRow> realizedItemPair in realizedDataRows ) { if( !itemRange.Intersect( new SelectionRange( realizedItemPair.Key ) ).IsEmpty ) { foreach( DataCell dataCell in realizedItemPair.Value.CreatedCells ) { if( dataCell.IsContainerVirtualized ) continue; int columnPosition; if( !columnsVisiblePosition.TryGetValue( dataCell.ParentColumn, out columnPosition ) ) continue; if( !columnRange.Intersect( new SelectionRange( columnPosition ) ).IsEmpty ) { dataCell.SetIsSelected( selected ); } } } } } }
public void OffsetIndex( int itemStartIndex, int offset ) { // Used to offset index after an add or remove from the data source of the grid. SelectionRange offsetRange = new SelectionRange( itemStartIndex, itemStartIndex + Math.Abs( offset ) - 1 ); for( int i = this.Count - 1; i >= 0; i-- ) { SelectionCellRangeWithItems cellRangeWithItems = m_list[ i ]; SelectionRange itemRange = cellRangeWithItems.ItemRange; SelectionRange columnRange = cellRangeWithItems.ColumnRange; if( offsetRange > itemRange ) continue; SelectionRange itemRangeIntersection = itemRange.Intersect( offsetRange ); object[] originalItems = cellRangeWithItems.ItemRangeWithItems.Items; if( !itemRangeIntersection.IsEmpty ) { // Should only happen when adding since when we remove data from the source, we remove the // the range from the list. Debug.Assert( offset > 0 ); // Offset the index higher than the start index of the new added item SelectionRange topRange; SelectionRange bottomRange; if( itemRange.StartIndex > itemRange.EndIndex ) { if( itemRangeIntersection.EndIndex == itemRange.EndIndex ) { SelectionRange newItemRange = new SelectionRange( itemRange.StartIndex + offset, itemRange.EndIndex + offset ); m_list[ i ] = new SelectionCellRangeWithItems( newItemRange, originalItems, columnRange ); continue; } else { int bottomRangeEndIndex = itemStartIndex + offset; bottomRange = new SelectionRange( itemStartIndex - 1, itemRange.EndIndex ); topRange = new SelectionRange( itemRange.Length - bottomRange.Length - 1 + bottomRangeEndIndex, bottomRangeEndIndex ); } } else { if( itemRangeIntersection.StartIndex == itemRange.StartIndex ) { SelectionRange newItemRange = new SelectionRange( itemRange.StartIndex + offset, itemRange.EndIndex + offset ); m_list[ i ] = new SelectionCellRangeWithItems( newItemRange, originalItems, columnRange ); continue; } else { int bottomRangeStartIndex = itemStartIndex + offset; topRange = new SelectionRange( itemRange.StartIndex, itemStartIndex - 1 ); bottomRange = new SelectionRange( bottomRangeStartIndex, itemRange.Length - topRange.Length - 1 + bottomRangeStartIndex ); } } object[] topItems = null; object[] bottomItems = null; if( originalItems != null ) { topItems = new object[ topRange.Length ]; Array.Copy( originalItems, 0, topItems, 0, topItems.Length ); bottomItems = new object[ bottomRange.Length ]; Array.Copy( originalItems, topItems.Length, bottomItems, 0, bottomItems.Length ); } m_list[ i ] = new SelectionCellRangeWithItems( topRange, topItems, columnRange ); m_list.Insert( i + 1, new SelectionCellRangeWithItems( bottomRange, bottomItems, columnRange ) ); } else { // Offset the index by the count added SelectionRange newItemRange = new SelectionRange( itemRange.StartIndex + offset, itemRange.EndIndex + offset ); m_list[ i ] = new SelectionCellRangeWithItems( newItemRange, originalItems, columnRange ); } } }
public bool SelectCells( SelectionCellRangeWithItems cellRangeWithItems ) { SelectionRange itemRange = cellRangeWithItems.ItemRange; if( itemRange.IsEmpty ) throw new ArgumentException( "cellRangeWithItems.ItemRange can't be empty", "cellRangeWithItems" ); if( cellRangeWithItems.Length == 1 ) { if( !m_cellsToUnselect.Remove( cellRangeWithItems ) ) { if( m_owner.SelectedCellsStore.Contains( cellRangeWithItems ) ) return false; if( m_cellsToSelect.Contains( cellRangeWithItems.CellRange ) ) return false; this.m_cellsToSelect.Add( cellRangeWithItems ); } return true; } else { bool selectionChanged = m_cellsToUnselect.Remove( cellRangeWithItems ); SelectedCellsStorage tempStorage = new SelectedCellsStorage( m_owner, 8 ); tempStorage.Add( cellRangeWithItems ); // Remove the currently selected item from the new range to select foreach( SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_owner.SelectedCellsStore ) { tempStorage.Remove( existingSelectionCellRangeWithItems ); } // Remove the pending item to be selected from the new range to select foreach( SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect ) { tempStorage.Remove( existingSelectionCellRangeWithItems ); } if( tempStorage.Count > 0 ) { selectionChanged = true; foreach( SelectionCellRangeWithItems cellRangeWithItemsToAdd in tempStorage ) { m_cellsToSelect.Add( cellRangeWithItemsToAdd ); } } return selectionChanged; } }
private void SetIsSelectedOnDataCell( Dictionary <ColumnBase, int> columnsVisiblePosition, Dictionary <int, DataRow> realizedDataRows, SelectionCellRangeWithItems cellRangeWithItems, bool selected) { var rangeItems = cellRangeWithItems.ItemRangeWithItems.Items; var selectionChanged = false; var columnRange = cellRangeWithItems.ColumnRange; if (rangeItems != null) { int itemsCount = rangeItems.Length; for (int i = 0; i < itemsCount; i++) { var rangeItemAsDataRow = rangeItems[i] as DataRow; if (rangeItemAsDataRow != null) { selectionChanged = true; foreach (var dataCell in rangeItemAsDataRow.CreatedCells) { if (dataCell.IsContainerVirtualized) { continue; } int columnPosition; if (!columnsVisiblePosition.TryGetValue(dataCell.ParentColumn, out columnPosition)) { continue; } if (!columnRange.Intersect(new SelectionRange(columnPosition)).IsEmpty) { dataCell.SetIsSelected(selected); } } } else { // We take for granted that no item will be a DataRow break; } } } if (!selectionChanged) { var itemRange = cellRangeWithItems.ItemRange; foreach (var realizedItemPair in realizedDataRows) { if (!itemRange.Intersect(new SelectionRange(realizedItemPair.Key)).IsEmpty) { foreach (var dataCell in realizedItemPair.Value.CreatedCells) { if (dataCell.IsContainerVirtualized) { continue; } int columnPosition; if (!columnsVisiblePosition.TryGetValue(dataCell.ParentColumn, out columnPosition)) { continue; } if (!columnRange.Intersect(new SelectionRange(columnPosition)).IsEmpty) { dataCell.SetIsSelected(selected); } } } } } }
public void OffsetIndexBasedOnSourceNewIndex( int maxOffset ) { if( m_dataGridContext == null ) throw new InvalidOperationException( "We must have a DataGridContext to find the new index." ); CollectionView sourceItems = m_dataGridContext.Items; for( int i = this.Count - 1; i >= 0; i-- ) { SelectionCellRangeWithItems cellRangeWithItems = m_list[ i ]; SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems; object[] items = itemRangeWithItems.Items; if( items == null ) throw new InvalidOperationException( "We should have items to find the new index." ); object item = items[ 0 ]; SelectionRange itemRange = itemRangeWithItems.Range; int startIndex = Math.Min( itemRange.StartIndex, itemRange.EndIndex ); if( maxOffset < 0 ) { for( int j = 0; j >= maxOffset; j-- ) { if( object.Equals( sourceItems.GetItemAt( startIndex ), item ) ) { if( j != 0 ) { SelectionRange newItemRange = new SelectionRange( itemRange.StartIndex + j, itemRange.EndIndex + j ); m_list[ i ] = new SelectionCellRangeWithItems( newItemRange, items, cellRangeWithItems.ColumnRange ); } break; } startIndex--; if( startIndex < 0 ) break; } } else { int sourceItemCount = sourceItems.Count; for( int j = 0; j <= maxOffset; j++ ) { if( object.Equals( sourceItems.GetItemAt( startIndex ), item ) ) { if( j != 0 ) { SelectionRange newItemRange = new SelectionRange( itemRange.StartIndex + j, itemRange.EndIndex + j ); m_list[ i ] = new SelectionCellRangeWithItems( newItemRange, items, cellRangeWithItems.ColumnRange ); } break; } startIndex++; if( startIndex >= sourceItemCount ) break; } } } }
public bool UnselectCells(SelectionCellRangeWithItems cellRangeWithItems) { SelectionRange itemRange = cellRangeWithItems.ItemRange; SelectionRange columnRange = cellRangeWithItems.ColumnRange; SelectionCellRange cellRange = cellRangeWithItems.CellRange; if (itemRange.IsEmpty) { // We have no index we have to remove based on item bool selectionChanged = false; List <SelectionCellRangeWithItems> cellsRangeToRemove = new List <SelectionCellRangeWithItems>(); List <object> itemsToUnselect = new List <object>(cellRangeWithItems.ItemRangeWithItems.Items); int count = itemsToUnselect.Count; for (int i = count - 1; i >= 0; i--) { object itemToUnselect = itemsToUnselect[i]; foreach (SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect) { SelectionRange columnIntersection = columnRange.Intersect(existingSelectionCellRangeWithItems.ColumnRange); if (columnIntersection.IsEmpty) { continue; } int index = Array.IndexOf(existingSelectionCellRangeWithItems.ItemRangeWithItems.Items, itemToUnselect); if (index > -1) { cellsRangeToRemove.Add( new SelectionCellRangeWithItems( new SelectionRange(existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset(index)), new object[] { itemToUnselect }, columnIntersection)); } } } // Remove the currently unselected item from the new range to select foreach (SelectionCellRangeWithItems cellRangeToRemove in cellsRangeToRemove) { selectionChanged |= m_cellsToSelect.Remove(cellRangeToRemove); } count = itemsToUnselect.Count; for (int i = 0; i < count; i++) { object itemToUnselect = itemsToUnselect[i]; foreach (SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_owner.SelectedCellsStore) { SelectionRange columnIntersection = columnRange.Intersect(existingSelectionCellRangeWithItems.ColumnRange); if (columnIntersection.IsEmpty) { continue; } int index = Array.IndexOf(existingSelectionCellRangeWithItems.ItemRangeWithItems.Items, itemToUnselect); if (index > -1) { index = existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset(index); SelectionCellRange cellRangeTemp = new SelectionCellRange( new SelectionRange(existingSelectionCellRangeWithItems.ItemRange.GetIndexFromItemOffset(index)), columnIntersection); if (!m_cellsToUnselect.Contains(cellRangeTemp)) { selectionChanged = true; m_cellsToUnselect.Add(new SelectionCellRangeWithItems( cellRangeTemp.ItemRange, new object[] { itemToUnselect }, cellRangeTemp.ColumnRange)); } } } } return(selectionChanged); } if (cellRangeWithItems.Length == 1) { if (!m_cellsToSelect.Remove(cellRangeWithItems)) { if (!m_owner.SelectedCellsStore.Contains(cellRange)) { return(false); } if (m_cellsToUnselect.Contains(cellRange)) { return(false); } m_cellsToUnselect.Add(cellRangeWithItems); } return(true); } else { SelectedCellsStorage tempStorage = new SelectedCellsStorage(m_owner); tempStorage.Add(cellRangeWithItems); // Remove the currently selected item from the new range to select foreach (SelectionCellRangeWithItems existingSelectionCellRangeWithItems in m_cellsToSelect) { tempStorage.Remove(existingSelectionCellRangeWithItems); } bool selectionChanged = m_cellsToSelect.Remove(cellRangeWithItems); if (tempStorage.Count > 0) { selectionChanged = true; foreach (SelectionCellRangeWithItems cellRangeWithItemsToAdd in tempStorage) { Debug.Assert(!m_cellsToUnselect.Contains(cellRangeWithItemsToAdd.CellRange)); m_cellsToUnselect.Add(cellRangeWithItemsToAdd); } } return(selectionChanged); } }
public int Add( SelectionCellRangeWithItems cellRangeWithItems ) { m_cellsCount += cellRangeWithItems.Length; SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems; SelectedItemsStorage.UpdateSelectionRangeWithItemsFromAdd( m_dataGridContext, itemRangeWithItems, ref itemRangeWithItems ); m_list.Add( new SelectionCellRangeWithItems( itemRangeWithItems.Range, itemRangeWithItems.Items, cellRangeWithItems.ColumnRange ) ); return m_list.Count - 1; }
public bool SelectItemCells( int itemIndex, object item, HashedLinkedList <ColumnBase> columnsByVisiblePosition, bool preserveSelection) { bool selectionDone = true; int columnsCount = columnsByVisiblePosition.Count; m_toDeferSelect.Clear(); SelectionCellRangeWithItems rangeWithItemsToSelect = new SelectionCellRangeWithItems( new SelectionRange(itemIndex), new object[] { item }, new SelectionRange(0, columnsCount - 1)); SelectionCellRange cellRange = rangeWithItemsToSelect.CellRange; m_cellsToSelect.Clear(); SelectedCellsStorage selectedCellsInChange = m_owner.SelectedCellsStore; // Remove all currently selected Cells from the new selectionRange // to avoid duplicate SelectionCellRange SelectedCellsStorage tempStorage = new SelectedCellsStorage(null); tempStorage.Add(rangeWithItemsToSelect); for (int i = 0; i < selectedCellsInChange.Count; i++) { tempStorage.Remove(selectedCellsInChange[i]); } foreach (ColumnBase column in columnsByVisiblePosition) { if (!column.Visible) { tempStorage.Remove(new SelectionCellRangeWithItems(itemIndex, item, column.VisiblePosition)); } } int tempStorageCount = tempStorage.Count; // All Cells are already selected if (tempStorageCount == 0) { if (!m_cellsToUnselect.Contains(cellRange)) { selectionDone = false; } m_cellsToUnselect.Clear(); if (!preserveSelection) { foreach (SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange) { m_cellsToUnselect.Add(selectedCellRangeWithItems); } } m_cellsToUnselect.Remove(rangeWithItemsToSelect); } else { // Add each range to selection for (int i = 0; i < tempStorageCount; i++) { m_cellsToSelect.Add(tempStorage[i]); } m_cellsToUnselect.Clear(); if (!preserveSelection) { foreach (SelectionCellRangeWithItems selectedCellRangeWithItems in selectedCellsInChange) { tempStorage = new SelectedCellsStorage(null); tempStorage.Add(selectedCellRangeWithItems); tempStorage.Remove(rangeWithItemsToSelect); tempStorageCount = tempStorage.Count; for (int i = 0; i < tempStorageCount; i++) { m_cellsToUnselect.Add(tempStorage[i]); } } } } if (!preserveSelection) { this.UnselectAllItems(); } return(selectionDone); }
public void Visit(DataGridContext sourceContext, int startSourceDataItemIndex, int endSourceDataItemIndex, ref bool stopVisit) { SelectionManager selectionChangerManager = sourceContext.DataGridControl.SelectionChangerManager; if (m_selectedColumns != null) { int columnCount = sourceContext.Columns.Count; if (columnCount == 0) { return; } SelectionRange contextColumnMaxRange = new SelectionRange(0, columnCount - 1); for (int i = 0; i < m_selectedColumns.Length; i++) { SelectionRange selectionRange = m_selectedColumns[i]; SelectionRange intersectionSelectionRange = selectionRange.Intersect(contextColumnMaxRange); if (intersectionSelectionRange.IsEmpty) { continue; } #if DEBUG string action = (m_unselect) ? "Removing" : "Adding"; Debug.WriteLine("Selection : " + action + " cell : (" + startSourceDataItemIndex.ToString() + " - " + endSourceDataItemIndex.ToString() + ") - (" + intersectionSelectionRange.StartIndex.ToString() + " - " + intersectionSelectionRange.EndIndex.ToString() + ")"); #endif var cellRange = new SelectionCellRangeWithItems(new SelectionRange(startSourceDataItemIndex, endSourceDataItemIndex), null, intersectionSelectionRange); if (m_unselect) { selectionChangerManager.UnselectCells(sourceContext, cellRange); } else { selectionChangerManager.SelectCells(sourceContext, cellRange); } } } else { #if DEBUG string action = (m_unselect) ? "Removing" : "Adding"; Debug.WriteLine("Selection : " + action + " Adding item : " + startSourceDataItemIndex.ToString() + " - " + endSourceDataItemIndex.ToString()); #endif var itemRange = new SelectionRangeWithItems(new SelectionRange(startSourceDataItemIndex, endSourceDataItemIndex), null); if (m_unselect) { selectionChangerManager.UnselectItems(sourceContext, itemRange); } else { selectionChangerManager.SelectItems(sourceContext, itemRange); } } }
public void UpdateSelectionAfterSourceDataItemReplaced(NotifyCollectionChangedEventArgs e) { Debug.Assert(e.OldItems.Count == e.NewItems.Count); Debug.Assert(e.OldStartingIndex == e.NewStartingIndex); SelectedItemsStorage selectedItemsStorage = m_owner.SelectedItemsStore; SelectedCellsStorage selectedCellsStorage = m_owner.SelectedCellsStore; int oldItemIndex = e.OldStartingIndex; IList oldItems = e.OldItems; IList newItems = e.NewItems; int replacedItemCount = oldItems.Count; int cellRangeCount = selectedCellsStorage.Count; if (oldItemIndex >= 0) { int itemIndex = oldItemIndex; for (int i = 0; i < replacedItemCount; i++) { object newItem = newItems[i]; if (selectedItemsStorage.Contains(itemIndex)) { this.UnselectItems(new SelectionRangeWithItems(itemIndex, oldItems[i])); this.SelectItems(new SelectionRangeWithItems(itemIndex, newItem)); } SelectionCellRange replacedCellRange = new SelectionCellRange( new SelectionRange(itemIndex), new SelectionRange(0, int.MaxValue - 1)); for (int j = 0; j < cellRangeCount; j++) { SelectionCellRangeWithItems cellRangeWithItems = selectedCellsStorage[j]; SelectionCellRange cellRange = cellRangeWithItems.CellRange; if (!cellRange.Intersect(replacedCellRange).IsEmpty) { object[] items = cellRangeWithItems.ItemRangeWithItems.Items; if (items != null) { items[cellRange.ItemRange.GetOffsetFromItemIndex(itemIndex)] = newItem; } } } itemIndex++; } } else { CollectionView sourceItems = m_owner.Items; for (int i = 0; i < replacedItemCount; i++) { object newItem = newItems[i]; int itemIndex = sourceItems.IndexOf(newItem); if (itemIndex < 0) { continue; } if (selectedItemsStorage.Contains(itemIndex)) { this.UnselectItems(new SelectionRangeWithItems(itemIndex, oldItems[i])); this.SelectItems(new SelectionRangeWithItems(itemIndex, newItem)); } SelectionCellRange replacedCellRange = new SelectionCellRange( new SelectionRange(itemIndex), new SelectionRange(0, int.MaxValue - 1)); for (int j = 0; j < cellRangeCount; j++) { SelectionCellRangeWithItems cellRangeWithItems = selectedCellsStorage[j]; SelectionCellRange cellRange = cellRangeWithItems.CellRange; if (!cellRange.Intersect(replacedCellRange).IsEmpty) { object[] items = cellRangeWithItems.ItemRangeWithItems.Items; if (items != null) { items[cellRange.ItemRange.GetOffsetFromItemIndex(itemIndex)] = newItem; } } } } } }
public bool ToggleItemCellsSelection( DataGridContext dataGridContext, int itemIndex, object item ) { SelectionRange itemRange = new SelectionRange( itemIndex ); SelectionRange cellRange = new SelectionRange( 0, Math.Max( 0, dataGridContext.Columns.Count - 1 ) ); // Select all visible cells for this itemIndex SelectionCellRangeWithItems selection = new SelectionCellRangeWithItems( itemRange, new object[] { item }, cellRange ); SelectedCellsStorage tempStorage = new SelectedCellsStorage( null, 1 ); tempStorage.Add( selection ); foreach( ColumnBase column in dataGridContext.ColumnsByVisiblePosition ) { if( !column.Visible ) tempStorage.Remove( new SelectionCellRangeWithItems( itemIndex, item, column.VisiblePosition ) ); } int tempStorageCount = tempStorage.Count; bool allCellSelected = true; foreach( SelectionCellRangeWithItems allCellSelection in tempStorage ) { if( !dataGridContext.SelectedCellsStore.Contains( allCellSelection ) ) { allCellSelected = false; break; } } bool selectionDone = true; if( allCellSelected ) { foreach( SelectionCellRangeWithItems allCellSelection in tempStorage ) { selectionDone &= this.UnselectCells( dataGridContext, allCellSelection ); } } else { foreach( SelectionCellRangeWithItems allCellSelection in tempStorage ) { selectionDone &= this.SelectCells( dataGridContext, allCellSelection ); } } return selectionDone; }