Free cookie consent management tool by TermsFeed Policy Generator

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

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

Forgot to update the caption of the ViewHost, when the active view is changed (ticket #1155).

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