Free cookie consent management tool by TermsFeed Policy Generator

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

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

Corrected bug in the ViewHost to handle contents without views correctly (ticket #1133)

File size: 9.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.MainForm.WindowsForms {
29  [Content(typeof(IContent))]
30  public sealed partial class ViewHost : AsynchronousContentView {
31    public ViewHost() {
32      InitializeComponent();
33      startDragAndDrop = false;
34      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
35
36      viewType = null;
37      activeView = null;
38      Content = null;
39      messageLabel.Visible = false;
40      viewsLabel.Visible = false;
41    }
42
43    private IContentView activeView;
44    public IContentView ActiveView {
45      get { return this.activeView; }
46      private set {
47        if (activeView != value) {
48          if (activeView != null) {
49            DeregisterActiveViewEvents();
50            View view = activeView as View;
51            if (view != null) {
52              view.OnHidden(EventArgs.Empty);
53              Controls.Remove(view);
54              view.Dispose();
55            }
56          }
57          activeView = value;
58          if (activeView != null) {
59            viewType = activeView.GetType();
60            RegisterActiveViewEvents();
61            View view = activeView as View;
62            if (view != null) {
63              view.OnShown(new ViewShownEventArgs(view, false));
64              Controls.Add(view);
65            }
66          } else viewType = null;
67          OnActiveViewChanged();
68        }
69      }
70    }
71    private Control ActiveViewControl {
72      get { return ActiveView as Control; }
73    }
74
75    private Type viewType;
76    public Type ViewType {
77      get { return this.viewType; }
78      set {
79        if (viewType != value) {
80          if (value != null && Content != null && !ViewCanShowContent(value, Content))
81            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
82                                                      value, Content.GetType()));
83          viewType = value;
84          OnViewTypeChanged();
85        }
86      }
87    }
88
89    public new bool Enabled {
90      get { return base.Enabled; }
91      set {
92        base.Enabled = value;
93        this.viewsLabel.Enabled = value;
94      }
95    }
96
97    protected override void OnContentChanged() {
98      viewContextMenuStrip.Item = Content;
99      //change ViewType if view of ViewType can not show content or is null
100      if (Content != null) {
101        if (!ViewCanShowContent(viewType, Content)) {
102          Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
103          if (defaultViewType != null)
104            ViewType = defaultViewType;
105          else if (viewContextMenuStrip.Items.Count > 0)  // create first available view if no default view is available
106            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
107          else {
108            ViewType = null;
109            ActiveView = null;
110          }
111        }
112        if (ActiveView != null) {
113          ActiveView.Content = Content;
114          if (ActiveViewControl != null) ActiveViewControl.Visible = true;
115        }
116      } else if (ActiveViewControl != null)
117        ActiveViewControl.Visible = false;
118
119      UpdateLabels();
120    }
121
122    private void UpdateLabels() {
123      if (Content != null && viewContextMenuStrip.Items.Count > 0) {
124        messageLabel.Visible = false;
125        viewsLabel.Visible = true;
126      } else if (Content != null) {
127        messageLabel.Visible = true;
128        viewsLabel.Visible = false;
129      } else {
130        messageLabel.Visible = false;
131        viewsLabel.Visible = false;
132      }
133    }
134
135    private void OnViewTypeChanged() {
136      if (viewType != null) {
137        if (!ViewCanShowContent(viewType, Content))
138          throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
139                                                            viewType, Content.GetType()));
140        IContentView view;
141        view = MainFormManager.CreateView(viewType);
142        view.ReadOnly = this.ReadOnly;
143        view.Locked = this.Locked;
144        ActiveView = view; //necessary to allow the views to change the status of the viewhost
145        view.Content = Content;
146
147        UpdateActiveMenuItem();
148      }
149    }
150
151    private void OnActiveViewChanged() {
152      this.SuspendRepaint();
153      if (activeView != null) {
154        UpdateActiveMenuItem();
155        this.ActiveView.ReadOnly = this.ReadOnly;
156        this.ActiveView.Locked = this.Locked;
157        this.ActiveViewControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
158        this.ActiveViewControl.Size = new System.Drawing.Size(this.Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
159      }
160      this.ResumeRepaint(true);
161    }
162
163    private void RegisterActiveViewEvents() {
164      activeView.Changed += new EventHandler(activeView_Changed);
165      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
166    }
167    private void DeregisterActiveViewEvents() {
168      activeView.Changed -= new EventHandler(activeView_Changed);
169      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
170    }
171    private void activeView_CaptionChanged(object sender, EventArgs e) {
172      this.ActiveViewChanged();
173    }
174    private void activeView_Changed(object sender, EventArgs e) {
175      this.ActiveViewChanged();
176    }
177    private void ActiveViewChanged() {
178      if (ActiveView != null) {
179        this.Caption = this.ActiveView.Caption;
180        this.ReadOnly = this.ActiveView.ReadOnly;
181        this.Locked = this.ActiveView.Locked;
182      }
183    }
184
185    protected override void OnSizeChanged(EventArgs e) {
186      //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
187      //not implemented with a panel to reduce the number of nested controls
188      if (this.Handle != null)
189        this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
190    }
191    private void OnSizeChangedHelper(EventArgs e) {
192      base.OnSizeChanged(e);
193      this.viewsLabel.Location = new System.Drawing.Point(this.Width - this.viewsLabel.Margin.Right - this.viewsLabel.Width, this.viewsLabel.Margin.Top);
194    }
195
196    #region forwarding of view events
197    internal protected override void OnShown(ViewShownEventArgs e) {
198      base.OnShown(e);
199      View view = this.ActiveView as View;
200      if (view != null)
201        view.OnShown(e);
202    }
203    internal protected override void OnHidden(EventArgs e) {
204      base.OnHidden(e);
205      View view = this.ActiveView as View;
206      if (view != null)
207        view.OnHidden(e);
208    }
209    internal protected override void OnClosing(FormClosingEventArgs e) {
210      base.OnClosing(e);
211      View view = ActiveView as View;
212      if (view != null)
213        view.OnClosing(e);
214    }
215    internal protected override void OnClosed(FormClosedEventArgs e) {
216      base.OnClosed(e);
217      View view = ActiveView as View;
218      if (view != null)
219        view.OnClosed(e);
220    }
221    #endregion
222
223    #region GUI actions
224    private void UpdateActiveMenuItem() {
225      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
226        if (item.Key == viewType) {
227          item.Value.Checked = true;
228          item.Value.Enabled = false;
229        } else {
230          item.Value.Checked = false;
231          item.Value.Enabled = true;
232        }
233      }
234    }
235
236    private bool ViewCanShowContent(Type viewType, object content) {
237      if (content == null) // every view can display null
238        return true;
239      if (viewType == null)
240        return false;
241      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
242    }
243
244    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
245      IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
246      if (view != null) {
247        view.ReadOnly = this.ReadOnly;
248        view.Locked = this.Locked;
249      }
250    }
251    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
252      Type viewType = (Type)e.ClickedItem.Tag;
253      ViewType = viewType;
254    }
255
256    private bool startDragAndDrop;
257    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
258      if (!Locked) {
259        startDragAndDrop = true;
260        viewsLabel.Capture = false;
261        viewsLabel.Focus();
262      }
263    }
264    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
265      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
266        DataObject data = new DataObject();
267        data.SetData("Type", Content.GetType());
268        data.SetData("Value", Content);
269        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
270      } else
271        startDragAndDrop = false;
272    }
273    #endregion
274  }
275}
Note: See TracBrowser for help on using the repository browser.