Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.WinFormsUI/2.3.1/WinFormsUI-2.3.1/Docking/DockContentHandler.cs @ 6675

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

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

File size: 29.3 KB
Line 
1using System;
2using System.ComponentModel;
3using System.Diagnostics.CodeAnalysis;
4using System.Drawing;
5using System.Windows.Forms;
6
7namespace WeifenLuo.WinFormsUI.Docking {
8  public delegate string GetPersistStringCallback();
9
10  public class DockContentHandler : IDisposable, IDockDragSource {
11    public DockContentHandler(Form form)
12      : this(form, null) {
13    }
14
15    public DockContentHandler(Form form, GetPersistStringCallback getPersistStringCallback) {
16      if (!(form is IDockContent))
17        throw new ArgumentException(Strings.DockContent_Constructor_InvalidForm, "form");
18
19      m_form = form;
20      m_getPersistStringCallback = getPersistStringCallback;
21
22      m_events = new EventHandlerList();
23      Form.Disposed += new EventHandler(Form_Disposed);
24      Form.TextChanged += new EventHandler(Form_TextChanged);
25    }
26
27    public void Dispose() {
28      Dispose(true);
29      GC.SuppressFinalize(this);
30    }
31
32    protected virtual void Dispose(bool disposing) {
33      if (disposing) {
34        lock (this) {
35          DockPanel = null;
36          if (m_autoHideTab != null)
37            m_autoHideTab.Dispose();
38          if (m_tab != null)
39            m_tab.Dispose();
40
41          Form.Disposed -= new EventHandler(Form_Disposed);
42          Form.TextChanged -= new EventHandler(Form_TextChanged);
43          m_events.Dispose();
44        }
45      }
46    }
47
48    private Form m_form;
49    public Form Form {
50      get { return m_form; }
51    }
52
53    public IDockContent Content {
54      get { return Form as IDockContent; }
55    }
56
57    private IDockContent m_previousActive = null;
58    public IDockContent PreviousActive {
59      get { return m_previousActive; }
60      internal set { m_previousActive = value; }
61    }
62
63    private IDockContent m_nextActive = null;
64    public IDockContent NextActive {
65      get { return m_nextActive; }
66      internal set { m_nextActive = value; }
67    }
68
69    private EventHandlerList m_events;
70    private EventHandlerList Events {
71      get { return m_events; }
72    }
73
74    private bool m_allowEndUserDocking = true;
75    public bool AllowEndUserDocking {
76      get { return m_allowEndUserDocking; }
77      set { m_allowEndUserDocking = value; }
78    }
79
80    private double m_autoHidePortion = 0.25;
81    public double AutoHidePortion {
82      get { return m_autoHidePortion; }
83      set {
84        if (value <= 0)
85          throw (new ArgumentOutOfRangeException(Strings.DockContentHandler_AutoHidePortion_OutOfRange));
86
87        if (m_autoHidePortion == value)
88          return;
89
90        m_autoHidePortion = value;
91
92        if (DockPanel == null)
93          return;
94
95        if (DockPanel.ActiveAutoHideContent == Content)
96          DockPanel.PerformLayout();
97      }
98    }
99
100    private bool m_closeButton = true;
101    public bool CloseButton {
102      get { return m_closeButton; }
103      set {
104        if (m_closeButton == value)
105          return;
106
107        m_closeButton = value;
108        if (Pane != null)
109          if (Pane.ActiveContent.DockHandler == this)
110            Pane.RefreshChanges();
111      }
112    }
113
114    private bool m_closeButtonVisible = true;
115    /// <summary>
116    /// Determines whether the close button is visible on the content
117    /// </summary>
118    public bool CloseButtonVisible {
119      get { return m_closeButtonVisible; }
120      set { m_closeButtonVisible = value; }
121    }
122
123    private DockState DefaultDockState {
124      get {
125        if (ShowHint != DockState.Unknown && ShowHint != DockState.Hidden)
126          return ShowHint;
127
128        if ((DockAreas & DockAreas.Document) != 0)
129          return DockState.Document;
130        if ((DockAreas & DockAreas.DockRight) != 0)
131          return DockState.DockRight;
132        if ((DockAreas & DockAreas.DockLeft) != 0)
133          return DockState.DockLeft;
134        if ((DockAreas & DockAreas.DockBottom) != 0)
135          return DockState.DockBottom;
136        if ((DockAreas & DockAreas.DockTop) != 0)
137          return DockState.DockTop;
138
139        return DockState.Unknown;
140      }
141    }
142
143    private DockState DefaultShowState {
144      get {
145        if (ShowHint != DockState.Unknown)
146          return ShowHint;
147
148        if ((DockAreas & DockAreas.Document) != 0)
149          return DockState.Document;
150        if ((DockAreas & DockAreas.DockRight) != 0)
151          return DockState.DockRight;
152        if ((DockAreas & DockAreas.DockLeft) != 0)
153          return DockState.DockLeft;
154        if ((DockAreas & DockAreas.DockBottom) != 0)
155          return DockState.DockBottom;
156        if ((DockAreas & DockAreas.DockTop) != 0)
157          return DockState.DockTop;
158        if ((DockAreas & DockAreas.Float) != 0)
159          return DockState.Float;
160
161        return DockState.Unknown;
162      }
163    }
164
165    private DockAreas m_allowedAreas = DockAreas.DockLeft | DockAreas.DockRight | DockAreas.DockTop | DockAreas.DockBottom | DockAreas.Document | DockAreas.Float;
166    public DockAreas DockAreas {
167      get { return m_allowedAreas; }
168      set {
169        if (m_allowedAreas == value)
170          return;
171
172        if (!DockHelper.IsDockStateValid(DockState, value))
173          throw (new InvalidOperationException(Strings.DockContentHandler_DockAreas_InvalidValue));
174
175        m_allowedAreas = value;
176
177        if (!DockHelper.IsDockStateValid(ShowHint, m_allowedAreas))
178          ShowHint = DockState.Unknown;
179      }
180    }
181
182    private DockState m_dockState = DockState.Unknown;
183    public DockState DockState {
184      get { return m_dockState; }
185      set {
186        if (m_dockState == value)
187          return;
188
189        DockPanel.SuspendLayout(true);
190
191        if (value == DockState.Hidden)
192          IsHidden = true;
193        else
194          SetDockState(false, value, Pane);
195
196        DockPanel.ResumeLayout(true, true);
197      }
198    }
199
200    private DockPanel m_dockPanel = null;
201    public DockPanel DockPanel {
202      get { return m_dockPanel; }
203      set {
204        if (m_dockPanel == value)
205          return;
206
207        Pane = null;
208
209        if (m_dockPanel != null)
210          m_dockPanel.RemoveContent(Content);
211
212        if (m_tab != null) {
213          m_tab.Dispose();
214          m_tab = null;
215        }
216
217        if (m_autoHideTab != null) {
218          m_autoHideTab.Dispose();
219          m_autoHideTab = null;
220        }
221
222        m_dockPanel = value;
223
224        if (m_dockPanel != null) {
225          m_dockPanel.AddContent(Content);
226          Form.TopLevel = false;
227          Form.FormBorderStyle = FormBorderStyle.None;
228          Form.ShowInTaskbar = false;
229          Form.WindowState = FormWindowState.Normal;
230          NativeMethods.SetWindowPos(Form.Handle, IntPtr.Zero, 0, 0, 0, 0,
231            Win32.FlagsSetWindowPos.SWP_NOACTIVATE |
232            Win32.FlagsSetWindowPos.SWP_NOMOVE |
233            Win32.FlagsSetWindowPos.SWP_NOSIZE |
234            Win32.FlagsSetWindowPos.SWP_NOZORDER |
235            Win32.FlagsSetWindowPos.SWP_NOOWNERZORDER |
236            Win32.FlagsSetWindowPos.SWP_FRAMECHANGED);
237        }
238      }
239    }
240
241    public Icon Icon {
242      get { return Form.Icon; }
243    }
244
245    public DockPane Pane {
246      get { return IsFloat ? FloatPane : PanelPane; }
247      set {
248        if (Pane == value)
249          return;
250
251        DockPanel.SuspendLayout(true);
252
253        DockPane oldPane = Pane;
254
255        SuspendSetDockState();
256        FloatPane = (value == null ? null : (value.IsFloat ? value : FloatPane));
257        PanelPane = (value == null ? null : (value.IsFloat ? PanelPane : value));
258        ResumeSetDockState(IsHidden, value != null ? value.DockState : DockState.Unknown, oldPane);
259
260        DockPanel.ResumeLayout(true, true);
261      }
262    }
263
264    private bool m_isHidden = true;
265    public bool IsHidden {
266      get { return m_isHidden; }
267      set {
268        if (m_isHidden == value)
269          return;
270
271        SetDockState(value, VisibleState, Pane);
272      }
273    }
274
275    private string m_tabText = null;
276    public string TabText {
277      get { return m_tabText == null || m_tabText == "" ? Form.Text : m_tabText; }
278      set {
279        if (m_tabText == value)
280          return;
281
282        m_tabText = value;
283        if (Pane != null)
284          Pane.RefreshChanges();
285      }
286    }
287
288    private DockState m_visibleState = DockState.Unknown;
289    public DockState VisibleState {
290      get { return m_visibleState; }
291      set {
292        if (m_visibleState == value)
293          return;
294
295        SetDockState(IsHidden, value, Pane);
296      }
297    }
298
299    private bool m_isFloat = false;
300    public bool IsFloat {
301      get { return m_isFloat; }
302      set {
303        if (m_isFloat == value)
304          return;
305
306        DockState visibleState = CheckDockState(value);
307
308        if (visibleState == DockState.Unknown)
309          throw new InvalidOperationException(Strings.DockContentHandler_IsFloat_InvalidValue);
310
311        SetDockState(IsHidden, visibleState, Pane);
312      }
313    }
314
315    [SuppressMessage("Microsoft.Naming", "CA1720:AvoidTypeNamesInParameters")]
316    public DockState CheckDockState(bool isFloat) {
317      DockState dockState;
318
319      if (isFloat) {
320        if (!IsDockStateValid(DockState.Float))
321          dockState = DockState.Unknown;
322        else
323          dockState = DockState.Float;
324      } else {
325        dockState = (PanelPane != null) ? PanelPane.DockState : DefaultDockState;
326        if (dockState != DockState.Unknown && !IsDockStateValid(dockState))
327          dockState = DockState.Unknown;
328      }
329
330      return dockState;
331    }
332
333    private DockPane m_panelPane = null;
334    public DockPane PanelPane {
335      get { return m_panelPane; }
336      set {
337        if (m_panelPane == value)
338          return;
339
340        if (value != null) {
341          if (value.IsFloat || value.DockPanel != DockPanel)
342            throw new InvalidOperationException(Strings.DockContentHandler_DockPane_InvalidValue);
343        }
344
345        DockPane oldPane = Pane;
346
347        if (m_panelPane != null)
348          RemoveFromPane(m_panelPane);
349        m_panelPane = value;
350        if (m_panelPane != null) {
351          m_panelPane.AddContent(Content);
352          SetDockState(IsHidden, IsFloat ? DockState.Float : m_panelPane.DockState, oldPane);
353        } else
354          SetDockState(IsHidden, DockState.Unknown, oldPane);
355      }
356    }
357
358    private void RemoveFromPane(DockPane pane) {
359      pane.RemoveContent(Content);
360      SetPane(null);
361      if (pane.Contents.Count == 0)
362        pane.Dispose();
363    }
364
365    private DockPane m_floatPane = null;
366    public DockPane FloatPane {
367      get { return m_floatPane; }
368      set {
369        if (m_floatPane == value)
370          return;
371
372        if (value != null) {
373          if (!value.IsFloat || value.DockPanel != DockPanel)
374            throw new InvalidOperationException(Strings.DockContentHandler_FloatPane_InvalidValue);
375        }
376
377        DockPane oldPane = Pane;
378
379        if (m_floatPane != null)
380          RemoveFromPane(m_floatPane);
381        m_floatPane = value;
382        if (m_floatPane != null) {
383          m_floatPane.AddContent(Content);
384          SetDockState(IsHidden, IsFloat ? DockState.Float : VisibleState, oldPane);
385        } else
386          SetDockState(IsHidden, DockState.Unknown, oldPane);
387      }
388    }
389
390    private int m_countSetDockState = 0;
391    private void SuspendSetDockState() {
392      m_countSetDockState++;
393    }
394
395    private void ResumeSetDockState() {
396      m_countSetDockState--;
397      if (m_countSetDockState < 0)
398        m_countSetDockState = 0;
399    }
400
401    internal bool IsSuspendSetDockState {
402      get { return m_countSetDockState != 0; }
403    }
404
405    private void ResumeSetDockState(bool isHidden, DockState visibleState, DockPane oldPane) {
406      ResumeSetDockState();
407      SetDockState(isHidden, visibleState, oldPane);
408    }
409
410    internal void SetDockState(bool isHidden, DockState visibleState, DockPane oldPane) {
411      if (IsSuspendSetDockState)
412        return;
413
414      if (DockPanel == null && visibleState != DockState.Unknown)
415        throw new InvalidOperationException(Strings.DockContentHandler_SetDockState_NullPanel);
416
417      if (visibleState == DockState.Hidden || (visibleState != DockState.Unknown && !IsDockStateValid(visibleState)))
418        throw new InvalidOperationException(Strings.DockContentHandler_SetDockState_InvalidState);
419
420      DockPanel dockPanel = DockPanel;
421      if (dockPanel != null)
422        dockPanel.SuspendLayout(true);
423
424      SuspendSetDockState();
425
426      DockState oldDockState = DockState;
427
428      if (m_isHidden != isHidden || oldDockState == DockState.Unknown) {
429        m_isHidden = isHidden;
430      }
431      m_visibleState = visibleState;
432      m_dockState = isHidden ? DockState.Hidden : visibleState;
433
434      if (visibleState == DockState.Unknown)
435        Pane = null;
436      else {
437        m_isFloat = (m_visibleState == DockState.Float);
438
439        if (Pane == null)
440          Pane = DockPanel.DockPaneFactory.CreateDockPane(Content, visibleState, true);
441        else if (Pane.DockState != visibleState) {
442          if (Pane.Contents.Count == 1)
443            Pane.SetDockState(visibleState);
444          else
445            Pane = DockPanel.DockPaneFactory.CreateDockPane(Content, visibleState, true);
446        }
447      }
448
449      if (Form.ContainsFocus)
450        if (DockState == DockState.Hidden || DockState == DockState.Unknown)
451          DockPanel.ContentFocusManager.GiveUpFocus(Content);
452
453      SetPaneAndVisible(Pane);
454
455      if (oldPane != null && !oldPane.IsDisposed && oldDockState == oldPane.DockState)
456        RefreshDockPane(oldPane);
457
458      if (Pane != null && DockState == Pane.DockState) {
459        if ((Pane != oldPane) ||
460          (Pane == oldPane && oldDockState != oldPane.DockState))
461          // Avoid early refresh of hidden AutoHide panes
462          if ((Pane.DockWindow == null || Pane.DockWindow.Visible || Pane.IsHidden) && !Pane.IsAutoHide)
463            RefreshDockPane(Pane);
464      }
465
466      if (oldDockState != DockState) {
467        if (DockState == DockState.Hidden || DockState == DockState.Unknown ||
468            DockHelper.IsDockStateAutoHide(DockState))
469          DockPanel.ContentFocusManager.RemoveFromList(Content);
470        else
471          DockPanel.ContentFocusManager.AddToList(Content);
472
473        OnDockStateChanged(EventArgs.Empty);
474      }
475      ResumeSetDockState();
476
477      if (dockPanel != null)
478        dockPanel.ResumeLayout(true, true);
479    }
480
481    private static void RefreshDockPane(DockPane pane) {
482      pane.RefreshChanges();
483      pane.ValidateActiveContent();
484    }
485
486    internal string PersistString {
487      get { return GetPersistStringCallback == null ? Form.GetType().ToString() : GetPersistStringCallback(); }
488    }
489
490    private GetPersistStringCallback m_getPersistStringCallback = null;
491    public GetPersistStringCallback GetPersistStringCallback {
492      get { return m_getPersistStringCallback; }
493      set { m_getPersistStringCallback = value; }
494    }
495
496
497    private bool m_hideOnClose = false;
498    public bool HideOnClose {
499      get { return m_hideOnClose; }
500      set { m_hideOnClose = value; }
501    }
502
503    private DockState m_showHint = DockState.Unknown;
504    public DockState ShowHint {
505      get { return m_showHint; }
506      set {
507        if (!DockHelper.IsDockStateValid(value, DockAreas))
508          throw (new InvalidOperationException(Strings.DockContentHandler_ShowHint_InvalidValue));
509
510        if (m_showHint == value)
511          return;
512
513        m_showHint = value;
514      }
515    }
516
517    private bool m_isActivated = false;
518    public bool IsActivated {
519      get { return m_isActivated; }
520      internal set {
521        if (m_isActivated == value)
522          return;
523
524        m_isActivated = value;
525      }
526    }
527
528    public bool IsDockStateValid(DockState dockState) {
529      if (DockPanel != null && dockState == DockState.Document && DockPanel.DocumentStyle == DocumentStyle.SystemMdi)
530        return false;
531      else
532        return DockHelper.IsDockStateValid(dockState, DockAreas);
533    }
534
535    private ContextMenu m_tabPageContextMenu = null;
536    public ContextMenu TabPageContextMenu {
537      get { return m_tabPageContextMenu; }
538      set { m_tabPageContextMenu = value; }
539    }
540
541    private string m_toolTipText = null;
542    public string ToolTipText {
543      get { return m_toolTipText; }
544      set { m_toolTipText = value; }
545    }
546
547    public void Activate() {
548      if (DockPanel == null)
549        Form.Activate();
550      else if (Pane == null)
551        Show(DockPanel);
552      else {
553        IsHidden = false;
554        Pane.ActiveContent = Content;
555        if (DockState == DockState.Document && DockPanel.DocumentStyle == DocumentStyle.SystemMdi) {
556          Form.Activate();
557          return;
558        } else if (DockHelper.IsDockStateAutoHide(DockState))
559          DockPanel.ActiveAutoHideContent = Content;
560
561        if (!Form.ContainsFocus)
562          DockPanel.ContentFocusManager.Activate(Content);
563      }
564    }
565
566    public void GiveUpFocus() {
567      DockPanel.ContentFocusManager.GiveUpFocus(Content);
568    }
569
570    private IntPtr m_activeWindowHandle = IntPtr.Zero;
571    internal IntPtr ActiveWindowHandle {
572      get { return m_activeWindowHandle; }
573      set { m_activeWindowHandle = value; }
574    }
575
576    public void Hide() {
577      IsHidden = true;
578    }
579
580    internal void SetPaneAndVisible(DockPane pane) {
581      SetPane(pane);
582      SetVisible();
583    }
584
585    private void SetPane(DockPane pane) {
586      if (pane != null && pane.DockState == DockState.Document && DockPanel.DocumentStyle == DocumentStyle.DockingMdi) {
587        if (Form.Parent is DockPane)
588          SetParent(null);
589        if (Form.MdiParent != DockPanel.ParentForm) {
590          FlagClipWindow = true;
591          Form.MdiParent = DockPanel.ParentForm;
592        }
593      } else {
594        FlagClipWindow = true;
595        if (Form.MdiParent != null)
596          Form.MdiParent = null;
597        if (Form.TopLevel)
598          Form.TopLevel = false;
599        SetParent(pane);
600      }
601    }
602
603    internal void SetVisible() {
604      bool visible;
605
606      if (IsHidden)
607        visible = false;
608      else if (Pane != null && Pane.DockState == DockState.Document && DockPanel.DocumentStyle == DocumentStyle.DockingMdi)
609        visible = true;
610      else if (Pane != null && Pane.ActiveContent == Content)
611        visible = true;
612      else if (Pane != null && Pane.ActiveContent != Content)
613        visible = false;
614      else
615        visible = Form.Visible;
616
617      if (Form.Visible != visible)
618        Form.Visible = visible;
619    }
620
621    private void SetParent(Control value) {
622      if (Form.Parent == value)
623        return;
624
625      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
626      // Workaround of .Net Framework bug:
627      // Change the parent of a control with focus may result in the first
628      // MDI child form get activated.
629      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
630      bool bRestoreFocus = false;
631      if (Form.ContainsFocus) {
632        //Suggested as a fix for a memory leak by bugreports
633        if (value == null && !IsFloat)
634          DockPanel.ContentFocusManager.GiveUpFocus(this.Content);
635        else {
636          DockPanel.SaveFocus();
637          bRestoreFocus = true;
638        }
639      }
640      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
641
642      Form.Parent = value;
643
644      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
645      // Workaround of .Net Framework bug:
646      // Change the parent of a control with focus may result in the first
647      // MDI child form get activated.
648      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
649      if (bRestoreFocus)
650        Activate();
651      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
652    }
653
654    public void Show() {
655      if (DockPanel == null)
656        Form.Show();
657      else
658        Show(DockPanel);
659    }
660
661    public void Show(DockPanel dockPanel) {
662      if (dockPanel == null)
663        throw (new ArgumentNullException(Strings.DockContentHandler_Show_NullDockPanel));
664
665      if (DockState == DockState.Unknown)
666        Show(dockPanel, DefaultShowState);
667      else
668        Activate();
669    }
670
671    public void Show(DockPanel dockPanel, DockState dockState) {
672      if (dockPanel == null)
673        throw (new ArgumentNullException(Strings.DockContentHandler_Show_NullDockPanel));
674
675      if (dockState == DockState.Unknown || dockState == DockState.Hidden)
676        throw (new ArgumentException(Strings.DockContentHandler_Show_InvalidDockState));
677
678      dockPanel.SuspendLayout(true);
679
680      DockPanel = dockPanel;
681
682      if (dockState == DockState.Float && FloatPane == null)
683        Pane = DockPanel.DockPaneFactory.CreateDockPane(Content, DockState.Float, true);
684      else if (PanelPane == null) {
685        DockPane paneExisting = null;
686        foreach (DockPane pane in DockPanel.Panes)
687          if (pane.DockState == dockState) {
688            paneExisting = pane;
689            break;
690          }
691
692        if (paneExisting == null)
693          Pane = DockPanel.DockPaneFactory.CreateDockPane(Content, dockState, true);
694        else
695          Pane = paneExisting;
696      }
697
698      DockState = dockState;
699      dockPanel.ResumeLayout(true, true); //we'll resume the layout before activating to ensure that the position
700      Activate();                         //and size of the form are finally processed before the form is shown
701    }
702
703    [SuppressMessage("Microsoft.Naming", "CA1720:AvoidTypeNamesInParameters")]
704    public void Show(DockPanel dockPanel, Rectangle floatWindowBounds) {
705      if (dockPanel == null)
706        throw (new ArgumentNullException(Strings.DockContentHandler_Show_NullDockPanel));
707
708      dockPanel.SuspendLayout(true);
709
710      DockPanel = dockPanel;
711      if (FloatPane == null) {
712        IsHidden = true;  // to reduce the screen flicker
713        FloatPane = DockPanel.DockPaneFactory.CreateDockPane(Content, DockState.Float, false);
714        FloatPane.FloatWindow.StartPosition = FormStartPosition.Manual;
715      }
716
717      FloatPane.FloatWindow.Bounds = floatWindowBounds;
718
719      Show(dockPanel, DockState.Float);
720      Activate();
721
722      dockPanel.ResumeLayout(true, true);
723    }
724
725    public void Show(DockPane pane, IDockContent beforeContent) {
726      if (pane == null)
727        throw (new ArgumentNullException(Strings.DockContentHandler_Show_NullPane));
728
729      if (beforeContent != null && pane.Contents.IndexOf(beforeContent) == -1)
730        throw (new ArgumentException(Strings.DockContentHandler_Show_InvalidBeforeContent));
731
732      pane.DockPanel.SuspendLayout(true);
733
734      DockPanel = pane.DockPanel;
735      Pane = pane;
736      pane.SetContentIndex(Content, pane.Contents.IndexOf(beforeContent));
737      Show();
738
739      pane.DockPanel.ResumeLayout(true, true);
740    }
741
742    public void Show(DockPane previousPane, DockAlignment alignment, double proportion) {
743      if (previousPane == null)
744        throw (new ArgumentException(Strings.DockContentHandler_Show_InvalidPrevPane));
745
746      if (DockHelper.IsDockStateAutoHide(previousPane.DockState))
747        throw (new ArgumentException(Strings.DockContentHandler_Show_InvalidPrevPane));
748
749      previousPane.DockPanel.SuspendLayout(true);
750
751      DockPanel = previousPane.DockPanel;
752      DockPanel.DockPaneFactory.CreateDockPane(Content, previousPane, alignment, proportion, true);
753      Show();
754
755      previousPane.DockPanel.ResumeLayout(true, true);
756    }
757
758    public void Close() {
759      DockPanel dockPanel = DockPanel;
760      if (dockPanel != null)
761        dockPanel.SuspendLayout(true);
762      Form.Close();
763      if (dockPanel != null)
764        dockPanel.ResumeLayout(true, true);
765
766    }
767
768    private DockPaneStripBase.Tab m_tab = null;
769    internal DockPaneStripBase.Tab GetTab(DockPaneStripBase dockPaneStrip) {
770      if (m_tab == null)
771        m_tab = dockPaneStrip.CreateTab(Content);
772
773      return m_tab;
774    }
775
776    private IDisposable m_autoHideTab = null;
777    internal IDisposable AutoHideTab {
778      get { return m_autoHideTab; }
779      set { m_autoHideTab = value; }
780    }
781
782    #region Events
783    private static readonly object DockStateChangedEvent = new object();
784    public event EventHandler DockStateChanged {
785      add { Events.AddHandler(DockStateChangedEvent, value); }
786      remove { Events.RemoveHandler(DockStateChangedEvent, value); }
787    }
788    protected virtual void OnDockStateChanged(EventArgs e) {
789      EventHandler handler = (EventHandler)Events[DockStateChangedEvent];
790      if (handler != null)
791        handler(this, e);
792    }
793    #endregion
794
795    private void Form_Disposed(object sender, EventArgs e) {
796      Dispose();
797    }
798
799    private void Form_TextChanged(object sender, EventArgs e) {
800      if (DockHelper.IsDockStateAutoHide(DockState))
801        DockPanel.RefreshAutoHideStrip();
802      else if (Pane != null) {
803        if (Pane.FloatWindow != null)
804          Pane.FloatWindow.SetText();
805        Pane.RefreshChanges();
806      }
807    }
808
809    private bool m_flagClipWindow = false;
810    internal bool FlagClipWindow {
811      get { return m_flagClipWindow; }
812      set {
813        if (m_flagClipWindow == value)
814          return;
815
816        m_flagClipWindow = value;
817        if (m_flagClipWindow)
818          Form.Region = new Region(Rectangle.Empty);
819        else
820          Form.Region = null;
821      }
822    }
823
824    private ContextMenuStrip m_tabPageContextMenuStrip = null;
825    public ContextMenuStrip TabPageContextMenuStrip {
826      get { return m_tabPageContextMenuStrip; }
827      set { m_tabPageContextMenuStrip = value; }
828    }
829
830    #region IDockDragSource Members
831
832    Control IDragSource.DragControl {
833      get { return Form; }
834    }
835
836    bool IDockDragSource.CanDockTo(DockPane pane) {
837      if (!IsDockStateValid(pane.DockState))
838        return false;
839
840      if (Pane == pane && pane.DisplayingContents.Count == 1)
841        return false;
842
843      return true;
844    }
845
846    Rectangle IDockDragSource.BeginDrag(Point ptMouse) {
847      Size size;
848      DockPane floatPane = this.FloatPane;
849      if (DockState == DockState.Float || floatPane == null || floatPane.FloatWindow.NestedPanes.Count != 1)
850        size = DockPanel.DefaultFloatWindowSize;
851      else
852        size = floatPane.FloatWindow.Size;
853
854      Point location;
855      Rectangle rectPane = Pane.ClientRectangle;
856      if (DockState == DockState.Document) {
857        if (Pane.DockPanel.DocumentTabStripLocation == DocumentTabStripLocation.Bottom)
858          location = new Point(rectPane.Left, rectPane.Bottom - size.Height);
859        else
860          location = new Point(rectPane.Left, rectPane.Top);
861      } else {
862        location = new Point(rectPane.Left, rectPane.Bottom);
863        location.Y -= size.Height;
864      }
865      location = Pane.PointToScreen(location);
866
867      if (ptMouse.X > location.X + size.Width)
868        location.X += ptMouse.X - (location.X + size.Width) + Measures.SplitterSize;
869
870      return new Rectangle(location, size);
871    }
872
873    public void FloatAt(Rectangle floatWindowBounds) {
874      DockPane pane = DockPanel.DockPaneFactory.CreateDockPane(Content, floatWindowBounds, true);
875    }
876
877    public void DockTo(DockPane pane, DockStyle dockStyle, int contentIndex) {
878      if (dockStyle == DockStyle.Fill) {
879        bool samePane = (Pane == pane);
880        if (!samePane)
881          Pane = pane;
882
883        if (contentIndex == -1 || !samePane)
884          pane.SetContentIndex(Content, contentIndex);
885        else {
886          DockContentCollection contents = pane.Contents;
887          int oldIndex = contents.IndexOf(Content);
888          int newIndex = contentIndex;
889          if (oldIndex < newIndex) {
890            newIndex += 1;
891            if (newIndex > contents.Count - 1)
892              newIndex = -1;
893          }
894          pane.SetContentIndex(Content, newIndex);
895        }
896      } else {
897        DockPane paneFrom = DockPanel.DockPaneFactory.CreateDockPane(Content, pane.DockState, true);
898        INestedPanesContainer container = pane.NestedPanesContainer;
899        if (dockStyle == DockStyle.Left)
900          paneFrom.DockTo(container, pane, DockAlignment.Left, 0.5);
901        else if (dockStyle == DockStyle.Right)
902          paneFrom.DockTo(container, pane, DockAlignment.Right, 0.5);
903        else if (dockStyle == DockStyle.Top)
904          paneFrom.DockTo(container, pane, DockAlignment.Top, 0.5);
905        else if (dockStyle == DockStyle.Bottom)
906          paneFrom.DockTo(container, pane, DockAlignment.Bottom, 0.5);
907
908        paneFrom.DockState = pane.DockState;
909      }
910    }
911
912    public void DockTo(DockPanel panel, DockStyle dockStyle) {
913      if (panel != DockPanel)
914        throw new ArgumentException(Strings.IDockDragSource_DockTo_InvalidPanel, "panel");
915
916      DockPane pane;
917
918      if (dockStyle == DockStyle.Top)
919        pane = DockPanel.DockPaneFactory.CreateDockPane(Content, DockState.DockTop, true);
920      else if (dockStyle == DockStyle.Bottom)
921        pane = DockPanel.DockPaneFactory.CreateDockPane(Content, DockState.DockBottom, true);
922      else if (dockStyle == DockStyle.Left)
923        pane = DockPanel.DockPaneFactory.CreateDockPane(Content, DockState.DockLeft, true);
924      else if (dockStyle == DockStyle.Right)
925        pane = DockPanel.DockPaneFactory.CreateDockPane(Content, DockState.DockRight, true);
926      else if (dockStyle == DockStyle.Fill)
927        pane = DockPanel.DockPaneFactory.CreateDockPane(Content, DockState.Document, true);
928      else
929        return;
930    }
931
932    #endregion
933  }
934}
Note: See TracBrowser for help on using the repository browser.