Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/WinFormsUI/Docking/DockContentHandler.cs @ 2594

Last change on this file since 2594 was 2134, checked in by gkronber, 15 years ago

Added up to date source of Weifen Luo dock panel suit in a separate project (and added strong name key). Removed binary versions of Weifen Luo dock panel suite and references to it. #687 (Update AdvancedOptimizationFrontend to use more recent version of Weifen Luo Docking library)

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