Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added possibility to hide the ViewHost icon (ticket #1312).

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