Esempio n. 1
0
            public int Compare(SelectionCellRangeWithItemsWrapper item)
            {
                if (m_target == item)
                {
                    return(0);
                }

                var xri = m_target.Value;
                var yri = item.Value;

                int compare;

                compare = ItemComparer.Compare(xri.ItemRange, yri.ItemRange);
                if (compare != 0)
                {
                    Debug.Assert(((compare < 0) && ItemRangeComparer.Compare(xri.ItemRange, item) <= 0) ||
                                 ((compare > 0) && ItemRangeComparer.Compare(xri.ItemRange, item) >= 0));

                    return(compare);
                }

                Debug.Assert(ItemRangeComparer.Compare(xri.ItemRange, item) == 0);

                compare = ItemComparer.Compare(xri.CellRange.ColumnRange, yri.CellRange.ColumnRange);
                if (compare != 0)
                {
                    return(compare);
                }

                Debug.Assert(xri.CellRange == yri.CellRange);

                return(m_target.Index - item.Index);
            }
Esempio n. 2
0
        private IEnumerable <int> IndexOfOverlap(SelectionCellRange target)
        {
            if ((m_sortedRanges.Count <= 0) || SelectedCellsStorage.IsEmpty(target))
            {
                yield break;
            }

            var comparer = new ItemRangeComparer(target.ItemRange);

            var index = this.FindIndex(comparer);

            if (index < 0)
            {
                yield break;
            }

            while (index > 0)
            {
                if (comparer.Compare(m_sortedRanges[index - 1]) != 0)
                {
                    break;
                }

                index--;
            }

            for (int i = index; i < m_sortedRanges.Count; i++)
            {
                var wrapper = m_sortedRanges[i];
                if (comparer.Compare(wrapper) != 0)
                {
                    break;
                }

                var currentRangeWithItems = wrapper.Value;
                var overlap = target.Intersect(currentRangeWithItems.CellRange);

                if (!SelectedCellsStorage.IsEmpty(overlap))
                {
                    yield return(wrapper.Index);
                }
            }
        }
Esempio n. 3
0
 public int Compare(SelectionCellRangeWithItemsWrapper item)
 {
     return(ItemRangeComparer.Compare(m_target, item));
 }
Esempio n. 4
0
        public void OffsetIndex(int startIndex, int offset)
        {
            if (m_sortedRanges.Count == 0)
            {
                return;
            }

            // Used to offset index after an add or remove from the data source of the grid.
            var offsetRange = new SelectionRange(startIndex, startIndex + Math.Abs(offset) - 1);
            var comparer    = new ItemRangeComparer(offsetRange);

            // Find the first range that is greater or that overlaps the target range.
            var index = this.FindIndex(comparer);

            if (index < 0)
            {
                index = ~index;
            }
            else
            {
                while (index > 0)
                {
                    if (comparer.Compare(m_sortedRanges[index - 1]) != 0)
                    {
                        break;
                    }

                    index--;
                }
            }

            var matches = m_sortedRanges.Skip(index).OrderByDescending(item => item.Index).ToList();

            // Adjust the range of all ranges greater or that overlaps the target range.
            foreach (var currentRangeWithItemsWrapper in matches)
            {
                Debug.Assert(comparer.Compare(currentRangeWithItemsWrapper) <= 0);

                var currentRangeIndex     = currentRangeWithItemsWrapper.Index;
                var currentRangeWithItems = currentRangeWithItemsWrapper.Value;
                var currentRange          = currentRangeWithItems.ItemRange;
                var currentRangeItems     = currentRangeWithItems.ItemRangeWithItems.Items;

                Debug.Assert(!(offsetRange > currentRange));

                var currentRangeIntersection = currentRange.Intersect(offsetRange);

                // The range overlaps.
                if (!currentRangeIntersection.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);

                    SelectionRange topRange;
                    SelectionRange bottomRange;

                    if (currentRange.StartIndex > currentRange.EndIndex)
                    {
                        if (currentRangeIntersection.EndIndex == currentRange.EndIndex)
                        {
                            this[currentRangeIndex] = new SelectionCellRangeWithItems(
                                new SelectionRange(currentRange.StartIndex + offset, currentRange.EndIndex + offset),
                                currentRangeItems,
                                currentRangeWithItems.ColumnRange);
                            continue;
                        }
                        else
                        {
                            topRange    = new SelectionRange(currentRange.StartIndex + offset, startIndex + offset);
                            bottomRange = new SelectionRange(startIndex - 1, currentRange.EndIndex);
                        }
                    }
                    else
                    {
                        if (currentRangeIntersection.StartIndex == currentRange.StartIndex)
                        {
                            this[currentRangeIndex] = new SelectionCellRangeWithItems(
                                new SelectionRange(currentRange.StartIndex + offset, currentRange.EndIndex + offset),
                                currentRangeItems,
                                currentRangeWithItems.ColumnRange);
                            continue;
                        }
                        else
                        {
                            topRange    = new SelectionRange(currentRange.StartIndex, startIndex - 1);
                            bottomRange = new SelectionRange(startIndex + offset, currentRange.EndIndex + offset);
                        }
                    }

                    object[] topItems    = null;
                    object[] bottomItems = null;

                    if (currentRangeItems != null)
                    {
                        topItems = new object[topRange.Length];
                        Array.Copy(currentRangeItems, 0, topItems, 0, topItems.Length);

                        bottomItems = new object[bottomRange.Length];
                        Array.Copy(currentRangeItems, topItems.Length, bottomItems, 0, bottomItems.Length);
                    }

                    this[currentRangeIndex] = new SelectionCellRangeWithItems(topRange, topItems, currentRangeWithItems.ColumnRange);
                    this.Insert(currentRangeIndex + 1, new SelectionCellRangeWithItems(bottomRange, bottomItems, currentRangeWithItems.ColumnRange), true);
                }
                // The range is greater.
                else
                {
                    this[currentRangeIndex] = new SelectionCellRangeWithItems(
                        new SelectionRange(currentRange.StartIndex + offset, currentRange.EndIndex + offset),
                        currentRangeItems,
                        currentRangeWithItems.ColumnRange);
                }
            }
        }