Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.MainForm.WindowsForms/3.3/ViewHost.cs @ 11553

Last change on this file since 11553 was 4521, checked in by mkommend, 14 years ago

Deregistered content events before a view is disposed in the ViewHost (ticket #1211).

File size: 9.6 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
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      startDragAndDrop = false;
35      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
36
37      viewType = null;
38      activeView = null;
39      Content = null;
40      messageLabel.Visible = false;
41      viewsLabel.Visible = false;
42    }
43
44    private IContentView cachedView;
45    private IContentView activeView;
46    public IContentView ActiveView {
47      get { return activeView; }
48      private set {
49        if (activeView != value) {
50          if (activeView != null) {
51            cachedView = activeView;
52            DeregisterActiveViewEvents();
53            View cached = cachedView as View;
54            if (cached != null) {
55              cached.OnHidden(EventArgs.Empty);
56              cached.Visible = false;
57            }
58          }
59
60          activeView = value;
61
62          if (activeView != null) {
63            #region disposed cachedView
64            if (activeView != cachedView) {
65              if (cachedView != null) cachedView.Content = null;  //needed to deregister events
66              View cached = cachedView as View;
67              if (cached != null) {
68                Controls.Remove(cached);
69                cached.Dispose();
70              }
71              cachedView = null;
72            }
73            #endregion
74
75            this.Caption = activeView.Caption;
76            viewType = activeView.GetType();
77            RegisterActiveViewEvents();
78            View view = activeView as View;
79            if (view != null) {
80              view.Visible = true;
81              view.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
82              view.Size = new Size(Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
83              view.OnShown(new ViewShownEventArgs(view, false));
84              if (!Controls.Contains((view))) Controls.Add(view);
85            }
86          } else viewType = null;
87        }
88      }
89    }
90
91    private Type viewType;
92    public Type ViewType {
93      get { return viewType; }
94      set {
95        if (viewType != value) {
96          if (value != null && Content != null && !ViewCanShowContent(value, Content))
97            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
98                                                      value, Content.GetType()));
99          viewType = value;
100          OnViewTypeChanged();
101        }
102      }
103    }
104
105    protected override void SetEnabledStateOfControls() {
106      Enabled = Content != null;
107    }
108
109    protected override void OnContentChanged() {
110      viewContextMenuStrip.Item = Content;
111      //change ViewType if view of ViewType can not show content or is null
112      if (Content != null) {
113        if (!ViewCanShowContent(viewType, Content)) {
114          Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
115          if (cachedView != null && cachedView.GetType() == defaultViewType)
116            ActiveView = cachedView;
117          else if (defaultViewType != null)
118            ViewType = defaultViewType;
119          else if (viewContextMenuStrip.Items.Count > 0)  // create first available view if no default view is available
120            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
121          else {
122            ViewType = null;
123            ActiveView = null;
124          }
125        }
126        if (ActiveView != null) ActiveView.Content = Content;
127      } else ActiveView = null;
128      UpdateLabels();
129      UpdateActiveMenuItem();
130    }
131
132    private void UpdateLabels() {
133      if (Content != null && viewContextMenuStrip.Items.Count > 0) {
134        messageLabel.Visible = false;
135        viewsLabel.Visible = true;
136      } else if (Content != null) {
137        messageLabel.Visible = true;
138        viewsLabel.Visible = false;
139      } else {
140        messageLabel.Visible = false;
141        viewsLabel.Visible = false;
142      }
143    }
144
145    private void OnViewTypeChanged() {
146      if (viewType != null) {
147        if (!ViewCanShowContent(viewType, Content))
148          throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
149                                                            viewType, Content.GetType()));
150        IContentView view = MainFormManager.CreateView(viewType);
151        view.Locked = Locked;
152        view.ReadOnly = ReadOnly;
153        ActiveView = view; //necessary to allow the views to change the status of the viewhost
154        view.Content = Content;
155
156        UpdateActiveMenuItem();
157      }
158    }
159
160    private void RegisterActiveViewEvents() {
161      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
162      activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
163    }
164    private void DeregisterActiveViewEvents() {
165      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
166      activeView.LockedChanged -= new EventHandler(activeView_LockedChanged);
167    }
168    private void activeView_CaptionChanged(object sender, EventArgs e) {
169      Caption = activeView.Caption;
170    }
171    private void activeView_LockedChanged(object sender, EventArgs e) {
172      Locked = activeView.Locked;
173    }
174
175    protected override void OnSizeChanged(EventArgs e) {
176      //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
177      //not implemented with a panel to reduce the number of nested controls
178      if (Handle != null)
179        this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
180    }
181    private void OnSizeChangedHelper(EventArgs e) {
182      base.OnSizeChanged(e);
183      viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
184    }
185
186    #region forwarding of view events
187    internal protected override void OnShown(ViewShownEventArgs e) {
188      base.OnShown(e);
189      View view = ActiveView as View;
190      if (view != null)
191        view.OnShown(e);
192    }
193    internal protected override void OnHidden(EventArgs e) {
194      base.OnHidden(e);
195      View view = ActiveView as View;
196      if (view != null)
197        view.OnHidden(e);
198    }
199    internal protected override void OnClosing(FormClosingEventArgs e) {
200      base.OnClosing(e);
201      View view = ActiveView as View;
202      if (view != null)
203        view.OnClosing(e);
204    }
205    internal protected override void OnClosed(FormClosedEventArgs e) {
206      base.OnClosed(e);
207      View view = ActiveView as View;
208      if (view != null)
209        view.OnClosed(e);
210    }
211    #endregion
212
213    #region GUI actions
214    private void UpdateActiveMenuItem() {
215      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
216        if (item.Key == viewType) {
217          item.Value.Checked = true;
218          item.Value.Enabled = false;
219        } else {
220          item.Value.Checked = false;
221          item.Value.Enabled = true;
222        }
223      }
224    }
225
226    private bool ViewCanShowContent(Type viewType, object content) {
227      if (content == null) // every view can display null
228        return true;
229      if (viewType == null)
230        return false;
231      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
232    }
233
234    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
235      IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
236      if (view != null) {
237        view.ReadOnly = this.ReadOnly;
238        view.Locked = this.Locked;
239      }
240    }
241    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
242      Type viewType = (Type)e.ClickedItem.Tag;
243      ViewType = viewType;
244    }
245
246    private bool startDragAndDrop;
247    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
248      if (!Locked) {
249        startDragAndDrop = true;
250        viewsLabel.Capture = false;
251        viewsLabel.Focus();
252      }
253    }
254    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
255      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
256        DataObject data = new DataObject();
257        data.SetData("Type", Content.GetType());
258        data.SetData("Value", Content);
259        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
260      } else
261        startDragAndDrop = false;
262    }
263    #endregion
264  }
265}
Note: See TracBrowser for help on using the repository browser.