Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9920 was 9920, checked in by ascheibe, 11 years ago

#2100 moved help system from View to ViewHost

File size: 12.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 dispose 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              if (ViewsLabelVisible) {
96                view.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
97                view.Size = new Size(Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
98              } else view.Dock = DockStyle.Fill;
99              if (!Controls.Contains((view))) Controls.Add(view);
100              view.OnShown(new ViewShownEventArgs(view, false));
101            }
102          } else viewType = null;
103          configurationLabel.Visible = activeView is IConfigureableView;
104          configurationLabel.Enabled = activeView != null && !activeView.Locked;
105
106          if (activeView != null && ViewAttribute.HasHelpResourcePath(activeView.GetType())) {
107            if (configurationLabel.Visible) {
108              helpLabel.Top = 44;
109            } else {
110              helpLabel.Top = 22;
111            }
112            helpLabel.Visible = true;
113          } else {
114            helpLabel.Visible = false;
115            helpLabel.Top = 44;
116          }
117        }
118      }
119    }
120
121    private Type viewType;
122    public Type ViewType {
123      get { return viewType; }
124      set {
125        if (viewType != value) {
126          if (value == typeof(ViewHost))
127            throw new ArgumentException("Directly nested ViewHosts are not allowed.");
128          if (value != null && Content != null && !ViewCanShowContent(value, Content))
129            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".", value, Content.GetType()));
130
131          viewType = value;
132          OnViewTypeChanged();
133        }
134      }
135    }
136
137    protected override void SetEnabledStateOfControls() {
138      Enabled = Content != null;
139    }
140
141    protected override void OnContentChanged() {
142      viewContextMenuStrip.Item = Content;
143      //change ViewType if view of ViewType can not show content or is null
144      if (Content != null) {
145        if (!ViewCanShowContent(viewType, Content)) {
146          Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
147          if (cachedView != null && cachedView.GetType() == defaultViewType)
148            ActiveView = cachedView;
149          else if (defaultViewType != null)
150            ViewType = defaultViewType;
151          else if (viewContextMenuStrip.Items.Count > 0)  // create first available view if no default view is available
152            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
153          else {
154            ViewType = null;
155            ActiveView = null;
156          }
157        }
158        if (ActiveView != null) ActiveView.Content = Content;
159      } else ActiveView = null;
160      UpdateLabels();
161      UpdateActiveMenuItem();
162    }
163
164    private void UpdateLabels() {
165      if (Content != null && viewContextMenuStrip.Items.Count > 0) {
166        messageLabel.Visible = false;
167        viewsLabel.Visible = viewsLabelVisible;
168      } else if (Content != null) {
169        messageLabel.Visible = true;
170        viewsLabel.Visible = false;
171      } else {
172        messageLabel.Visible = false;
173        viewsLabel.Visible = false;
174      }
175    }
176
177    private void OnViewTypeChanged() {
178      if (viewType != null) {
179        if (!ViewCanShowContent(viewType, Content))
180          throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
181                                                            viewType, Content.GetType()));
182        IContentView view = MainFormManager.CreateView(viewType);
183        view.Locked = Locked;
184        view.ReadOnly = ReadOnly;
185        ActiveView = view; //necessary to allow the views to change the status of the viewhost
186        view.Content = Content;
187
188        UpdateActiveMenuItem();
189      }
190    }
191
192    private void RegisterActiveViewEvents() {
193      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
194      activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
195      activeView.Changed += new EventHandler(activeView_Changed);
196    }
197    private void DeregisterActiveViewEvents() {
198      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
199      activeView.LockedChanged -= new EventHandler(activeView_LockedChanged);
200      activeView.Changed -= new EventHandler(activeView_Changed);
201    }
202    private void activeView_CaptionChanged(object sender, EventArgs e) {
203      Caption = activeView.Caption;
204    }
205    private void activeView_LockedChanged(object sender, EventArgs e) {
206      Locked = activeView.Locked;
207      configurationLabel.Enabled = !activeView.Locked;
208    }
209    private void activeView_Changed(object sender, EventArgs e) {
210      OnChanged();
211    }
212
213    protected override void OnSizeChanged(EventArgs e) {
214      //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
215      //not implemented with a panel to reduce the number of nested controls
216      //also cf. http://connect.microsoft.com/VisualStudio/feedback/details/98368/csc-incorrectly-allows-comparison-between-intptr-and-null
217      if (Handle != IntPtr.Zero)
218        this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
219    }
220    private void OnSizeChangedHelper(EventArgs e) {
221      base.OnSizeChanged(e);
222      viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
223      configurationLabel.Location = new Point(Width - configurationLabel.Margin.Right - configurationLabel.Width, viewsLabel.Bottom + viewsLabel.Margin.Bottom + configurationLabel.Margin.Top);
224
225    }
226
227    #region forwarding of view events
228    internal protected override void OnShown(ViewShownEventArgs e) {
229      base.OnShown(e);
230      View view = ActiveView as View;
231      if (view != null)
232        view.OnShown(e);
233    }
234    internal protected override void OnHidden(EventArgs e) {
235      base.OnHidden(e);
236      View view = ActiveView as View;
237      if (view != null)
238        view.OnHidden(e);
239    }
240    internal protected override void OnClosing(FormClosingEventArgs e) {
241      base.OnClosing(e);
242      View view = ActiveView as View;
243      if (view != null)
244        view.OnClosing(e);
245    }
246    internal protected override void OnClosed(FormClosedEventArgs e) {
247      base.OnClosed(e);
248      View view = ActiveView as View;
249      if (view != null)
250        view.OnClosed(e);
251    }
252    #endregion
253
254    #region GUI actions
255    private void UpdateActiveMenuItem() {
256      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
257        if (item.Key == viewType) {
258          item.Value.Checked = true;
259          item.Value.Enabled = false;
260        } else {
261          item.Value.Checked = false;
262          item.Value.Enabled = true;
263        }
264      }
265    }
266
267    private bool ViewCanShowContent(Type viewType, object content) {
268      if (content == null) // every view can display null
269        return true;
270      if (viewType == null)
271        return false;
272      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
273    }
274
275    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
276      IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
277      if (view != null) {
278        view.ReadOnly = this.ReadOnly;
279        view.Locked = this.Locked;
280      }
281    }
282    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
283      Type viewType = (Type)e.ClickedItem.Tag;
284      ViewType = viewType;
285    }
286
287    private bool startDragAndDrop;
288    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
289      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
290        Screen screen = Screen.FromControl(viewsLabel);
291        int rightBorder = viewsLabel.PointToScreen(viewsLabel.Location).X + viewContextMenuStrip.Width; //
292        rightBorder = rightBorder - screen.Bounds.X; //pixel position on active screen
293
294        if (rightBorder < screen.Bounds.Width)
295          viewContextMenuStrip.Show(viewsLabel, viewsLabel.Margin.Left, viewsLabel.Margin.Top);
296        else
297          viewContextMenuStrip.Show(screen.Bounds.X + screen.Bounds.Width - viewContextMenuStrip.Width, viewsLabel.PointToScreen(viewsLabel.Location).Y - viewsLabel.Margin.Top);
298      } else if (!Locked) {
299        startDragAndDrop = true;
300        viewsLabel.Capture = false;
301        viewsLabel.Focus();
302      }
303    }
304    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
305      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
306        DataObject data = new DataObject();
307        data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, Content);
308        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
309      } else
310        startDragAndDrop = false;
311    }
312
313    private void configurationLabel_DoubleClick(object sender, MouseEventArgs e) {
314      ((IConfigureableView)ActiveView).ShowConfiguration();
315    }
316
317    private void helpLabel_DoubleClick(object sender, EventArgs e) {
318      using (InfoBox dialog = new InfoBox("Help for " + ViewAttribute.GetViewName(ActiveView.GetType()), ViewAttribute.GetHelpResourcePath(ActiveView.GetType()), ActiveView)) {
319        dialog.ShowDialog(this);
320      }
321    }
322    #endregion
323  }
324}
Note: See TracBrowser for help on using the repository browser.