Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4435 was 4435, checked in by mkommend, 13 years ago

Adjusted the SetEnabledStateOfControls method in all views, added the Enabled property into the IView interface and adapted the ViewHost, View and ContentView class (ticket #1155).

File size: 9.0 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.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
64              view.Size = new System.Drawing.Size(this.Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
65              view.OnShown(new ViewShownEventArgs(view, false));
66              Controls.Add(view);
67            }
68          } else viewType = null;
69        }
70      }
71    }
72    private Control ActiveViewControl {
73      get { return ActiveView as Control; }
74    }
75
76    private Type viewType;
77    public Type ViewType {
78      get { return this.viewType; }
79      set {
80        if (viewType != value) {
81          if (value != null && Content != null && !ViewCanShowContent(value, Content))
82            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
83                                                      value, Content.GetType()));
84          viewType = value;
85          OnViewTypeChanged();
86        }
87      }
88    }
89
90    protected override void OnContentChanged() {
91      viewContextMenuStrip.Item = Content;
92      //change ViewType if view of ViewType can not show content or is null
93      if (Content != null) {
94        if (!ViewCanShowContent(viewType, Content)) {
95          Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
96          if (defaultViewType != null)
97            ViewType = defaultViewType;
98          else if (viewContextMenuStrip.Items.Count > 0)  // create first available view if no default view is available
99            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
100          else {
101            ViewType = null;
102            ActiveView = null;
103          }
104        }
105        if (ActiveView != null) ActiveView.Content = Content;
106      } else ActiveView = null;
107      UpdateLabels();
108      UpdateActiveMenuItem();
109    }
110
111    private void UpdateLabels() {
112      if (Content != null && viewContextMenuStrip.Items.Count > 0) {
113        messageLabel.Visible = false;
114        viewsLabel.Visible = true;
115      } else if (Content != null) {
116        messageLabel.Visible = true;
117        viewsLabel.Visible = false;
118      } else {
119        messageLabel.Visible = false;
120        viewsLabel.Visible = false;
121      }
122    }
123
124    private void OnViewTypeChanged() {
125      if (viewType != null) {
126        if (!ViewCanShowContent(viewType, Content))
127          throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
128                                                            viewType, Content.GetType()));
129        IContentView view;
130        view = MainFormManager.CreateView(viewType);
131        view.Locked = this.Locked;
132        view.ReadOnly = this.ReadOnly;
133        ActiveView = view; //necessary to allow the views to change the status of the viewhost
134        view.Content = Content;
135
136        UpdateActiveMenuItem();
137      }
138    }
139
140    private void RegisterActiveViewEvents() {
141      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
142      activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
143    }
144    private void DeregisterActiveViewEvents() {
145      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
146      activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
147    }
148    private void activeView_CaptionChanged(object sender, EventArgs e) {
149      this.Caption = activeView.Caption;
150    }
151    private void activeView_LockedChanged(object sender, EventArgs e) {
152      this.Locked = activeView.Locked;
153    }
154
155    protected override void OnSizeChanged(EventArgs e) {
156      //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
157      //not implemented with a panel to reduce the number of nested controls
158      if (this.Handle != null)
159        this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
160    }
161    private void OnSizeChangedHelper(EventArgs e) {
162      base.OnSizeChanged(e);
163      this.viewsLabel.Location = new System.Drawing.Point(this.Width - this.viewsLabel.Margin.Right - this.viewsLabel.Width, this.viewsLabel.Margin.Top);
164    }
165
166    #region forwarding of view events
167    internal protected override void OnShown(ViewShownEventArgs e) {
168      base.OnShown(e);
169      View view = this.ActiveView as View;
170      if (view != null)
171        view.OnShown(e);
172    }
173    internal protected override void OnHidden(EventArgs e) {
174      base.OnHidden(e);
175      View view = this.ActiveView as View;
176      if (view != null)
177        view.OnHidden(e);
178    }
179    internal protected override void OnClosing(FormClosingEventArgs e) {
180      base.OnClosing(e);
181      View view = ActiveView as View;
182      if (view != null)
183        view.OnClosing(e);
184    }
185    internal protected override void OnClosed(FormClosedEventArgs e) {
186      base.OnClosed(e);
187      View view = ActiveView as View;
188      if (view != null)
189        view.OnClosed(e);
190    }
191    #endregion
192
193    #region GUI actions
194    private void UpdateActiveMenuItem() {
195      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
196        if (item.Key == viewType) {
197          item.Value.Checked = true;
198          item.Value.Enabled = false;
199        } else {
200          item.Value.Checked = false;
201          item.Value.Enabled = true;
202        }
203      }
204    }
205
206    private bool ViewCanShowContent(Type viewType, object content) {
207      if (content == null) // every view can display null
208        return true;
209      if (viewType == null)
210        return false;
211      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
212    }
213
214    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
215      IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
216      if (view != null) {
217        view.ReadOnly = this.ReadOnly;
218        view.Locked = this.Locked;
219      }
220    }
221    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
222      Type viewType = (Type)e.ClickedItem.Tag;
223      ViewType = viewType;
224    }
225
226    private bool startDragAndDrop;
227    private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
228      if (!Locked) {
229        startDragAndDrop = true;
230        viewsLabel.Capture = false;
231        viewsLabel.Focus();
232      }
233    }
234    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
235      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
236        DataObject data = new DataObject();
237        data.SetData("Type", Content.GetType());
238        data.SetData("Value", Content);
239        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
240      } else
241        startDragAndDrop = false;
242    }
243    #endregion
244  }
245}
Note: See TracBrowser for help on using the repository browser.