Esempio n. 1
0
            private static int Compare(SelectionRange xr, SelectionRange yr)
            {
                int xs, xe, ys, ye;

                ItemComparer.GetBounds(xr, out xs, out xe);
                ItemComparer.GetBounds(yr, out ys, out ye);

                if (xs < ys)
                {
                    return(-1);
                }
                else if (xs > ys)
                {
                    return(1);
                }

                Debug.Assert(!xr.Intersect(yr).IsEmpty);

                if (xe < ye)
                {
                    return(-1);
                }
                else if (xe > ye)
                {
                    return(1);
                }

                Debug.Assert(xr == yr);
                return(0);
            }
        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);
                }
            }
        }
Esempio n. 3
0
            internal static int Compare(SelectionRange range, SelectionCellRangeWithItemsWrapper wrapper)
            {
                var itemRange = wrapper.Value.ItemRange;

                if (range < itemRange)
                {
                    return(-1);
                }

                if (range > itemRange)
                {
                    return(1);
                }

                Debug.Assert(!range.Intersect(itemRange).IsEmpty);

                return(0);
            }
Esempio n. 4
0
        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;
                    }

                    Debug.WriteLine("Selection : Adding cell : (" + startSourceDataItemIndex.ToString() + " - " + endSourceDataItemIndex.ToString() + ") - ("
                                    + intersectionSelectionRange.StartIndex.ToString() + " - " + intersectionSelectionRange.EndIndex.ToString() + ")");

                    selectionChangerManager.SelectCells(
                        sourceContext,
                        new SelectionCellRangeWithItems(new SelectionRange(startSourceDataItemIndex, endSourceDataItemIndex), null, intersectionSelectionRange));
                }
            }
            else
            {
                Debug.WriteLine("Selection : Adding item : " + startSourceDataItemIndex.ToString() + " - " + endSourceDataItemIndex.ToString());

                selectionChangerManager.SelectItems(
                    sourceContext,
                    new SelectionRangeWithItems(new SelectionRange(startSourceDataItemIndex, endSourceDataItemIndex), null));
            }
        }
        public SelectionCellRange Intersect(SelectionCellRange range)
        {
            SelectionCellRange cellRangeIntersection = SelectionCellRange.Empty;

            SelectionRange itemRange             = range.ItemRange;
            SelectionRange itemRangeIntersection = m_itemRange.Intersect(itemRange);

            if (itemRangeIntersection.IsEmpty)
            {
                return(SelectionCellRange.Empty);
            }

            SelectionRange columnRange             = range.ColumnRange;
            SelectionRange columnRangeIntersection = m_columnRange.Intersect(columnRange);

            if (columnRangeIntersection.IsEmpty)
            {
                return(SelectionCellRange.Empty);
            }

            return(new SelectionCellRange(itemRangeIntersection, columnRangeIntersection));
        }
        public object[] GetItems(SelectionRange range)
        {
            if (m_items == null)
            {
                return(null);
            }

            if (range.IsEmpty)
            {
                return(new object[0]);
            }

            int  startOffset;
            bool reverseOrder;

            SelectionRange rangeIntersection = m_range.Intersect(range);

            if (m_range.StartIndex > m_range.EndIndex)
            {
                startOffset  = m_range.StartIndex - rangeIntersection.StartIndex;
                reverseOrder = range.StartIndex < range.EndIndex;
            }
            else
            {
                startOffset  = rangeIntersection.StartIndex - m_range.StartIndex;
                reverseOrder = range.StartIndex > range.EndIndex;
            }

            object[] items = new object[rangeIntersection.Length];
            Array.Copy(m_items, startOffset, items, 0, items.Length);

            if (reverseOrder)
            {
                Array.Reverse(items);
            }

            return(items);
        }
        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 bool UnselectItems(SelectionRangeWithItems rangeWithItems)
        {
            if (!(m_owner.ItemsSourceCollection is DataGridVirtualizingCollectionViewBase))
            {
                if (m_toDeferSelect.Count > 0)
                {
                    foreach (object item in rangeWithItems.Items)
                    {
                        m_toDeferSelect.Remove(item);
                    }
                }
            }

            SelectionRange range = rangeWithItems.Range;

            if (range.IsEmpty)
            {
                // We have no index we have to remove based on item
                bool selectionChanged = false;

                List <SelectionRangeWithItems> itemsRangeToRemove = new List <SelectionRangeWithItems>();
                List <object> itemsToUnselect = new List <object>(rangeWithItems.Items);
                int           count           = itemsToUnselect.Count;

                for (int i = count - 1; i >= 0; i--)
                {
                    object itemToUnselect = itemsToUnselect[i];
                    bool   selectionAdded = false;

                    foreach (SelectionRangeWithItems existingSelectionRangeWithItems in m_itemsToSelect)
                    {
                        int index = Array.IndexOf(existingSelectionRangeWithItems.Items, itemToUnselect);

                        if (index > -1)
                        {
                            selectionAdded = true;

                            itemsRangeToRemove.Add(
                                new SelectionRangeWithItems(
                                    existingSelectionRangeWithItems.Range.GetIndexFromItemOffset(index),
                                    itemToUnselect));
                        }
                    }

                    if (selectionAdded)
                    {
                        itemsToUnselect.RemoveAt(i);
                    }
                }

                // Remove the currently unselected item from the new range to select
                foreach (SelectionRangeWithItems itemRangeToRemove in itemsRangeToRemove)
                {
                    selectionChanged |= m_itemsToSelect.Remove(itemRangeToRemove);
                }

                count = itemsToUnselect.Count;

                for (int i = 0; i < count; i++)
                {
                    object itemToUnselect = itemsToUnselect[i];

                    foreach (SelectionRangeWithItems existingSelectionRangeWithItems in m_owner.SelectedItemsStore)
                    {
                        int index = Array.IndexOf(existingSelectionRangeWithItems.Items, itemToUnselect);

                        if (index >= 0)
                        {
                            index = existingSelectionRangeWithItems.Range.GetIndexFromItemOffset(index);

                            if (!m_itemsToUnselect.Contains(index))
                            {
                                selectionChanged = true;
                                m_itemsToUnselect.Add(new SelectionRangeWithItems(index, itemToUnselect));
                            }
                        }
                    }
                }

                return(selectionChanged);
            }

            if (range.Length == 1)
            {
                if (!m_itemsToSelect.Remove(rangeWithItems))
                {
                    if (!m_owner.SelectedItemsStore.Contains(range))
                    {
                        return(false);
                    }

                    if (m_itemsToUnselect.Contains(range))
                    {
                        return(false);
                    }

                    m_itemsToUnselect.Add(rangeWithItems);
                }

                return(true);
            }
            else
            {
                SelectedItemsStorage tempStorage = new SelectedItemsStorage(m_owner);
                tempStorage.Add(rangeWithItems);

                // Remove the currently selected item from the new range to select
                foreach (SelectionRangeWithItems existingSelectionRangeWithItems in m_itemsToSelect)
                {
                    if (!range.Intersect(existingSelectionRangeWithItems.Range).IsEmpty)
                    {
                        tempStorage.Remove(existingSelectionRangeWithItems);
                    }
                }

                bool selectionChanged = m_itemsToSelect.Remove(rangeWithItems);

                if (tempStorage.Count > 0)
                {
                    selectionChanged = true;

                    foreach (SelectionRangeWithItems rangeWithItemsToAdd in tempStorage)
                    {
                        Debug.Assert(!m_itemsToUnselect.Contains(rangeWithItemsToAdd.Range));
                        m_itemsToUnselect.Add(rangeWithItemsToAdd);
                    }
                }

                return(selectionChanged);
            }
        }
Esempio n. 9
0
        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 Remove(SelectionRangeWithItems rangeWithItemsToRemove)
        {
            bool           removed       = false;
            SelectionRange rangeToRemove = rangeWithItemsToRemove.Range;

            object[] itemsToRemove        = rangeWithItemsToRemove.Items;
            int      itemsToRemoveCount   = (itemsToRemove == null) ? 0 : itemsToRemove.Length;
            int      itemOffsetToRemove   = 0;
            bool     rangeToRemoveIsEmpty = rangeToRemove.IsEmpty;

            do
            {
                for (int i = m_list.Count - 1; i >= 0; i--)
                {
                    SelectionRangeWithItems rangeWithItems = m_list[i];
                    SelectionRange          range          = rangeWithItems.Range;
                    SelectionRange          rangeIntersection;
                    object[] oldRangeItems = rangeWithItems.Items;

                    if (rangeToRemoveIsEmpty)
                    {
                        int itemOffset = Array.IndexOf(oldRangeItems, itemsToRemove[itemOffsetToRemove]);

                        if (itemOffset == -1)
                        {
                            continue;
                        }

                        rangeIntersection = new SelectionRange(range.GetIndexFromItemOffset(itemOffset));
                    }
                    else
                    {
                        rangeIntersection = range.Intersect(rangeToRemove);

                        if (rangeIntersection.IsEmpty)
                        {
                            continue;
                        }

                        if (!rangeWithItems.IsItemsEqual(rangeIntersection, rangeWithItemsToRemove))
                        {
                            continue;
                        }
                    }

                    removed       = true;
                    m_itemsCount -= rangeIntersection.Length;
                    SelectionRange[] newRanges = range.Exclude(rangeIntersection);

                    if (newRanges.Length == 0)
                    {
                        m_list.RemoveAt(i);
                    }
                    else
                    {
                        SelectionRange newRange = newRanges[0];
                        m_list[i] = new SelectionRangeWithItems(newRange, rangeWithItems.GetItems(newRange));

                        if (newRanges.Length > 1)
                        {
                            Debug.Assert(newRanges.Length == 2);
                            newRange = newRanges[1];
                            m_list.Insert(i + 1, new SelectionRangeWithItems(newRange, rangeWithItems.GetItems(newRange)));
                        }
                    }
                }

                itemOffsetToRemove++;
            } while((rangeToRemoveIsEmpty) && (itemOffsetToRemove < itemsToRemoveCount));

            return(removed);
        }