Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ViewHost.cs @ 10089

Last change on this file since 10089 was 10089, checked in by jkarder, 10 years ago

#2116:

  • added hotlinking functionality
  • added methods for outermost view host detection and manipulation
File size: 15.0 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      breadcrumbControl.ViewHost = this;
45    }
46
47    private bool viewsLabelVisible;
48    public bool ViewsLabelVisible {
49      get { return viewsLabelVisible; }
50      set {
51        if (viewsLabelVisible != value) {
52          viewsLabelVisible = value;
53          viewsLabel.Visible = value;
54          View view = activeView as View;
55          if (view != null) AdjustActiveViewSize();
56        }
57      }
58    }
59
60    public bool HotlinkingEnabled { get; set; }
61
62    private IContentView cachedView;
63    private IContentView activeView;
64    public IContentView ActiveView {
65      get { return activeView; }
66      private set {
67        if (activeView != value) {
68          if (activeView != null) {
69            cachedView = activeView;
70            DeregisterActiveViewEvents();
71            View cached = cachedView as View;
72            if (cached != null) {
73              cached.OnHidden(EventArgs.Empty);
74              cached.Visible = false;
75            }
76          }
77
78          activeView = value;
79
80          if (activeView != null) {
81            #region dispose cachedView
82            if (activeView != cachedView) {
83              if (cachedView != null) cachedView.Content = null;  //needed to deregister events
84              View cached = cachedView as View;
85              if (cached != null) {
86                Controls.Remove(cached);
87                cached.Dispose();
88              }
89              cachedView = null;
90            }
91            #endregion
92
93            Caption = activeView.Caption;
94            viewType = activeView.GetType();
95            RegisterActiveViewEvents();
96            View view = activeView as View;
97            if (view != null) {
98              view.Visible = true;
99              AdjustActiveViewSize();
100              if (!Controls.Contains((view))) Controls.Add(view);
101              view.OnShown(new ViewShownEventArgs(view, false));
102            }
103          } else viewType = null;
104          configurationLabel.Visible = activeView is IConfigureableView;
105          configurationLabel.Enabled = activeView != null && !activeView.Locked;
106
107          helpLabel.Visible = activeView != null && ViewAttribute.HasHelpResourcePath(activeView.GetType());
108          helpLabel.Top = CalculateHelpLabelPosY();
109        }
110      }
111    }
112
113    private Type viewType;
114    public Type ViewType {
115      get { return viewType; }
116      set {
117        if (viewType != value) {
118          if (value == typeof(ViewHost))
119            throw new ArgumentException("Directly nested ViewHosts are not allowed.");
120          if (value != null && Content != null && !ViewCanShowContent(value, Content))
121            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".", value, Content.GetType()));
122
123          viewType = value;
124          OnViewTypeChanged();
125        }
126      }
127    }
128
129    public bool ShowBreadcrumbs {
130      get { return viewContextMenuStrip.ShowBreadcrumbsToolStripMenuItem.Checked; }
131      set { viewContextMenuStrip.ShowBreadcrumbsToolStripMenuItem.Checked = value; }
132    }
133
134    private bool isOutermostViewHost;
135    public IEnumerable<IContent> Breadcrumbs { get { return breadcrumbControl.Breadcrumbs; } }
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      UpdateBreadcrumbControl();
163    }
164
165    private void UpdateLabels() {
166      if (Content != null && viewContextMenuStrip.Items.Count > 0) {
167        messageLabel.Visible = false;
168        viewsLabel.Visible = viewsLabelVisible;
169      } else if (Content != null) {
170        messageLabel.Visible = true;
171        viewsLabel.Visible = false;
172      } else {
173        messageLabel.Visible = false;
174        viewsLabel.Visible = false;
175      }
176    }
177
178    private void OnViewTypeChanged() {
179      if (viewType != null) {
180        if (!ViewCanShowContent(viewType, Content))
181          throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
182                                                            viewType, Content.GetType()));
183        IContentView view = MainFormManager.CreateView(viewType);
184        view.Locked = Locked;
185        view.ReadOnly = ReadOnly;
186        ActiveView = view; //necessary to allow the views to change the status of the viewhost
187        view.Content = Content;
188
189        UpdateActiveMenuItem();
190      }
191    }
192
193    private void RegisterActiveViewEvents() {
194      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
195      activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
196      activeView.Changed += new EventHandler(activeView_Changed);
197    }
198    private void DeregisterActiveViewEvents() {
199      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
200      activeView.LockedChanged -= new EventHandler(activeView_LockedChanged);
201      activeView.Changed -= new EventHandler(activeView_Changed);
202    }
203    private void activeView_CaptionChanged(object sender, EventArgs e) {
204      Caption = activeView.Caption;
205    }
206    private void activeView_LockedChanged(object sender, EventArgs e) {
207      Locked = activeView.Locked;
208      configurationLabel.Enabled = !activeView.Locked;
209    }
210    private void activeView_Changed(object sender, EventArgs e) {
211      OnChanged();
212    }
213    private void ViewHost_VisibleChanged(object sender, EventArgs e) {
214      PerformOutermostViewHostDetection();
215    }
216    private void viewContextMenuStrip_ShowBreadcrumbsChanged(object sender, EventArgs e) {
217      UpdateBreadcrumbControl();
218      AdjustActiveViewSize();
219    }
220
221    protected override void OnSizeChanged(EventArgs e) {
222      //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
223      //not implemented with a panel to reduce the number of nested controls
224      //also cf. http://connect.microsoft.com/VisualStudio/feedback/details/98368/csc-incorrectly-allows-comparison-between-intptr-and-null
225      if (Handle != IntPtr.Zero)
226        this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
227    }
228    private void OnSizeChangedHelper(EventArgs e) {
229      base.OnSizeChanged(e);
230      viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
231      configurationLabel.Location = new Point(Width - configurationLabel.Margin.Right - configurationLabel.Width, viewsLabel.Bottom + viewsLabel.Margin.Bottom + configurationLabel.Margin.Top);
232      helpLabel.Location = new Point(Width - helpLabel.Margin.Right - helpLabel.Width, CalculateHelpLabelPosY());
233    }
234
235    private int CalculateHelpLabelPosY() {
236      if (activeView != null && ViewAttribute.HasHelpResourcePath(activeView.GetType()) && !configurationLabel.Visible) {
237        return configurationLabel.Top;
238      }
239      return configurationLabel.Bottom + configurationLabel.Margin.Bottom + helpLabel.Margin.Top;
240    }
241
242    #region forwarding of view events
243    internal protected override void OnShown(ViewShownEventArgs e) {
244      base.OnShown(e);
245      View view = ActiveView as View;
246      if (view != null)
247        view.OnShown(e);
248    }
249    internal protected override void OnHidden(EventArgs e) {
250      base.OnHidden(e);
251      View view = ActiveView as View;
252      if (view != null)
253        view.OnHidden(e);
254    }
255    internal protected override void OnClosing(FormClosingEventArgs e) {
256      base.OnClosing(e);
257      View view = ActiveView as View;
258      if (view != null)
259        view.OnClosing(e);
260    }
261    internal protected override void OnClosed(FormClosedEventArgs e) {
262      base.OnClosed(e);
263      View view = ActiveView as View;
264      if (view != null)
265        view.OnClosed(e);
266    }
267    #endregion
268
269    #region GUI actions
270    private void UpdateActiveMenuItem() {
271      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
272        if (item.Key == viewType) {
273          item.Value.Checked = true;
274          item.Value.Enabled = false;
275        } else {
276          item.Value.Checked = false;
277          item.Value.Enabled = true;
278        }
279      }
280    }
281
282    private void UpdateBreadcrumbControl() {
283      breadcrumbControl.Visible = ShowBreadcrumbs;
284      if (ShowBreadcrumbs)
285        UpdateBreadcrumbTrail(breadcrumbControl.Breadcrumbs, BuildBreadcrumbTrail());
286    }
287
288    private bool ViewCanShowContent(Type viewType, object content) {
289      if (content == null) // every view can display null
290        return true;
291      if (viewType == null)
292        return false;
293      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
294    }
295
296    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
297      IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
298      if (view != null) {
299        view.ReadOnly = ReadOnly;
300        view.Locked = Locked;
301      }
302    }
303    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
304      Type viewType = (Type)e.ClickedItem.Tag;
305      ViewType = viewType;
306    }
307
308    private bool startDragAndDrop;
309    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
310      if (e.Button == MouseButtons.Right) {
311        Screen screen = Screen.FromControl(viewsLabel);
312        int rightBorder = viewsLabel.PointToScreen(viewsLabel.Location).X + viewContextMenuStrip.Width;
313        rightBorder = rightBorder - screen.Bounds.X; //pixel position on active screen
314
315        if (rightBorder < screen.Bounds.Width)
316          viewContextMenuStrip.Show(viewsLabel, viewsLabel.Margin.Left, viewsLabel.Margin.Top);
317        else
318          viewContextMenuStrip.Show(screen.Bounds.X + screen.Bounds.Width - viewContextMenuStrip.Width, viewsLabel.PointToScreen(viewsLabel.Location).Y - viewsLabel.Margin.Top);
319      } else if (!Locked) {
320        startDragAndDrop = true;
321        viewsLabel.Capture = false;
322        viewsLabel.Focus();
323      }
324    }
325    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
326      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
327        DataObject data = new DataObject();
328        data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, Content);
329        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
330      } else
331        startDragAndDrop = false;
332    }
333
334    private void configurationLabel_DoubleClick(object sender, MouseEventArgs e) {
335      ((IConfigureableView)ActiveView).ShowConfiguration();
336    }
337
338    private void helpLabel_DoubleClick(object sender, EventArgs e) {
339      using (InfoBox dialog = new InfoBox("Help for " + ViewAttribute.GetViewName(ActiveView.GetType()), ViewAttribute.GetHelpResourcePath(ActiveView.GetType()), ActiveView)) {
340        dialog.ShowDialog(this);
341      }
342    }
343    #endregion
344
345    #region Helpers
346    private void AdjustActiveViewSize() {
347      var view = activeView as View;
348      if (view == null) return;
349      int width = viewsLabelVisible ? Width - viewsLabel.Width - viewsLabel.Margin.Left - viewsLabel.Margin.Right : Width;
350      int height = ShowBreadcrumbs ? Height - viewsLabel.Height - viewsLabel.Margin.Top - viewsLabel.Margin.Bottom : Height;
351      view.Location = new Point(0, Height - height);
352      view.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
353      view.Size = new Size(width, height);
354    }
355
356    private IEnumerable<IContent> BuildBreadcrumbTrail() {
357      var ll = new LinkedList<IContent>();
358      for (var control = (Control)this; control != null; control = control.Parent) {
359        var viewHost = control as ViewHost;
360        if (viewHost != null && viewHost.Content != null)
361          ll.AddFirst(viewHost.Content);
362      }
363      return ll;
364    }
365
366    public void UpdateBreadcrumbTrail(IEnumerable<IContent> oldCrumbs, IEnumerable<IContent> newCrumbs) {
367      if (!newCrumbs.Any()) return;
368      var ll = new LinkedList<IContent>();
369      foreach (var c in oldCrumbs) {
370        if (c != newCrumbs.First())
371          ll.AddLast(c);
372        else break;
373      }
374      foreach (var c in newCrumbs)
375        ll.AddLast(c);
376      breadcrumbControl.Breadcrumbs = ll;
377    }
378
379    private void PerformOutermostViewHostDetection() {
380      var mainForm = MainFormManager.GetMainForm<MainForm>();
381      var outermostViewHost = mainForm.GetOutermostViewOfType<ViewHost>(this);
382      isOutermostViewHost = outermostViewHost == this;
383      viewContextMenuStrip.ShowBreadcrumbsToolStripMenuItem.Checked = isOutermostViewHost;
384    }
385    #endregion
386  }
387}
Note: See TracBrowser for help on using the repository browser.