Exemplo n.º 1
0
        private bool ListViewContains(TreeListViewItem item)
        {
            if (TreeListView == null)
            {
                return(false);
            }
            if (TreeListView.InvokeRequired)
            {
                throw(new Exception("Invoke required"));
            }
            ListView     listview     = (ListView)TreeListView;
            ListViewItem listviewitem = (ListViewItem)item;

            try{
                foreach (ListViewItem temp in listview.Items)
                {
                    if (temp == listviewitem)
                    {
                        return(true);
                    }
                }
            }
            catch {}
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds an item in the collection and in the TreeListView
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public virtual TreeListViewItem Add(string value)
        {
            TreeListViewItem item = new TreeListViewItem(value);

            Add(item);
            return(item);
        }
Exemplo n.º 3
0
        internal void RemoveAtInternal(int index)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw(new Exception("Invoke required"));
                }
            }
            TreeListViewItem item = this[index];

            if (this[index].Visible && this.TreeListView != null)
            {
                item.Hide();
            }
            List.RemoveAt(index);
            item.SetParent(null);
            // Redraw parent if no more children
            if (Count == 0 && TreeListView != null && Parent != null)
            {
                Parent.Redraw();
            }
            // Redraw new last item
            if (Count > 0 && TreeListView != null && index == Count)
            {
                this[index - 1].Redraw();
            }
            OnItemRemoved(new TreeListViewEventArgs(item, TreeListViewAction.Unknown));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds an item in the collection and in the TreeListView
        /// </summary>
        /// <param name="value"></param>
        /// <param name="imageindex"></param>
        /// <returns></returns>
        public virtual TreeListViewItem Add(string value, int imageindex)
        {
            TreeListViewItem item = new TreeListViewItem(value, imageindex);

            Add(item);
            return(item);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds an item in the collection and in the TreeListView
        /// </summary>
        /// <param name="item"></param>
        /// <returns>Index of the item in the collection</returns>
        public virtual int Add(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw(new Exception("Invoke required"));
                }
            }
            // Do not add the item if the collection owns a TreeListView recursively
            // and the item already owns a TreeListView
            if (TreeListView != null && item.ListView != null)
            {
                throw(new Exception("The Item is already in a TreeListView"));
            }
            int index = GetInsertCollectionIndex(item);

            if (index == -1)
            {
                return(-1);
            }
            if (Parent != null)
            {
                item.SetParent(Parent);
            }
            item.Items.Comparer = this.Comparer;
            int treelistviewindex = GetInsertTreeListViewIndex(item, index);

            // Insert in the ListView
            if (treelistviewindex > -1)
            {
                ListView listview = (ListView)TreeListView;
                listview.Items.Insert(treelistviewindex, (ListViewItem)item);
                if (item.IsExpanded)
                {
                    item.Expand();
                }
                item.SetIndentation();
            }
            // Insert in this collection
            if (index > -1)
            {
                List.Insert(index, item);
            }
            if (index > -1)
            {
                OnItemAdded(new TreeListViewEventArgs(item, TreeListViewAction.Unknown));
            }
            if (Count == 1 && TreeListView != null && Parent != null)
            {
                if (Parent.Visible)
                {
                    Parent.Redraw();
                }
            }
            return(index);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Check if this node is one of the parents of an item (recursively)
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public bool IsAParentOf(TreeListViewItem item)
 {
     TreeListViewItem[] parents = item.ParentsInHierarch;
     foreach (TreeListViewItem parent in parents)
     {
         if (parent == this)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 7
0
        private int GetInsertTreeListViewIndex(TreeListViewItem item, int collectionindex)
        {
            if (TreeListView == null)
            {
                return(-1);
            }
            if (TreeListView.InvokeRequired)
            {
                throw(new Exception("Invoke required"));
            }
            if (Owner != null)
            {
                int a = 0;
                a++;
            }
            int index = -1;

            // First level item (no parent)
            if (Owner != null && collectionindex != -1)
            {
                if (collectionindex == 0)
                {
                    index = 0;
                }
                else
                {
                    index =
                        this[collectionindex - 1].LastChildIndexInListView + 1;
                }
            }
            else if (Parent != null && collectionindex != -1)
            {
                if (!Parent.Visible || !Parent.IsExpanded)
                {
                    index = -1;
                }
                else
                {
                    if (collectionindex == 0)
                    {
                        index = Parent.Index + 1;
                    }
                    else
                    {
                        index =
                            Parent.Items[collectionindex - 1].LastChildIndexInListView + 1;
                    }
                }
            }
            return(index);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Remove an item from the collection and the TreeListView
        /// </summary>
        /// <param name="item"></param>
        public virtual void Remove(TreeListViewItem item)
        {
            CTreeListView treelistview = this.TreeListView;

            if (treelistview != null)
            {
                treelistview.BeginUpdate();
            }
            RemoveInternal(item);
            if (treelistview != null)
            {
                treelistview.EndUpdate();
            }
        }
Exemplo n.º 9
0
        internal void RemoveInternal(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw(new Exception("Invoke required"));
                }
            }
            int index = GetIndexOf(item);

            if (index == -1)
            {
                return;
            }
            RemoveAtInternal(index);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Transforms the collection to an array
        /// </summary>
        public TreeListViewItem[] ToArray()
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw(new Exception("Invoke required"));
                }
            }
            int size = this.Count;

            TreeListViewItem[] eltsArray = new TreeListViewItem[size];
            for (int i = 0; i < size; i++)
            {
                eltsArray[i] = this[i];
            }
            return(eltsArray);
        }
Exemplo n.º 11
0
        private int GetInsertCollectionIndex(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw(new Exception("Invoke required"));
                }
            }
            int index = -1;

            if (!_sortable)
            {
                index = Count;
            }
            else if (!Contains(item) && !ListViewContains(item))
            {
                switch (SortOrder)
                {
                // No sortorder -> at the end of the collection
                case System.Windows.Forms.SortOrder.None:
                    index = this.Count;
                    break;

                default:
                    for (int i = 0; i < this.Count; i++)
                    {
                        // Change the index for the compare if the order is descending
                        int indexcompare = i;
                        int comp         = Comparer.Compare(item, this[indexcompare]);
                        if (comp <= 0)
                        {
                            index = indexcompare;
                            break;
                        }
                    }
                    index = index == -1 ? this.Count : index;
                    break;
                }
            }
            return(index);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Gets the index of an item in the collection
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int GetIndexOf(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw(new Exception("Invoke required"));
                }
            }
            int index = -1;

            for (int i = 0; i < this.Count; i++)
            {
                if (this[i] == item)
                {
                    index = i; break;
                }
            }
            return(index);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Returns true if this collection contains an item
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public virtual bool Contains(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw(new Exception("Invoke required"));
                }
            }
            bool res = false;

            foreach (TreeListViewItem elt in this)
            {
                if (item == elt)
                {
                    res = true;
                    break;
                }
            }
            return(res);
        }
Exemplo n.º 14
0
            /// <summary>
            /// Compare two TreeListViewItems
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <returns></returns>
            public int Compare(object x, object y)
            {
                TreeListViewItem a = (TreeListViewItem)x;
                TreeListViewItem b = (TreeListViewItem)y;
                int res            = 1;

                if (Column < a.SubItems.Count && Column < b.SubItems.Count)
                {
                    res = string.CompareOrdinal(a.SubItems[Column].Text.ToUpper(), b.SubItems[Column].Text.ToUpper());
                }
                switch (SortOrder)
                {
                case SortOrder.Ascending:
                    return(res);

                case SortOrder.Descending:
                    return(-res);

                default:
                    return(1);
                }
            }
Exemplo n.º 15
0
 internal void SetParent(TreeListViewItem parent)
 {
     _parent = parent;
 }
Exemplo n.º 16
0
 /// <summary>
 /// Create a collection within a TreeListViewItem
 /// </summary>
 /// <param name="parent"></param>
 public TreeListViewItemCollection(TreeListViewItem parent)
 {
     _parent = parent;
 }
 /// <summary>
 /// Index of an item
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public int IndexOf(TreeListViewItem item)
 {
     return(base.IndexOf((ListViewItem)item));
 }
Exemplo n.º 18
0
        internal void ExpandInternal()
        {
            if (IsInATreeListView)
            {
                if (ListView.InvokeRequired)
                {
                    throw(new Exception("Invoke Required"));
                }
            }

            TreeListViewItem selItem = null;

            if (TreeListView != null)
            {
                selItem = TreeListView.FocusedItem;
            }

            // Must set ListView.checkDirection to CheckDirection.None.
            // Forbid recursively checking.
            CheckDirection oldDirection = CheckDirection.All;

            if (ListView != null)
            {
                oldDirection             = ListView._checkDirection;
                ListView._checkDirection = CheckDirection.None;
            }

            // The item wasn't expanded -> raise an event
            if (Visible && !_isexpanded && ListView != null)
            {
                TreeListViewCancelEventArgs e = new TreeListViewCancelEventArgs(
                    this, TreeListViewAction.Expand);
                ListView.RaiseBeforeExpand(e);
                if (e.Cancel)
                {
                    return;
                }
            }

            if (Visible)
            {
                for (int i = Items.Count - 1; i >= 0; i--)
                {
                    TreeListViewItem item = this.Items[i];
                    if (!item.Visible)
                    {
                        ListView LView = this.ListView;
                        LView.Items.Insert(
                            this.Index + 1, item);
                        item.SetIndentation();
                    }
                    if (item.IsExpanded)
                    {
                        item.Expand();
                    }
                }
            }
            // The item wasn't expanded -> raise an event
            if (Visible && !_isexpanded && IsInATreeListView)
            {
                this._isexpanded = true;
                TreeListViewEventArgs e = new TreeListViewEventArgs(
                    this, TreeListViewAction.Expand);
                ListView.RaiseAfterExpand(e);
                if (AfterExpand != null)
                {
                    AfterExpand(this);
                }
            }
            this._isexpanded = true;

            // Reset ListView.checkDirection
            if (IsInATreeListView)
            {
                ListView._checkDirection = oldDirection;
            }
            if (TreeListView != null && selItem != null)
            {
                if (selItem.Visible)
                {
                    selItem.Focused = true;
                }
            }
        }
Exemplo n.º 19
0
        internal void CollapseInternal()
        {
            if (IsInATreeListView)
            {
                if (ListView.InvokeRequired)
                {
                    throw(new Exception("Invoke Required"));
                }
            }
            TreeListViewItem selItem = null;

            if (TreeListView != null)
            {
                selItem = TreeListView.FocusedItem;
            }
            // The item was expanded -> raise an event
            if (Visible && _isexpanded && ListView != null)
            {
                TreeListViewCancelEventArgs e = new TreeListViewCancelEventArgs(
                    this, TreeListViewAction.Collapse);
                ListView.RaiseBeforeCollapse(e);
                if (e.Cancel)
                {
                    return;
                }
            }

            // Collapse
            if (this.Visible)
            {
                foreach (TreeListViewItem item in Items)
                {
                    item.Hide();
                }
            }

            // The item was expanded -> raise an event
            if (Visible && _isexpanded && IsInATreeListView)
            {
                this._isexpanded = false;
                TreeListViewEventArgs e = new TreeListViewEventArgs(
                    this, TreeListViewAction.Collapse);
                ListView.RaiseAfterCollapse(e);
                if (AfterCollapse != null)
                {
                    AfterCollapse(this);
                }
            }
            this._isexpanded = false;
            if (IsInATreeListView && selItem != null)
            {
                if (selItem.Visible)
                {
                    selItem.Focused = true;
                }
                else
                {
                    ListView listview = (ListView)TreeListView;
                    listview.SelectedItems.Clear();
                    TreeListViewItem[] items = selItem.ParentsInHierarch;
                    for (int i = items.Length - 1; i >= 0; i--)
                    {
                        if (items[i].Visible)
                        {
                            items[i].Focused = true;
                            break;
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Returns true if the specified item is in the collection
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public bool Contains(TreeListViewItem item)
 {
     return(base.Contains((ListViewItem)item));
 }