source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/ViewHost.cs @ 3655

Last change on this file since 3655 was 3655, checked in by mkommend, 12 years ago

corrected resizing behavior in ViewHost (ticket #972)

File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.MainForm;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.MainForm.WindowsForms {
30  [Content(typeof(IContent))]
31  public sealed partial class ViewHost : AsynchronousContentView {
32    public ViewHost() {
33      InitializeComponent();
34      cachedViews = new Dictionary<Type, IContentView>();
35      viewType = null;
36      startDragAndDrop = false;
37      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
38      activeView = null;
39      Content = null;
40      OnContentChanged();
41    }
42
43    private bool viewShown;
44    private Dictionary<Type, IContentView> cachedViews;
45    public IEnumerable<IContentView> Views {
46      get { return cachedViews.Values; }
47    }
48
49    private IContentView activeView;
50    public IContentView ActiveView {
51      get { return this.activeView; }
52      private set {
53        if (activeView != value) {
54          if (activeView != null) {
55            DeregisterActiveViewEvents();
56            View view = activeView as View;
57            if (view != null)
58              view.OnHidden(EventArgs.Empty);
59          }
60          activeView = value;
61          if (activeView != null) {
62            RegisterActiveViewEvents();
63            View view = activeView as View;
64            if (view != null)
65              view.OnShown(new ViewShownEventArgs(view, false));
66          }
67          ActiveViewChanged();
68          OnViewTypeChanged();
69        }
70      }
71    }
72    private Type viewType;
73    public Type ViewType {
74      get { return this.viewType; }
75      set {
76        if (viewType != value) {
77          if (value != null && !ViewCanShowContent(value, Content))
78            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
79                                                      value, Content.GetType()));
80          viewType = value;
81          OnViewTypeChanged();
82        }
83      }
84    }
85    public new IContent Content {
86      get { return base.Content; }
87      set {
88        if (value == null || this.Content == null || value.GetType() != this.Content.GetType())
89          cachedViews.Clear();
90
91        base.Content = value;
92      }
93    }
94
95    public new bool Enabled {
96      get { return base.Enabled; }
97      set {
98        this.SuspendRepaint();
99        base.Enabled = value;
100        this.viewsLabel.Enabled = value;
101        this.ResumeRepaint(true);
102      }
103    }
104
105    protected override void OnContentChanged() {
106      messageLabel.Visible = false;
107      viewsLabel.Visible = false;
108      viewPanel.Visible = false;
109      viewContextMenuStrip.Item = Content;
110
111      if (Content != null) {
112        if (viewContextMenuStrip.Items.Count == 0)
113          messageLabel.Visible = true;
114        else {
115          viewsLabel.Visible = true;
116          viewPanel.Visible = true;
117        }
118
119        if (!ViewCanShowContent(viewType, Content)) {
120          ViewType = MainFormManager.GetDefaultViewType(Content.GetType());
121          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
122            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
123        }
124        UpdateActiveMenuItem();
125        foreach (IContentView view in cachedViews.Values)
126          view.Content = this.Content;
127      } else {
128        if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
129        viewPanel.Controls.Clear();
130        cachedViews.Clear();
131      }
132    }
133
134    private void OnViewTypeChanged() {
135      viewPanel.Controls.Clear();
136      if (viewType == null || Content == null)
137        return;
138      if (!ViewCanShowContent(viewType, Content))
139        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
140                                                          viewType, Content.GetType()));
141      if (viewPanel.Height <= 10 || viewPanel.Width <= 10) {
142        viewShown = false;
143        return;
144      }
145
146      viewShown = true;
147      UpdateActiveMenuItem();
148      IContentView view;
149      if (cachedViews.ContainsKey(ViewType))
150        view = cachedViews[ViewType];
151      else {
152        view = MainFormManager.CreateView(viewType);
153        view.ReadOnly = this.ReadOnly;
154        view.Locked = this.Locked;
155        cachedViews.Add(viewType, view);
156      }
157      this.ActiveView = view;
158      Control control = (Control)view;
159      control.Dock = DockStyle.Fill;
160      viewPanel.Controls.Add(control);
161      viewPanel.Visible = true;
162      view.Content = Content;
163    }
164
165    private void viewPanel_Resize(object sender, EventArgs e) {
166      if (!viewShown)
167        this.OnViewTypeChanged();
168    }
169
170    private void RegisterActiveViewEvents() {
171      activeView.Changed += new EventHandler(activeView_Changed);
172      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
173    }
174    private void DeregisterActiveViewEvents() {
175      activeView.Changed -= new EventHandler(activeView_Changed);
176      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
177    }
178    private void activeView_CaptionChanged(object sender, EventArgs e) {
179      this.ActiveViewChanged();
180    }
181    private void activeView_Changed(object sender, EventArgs e) {
182      this.ActiveViewChanged();
183    }
184    private void ActiveViewChanged() {
185      if (ActiveView != null) {
186        this.Caption = this.ActiveView.Caption;
187        this.Locked = this.ActiveView.Locked;
188      }
189    }
190    #region forwarding of view events
191    protected override void OnReadOnlyChanged() {
192      base.OnReadOnlyChanged();
193      foreach (IContentView view in cachedViews.Values)
194        view.ReadOnly = this.ReadOnly;
195    }
196    protected override void OnLockedChanged() {
197      base.OnLockedChanged();
198      foreach (IContentView view in cachedViews.Values)
199        view.Locked = this.Locked;
200    }
201    internal protected override void OnShown(ViewShownEventArgs e) {
202      base.OnShown(e);
203      View view = this.ActiveView as View;
204      if (view != null)
205        view.OnShown(e);
206    }
207    internal protected override void OnHidden(EventArgs e) {
208      base.OnHidden(e);
209      View view = this.ActiveView as View;
210      if (view != null)
211        view.OnHidden(e);
212    }
213    internal protected override void OnClosing(FormClosingEventArgs e) {
214      base.OnClosing(e);
215      foreach (View view in this.Views.OfType<View>())
216        view.OnClosing(e);
217    }
218    internal protected override void OnClosed(FormClosedEventArgs e) {
219      base.OnClosed(e);
220      foreach (View view in this.Views.OfType<View>())
221        view.OnClosed(e);
222    }
223    #endregion
224
225    #region GUI actions
226    private void UpdateActiveMenuItem() {
227      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
228        if (item.Key == viewType) {
229          item.Value.Checked = true;
230          item.Value.Enabled = false;
231        } else {
232          item.Value.Checked = false;
233          item.Value.Enabled = true;
234        }
235      }
236    }
237
238    private bool ViewCanShowContent(Type viewType, object content) {
239      if (content == null) // every view can display null
240        return true;
241      if (viewType == null)
242        return false;
243      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
244    }
245
246    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
247      IContentView view = MainFormManager.MainForm.ShowContent(this.Content);
248      view.ReadOnly = this.ReadOnly;
249      view.Locked = this.Locked;
250    }
251    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
252      Type viewType = (Type)e.ClickedItem.Tag;
253      ViewType = viewType;
254    }
255
256    private bool startDragAndDrop;
257    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
258      if (!Locked) {
259        startDragAndDrop = true;
260        viewsLabel.Capture = false;
261        viewsLabel.Focus();
262      }
263    }
264    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
265      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
266        DataObject data = new DataObject();
267        data.SetData("Type", Content.GetType());
268        data.SetData("Value", Content);
269        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
270      } else
271        startDragAndDrop = false;
272    }
273    #endregion
274  }
275}
Note: See TracBrowser for help on using the repository browser.