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