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
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    public IEnumerable<IContent> Breadcrumbs { get { return breadcrumbControl.Breadcrumbs; } }
135
136    protected override void SetEnabledStateOfControls() {
137      Enabled = Content != null;
138    }
139
140    protected override void OnContentChanged() {
141      viewContextMenuStrip.Item = Content;
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());
146          if (cachedView != null && cachedView.GetType() == defaultViewType)
147            ActiveView = cachedView;
148          else if (defaultViewType != null)
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;
152          else {
153            ViewType = null;
154            ActiveView = null;
155          }
156        }
157        if (ActiveView != null) ActiveView.Content = Content;
158      } else ActiveView = null;
159      UpdateLabels();
160      UpdateActiveMenuItem();
161      UpdateBreadcrumbControl();
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    private void ViewHost_VisibleChanged(object sender, EventArgs e) {
213      PerformOutermostViewHostDetection();
214    }
215    private void viewContextMenuStrip_ShowBreadcrumbsChanged(object sender, EventArgs e) {
216      UpdateBreadcrumbControl();
217      AdjustActiveViewSize();
218    }
219
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
223      //also cf. http://connect.microsoft.com/VisualStudio/feedback/details/98368/csc-incorrectly-allows-comparison-between-intptr-and-null
224      if (Handle != IntPtr.Zero)
225        this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
226    }
227    private void OnSizeChangedHelper(EventArgs e) {
228      base.OnSizeChanged(e);
229      viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
230      configurationLabel.Location = new Point(Width - configurationLabel.Margin.Right - configurationLabel.Width, viewsLabel.Bottom + viewsLabel.Margin.Bottom + configurationLabel.Margin.Top);
231      helpLabel.Location = new Point(Width - helpLabel.Margin.Right - helpLabel.Width, CalculateHelpLabelPosY());
232    }
233
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;
239    }
240
241    #region forwarding of view events
242    internal protected override void OnShown(ViewShownEventArgs e) {
243      base.OnShown(e);
244      View view = ActiveView as View;
245      if (view != null)
246        view.OnShown(e);
247    }
248    internal protected override void OnHidden(EventArgs e) {
249      base.OnHidden(e);
250      View view = ActiveView as View;
251      if (view != null)
252        view.OnHidden(e);
253    }
254    internal protected override void OnClosing(FormClosingEventArgs e) {
255      base.OnClosing(e);
256      View view = ActiveView as View;
257      if (view != null)
258        view.OnClosing(e);
259    }
260    internal protected override void OnClosed(FormClosedEventArgs e) {
261      base.OnClosed(e);
262      View view = ActiveView as View;
263      if (view != null)
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
281    private void UpdateBreadcrumbControl() {
282      breadcrumbControl.Visible = ShowBreadcrumbs && Content != null;
283      if (ShowBreadcrumbs)
284        UpdateBreadcrumbTrail(breadcrumbControl.Breadcrumbs, BuildBreadcrumbTrail());
285    }
286
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) {
296      IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
297      if (view != null) {
298        view.ReadOnly = ReadOnly;
299        view.Locked = Locked;
300      }
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) {
309      if (e.Button == MouseButtons.Right) {
310        Screen screen = Screen.FromControl(viewsLabel);
311        int rightBorder = viewsLabel.PointToScreen(viewsLabel.Location).X + viewContextMenuStrip.Width;
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) {
319        startDragAndDrop = true;
320        viewsLabel.Capture = false;
321        viewsLabel.Focus();
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();
327        data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, Content);
328        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
329      } else
330        startDragAndDrop = false;
331    }
332
333    private void configurationLabel_DoubleClick(object sender, MouseEventArgs e) {
334      ((IConfigureableView)ActiveView).ShowConfiguration();
335    }
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    }
342    #endregion
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>();
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        }
374      }
375      foreach (var c in newCrumbs)
376        ll.AddLast(c);
377      breadcrumbControl.Breadcrumbs = ll;
378    }
379
380    private void PerformOutermostViewHostDetection() {
381      var parentContentViews = GetParentViewsOfType<ContentView>();
382      viewContextMenuStrip.ShowBreadcrumbsToolStripMenuItem.Checked = !parentContentViews.Any();
383    }
384    #endregion
385  }
386}
Note: See TracBrowser for help on using the repository browser.