Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10107 was 10106, checked in by jkarder, 11 years ago

#2116:

  • refactored outermost view host detection
  • fixed update logic of breadcrumb trail
  • fixed some views
File size: 14.9 KB
RevLine 
[3437]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3437]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;
[4510]24using System.Drawing;
[3437]25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.MainForm.WindowsForms {
[3557]30  [Content(typeof(IContent))]
[3437]31  public sealed partial class ViewHost : AsynchronousContentView {
32    public ViewHost() {
33      InitializeComponent();
34      startDragAndDrop = false;
[10042]35      viewContextMenuStrip.IgnoredViewTypes = new List<Type> { typeof(ViewHost) };
[4011]36
37      viewType = null;
[3437]38      activeView = null;
[3497]39      Content = null;
[4011]40      messageLabel.Visible = false;
41      viewsLabel.Visible = false;
[5012]42      viewsLabelVisible = true;
[10042]43
44      breadcrumbControl.ViewHost = this;
[3437]45    }
46
[5012]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;
[10042]55          if (view != null) AdjustActiveViewSize();
[5012]56        }
57      }
58    }
59
[10089]60    public bool HotlinkingEnabled { get; set; }
61
[4510]62    private IContentView cachedView;
[3437]63    private IContentView activeView;
64    public IContentView ActiveView {
[4510]65      get { return activeView; }
[3437]66      private set {
67        if (activeView != value) {
68          if (activeView != null) {
[4510]69            cachedView = activeView;
[3437]70            DeregisterActiveViewEvents();
[4510]71            View cached = cachedView as View;
72            if (cached != null) {
73              cached.OnHidden(EventArgs.Empty);
74              cached.Visible = false;
[4299]75            }
[3437]76          }
[4521]77
[3437]78          activeView = value;
[4521]79
[3437]80          if (activeView != null) {
[5956]81            #region dispose cachedView
[4521]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);
[6951]87                cached.Dispose();
[4521]88              }
89              cachedView = null;
[4510]90            }
[4521]91            #endregion
[4510]92
[10042]93            Caption = activeView.Caption;
[4011]94            viewType = activeView.GetType();
[3437]95            RegisterActiveViewEvents();
96            View view = activeView as View;
[4299]97            if (view != null) {
[4510]98              view.Visible = true;
[10042]99              AdjustActiveViewSize();
[5956]100              if (!Controls.Contains((view))) Controls.Add(view);
[3437]101              view.OnShown(new ViewShownEventArgs(view, false));
[4011]102            }
[4103]103          } else viewType = null;
[6342]104          configurationLabel.Visible = activeView is IConfigureableView;
105          configurationLabel.Enabled = activeView != null && !activeView.Locked;
[9920]106
[9921]107          helpLabel.Visible = activeView != null && ViewAttribute.HasHelpResourcePath(activeView.GetType());
108          helpLabel.Top = CalculateHelpLabelPosY();
[3437]109        }
110      }
111    }
[4011]112
[3437]113    private Type viewType;
114    public Type ViewType {
[4510]115      get { return viewType; }
[3437]116      set {
[5278]117        if (viewType != value) {
[5318]118          if (value == typeof(ViewHost))
119            throw new ArgumentException("Directly nested ViewHosts are not allowed.");
[4011]120          if (value != null && Content != null && !ViewCanShowContent(value, Content))
[5318]121            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".", value, Content.GetType()));
[5278]122
[3437]123          viewType = value;
124          OnViewTypeChanged();
125        }
126      }
127    }
[3466]128
[10042]129    public bool ShowBreadcrumbs {
130      get { return viewContextMenuStrip.ShowBreadcrumbsToolStripMenuItem.Checked; }
131      set { viewContextMenuStrip.ShowBreadcrumbsToolStripMenuItem.Checked = value; }
132    }
133
134    public IEnumerable<IContent> Breadcrumbs { get { return breadcrumbControl.Breadcrumbs; } }
135
[4510]136    protected override void SetEnabledStateOfControls() {
137      Enabled = Content != null;
138    }
139
[3437]140    protected override void OnContentChanged() {
[3466]141      viewContextMenuStrip.Item = Content;
[4299]142      //change ViewType if view of ViewType can not show content or is null
143      if (Content != null) {
144        if (!ViewCanShowContent(viewType, Content)) {
145          Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
[4510]146          if (cachedView != null && cachedView.GetType() == defaultViewType)
147            ActiveView = cachedView;
148          else if (defaultViewType != null)
[4299]149            ViewType = defaultViewType;
150          else if (viewContextMenuStrip.Items.Count > 0)  // create first available view if no default view is available
151            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
[4314]152          else {
[4299]153            ViewType = null;
[4314]154            ActiveView = null;
155          }
[3466]156        }
[4344]157        if (ActiveView != null) ActiveView.Content = Content;
158      } else ActiveView = null;
[4299]159      UpdateLabels();
[4418]160      UpdateActiveMenuItem();
[10042]161      UpdateBreadcrumbControl();
[4299]162    }
[4011]163
[4299]164    private void UpdateLabels() {
[4011]165      if (Content != null && viewContextMenuStrip.Items.Count > 0) {
166        messageLabel.Visible = false;
[5012]167        viewsLabel.Visible = viewsLabelVisible;
[4011]168      } else if (Content != null) {
169        messageLabel.Visible = true;
170        viewsLabel.Visible = false;
[3437]171      } else {
[3664]172        messageLabel.Visible = false;
[3924]173        viewsLabel.Visible = false;
[3437]174      }
175    }
176
177    private void OnViewTypeChanged() {
[4011]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()));
[4510]182        IContentView view = MainFormManager.CreateView(viewType);
183        view.Locked = Locked;
184        view.ReadOnly = ReadOnly;
[4299]185        ActiveView = view; //necessary to allow the views to change the status of the viewhost
186        view.Content = Content;
187
[4011]188        UpdateActiveMenuItem();
[3655]189      }
[4011]190    }
[3437]191
192    private void RegisterActiveViewEvents() {
193      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
[4435]194      activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
[9315]195      activeView.Changed += new EventHandler(activeView_Changed);
[3437]196    }
197    private void DeregisterActiveViewEvents() {
[4511]198      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
199      activeView.LockedChanged -= new EventHandler(activeView_LockedChanged);
[9315]200      activeView.Changed -= new EventHandler(activeView_Changed);
[3437]201    }
202    private void activeView_CaptionChanged(object sender, EventArgs e) {
[4510]203      Caption = activeView.Caption;
[3437]204    }
[4435]205    private void activeView_LockedChanged(object sender, EventArgs e) {
[4510]206      Locked = activeView.Locked;
[6342]207      configurationLabel.Enabled = !activeView.Locked;
[3437]208    }
[9315]209    private void activeView_Changed(object sender, EventArgs e) {
210      OnChanged();
211    }
[10042]212    private void ViewHost_VisibleChanged(object sender, EventArgs e) {
[10089]213      PerformOutermostViewHostDetection();
[10042]214    }
215    private void viewContextMenuStrip_ShowBreadcrumbsChanged(object sender, EventArgs e) {
216      UpdateBreadcrumbControl();
217      AdjustActiveViewSize();
218    }
[4011]219
[4084]220    protected override void OnSizeChanged(EventArgs e) {
221      //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
222      //not implemented with a panel to reduce the number of nested controls
[8587]223      //also cf. http://connect.microsoft.com/VisualStudio/feedback/details/98368/csc-incorrectly-allows-comparison-between-intptr-and-null
224      if (Handle != IntPtr.Zero)
[4084]225        this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
226    }
227    private void OnSizeChangedHelper(EventArgs e) {
228      base.OnSizeChanged(e);
[4510]229      viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
[6342]230      configurationLabel.Location = new Point(Width - configurationLabel.Margin.Right - configurationLabel.Width, viewsLabel.Bottom + viewsLabel.Margin.Bottom + configurationLabel.Margin.Top);
[9921]231      helpLabel.Location = new Point(Width - helpLabel.Margin.Right - helpLabel.Width, CalculateHelpLabelPosY());
232    }
[6951]233
[9921]234    private int CalculateHelpLabelPosY() {
235      if (activeView != null && ViewAttribute.HasHelpResourcePath(activeView.GetType()) && !configurationLabel.Visible) {
236        return configurationLabel.Top;
237      }
238      return configurationLabel.Bottom + configurationLabel.Margin.Bottom + helpLabel.Margin.Top;
[4084]239    }
240
[3437]241    #region forwarding of view events
242    internal protected override void OnShown(ViewShownEventArgs e) {
243      base.OnShown(e);
[4510]244      View view = ActiveView as View;
[3437]245      if (view != null)
246        view.OnShown(e);
247    }
248    internal protected override void OnHidden(EventArgs e) {
249      base.OnHidden(e);
[4510]250      View view = ActiveView as View;
[3437]251      if (view != null)
252        view.OnHidden(e);
253    }
254    internal protected override void OnClosing(FormClosingEventArgs e) {
255      base.OnClosing(e);
[4299]256      View view = ActiveView as View;
257      if (view != null)
[3437]258        view.OnClosing(e);
259    }
260    internal protected override void OnClosed(FormClosedEventArgs e) {
261      base.OnClosed(e);
[4299]262      View view = ActiveView as View;
263      if (view != null)
[3437]264        view.OnClosed(e);
265    }
266    #endregion
267
268    #region GUI actions
269    private void UpdateActiveMenuItem() {
270      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
271        if (item.Key == viewType) {
272          item.Value.Checked = true;
273          item.Value.Enabled = false;
274        } else {
275          item.Value.Checked = false;
276          item.Value.Enabled = true;
277        }
278      }
279    }
280
[10042]281    private void UpdateBreadcrumbControl() {
[10103]282      breadcrumbControl.Visible = ShowBreadcrumbs && Content != null;
[10042]283      if (ShowBreadcrumbs)
284        UpdateBreadcrumbTrail(breadcrumbControl.Breadcrumbs, BuildBreadcrumbTrail());
285    }
286
[3437]287    private bool ViewCanShowContent(Type viewType, object content) {
288      if (content == null) // every view can display null
289        return true;
290      if (viewType == null)
291        return false;
292      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
293    }
294
295    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
[3796]296      IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
[3670]297      if (view != null) {
[10042]298        view.ReadOnly = ReadOnly;
299        view.Locked = Locked;
[3670]300      }
[3437]301    }
302    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
303      Type viewType = (Type)e.ClickedItem.Tag;
304      ViewType = viewType;
305    }
306
307    private bool startDragAndDrop;
308    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
[10042]309      if (e.Button == MouseButtons.Right) {
[5462]310        Screen screen = Screen.FromControl(viewsLabel);
[10042]311        int rightBorder = viewsLabel.PointToScreen(viewsLabel.Location).X + viewContextMenuStrip.Width;
[5462]312        rightBorder = rightBorder - screen.Bounds.X; //pixel position on active screen
313
314        if (rightBorder < screen.Bounds.Width)
315          viewContextMenuStrip.Show(viewsLabel, viewsLabel.Margin.Left, viewsLabel.Margin.Top);
316        else
317          viewContextMenuStrip.Show(screen.Bounds.X + screen.Bounds.Width - viewContextMenuStrip.Width, viewsLabel.PointToScreen(viewsLabel.Location).Y - viewsLabel.Margin.Top);
318      } else if (!Locked) {
[3437]319        startDragAndDrop = true;
320        viewsLabel.Capture = false;
[3497]321        viewsLabel.Focus();
[3437]322      }
323    }
324    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
325      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
326        DataObject data = new DataObject();
[5837]327        data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, Content);
[3437]328        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
329      } else
330        startDragAndDrop = false;
331    }
[6342]332
[7244]333    private void configurationLabel_DoubleClick(object sender, MouseEventArgs e) {
[6342]334      ((IConfigureableView)ActiveView).ShowConfiguration();
335    }
[9920]336
337    private void helpLabel_DoubleClick(object sender, EventArgs e) {
338      using (InfoBox dialog = new InfoBox("Help for " + ViewAttribute.GetViewName(ActiveView.GetType()), ViewAttribute.GetHelpResourcePath(ActiveView.GetType()), ActiveView)) {
339        dialog.ShowDialog(this);
340      }
341    }
[3437]342    #endregion
[10042]343
344    #region Helpers
345    private void AdjustActiveViewSize() {
346      var view = activeView as View;
347      if (view == null) return;
348      int width = viewsLabelVisible ? Width - viewsLabel.Width - viewsLabel.Margin.Left - viewsLabel.Margin.Right : Width;
349      int height = ShowBreadcrumbs ? Height - viewsLabel.Height - viewsLabel.Margin.Top - viewsLabel.Margin.Bottom : Height;
350      view.Location = new Point(0, Height - height);
351      view.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
352      view.Size = new Size(width, height);
353    }
354
355    private IEnumerable<IContent> BuildBreadcrumbTrail() {
356      var ll = new LinkedList<IContent>();
357      for (var control = (Control)this; control != null; control = control.Parent) {
358        var viewHost = control as ViewHost;
359        if (viewHost != null && viewHost.Content != null)
360          ll.AddFirst(viewHost.Content);
361      }
362      return ll;
363    }
364
365    public void UpdateBreadcrumbTrail(IEnumerable<IContent> oldCrumbs, IEnumerable<IContent> newCrumbs) {
366      if (!newCrumbs.Any()) return;
367      var ll = new LinkedList<IContent>();
[10106]368      if (newCrumbs.Contains(oldCrumbs.LastOrDefault())) {
369        foreach (var c in oldCrumbs) {
370          if (c != newCrumbs.First())
371            ll.AddLast(c);
372          else break;
373        }
[10042]374      }
375      foreach (var c in newCrumbs)
376        ll.AddLast(c);
377      breadcrumbControl.Breadcrumbs = ll;
378    }
379
[10089]380    private void PerformOutermostViewHostDetection() {
[10106]381      var parentContentViews = GetParentViewsOfType<ContentView>();
382      viewContextMenuStrip.ShowBreadcrumbsToolStripMenuItem.Checked = !parentContentViews.Any();
[10042]383    }
384    #endregion
[3437]385  }
386}
Note: See TracBrowser for help on using the repository browser.