Free cookie consent management tool by TermsFeed Policy Generator

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

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

Corrected deregistering of event handler in the view host (ticket #1211).

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