/// <summary>
        /// Removes all items from the collection.
        /// </summary>
        public void Clear()
        {
            _listView.BeginUpdate();

            //avoid firing one SelectedItemsChanged event for every selected item
            if (_listView.SelectedItems.Count > 0)
            {
                _listView.SelectedItems.Clear();
            }

            for (int index = 0; index < _data.Count; ++index)
            {
                TreeListViewItem item = this[index];

                item.Selected      = false;
                item.Focused       = false;
                item.OwnerListView = null;

                for (int subIndex = 0; subIndex < item.SubItems.Count; ++subIndex)
                {
                    if (item.SubItems[subIndex].ItemControl != null)
                    {
                        item.SubItems[subIndex].ItemControl.Parent  = null;
                        item.SubItems[subIndex].ItemControl.Visible = false;
                        item.SubItems[subIndex].ItemControl         = null;
                    }
                }
            }
            _data.Clear();

            _listView.EndUpdate();
        }
        /// <summary>
        /// Indicates the <see cref="TreeListViewItem"/> at the specified indexed
        /// location in the collection.  In C#, this property is the indexer for the
        /// <b>TreeListViewItemCollection</b> class.
        /// </summary>
        public TreeListViewItem this[int index]
        {
            get
            {
                return(_data[index] as TreeListViewItem);
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value", "TreeListView cannot contain null TreeListViewItems");
                }

                if (value != _data[index])
                {
                    // remove the existing item
                    TreeListViewItem item = this[index];
                    item.OwnerListView = null;

                    // and add the new item in place
                    _data[index]        = value;
                    value.OwnerListView = _listView;
                }
            }
        }
        /// <summary>
        /// Removes the specified collection of items from the collection.
        /// </summary>
        /// <param name="item">A list of items to remove from the collection.</param>
        public void RemoveRange(System.Collections.Generic.IList <TreeListViewItem> items)
        {
            if (items == null)
            {
                return;
            }

            // Aulofee customization - start. Line below added.
            _listView.BeginUpdate();

            TreeListViewItem recalculateFrom = null;

            for (int i = items.Count - 1; i >= 0; i--)
            {
                TreeListViewItem newRecalculateFrom = RemoveInternal(items[i]);
                if (recalculateFrom == null || newRecalculateFrom.Y < recalculateFrom.Y)
                {
                    recalculateFrom = newRecalculateFrom;
                }
            }

            _listView.RecalculateItemPositions(recalculateFrom);

            // Aulofee customization - start. Line below added.
            _listView.EndUpdate();
        }
        /// <summary>
        /// Inserts an existing <see cref="TreeListViewItem"/> object to the collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index location where the item is inserted.</param>
        /// <param name="item">The <b>TreeListViewItem</b> object to add to the collection.</param>
        public void Insert(int index, TreeListViewItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (_data.Count != 0 && index > 0)
            {
                TreeListViewItem previousItem = _data[index - 1] as TreeListViewItem;
                item.InternalPreviousItem     = previousItem;
                previousItem.InternalNextItem = item;
            }
            if (_data.Count - 1 >= index)
            {
                TreeListViewItem nextItem = _data[index] as TreeListViewItem;
                item.InternalNextItem = nextItem;
            }

            lock (_data.SyncRoot)
                _data.Insert(index, item);

            item.InternalParentItem = _owningItem;
            SetTreeListViewOwnerRecursive(item, _listView);
        }
        internal TreeListViewItem GetItemAt(int y)
        {
            TreeListViewItem lastItem = null;

            if (_items != null)
            {
                for (int index = 0; index < _items.Count; ++index)
                {
                    TreeListViewItem item = _items[index];

                    if (y >= item.Y && y < (item.Y + item.Height) && item._filter.Belongs(item))                    // base case if it is this node
                    {
                        return(item);
                    }

                    if (y < item.Y && lastItem != null && lastItem.Expanded)                    // must be a child of the last item
                    {
                        return(lastItem.GetItemAt(y));
                    }

                    lastItem = item;
                }

                if (lastItem != null && lastItem.Expanded)
                {
                    return(lastItem.GetItemAt(y));
                }
            }

            return(null);
        }
        /// <summary>
        /// Adds an existing <see cref="TreeListViewItem"/> object to the collection.
        /// </summary>
        /// <param name="item">The <b>TreeListViewItem</b> object to add to the collection.</param>
        /// <returns>The position into which the new element was inserted.</returns>
        public int Add(TreeListViewItem item)
        {
            int index = _data.Count;

            Insert(index, item);

            return(index);
        }
        /// <summary>
        /// Inserts an item to the collection with the specified properties at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index location where the item is inserted.</param>
        /// <param name="text">The text to display.</param>
        /// <param name="imageIndex">The index of the image to display.</param>
        /// <param name="selectedImageIndex">The index of the image to display when the item is selected.</param>
        /// <returns>The <see cref="TreeListViewItem"/> that was added to the collection.</returns>
        public TreeListViewItem Insert(int index, string text, int imageIndex, int selectedImageIndex)
        {
            TreeListViewItem item = new TreeListViewItem(text, imageIndex, selectedImageIndex);

            Insert(index, item);

            return(item);
        }
        /// <summary>
        /// Inserts an item to the collection with the specified text at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index location where the item is inserted.</param>
        /// <param name="text">The text to display.</param>
        /// <returns>The <see cref="TreeListViewItem"/> that was added to the collection.</returns>
        public TreeListViewItem Insert(int index, string text)
        {
            TreeListViewItem item = new TreeListViewItem(text);

            Insert(index, item);

            return(item);
        }
        private int AutoSizeItems(TreeListViewItemCollection items)
        {
            int colIndex        = this.Index;
            int colDisplayIndex = this.DisplayIndex;
            int twid            = 0;
            int mwid            = 0;
            int baseIndent      = 0;

            if (colDisplayIndex == 0)
            {
                if (ListView.ShowPlusMinus || (ListView.ShowTreeLines && ListView.ShowRootTreeLines))
                {
                    baseIndent += 16;
                }
            }

            for (int index = 0; index < items.Count; ++index)
            {
                twid = 0;
                TreeListViewItem item = items[index];

                Font fnt = item.Font;
                if (fnt == null)
                {
                    fnt = Font;
                }

                if (item.ImageIndex > -1 || item.SelectedImageIndex > -1)
                {
                    twid += 16;
                }

                if (colDisplayIndex == 0)
                {
                    twid += baseIndent + (16 * (item.Depth - 1));
                }

                twid += GetStringWidth(item.SubItems[colIndex].Text, fnt) + 10;

                twid += 5;
                if (twid > mwid)
                {
                    mwid = twid;
                }

                if (item.HasChildren && item.Expanded)
                {
                    twid = AutoSizeItems(item.Items);
                    if (twid > mwid)
                    {
                        mwid = twid;
                    }
                }
            }

            return(mwid);
        }
        internal void SetTreeListViewOwnerRecursive(TreeListViewItem item, TreeListView treeListView)
        {
            if (item.Items.Count > 0)
            {
                foreach (TreeListViewItem childItem in item.Items)
                {
                    SetTreeListViewOwnerRecursive(childItem, treeListView);
                }
            }

            item.OwnerListView = treeListView;
        }
Ejemplo n.º 11
0
        internal TreeListViewSubItemCollection(TreeListViewItem item)
        {
            _owningItem = item;

            if (_owningItem.ListView != null)
            {
                _data = new ArrayList(_owningItem.ListView.Columns.Count);
            }
            else
            {
                _data = new ArrayList();
            }
        }
        /// <summary>
        /// Removes the specified <see cref="TreeListViewItem"/> from the collection.
        /// </summary>
        /// <param name="item">A <see cref="TreeListViewItem"/> to remove from the collection.</param>
        /// <custom type="modified">Fixed bugs (the method was throwing exceptions) and improved the method.</custom>
        public void Remove(TreeListViewItem item)
        {
            // Aulofee customization - start. Bug fixed + improvments (initial method is at the end)
            if (item == null)
            {
                return;
            }

            // Aulofee customization - start. Line below added.
            _listView.BeginUpdate();

            TreeListViewItem recalculateFrom = RemoveInternal(item);

            _listView.RecalculateItemPositions(recalculateFrom);

            // Aulofee customization - start. Line below added.
            _listView.EndUpdate();
        }
        /// <summary>
        /// Performs the filtering for the passed in <see cref="TreeListViewItem"/>.
        /// </summary>
        /// <param name="o">An object that should represent a <see cref="TreeListViewItem"/>.</param>
        /// <returns></returns>
        public bool Belongs(object o)
        {
            TreeListViewItem item = o as TreeListViewItem;

            if (o == null)
            {
                return(false);
            }

            string actual = item.SubItems[_columnIndex].Text.ToLower(CultureInfo.CurrentCulture);

            if (actual.IndexOf(_string) != -1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Creates a copy of this <see cref="TreeListViewItem"/> that is not attached to a list.
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            TreeListViewItem lvi = new TreeListViewItem();

            lvi._backColor          = _backColor;
            lvi._font               = _font;
            lvi._foreColor          = _foreColor;
            lvi._imageIndex         = _imageIndex;
            lvi._selectedImageIndex = _selectedImageIndex;
            lvi._expandedImageIndex = _expandedImageIndex;
            lvi._listView           = _listView;
            lvi._tag = _tag;

            for (int index = 0; index < _subItems.Count; ++index)
            {
                lvi._subItems[index] = _subItems[index].Clone();
            }

            return(lvi);
        }
        public string GetPath(char pathSeperatorBefore, char pathSeperatorAfter)
        {
            string           nodePath    = string.Empty;
            TreeListViewItem currentItem = this;

            if (currentItem.ParentItem == currentItem.ListView.RootItem)
            {
                nodePath = currentItem.Text;
            }
            else
            {
                bool isLastItem = true;

                while (currentItem != currentItem.ListView.RootItem)
                {
                    if (pathSeperatorAfter == '\0')
                    {
                        if (isLastItem)
                        {
                            nodePath = currentItem.Text + nodePath;
                        }
                        else
                        {
                            nodePath = currentItem.Text + pathSeperatorBefore + nodePath;
                        }
                    }
                    else
                    {
                        nodePath = pathSeperatorBefore + currentItem.Text + pathSeperatorAfter + nodePath;
                    }

                    currentItem = currentItem.ParentItem;
                    isLastItem  = false;
                }
            }

            return(nodePath);
        }
        internal TreeListViewItem RemoveInternal(TreeListViewItem item)
        {
            TreeListViewItem recalculateFrom = null;

            if (item.InternalPreviousItem != null && item.InternalNextItem != null)
            {
                item.InternalPreviousItem.InternalNextItem = item.InternalNextItem;
                item.InternalNextItem.InternalPreviousItem = item.InternalPreviousItem;
                recalculateFrom = item.InternalPreviousItem;
            }
            else if (item.InternalPreviousItem != null)
            {
                item.InternalPreviousItem.InternalNextItem = null;
                recalculateFrom = item.InternalPreviousItem;
            }
            else if (item.InternalNextItem != null)
            {
                item.InternalNextItem.InternalPreviousItem = null;
            }

            lock (_data.SyncRoot)
                _data.Remove(item);

            item.Selected           = false;
            item.Focused            = false;
            item.OwnerListView      = null;
            item.InternalParentItem = null;

            foreach (TreeListViewSubItem si in item.SubItems)
            {
                if (si.ItemControl != null)
                {
                    si.ItemControl.Visible = false;
                }
            }

            return(recalculateFrom);
        }
        /// <summary>
        /// Sorts the elements using the specified comparer.
        /// </summary>
        /// <param name="comparer">The <see cref="IComparer"/> to use when comparing elements.</param>
        /// <param name="recursiveSort">Whether to sort these items child items as well.</param>
        public void Sort(IComparer comparer, bool recursiveSort)
        {
            try
            {
                _data.Sort(comparer);

                TreeListViewItem lastItem = null;
                TreeListViewItem curItem  = null;

                for (int index = 0; index < _data.Count; ++index)
                {
                    curItem = this[index];

                    curItem.InternalPreviousItem = lastItem;
                    if (lastItem != null)
                    {
                        lastItem.InternalNextItem = curItem;
                    }

                    lastItem = curItem;

                    if (recursiveSort && curItem.HasChildren)
                    {
                        curItem.Items.Sort(comparer, recursiveSort);
                    }
                }

                if (curItem != null)
                {
                    curItem.InternalNextItem = null;
                }
            }
            catch
            {
                // TODO: should likely refine this and determine the cause of the error and handle appropriately.
            }
        }
        /// <summary>
        /// Compares the two incoming <see cref="TreeListViewItem"/> elements.
        /// </summary>
        /// <param name="item1">The first item to compare.</param>
        /// <param name="item2">The second item to compare.</param>
        /// <returns>Zero if equal, -1 if item1 &lt; item2, 1 if item1 &gt; item2</returns>
        public int Compare(TreeListViewItem item1, TreeListViewItem item2)
        {
            for (int index = 0; index < _sortColumns.Length; ++index)
            {
                // cache these values so as to make all of this a little bit faster
                TreeListViewColumnHeader sortColumn = _sortColumns[index];
                int          sortColumnIndex        = _sortColumnIndices[index];
                SortOrder    sortOrder    = sortColumn.SortOrder;
                SortDataType sortDataType = sortColumn.SortDataType;

                if (item1 == null || item2 == null || sortOrder == SortOrder.None || sortDataType == SortDataType.None)
                {
                    return(0);                    // We don't know type, the order, or the data - so both objects are same for us
                }
                int n = 0;

                TreeListViewSubItem subItem1 = sortOrder == SortOrder.Ascending ? item1.SubItems[sortColumnIndex] : item2.SubItems[sortColumnIndex];
                TreeListViewSubItem subItem2 = sortOrder == SortOrder.Ascending ? item2.SubItems[sortColumnIndex] : item1.SubItems[sortColumnIndex];

                if (sortDataType == SortDataType.Custom)
                {
                    n = sortColumn.CustomSortComparer.Compare(subItem1, subItem2);
                }
                else
                {
                    n = CompareItems(subItem1.Text, subItem2.Text, sortDataType);
                }

                if (n != 0)
                {
                    return(n);
                }
            }

            return(0);
        }
 /// <summary>
 /// Returns the index within the collection of the specified item.
 /// </summary>
 /// <param name="item">A <see cref="TreeListViewItem"/> representing the item to locate in the collection.</param>
 /// <returns>The zero-based index of the item's location in the collection.  If the item is not located in the collection the return value is negative one (-1).</returns>
 public int IndexOf(TreeListViewItem item)
 {
     return(_data.IndexOf(item));
 }
 /// <summary>
 /// Determines whether the specified item is located in the collection.
 /// </summary>
 /// <param name="item">A <see cref="TreeListViewItem"/> representing the item to locate in the collection.</param>
 /// <returns><b>true</b> if the column is contained in the collection; otherwise, <b>false</b>.</returns>
 public bool Contains(TreeListViewItem item)
 {
     return(_data.Contains(item));
 }
 internal TreeListViewItemCollection(TreeListViewItem owningItem)
 {
     _owningItem = owningItem;
 }