Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ViewHost.cs @ 18242

Last change on this file since 18242 was 17819, checked in by mkommend, 4 years ago

#2028: Merged r17585 into stable.

File size: 12.9 KB
RevLine 
[3437]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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))]
[13258]31  public partial class ViewHost : AsynchronousContentView {
[3437]32    public ViewHost() {
33      InitializeComponent();
34      startDragAndDrop = false;
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;
[3437]43    }
44
[5012]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
[4510]58    private IContentView cachedView;
[3437]59    private IContentView activeView;
60    public IContentView ActiveView {
[4510]61      get { return activeView; }
[3437]62      private set {
63        if (activeView != value) {
64          if (activeView != null) {
[4510]65            cachedView = activeView;
[3437]66            DeregisterActiveViewEvents();
[4510]67            View cached = cachedView as View;
68            if (cached != null) {
69              cached.OnHidden(EventArgs.Empty);
70              cached.Visible = false;
[4299]71            }
[3437]72          }
[4521]73
[3437]74          activeView = value;
[4521]75
[3437]76          if (activeView != null) {
[5956]77            #region dispose cachedView
[4521]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);
[6951]83                cached.Dispose();
[4521]84              }
85              cachedView = null;
[4510]86            }
[4521]87            #endregion
[4510]88
[4465]89            this.Caption = activeView.Caption;
[4011]90            viewType = activeView.GetType();
[3437]91            RegisterActiveViewEvents();
92            View view = activeView as View;
[4299]93            if (view != null) {
[4510]94              view.Visible = true;
[13258]95              ConfigureViewLayout(view);
[5956]96              if (!Controls.Contains((view))) Controls.Add(view);
[3437]97              view.OnShown(new ViewShownEventArgs(view, false));
[4011]98            }
[4103]99          } else viewType = null;
[6342]100          configurationLabel.Visible = activeView is IConfigureableView;
101          configurationLabel.Enabled = activeView != null && !activeView.Locked;
[9977]102
103          helpLabel.Visible = activeView != null && ViewAttribute.HasHelpResourcePath(activeView.GetType());
104          helpLabel.Top = CalculateHelpLabelPosY();
[3437]105        }
106      }
107    }
[4011]108
[13258]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
[3437]116    private Type viewType;
117    public Type ViewType {
[4510]118      get { return viewType; }
[3437]119      set {
[5278]120        if (viewType != value) {
[5318]121          if (value == typeof(ViewHost))
122            throw new ArgumentException("Directly nested ViewHosts are not allowed.");
[4011]123          if (value != null && Content != null && !ViewCanShowContent(value, Content))
[5318]124            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".", value, Content.GetType()));
[5278]125
[3437]126          viewType = value;
127          OnViewTypeChanged();
128        }
129      }
130    }
[3466]131
[4510]132    protected override void SetEnabledStateOfControls() {
133      Enabled = Content != null;
134    }
135
[3437]136    protected override void OnContentChanged() {
[3466]137      viewContextMenuStrip.Item = Content;
[4299]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());
[4510]142          if (cachedView != null && cachedView.GetType() == defaultViewType)
143            ActiveView = cachedView;
144          else if (defaultViewType != null)
[4299]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;
[4314]148          else {
[4299]149            ViewType = null;
[4314]150            ActiveView = null;
151          }
[3466]152        }
[4344]153        if (ActiveView != null) ActiveView.Content = Content;
154      } else ActiveView = null;
[4299]155      UpdateLabels();
[4418]156      UpdateActiveMenuItem();
[4299]157    }
[4011]158
[4299]159    private void UpdateLabels() {
[4011]160      if (Content != null && viewContextMenuStrip.Items.Count > 0) {
161        messageLabel.Visible = false;
[5012]162        viewsLabel.Visible = viewsLabelVisible;
[4011]163      } else if (Content != null) {
164        messageLabel.Visible = true;
165        viewsLabel.Visible = false;
[3437]166      } else {
[3664]167        messageLabel.Visible = false;
[3924]168        viewsLabel.Visible = false;
[3437]169      }
170    }
171
[17819]172
[3437]173    private void OnViewTypeChanged() {
[17819]174      if (viewType == null) return;
175      if (!ViewCanShowContent(viewType, Content))
176        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".", viewType, Content.GetType()));
[4299]177
[17819]178      IContentView view = MainFormManager.CreateView(viewType);
179      view.Locked = Locked;
180      view.ReadOnly = ReadOnly;
181      ActiveView = view; //necessary to allow the views to change the status of the viewhost
182      view.Content = Content;
183
184      UpdateActiveMenuItem();
185
[4011]186    }
[3437]187
188    private void RegisterActiveViewEvents() {
189      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
[4435]190      activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
[17819]191      activeView.ReadOnlyChanged += new EventHandler(activeView_ReadOnlyChanged);
[9315]192      activeView.Changed += new EventHandler(activeView_Changed);
[3437]193    }
194    private void DeregisterActiveViewEvents() {
[4511]195      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
196      activeView.LockedChanged -= new EventHandler(activeView_LockedChanged);
[17819]197      activeView.ReadOnlyChanged -= new EventHandler(activeView_ReadOnlyChanged);
[9315]198      activeView.Changed -= new EventHandler(activeView_Changed);
[3437]199    }
200    private void activeView_CaptionChanged(object sender, EventArgs e) {
[4510]201      Caption = activeView.Caption;
[3437]202    }
[4435]203    private void activeView_LockedChanged(object sender, EventArgs e) {
[4510]204      Locked = activeView.Locked;
[6342]205      configurationLabel.Enabled = !activeView.Locked;
[3437]206    }
[17819]207    private void activeView_ReadOnlyChanged(object sender, EventArgs e) {
208      ReadOnly = activeView.ReadOnly;
209    }
210
[9315]211    private void activeView_Changed(object sender, EventArgs e) {
212      OnChanged();
213    }
[4011]214
[4084]215    protected override void OnSizeChanged(EventArgs e) {
216      //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
217      //not implemented with a panel to reduce the number of nested controls
[8587]218      //also cf. http://connect.microsoft.com/VisualStudio/feedback/details/98368/csc-incorrectly-allows-comparison-between-intptr-and-null
219      if (Handle != IntPtr.Zero)
[4084]220        this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
221    }
222    private void OnSizeChangedHelper(EventArgs e) {
223      base.OnSizeChanged(e);
[4510]224      viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
[6342]225      configurationLabel.Location = new Point(Width - configurationLabel.Margin.Right - configurationLabel.Width, viewsLabel.Bottom + viewsLabel.Margin.Bottom + configurationLabel.Margin.Top);
[9977]226      helpLabel.Location = new Point(Width - helpLabel.Margin.Right - helpLabel.Width, CalculateHelpLabelPosY());
227    }
[6951]228
[9977]229    private int CalculateHelpLabelPosY() {
230      if (activeView != null && ViewAttribute.HasHelpResourcePath(activeView.GetType()) && !configurationLabel.Visible) {
231        return configurationLabel.Top;
232      }
233      return configurationLabel.Bottom + configurationLabel.Margin.Bottom + helpLabel.Margin.Top;
[4084]234    }
235
[3437]236    #region forwarding of view events
[17819]237    protected internal override void OnShown(ViewShownEventArgs e) {
[3437]238      base.OnShown(e);
[4510]239      View view = ActiveView as View;
[3437]240      if (view != null)
241        view.OnShown(e);
242    }
[17819]243    protected internal override void OnHidden(EventArgs e) {
[3437]244      base.OnHidden(e);
[4510]245      View view = ActiveView as View;
[3437]246      if (view != null)
247        view.OnHidden(e);
248    }
[17819]249    protected internal override void OnClosing(FormClosingEventArgs e) {
[3437]250      base.OnClosing(e);
[4299]251      View view = ActiveView as View;
252      if (view != null)
[3437]253        view.OnClosing(e);
254    }
[17819]255    protected internal override void OnClosed(FormClosedEventArgs e) {
[3437]256      base.OnClosed(e);
[4299]257      View view = ActiveView as View;
258      if (view != null)
[3437]259        view.OnClosed(e);
260    }
261    #endregion
262
263    #region GUI actions
264    private void UpdateActiveMenuItem() {
265      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
266        if (item.Key == viewType) {
267          item.Value.Checked = true;
268          item.Value.Enabled = false;
269        } else {
270          item.Value.Checked = false;
271          item.Value.Enabled = true;
272        }
273      }
274    }
275
276    private bool ViewCanShowContent(Type viewType, object content) {
277      if (content == null) // every view can display null
278        return true;
279      if (viewType == null)
280        return false;
281      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
282    }
283
284    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
[3796]285      IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
[3670]286      if (view != null) {
287        view.ReadOnly = this.ReadOnly;
288        view.Locked = this.Locked;
289      }
[3437]290    }
291    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
292      Type viewType = (Type)e.ClickedItem.Tag;
293      ViewType = viewType;
294    }
295
296    private bool startDragAndDrop;
297    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
[5462]298      if (e.Button == System.Windows.Forms.MouseButtons.Right) {
299        Screen screen = Screen.FromControl(viewsLabel);
300        int rightBorder = viewsLabel.PointToScreen(viewsLabel.Location).X + viewContextMenuStrip.Width; //
301        rightBorder = rightBorder - screen.Bounds.X; //pixel position on active screen
302
303        if (rightBorder < screen.Bounds.Width)
304          viewContextMenuStrip.Show(viewsLabel, viewsLabel.Margin.Left, viewsLabel.Margin.Top);
305        else
306          viewContextMenuStrip.Show(screen.Bounds.X + screen.Bounds.Width - viewContextMenuStrip.Width, viewsLabel.PointToScreen(viewsLabel.Location).Y - viewsLabel.Margin.Top);
307      } else if (!Locked) {
[3437]308        startDragAndDrop = true;
309        viewsLabel.Capture = false;
[3497]310        viewsLabel.Focus();
[3437]311      }
312    }
313    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
314      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
315        DataObject data = new DataObject();
[5837]316        data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, Content);
[3437]317        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
318      } else
319        startDragAndDrop = false;
320    }
[6342]321
[7244]322    private void configurationLabel_DoubleClick(object sender, MouseEventArgs e) {
[6342]323      ((IConfigureableView)ActiveView).ShowConfiguration();
324    }
[9977]325
326    private void helpLabel_DoubleClick(object sender, EventArgs e) {
327      using (InfoBox dialog = new InfoBox("Help for " + ViewAttribute.GetViewName(ActiveView.GetType()), ViewAttribute.GetHelpResourcePath(ActiveView.GetType()), ActiveView)) {
328        dialog.ShowDialog(this);
329      }
330    }
[3437]331    #endregion
332  }
333}
Note: See TracBrowser for help on using the repository browser.