Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed caching from the ViewHost (ticket #1133).

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