Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8717 was 8587, checked in by ascheibe, 12 years ago

#1937

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