Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ViewHost.cs @ 16123

Last change on this file since 16123 was 13014, checked in by jkarder, 9 years ago

#2116: added new breadcrumb navigation prototype

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