Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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