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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 |
|
---|
28 | namespace 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) ActiveView.Content = Content;
|
---|
113 | } else ActiveView = null;
|
---|
114 | UpdateLabels();
|
---|
115 | UpdateActiveMenuItem();
|
---|
116 | }
|
---|
117 |
|
---|
118 | private void UpdateLabels() {
|
---|
119 | if (Content != null && viewContextMenuStrip.Items.Count > 0) {
|
---|
120 | messageLabel.Visible = false;
|
---|
121 | viewsLabel.Visible = true;
|
---|
122 | } else if (Content != null) {
|
---|
123 | messageLabel.Visible = true;
|
---|
124 | viewsLabel.Visible = false;
|
---|
125 | } else {
|
---|
126 | messageLabel.Visible = false;
|
---|
127 | viewsLabel.Visible = false;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void OnViewTypeChanged() {
|
---|
132 | if (viewType != null) {
|
---|
133 | if (!ViewCanShowContent(viewType, Content))
|
---|
134 | throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
135 | viewType, Content.GetType()));
|
---|
136 | IContentView view;
|
---|
137 | view = MainFormManager.CreateView(viewType);
|
---|
138 | view.ReadOnly = this.ReadOnly;
|
---|
139 | view.Locked = this.Locked;
|
---|
140 | ActiveView = view; //necessary to allow the views to change the status of the viewhost
|
---|
141 | view.Content = Content;
|
---|
142 |
|
---|
143 | UpdateActiveMenuItem();
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void OnActiveViewChanged() {
|
---|
148 | this.SuspendRepaint();
|
---|
149 | if (activeView != null) {
|
---|
150 | this.ActiveView.ReadOnly = this.ReadOnly;
|
---|
151 | this.ActiveView.Locked = this.Locked;
|
---|
152 | this.ActiveViewControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
|
---|
153 | this.ActiveViewControl.Size = new System.Drawing.Size(this.Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
|
---|
154 | }
|
---|
155 | this.ResumeRepaint(true);
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void RegisterActiveViewEvents() {
|
---|
159 | activeView.Changed += new EventHandler(activeView_Changed);
|
---|
160 | activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
|
---|
161 | }
|
---|
162 | private void DeregisterActiveViewEvents() {
|
---|
163 | activeView.Changed -= new EventHandler(activeView_Changed);
|
---|
164 | activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
|
---|
165 | }
|
---|
166 | private void activeView_CaptionChanged(object sender, EventArgs e) {
|
---|
167 | this.ActiveViewChanged();
|
---|
168 | }
|
---|
169 | private void activeView_Changed(object sender, EventArgs e) {
|
---|
170 | this.ActiveViewChanged();
|
---|
171 | }
|
---|
172 | private void ActiveViewChanged() {
|
---|
173 | if (ActiveView != null) {
|
---|
174 | this.Caption = this.ActiveView.Caption;
|
---|
175 | this.ReadOnly = this.ActiveView.ReadOnly;
|
---|
176 | this.Locked = this.ActiveView.Locked;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | protected override void OnSizeChanged(EventArgs e) {
|
---|
181 | //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
|
---|
182 | //not implemented with a panel to reduce the number of nested controls
|
---|
183 | if (this.Handle != null)
|
---|
184 | this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
|
---|
185 | }
|
---|
186 | private void OnSizeChangedHelper(EventArgs e) {
|
---|
187 | base.OnSizeChanged(e);
|
---|
188 | this.viewsLabel.Location = new System.Drawing.Point(this.Width - this.viewsLabel.Margin.Right - this.viewsLabel.Width, this.viewsLabel.Margin.Top);
|
---|
189 | }
|
---|
190 |
|
---|
191 | #region forwarding of view events
|
---|
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 | View view = ActiveView as View;
|
---|
207 | if (view != null)
|
---|
208 | view.OnClosing(e);
|
---|
209 | }
|
---|
210 | internal protected override void OnClosed(FormClosedEventArgs e) {
|
---|
211 | base.OnClosed(e);
|
---|
212 | View view = ActiveView as View;
|
---|
213 | if (view != null)
|
---|
214 | view.OnClosed(e);
|
---|
215 | }
|
---|
216 | #endregion
|
---|
217 |
|
---|
218 | #region GUI actions
|
---|
219 | private void UpdateActiveMenuItem() {
|
---|
220 | foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
|
---|
221 | if (item.Key == viewType) {
|
---|
222 | item.Value.Checked = true;
|
---|
223 | item.Value.Enabled = false;
|
---|
224 | } else {
|
---|
225 | item.Value.Checked = false;
|
---|
226 | item.Value.Enabled = true;
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | private bool ViewCanShowContent(Type viewType, object content) {
|
---|
232 | if (content == null) // every view can display null
|
---|
233 | return true;
|
---|
234 | if (viewType == null)
|
---|
235 | return false;
|
---|
236 | return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
|
---|
237 | }
|
---|
238 |
|
---|
239 | private void viewsLabel_DoubleClick(object sender, EventArgs e) {
|
---|
240 | IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
|
---|
241 | if (view != null) {
|
---|
242 | view.ReadOnly = this.ReadOnly;
|
---|
243 | view.Locked = this.Locked;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
|
---|
247 | Type viewType = (Type)e.ClickedItem.Tag;
|
---|
248 | ViewType = viewType;
|
---|
249 | }
|
---|
250 |
|
---|
251 | private bool startDragAndDrop;
|
---|
252 | private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
|
---|
253 | if (!Locked) {
|
---|
254 | startDragAndDrop = true;
|
---|
255 | viewsLabel.Capture = false;
|
---|
256 | viewsLabel.Focus();
|
---|
257 | }
|
---|
258 | }
|
---|
259 | private void viewsLabel_MouseLeave(object sender, EventArgs e) {
|
---|
260 | if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
|
---|
261 | DataObject data = new DataObject();
|
---|
262 | data.SetData("Type", Content.GetType());
|
---|
263 | data.SetData("Value", Content);
|
---|
264 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
265 | } else
|
---|
266 | startDragAndDrop = false;
|
---|
267 | }
|
---|
268 | #endregion
|
---|
269 | }
|
---|
270 | }
|
---|