Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.WinFormsUI/2.3.1/WinFormsUI-2.3.1/Docking/AutoHideStripBase.cs @ 4695

Last change on this file since 4695 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 13.4 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Diagnostics.CodeAnalysis;
5using System.Drawing;
6using System.Drawing.Drawing2D;
7using System.Windows.Forms;
8
9namespace WeifenLuo.WinFormsUI.Docking {
10  public abstract partial class AutoHideStripBase : Control {
11    [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
12    protected class Tab : IDisposable {
13      private IDockContent m_content;
14
15      protected internal Tab(IDockContent content) {
16        m_content = content;
17      }
18
19      ~Tab() {
20        Dispose(false);
21      }
22
23      public IDockContent Content {
24        get { return m_content; }
25      }
26
27      public void Dispose() {
28        Dispose(true);
29        GC.SuppressFinalize(this);
30      }
31
32      protected virtual void Dispose(bool disposing) {
33      }
34    }
35
36    [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
37    protected sealed class TabCollection : IEnumerable<Tab> {
38      #region IEnumerable Members
39      IEnumerator<Tab> IEnumerable<Tab>.GetEnumerator() {
40        for (int i = 0; i < Count; i++)
41          yield return this[i];
42      }
43
44      IEnumerator IEnumerable.GetEnumerator() {
45        for (int i = 0; i < Count; i++)
46          yield return this[i];
47      }
48      #endregion
49
50      internal TabCollection(DockPane pane) {
51        m_dockPane = pane;
52      }
53
54      private DockPane m_dockPane = null;
55      public DockPane DockPane {
56        get { return m_dockPane; }
57      }
58
59      public DockPanel DockPanel {
60        get { return DockPane.DockPanel; }
61      }
62
63      public int Count {
64        get { return DockPane.DisplayingContents.Count; }
65      }
66
67      public Tab this[int index] {
68        get {
69          IDockContent content = DockPane.DisplayingContents[index];
70          if (content == null)
71            throw (new ArgumentOutOfRangeException("index"));
72          if (content.DockHandler.AutoHideTab == null)
73            content.DockHandler.AutoHideTab = (DockPanel.AutoHideStripControl.CreateTab(content));
74          return content.DockHandler.AutoHideTab as Tab;
75        }
76      }
77
78      public bool Contains(Tab tab) {
79        return (IndexOf(tab) != -1);
80      }
81
82      public bool Contains(IDockContent content) {
83        return (IndexOf(content) != -1);
84      }
85
86      public int IndexOf(Tab tab) {
87        if (tab == null)
88          return -1;
89
90        return IndexOf(tab.Content);
91      }
92
93      public int IndexOf(IDockContent content) {
94        return DockPane.DisplayingContents.IndexOf(content);
95      }
96    }
97
98    [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
99    protected class Pane : IDisposable {
100      private DockPane m_dockPane;
101
102      protected internal Pane(DockPane dockPane) {
103        m_dockPane = dockPane;
104      }
105
106      ~Pane() {
107        Dispose(false);
108      }
109
110      public DockPane DockPane {
111        get { return m_dockPane; }
112      }
113
114      public TabCollection AutoHideTabs {
115        get {
116          if (DockPane.AutoHideTabs == null)
117            DockPane.AutoHideTabs = new TabCollection(DockPane);
118          return DockPane.AutoHideTabs as TabCollection;
119        }
120      }
121
122      public void Dispose() {
123        Dispose(true);
124        GC.SuppressFinalize(this);
125      }
126
127      protected virtual void Dispose(bool disposing) {
128      }
129    }
130
131    [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
132    protected sealed class PaneCollection : IEnumerable<Pane> {
133      private class AutoHideState {
134        public DockState m_dockState;
135        public bool m_selected = false;
136
137        public AutoHideState(DockState dockState) {
138          m_dockState = dockState;
139        }
140
141        public DockState DockState {
142          get { return m_dockState; }
143        }
144
145        public bool Selected {
146          get { return m_selected; }
147          set { m_selected = value; }
148        }
149      }
150
151      private class AutoHideStateCollection {
152        private AutoHideState[] m_states;
153
154        public AutoHideStateCollection() {
155          m_states = new AutoHideState[]  {
156                        new AutoHideState(DockState.DockTopAutoHide),
157                        new AutoHideState(DockState.DockBottomAutoHide),
158                        new AutoHideState(DockState.DockLeftAutoHide),
159                        new AutoHideState(DockState.DockRightAutoHide)
160                      };
161        }
162
163        public AutoHideState this[DockState dockState] {
164          get {
165            for (int i = 0; i < m_states.Length; i++) {
166              if (m_states[i].DockState == dockState)
167                return m_states[i];
168            }
169            throw new ArgumentOutOfRangeException("dockState");
170          }
171        }
172
173        public bool ContainsPane(DockPane pane) {
174          if (pane.IsHidden)
175            return false;
176
177          for (int i = 0; i < m_states.Length; i++) {
178            if (m_states[i].DockState == pane.DockState && m_states[i].Selected)
179              return true;
180          }
181          return false;
182        }
183      }
184
185      internal PaneCollection(DockPanel panel, DockState dockState) {
186        m_dockPanel = panel;
187        m_states = new AutoHideStateCollection();
188        States[DockState.DockTopAutoHide].Selected = (dockState == DockState.DockTopAutoHide);
189        States[DockState.DockBottomAutoHide].Selected = (dockState == DockState.DockBottomAutoHide);
190        States[DockState.DockLeftAutoHide].Selected = (dockState == DockState.DockLeftAutoHide);
191        States[DockState.DockRightAutoHide].Selected = (dockState == DockState.DockRightAutoHide);
192      }
193
194      private DockPanel m_dockPanel;
195      public DockPanel DockPanel {
196        get { return m_dockPanel; }
197      }
198
199      private AutoHideStateCollection m_states;
200      private AutoHideStateCollection States {
201        get { return m_states; }
202      }
203
204      public int Count {
205        get {
206          int count = 0;
207          foreach (DockPane pane in DockPanel.Panes) {
208            if (States.ContainsPane(pane))
209              count++;
210          }
211
212          return count;
213        }
214      }
215
216      public Pane this[int index] {
217        get {
218          int count = 0;
219          foreach (DockPane pane in DockPanel.Panes) {
220            if (!States.ContainsPane(pane))
221              continue;
222
223            if (count == index) {
224              if (pane.AutoHidePane == null)
225                pane.AutoHidePane = DockPanel.AutoHideStripControl.CreatePane(pane);
226              return pane.AutoHidePane as Pane;
227            }
228
229            count++;
230          }
231          throw new ArgumentOutOfRangeException("index");
232        }
233      }
234
235      public bool Contains(Pane pane) {
236        return (IndexOf(pane) != -1);
237      }
238
239      public int IndexOf(Pane pane) {
240        if (pane == null)
241          return -1;
242
243        int index = 0;
244        foreach (DockPane dockPane in DockPanel.Panes) {
245          if (!States.ContainsPane(pane.DockPane))
246            continue;
247
248          if (pane == dockPane.AutoHidePane)
249            return index;
250
251          index++;
252        }
253        return -1;
254      }
255
256      #region IEnumerable Members
257
258      IEnumerator<Pane> IEnumerable<Pane>.GetEnumerator() {
259        for (int i = 0; i < Count; i++)
260          yield return this[i];
261      }
262
263      IEnumerator IEnumerable.GetEnumerator() {
264        for (int i = 0; i < Count; i++)
265          yield return this[i];
266      }
267
268      #endregion
269    }
270
271    protected AutoHideStripBase(DockPanel panel) {
272      m_dockPanel = panel;
273      m_panesTop = new PaneCollection(panel, DockState.DockTopAutoHide);
274      m_panesBottom = new PaneCollection(panel, DockState.DockBottomAutoHide);
275      m_panesLeft = new PaneCollection(panel, DockState.DockLeftAutoHide);
276      m_panesRight = new PaneCollection(panel, DockState.DockRightAutoHide);
277
278      SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
279      SetStyle(ControlStyles.Selectable, false);
280    }
281
282    private DockPanel m_dockPanel;
283    protected DockPanel DockPanel {
284      get { return m_dockPanel; }
285    }
286
287    private PaneCollection m_panesTop;
288    protected PaneCollection PanesTop {
289      get { return m_panesTop; }
290    }
291
292    private PaneCollection m_panesBottom;
293    protected PaneCollection PanesBottom {
294      get { return m_panesBottom; }
295    }
296
297    private PaneCollection m_panesLeft;
298    protected PaneCollection PanesLeft {
299      get { return m_panesLeft; }
300    }
301
302    private PaneCollection m_panesRight;
303    protected PaneCollection PanesRight {
304      get { return m_panesRight; }
305    }
306
307    protected PaneCollection GetPanes(DockState dockState) {
308      if (dockState == DockState.DockTopAutoHide)
309        return PanesTop;
310      else if (dockState == DockState.DockBottomAutoHide)
311        return PanesBottom;
312      else if (dockState == DockState.DockLeftAutoHide)
313        return PanesLeft;
314      else if (dockState == DockState.DockRightAutoHide)
315        return PanesRight;
316      else
317        throw new ArgumentOutOfRangeException("dockState");
318    }
319
320    internal int GetNumberOfPanes(DockState dockState) {
321      return GetPanes(dockState).Count;
322    }
323
324    protected Rectangle RectangleTopLeft {
325      get {
326        int height = MeasureHeight();
327        return PanesTop.Count > 0 && PanesLeft.Count > 0 ? new Rectangle(0, 0, height, height) : Rectangle.Empty;
328      }
329    }
330
331    protected Rectangle RectangleTopRight {
332      get {
333        int height = MeasureHeight();
334        return PanesTop.Count > 0 && PanesRight.Count > 0 ? new Rectangle(Width - height, 0, height, height) : Rectangle.Empty;
335      }
336    }
337
338    protected Rectangle RectangleBottomLeft {
339      get {
340        int height = MeasureHeight();
341        return PanesBottom.Count > 0 && PanesLeft.Count > 0 ? new Rectangle(0, Height - height, height, height) : Rectangle.Empty;
342      }
343    }
344
345    protected Rectangle RectangleBottomRight {
346      get {
347        int height = MeasureHeight();
348        return PanesBottom.Count > 0 && PanesRight.Count > 0 ? new Rectangle(Width - height, Height - height, height, height) : Rectangle.Empty;
349      }
350    }
351
352    protected internal Rectangle GetTabStripRectangle(DockState dockState) {
353      int height = MeasureHeight();
354      if (dockState == DockState.DockTopAutoHide && PanesTop.Count > 0)
355        return new Rectangle(RectangleTopLeft.Width, 0, Width - RectangleTopLeft.Width - RectangleTopRight.Width, height);
356      else if (dockState == DockState.DockBottomAutoHide && PanesBottom.Count > 0)
357        return new Rectangle(RectangleBottomLeft.Width, Height - height, Width - RectangleBottomLeft.Width - RectangleBottomRight.Width, height);
358      else if (dockState == DockState.DockLeftAutoHide && PanesLeft.Count > 0)
359        return new Rectangle(0, RectangleTopLeft.Width, height, Height - RectangleTopLeft.Height - RectangleBottomLeft.Height);
360      else if (dockState == DockState.DockRightAutoHide && PanesRight.Count > 0)
361        return new Rectangle(Width - height, RectangleTopRight.Width, height, Height - RectangleTopRight.Height - RectangleBottomRight.Height);
362      else
363        return Rectangle.Empty;
364    }
365
366    private GraphicsPath m_displayingArea = null;
367    private GraphicsPath DisplayingArea {
368      get {
369        if (m_displayingArea == null)
370          m_displayingArea = new GraphicsPath();
371
372        return m_displayingArea;
373      }
374    }
375
376    private void SetRegion() {
377      DisplayingArea.Reset();
378      DisplayingArea.AddRectangle(RectangleTopLeft);
379      DisplayingArea.AddRectangle(RectangleTopRight);
380      DisplayingArea.AddRectangle(RectangleBottomLeft);
381      DisplayingArea.AddRectangle(RectangleBottomRight);
382      DisplayingArea.AddRectangle(GetTabStripRectangle(DockState.DockTopAutoHide));
383      DisplayingArea.AddRectangle(GetTabStripRectangle(DockState.DockBottomAutoHide));
384      DisplayingArea.AddRectangle(GetTabStripRectangle(DockState.DockLeftAutoHide));
385      DisplayingArea.AddRectangle(GetTabStripRectangle(DockState.DockRightAutoHide));
386      Region = new Region(DisplayingArea);
387    }
388
389    protected override void OnMouseDown(MouseEventArgs e) {
390      base.OnMouseDown(e);
391
392      if (e.Button != MouseButtons.Left)
393        return;
394
395      IDockContent content = HitTest();
396      if (content == null)
397        return;
398
399      content.DockHandler.Activate();
400    }
401
402    protected override void OnMouseHover(EventArgs e) {
403      base.OnMouseHover(e);
404
405      IDockContent content = HitTest();
406      if (content != null && DockPanel.ActiveAutoHideContent != content)
407        DockPanel.ActiveAutoHideContent = content;
408
409      // requires further tracking of mouse hover behavior,
410      ResetMouseEventArgs();
411    }
412
413    protected override void OnLayout(LayoutEventArgs levent) {
414      RefreshChanges();
415      base.OnLayout(levent);
416    }
417
418    internal void RefreshChanges() {
419      if (IsDisposed)
420        return;
421
422      SetRegion();
423      OnRefreshChanges();
424    }
425
426    protected virtual void OnRefreshChanges() {
427    }
428
429    protected internal abstract int MeasureHeight();
430
431    private IDockContent HitTest() {
432      Point ptMouse = PointToClient(Control.MousePosition);
433      return HitTest(ptMouse);
434    }
435
436    protected virtual Tab CreateTab(IDockContent content) {
437      return new Tab(content);
438    }
439
440    protected virtual Pane CreatePane(DockPane dockPane) {
441      return new Pane(dockPane);
442    }
443
444    protected abstract IDockContent HitTest(Point point);
445  }
446}
Note: See TracBrowser for help on using the repository browser.