source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/MainForms/DockForm.cs @ 13614

Last change on this file since 13614 was 13614, checked in by pfleck, 4 years ago

#1235

  • Clone menuitem is enabled only if view is not locked and content is deep cloneable.
  • Cloned item shows in same view as the original item.
  • Fixed possible NullReferenceException.
  • Fixed inconsistency which views were closed on "close all (but this)".
File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using WeifenLuo.WinFormsUI.Docking;
28
29namespace HeuristicLab.MainForm.WindowsForms {
30  /// <summary>
31  /// Displays the used view.
32  /// </summary>
33  internal partial class DockForm : DockContent {
34    public DockForm(IView view) {
35      InitializeComponent();
36      this.view = view;
37      if (view != null) {
38        if (view is UserControl) {
39          switch (((UserControl)view).Dock) {
40            case DockStyle.Left:
41              this.ShowHint = DockState.DockLeft;
42              break;
43            case DockStyle.Right:
44              this.ShowHint = DockState.DockRight;
45              break;
46            case DockStyle.Top:
47              this.ShowHint = DockState.DockTop;
48              break;
49            case DockStyle.Bottom:
50              this.ShowHint = DockState.DockBottom;
51              break;
52          }
53          Sidebar sidebar = view as Sidebar;
54          if (sidebar != null) {
55            if (sidebar.Collapsed)
56              this.ShowHint = DockState.DockLeftAutoHide;
57            else
58              this.ShowHint = DockState.DockLeft;
59            this.DockAreas = DockAreas.DockLeft | DockAreas.DockRight;
60          }
61
62          Type viewType = view.GetType();
63          Control control = (Control)view;
64          control.Dock = DockStyle.Fill;
65          this.view.CaptionChanged += new EventHandler(View_CaptionChanged);
66          UpdateText();
67          viewPanel.Controls.Add(control);
68        }
69      } else {
70        Label errorLabel = new Label();
71        errorLabel.Name = "errorLabel";
72        errorLabel.Text = "No view available";
73        errorLabel.AutoSize = false;
74        errorLabel.Dock = DockStyle.Fill;
75        viewPanel.Controls.Add(errorLabel);
76      }
77    }
78
79    protected override void Dispose(bool disposing) {
80      if (View != null)
81        View.CaptionChanged -= new System.EventHandler(View_CaptionChanged);
82      if (disposing && (components != null)) {
83        components.Dispose();
84      }
85      base.Dispose(disposing);
86    }
87
88    private IView view;
89    public IView View {
90      get { return this.view; }
91    }
92
93    private void UpdateText() {
94      if (InvokeRequired)
95        Invoke(new MethodInvoker(UpdateText));
96      else
97        this.Text = this.View.Caption;
98    }
99
100    protected override void OnDockStateChanged(EventArgs e) {
101      base.OnDockStateChanged(e);
102      Sidebar sidebar = view as Sidebar;
103      if (sidebar != null) {
104        if (this.DockState == DockState.DockLeftAutoHide || this.DockState == DockState.DockRightAutoHide)
105          sidebar.Collapsed = true;
106        else
107          sidebar.Collapsed = false;
108      }
109    }
110
111    #region View Events
112    private void View_CaptionChanged(object sender, EventArgs e) {
113      UpdateText();
114    }
115    #endregion
116
117    #region Context Menu Events
118    private void contextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
119      var contentView = View as IContentView;
120      var content = contentView != null ? contentView.Content : null;
121
122      cloneToolStripMenuItem.Enabled = contentView != null && !contentView.Locked && content is IDeepCloneable;
123    }
124
125    private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
126      Close();
127    }
128    private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) {
129      foreach (var dockForm in CurrentDockForms) {
130        dockForm.Close();
131      }
132    }
133    private void closeAllButThisToolStripMenuItem_Click(object sender, EventArgs e) {
134      foreach (var dockForm in CurrentDockForms.Except(this.ToEnumerable())) {
135        dockForm.Close();
136      }
137    }
138    private IEnumerable<DockForm> CurrentDockForms {
139      get {
140        var dockForms = Pane.Contents.OfType<DockForm>().Where(c => c.Pane == Pane); // Pane.Contents contains DockForms that are not placed on that pane
141        return dockForms.ToList(); // .ToList() necessary because closing a DockForm removes it from the Content collection
142      }
143    }
144
145    private void cloneToolStripMenuItem_Click(object sender, EventArgs e) {
146      var contentView = View as IContentView;
147      if (contentView == null) return;
148
149      var cloneable = contentView.Content as IDeepCloneable;
150      if (cloneable == null) return;
151
152      var clone = (IContent)cloneable.Clone();
153
154      var viewHost = contentView as ViewHost;
155      var newView = viewHost != null ? viewHost.ViewType : contentView.GetType();
156      MainFormManager.MainForm.ShowContent(clone, newView);
157    }
158    #endregion
159  }
160}
Note: See TracBrowser for help on using the repository browser.