Click here to Skip to main content
15,897,704 members
Articles / Desktop Programming / Windows Forms

Storm - the world's best IDE framework for .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (82 votes)
4 Feb 2010LGPL311 min read 278.5K   6.5K   340  
Create fast, flexible, and extensible IDE applications easily with Storm - it takes nearly no code at all!
//
//    ___ _____ ___  ___ __  __ 
//   / __|_   _/ _ \| _ \  \/  |
//   \__ \ | || (_) |   / |\/| |
//   |___/ |_| \___/|_|_\_|  |_|
// 
//   Storm.TabControl.dll
//   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//     Storm.TabControl.dll was created under the LGPL license.
//     It was based on another .dll and evolved from that one.
//     The original .dll was created by Hadi Eskandari.
//
//   What is Storm?
//   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//     Storm is a set of dynamic link libraries used in Theodor
//     "Vestras" Storm Kristensen's application "Moonlite".
//     
//
//   Thanks:
//   ¯¯¯¯¯¯¯
//     - Hadi Eskandari for creating and publishing the library.
//
//
//   Copyright (c) Theodor "Vestras" Storm Kristensen 2009
//   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//

namespace Storm.TabControl
{
    using System;
    using System.ComponentModel;
    using Storm.TabControl.Helpers;

    public class TabStripItemCollection : EventCollection
    {
        #region Members
        [Browsable(false)]
        public event CollectionChangeEventHandler CollectionChanged;

        private int lockUpdate;
        #endregion

        #region Ctor
        public TabStripItemCollection()
        {
            lockUpdate = 0;
        }
        #endregion

        #region Props
        public TabStripItem this[int index]
        {
            get
            {
                if (index < 0 || List.Count - 1 < index)
                    return null;

                return (TabStripItem)List[index];
            }
            set
            {
                List[index] = value;
            }
        }

        [Browsable(false)]
        public virtual int DrawnCount
        {
            get
            {
                int count = Count, res = 0;
                if (count == 0) return 0;
                for (int n = 0; n < count; n++)
                {
                    if (this[n].IsDrawn)
                        res++;
                }
                return res;
            }
        }

        public virtual TabStripItem LastVisible
        {
            get
            {
                for (int n = Count - 1; n > 0; n--)
                {
                    if (this[n].Visible)
                        return this[n];
                }

                return null;
            }
        }

        public virtual TabStripItem FirstVisible
        {
            get
            {
                for (int n = 0; n < Count; n++)
                {
                    if (this[n].Visible)
                        return this[n];
                }

                return null;
            }
        }

        [Browsable(false)]
        public virtual int VisibleCount
        {
            get
            {
                int count = Count, res = 0;
                if (count == 0) return 0;
                for (int n = 0; n < count; n++)
                {
                    if (this[n].Visible)
                        res++;
                }
                return res;
            }
        }
        #endregion

        #region Methods
        protected virtual void OnCollectionChanged(CollectionChangeEventArgs e)
        {
            if (CollectionChanged != null)
                CollectionChanged(this, e);
        }

        protected virtual void BeginUpdate()
        {
            lockUpdate++;
        }

        protected virtual void EndUpdate()
        {
            if (--lockUpdate == 0)
                OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, null));
        }

        public virtual void AddRange(TabStripItem[] items)
        {
            BeginUpdate();
            try
            {
                foreach (TabStripItem item in items)
                {
                    List.Add(item);
                }
            }
            finally
            {
                EndUpdate();
            }
        }

        public virtual void Assign(TabStripItemCollection collection)
        {
            BeginUpdate();
            try
            {
                Clear();
                for (int n = 0; n < collection.Count; n++)
                {
                    TabStripItem item = collection[n];
                    TabStripItem newItem = new TabStripItem();
                    newItem.Assign(item);
                    Add(newItem);
                }
            }
            finally
            {
                EndUpdate();
            }
        }

        public virtual int Add(TabStripItem item)
        {
            int res = IndexOf(item);
            if (res == -1) res = List.Add(item);
            return res;
        }

        public virtual void Remove(TabStripItem item)
        {
            if (List.Contains(item))
                List.Remove(item);
        }

        public virtual TabStripItem MoveTo(int newIndex, TabStripItem item)
        {
            int currentIndex = List.IndexOf(item);
            if (currentIndex >= 0)
            {
                RemoveAt(currentIndex);
                Insert(0, item);

                return item;
            }

            return null;
        }

        public virtual int IndexOf(TabStripItem item)
        {
            return List.IndexOf(item);
        }

        public virtual bool Contains(TabStripItem item)
        {
            return List.Contains(item);
        }

        public virtual void Insert(int index, TabStripItem item)
        {
            if (Contains(item)) return;
            List.Insert(index, item);
        }

        protected override void OnInsertComplete(int index, object item)
        {
            TabStripItem itm = item as TabStripItem;
            itm.Changed += new EventHandler(OnItem_Changed);
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, item));
        }

        protected override void OnRemove(int index, object item)
        {
            base.OnRemove(index, item);
            TabStripItem itm = item as TabStripItem;
            itm.Changed -= new EventHandler(OnItem_Changed);
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, item));
        }

        protected override void OnClear()
        {
            if (Count == 0) return;
            BeginUpdate();
            try
            {
                for (int n = Count - 1; n >= 0; n--)
                {
                    RemoveAt(n);
                }
            }
            finally
            {
                EndUpdate();
            }
        }

        protected virtual void OnItem_Changed(object sender, EventArgs e)
        {
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, sender));
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)



Comments and Discussions