DockContentCollection.cs
上传用户:szlfmled
上传日期:2020-11-22
资源大小:978k
文件大小:4k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. namespace WeifenLuo.WinFormsUI.Docking
  6. {
  7.     public class DockContentCollection : ReadOnlyCollection<IDockContent>
  8.     {
  9.         private static List<IDockContent> _emptyList = new List<IDockContent>(0);
  10.         internal DockContentCollection()
  11.             : base(new List<IDockContent>())
  12.         {
  13.         }
  14.         internal DockContentCollection(DockPane pane)
  15.             : base(_emptyList)
  16.         {
  17.             m_dockPane = pane;
  18.         }
  19.         private DockPane m_dockPane = null;
  20.         private DockPane DockPane
  21.         {
  22.             get { return m_dockPane; }
  23.         }
  24.         public new IDockContent this[int index]
  25.         {
  26.             get
  27.             {
  28.                 if (DockPane == null)
  29.                     return Items[index] as IDockContent;
  30.                 else
  31.                     return GetVisibleContent(index);
  32.             }
  33.         }
  34.         internal int Add(IDockContent content)
  35.         {
  36. #if DEBUG
  37. if (DockPane != null)
  38. throw new InvalidOperationException();
  39. #endif
  40.             if (Contains(content))
  41.                 return IndexOf(content);
  42.             Items.Add(content);
  43.             return Count - 1;
  44.         }
  45.         internal void AddAt(IDockContent content, int index)
  46.         {
  47. #if DEBUG
  48. if (DockPane != null)
  49. throw new InvalidOperationException();
  50. #endif
  51.             if (index < 0 || index > Items.Count - 1)
  52.                 return;
  53.             if (Contains(content))
  54.                 return;
  55.             Items.Insert(index, content);
  56.         }
  57.         public new bool Contains(IDockContent content)
  58.         {
  59.             if (DockPane == null)
  60.                 return Items.Contains(content);
  61.             else
  62.                 return (GetIndexOfVisibleContents(content) != -1);
  63.         }
  64.         public new int Count
  65.         {
  66.             get
  67.             {
  68.                 if (DockPane == null)
  69.                     return base.Count;
  70.                 else
  71.                     return CountOfVisibleContents;
  72.             }
  73.         }
  74.         public new int IndexOf(IDockContent content)
  75.         {
  76.             if (DockPane == null)
  77.             {
  78.                 if (!Contains(content))
  79.                     return -1;
  80.                 else
  81.                     return Items.IndexOf(content);
  82.             }
  83.             else
  84.                 return GetIndexOfVisibleContents(content);
  85.         }
  86.         internal void Remove(IDockContent content)
  87.         {
  88.             if (DockPane != null)
  89.                 throw new InvalidOperationException();
  90.             if (!Contains(content))
  91.                 return;
  92.             Items.Remove(content);
  93.         }
  94.         private int CountOfVisibleContents
  95.         {
  96.             get
  97.             {
  98. #if DEBUG
  99. if (DockPane == null)
  100. throw new InvalidOperationException();
  101. #endif
  102.                 int count = 0;
  103.                 foreach (IDockContent content in DockPane.Contents)
  104.                 {
  105.                     if (content.DockHandler.DockState == DockPane.DockState)
  106.                         count++;
  107.                 }
  108.                 return count;
  109.             }
  110.         }
  111.         private IDockContent GetVisibleContent(int index)
  112.         {
  113. #if DEBUG
  114. if (DockPane == null)
  115. throw new InvalidOperationException();
  116. #endif
  117.             int currentIndex = -1;
  118.             foreach (IDockContent content in DockPane.Contents)
  119.             {
  120.                 if (content.DockHandler.DockState == DockPane.DockState)
  121.                     currentIndex++;
  122.                 if (currentIndex == index)
  123.                     return content;
  124.             }
  125.             throw (new ArgumentOutOfRangeException());
  126.         }
  127.         private int GetIndexOfVisibleContents(IDockContent content)
  128.         {
  129. #if DEBUG
  130. if (DockPane == null)
  131. throw new InvalidOperationException();
  132. #endif
  133.             if (content == null)
  134.                 return -1;
  135.             int index = -1;
  136.             foreach (IDockContent c in DockPane.Contents)
  137.             {
  138.                 if (c.DockHandler.DockState == DockPane.DockState)
  139.                 {
  140.                     index++;
  141.                     if (c == content)
  142.                         return index;
  143.                 }
  144.             }
  145.             return -1;
  146.         }
  147.     }
  148. }