Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ViewHost.cs @ 6284

Last change on this file since 6284 was 5956, checked in by mkommend, 14 years ago

#1464: Corrected behavior of ViewHost and SymbolicExpressionTreeChart.

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