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

Last change on this file since 3646 was 3646, checked in by mkommend, 12 years ago

added checks of viewpanel size in ViewHost (ticket #972)

File size: 9.2 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.MainForm;
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      cachedViews = new Dictionary<Type, IContentView>();
35      viewType = null;
36      startDragAndDrop = false;
37      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
38      activeView = null;
39      Content = null;
40      OnContentChanged();
41    }
42
43    private Dictionary<Type, IContentView> cachedViews;
44    public IEnumerable<IContentView> Views {
45      get { return cachedViews.Values; }
46    }
47
48    private IContentView activeView;
49    public IContentView ActiveView {
50      get { return this.activeView; }
51      private set {
52        if (activeView != value) {
53          if (activeView != null) {
54            DeregisterActiveViewEvents();
55            View view = activeView as View;
56            if (view != null)
57              view.OnHidden(EventArgs.Empty);
58          }
59          activeView = value;
60          if (activeView != null) {
61            RegisterActiveViewEvents();
62            View view = activeView as View;
63            if (view != null)
64              view.OnShown(new ViewShownEventArgs(view, false));
65          }
66          ActiveViewChanged();
67          OnViewTypeChanged();
68        }
69      }
70    }
71    private Type viewType;
72    public Type ViewType {
73      get { return this.viewType; }
74      set {
75        if (viewType != value) {
76          if (value != null && !ViewCanShowContent(value, Content))
77            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
78                                                      value, Content.GetType()));
79          viewType = value;
80          OnViewTypeChanged();
81        }
82      }
83    }
84    public new IContent Content {
85      get { return base.Content; }
86      set {
87        if (value == null || this.Content == null || value.GetType() != this.Content.GetType())
88          cachedViews.Clear();
89
90        base.Content = value;
91      }
92    }
93
94    public new bool Enabled {
95      get { return base.Enabled; }
96      set {
97        this.SuspendRepaint();
98        base.Enabled = value;
99        this.viewsLabel.Enabled = value;
100        this.ResumeRepaint(true);
101      }
102    }
103
104    protected override void OnContentChanged() {
105      messageLabel.Visible = false;
106      viewsLabel.Visible = false;
107      viewPanel.Visible = false;
108      viewContextMenuStrip.Item = Content;
109
110      if (Content != null) {
111        if (viewContextMenuStrip.Items.Count == 0)
112          messageLabel.Visible = true;
113        else {
114          viewsLabel.Visible = true;
115          viewPanel.Visible = true;
116        }
117
118        if (!ViewCanShowContent(viewType, Content)) {
119          ViewType = MainFormManager.GetDefaultViewType(Content.GetType());
120          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
121            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
122        }
123        UpdateActiveMenuItem();
124        foreach (IContentView view in cachedViews.Values)
125          view.Content = this.Content;
126      } else {
127        if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
128        viewPanel.Controls.Clear();
129        cachedViews.Clear();
130      }
131    }
132
133    private void OnViewTypeChanged() {
134      viewPanel.Controls.Clear();
135      if (viewType == null || Content == null)
136        return;
137      if (!ViewCanShowContent(viewType, Content))
138        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
139                                                          viewType, Content.GetType()));
140      if (viewPanel.Height <= 10 || viewPanel.Width <= 10)
141        return;
142
143      UpdateActiveMenuItem();
144      IContentView view;
145      if (cachedViews.ContainsKey(ViewType))
146        view = cachedViews[ViewType];
147      else {
148        view = MainFormManager.CreateView(viewType);
149        view.ReadOnly = this.ReadOnly;
150        view.Locked = this.Locked;
151        cachedViews.Add(viewType, view);
152      }
153      this.ActiveView = view;
154      Control control = (Control)view;
155      control.Dock = DockStyle.Fill;
156      viewPanel.Controls.Add(control);
157      viewPanel.Visible = true;
158      view.Content = Content;
159    }
160
161    private void RegisterActiveViewEvents() {
162      activeView.Changed += new EventHandler(activeView_Changed);
163      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
164    }
165    private void DeregisterActiveViewEvents() {
166      activeView.Changed -= new EventHandler(activeView_Changed);
167      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
168    }
169    private void activeView_CaptionChanged(object sender, EventArgs e) {
170      this.ActiveViewChanged();
171    }
172    private void activeView_Changed(object sender, EventArgs e) {
173      this.ActiveViewChanged();
174    }
175    private void ActiveViewChanged() {
176      if (ActiveView != null) {
177        this.Caption = this.ActiveView.Caption;
178        this.Locked = this.ActiveView.Locked;
179      }
180    }
181    #region forwarding of view events
182    protected override void OnReadOnlyChanged() {
183      base.OnReadOnlyChanged();
184      foreach (IContentView view in cachedViews.Values)
185        view.ReadOnly = this.ReadOnly;
186    }
187    protected override void OnLockedChanged() {
188      base.OnLockedChanged();
189      foreach (IContentView view in cachedViews.Values)
190        view.Locked = this.Locked;
191    }
192    internal protected override void OnShown(ViewShownEventArgs e) {
193      base.OnShown(e);
194      View view = this.ActiveView as View;
195      if (view != null)
196        view.OnShown(e);
197    }
198    internal protected override void OnHidden(EventArgs e) {
199      base.OnHidden(e);
200      View view = this.ActiveView as View;
201      if (view != null)
202        view.OnHidden(e);
203    }
204    internal protected override void OnClosing(FormClosingEventArgs e) {
205      base.OnClosing(e);
206      foreach (View view in this.Views.OfType<View>())
207        view.OnClosing(e);
208    }
209    internal protected override void OnClosed(FormClosedEventArgs e) {
210      base.OnClosed(e);
211      foreach (View view in this.Views.OfType<View>())
212        view.OnClosed(e);
213    }
214    #endregion
215
216    #region GUI actions
217    private void UpdateActiveMenuItem() {
218      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
219        if (item.Key == viewType) {
220          item.Value.Checked = true;
221          item.Value.Enabled = false;
222        } else {
223          item.Value.Checked = false;
224          item.Value.Enabled = true;
225        }
226      }
227    }
228
229    private bool ViewCanShowContent(Type viewType, object content) {
230      if (content == null) // every view can display null
231        return true;
232      if (viewType == null)
233        return false;
234      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
235    }
236
237    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
238      IContentView view = MainFormManager.MainForm.ShowContent(this.Content);
239      view.ReadOnly = this.ReadOnly;
240      view.Locked = this.Locked;
241    }
242    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
243      Type viewType = (Type)e.ClickedItem.Tag;
244      ViewType = viewType;
245    }
246
247    private bool startDragAndDrop;
248    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
249      if (!Locked) {
250        startDragAndDrop = true;
251        viewsLabel.Capture = false;
252        viewsLabel.Focus();
253      }
254    }
255    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
256      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
257        DataObject data = new DataObject();
258        data.SetData("Type", Content.GetType());
259        data.SetData("Value", Content);
260        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
261      } else
262        startDragAndDrop = false;
263    }
264    #endregion
265  }
266}
Note: See TracBrowser for help on using the repository browser.