1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 |
|
---|
29 | namespace 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 | breadcrumbControl.ViewHost = this;
|
---|
45 | }
|
---|
46 |
|
---|
47 | private bool viewsLabelVisible;
|
---|
48 | public bool ViewsLabelVisible {
|
---|
49 | get { return viewsLabelVisible; }
|
---|
50 | set {
|
---|
51 | if (viewsLabelVisible != value) {
|
---|
52 | viewsLabelVisible = value;
|
---|
53 | viewsLabel.Visible = value;
|
---|
54 | View view = activeView as View;
|
---|
55 | if (view != null) AdjustActiveViewSize();
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private IContentView cachedView;
|
---|
61 | private IContentView activeView;
|
---|
62 | public IContentView ActiveView {
|
---|
63 | get { return activeView; }
|
---|
64 | private set {
|
---|
65 | if (activeView != value) {
|
---|
66 | if (activeView != null) {
|
---|
67 | cachedView = activeView;
|
---|
68 | DeregisterActiveViewEvents();
|
---|
69 | View cached = cachedView as View;
|
---|
70 | if (cached != null) {
|
---|
71 | cached.OnHidden(EventArgs.Empty);
|
---|
72 | cached.Visible = false;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | activeView = value;
|
---|
77 |
|
---|
78 | if (activeView != null) {
|
---|
79 | #region dispose cachedView
|
---|
80 | if (activeView != cachedView) {
|
---|
81 | if (cachedView != null) cachedView.Content = null; //needed to deregister events
|
---|
82 | View cached = cachedView as View;
|
---|
83 | if (cached != null) {
|
---|
84 | Controls.Remove(cached);
|
---|
85 | cached.Dispose();
|
---|
86 | }
|
---|
87 | cachedView = null;
|
---|
88 | }
|
---|
89 | #endregion
|
---|
90 |
|
---|
91 | Caption = activeView.Caption;
|
---|
92 | viewType = activeView.GetType();
|
---|
93 | RegisterActiveViewEvents();
|
---|
94 | View view = activeView as View;
|
---|
95 | if (view != null) {
|
---|
96 | view.Visible = true;
|
---|
97 | AdjustActiveViewSize();
|
---|
98 | if (!Controls.Contains((view))) Controls.Add(view);
|
---|
99 | view.OnShown(new ViewShownEventArgs(view, false));
|
---|
100 | }
|
---|
101 | } else viewType = null;
|
---|
102 | configurationLabel.Visible = activeView is IConfigureableView;
|
---|
103 | configurationLabel.Enabled = activeView != null && !activeView.Locked;
|
---|
104 |
|
---|
105 | helpLabel.Visible = activeView != null && ViewAttribute.HasHelpResourcePath(activeView.GetType());
|
---|
106 | helpLabel.Top = CalculateHelpLabelPosY();
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | private Type viewType;
|
---|
112 | public Type ViewType {
|
---|
113 | get { return viewType; }
|
---|
114 | set {
|
---|
115 | if (viewType != value) {
|
---|
116 | if (value == typeof(ViewHost))
|
---|
117 | throw new ArgumentException("Directly nested ViewHosts are not allowed.");
|
---|
118 | if (value != null && Content != null && !ViewCanShowContent(value, Content))
|
---|
119 | throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".", value, Content.GetType()));
|
---|
120 |
|
---|
121 | viewType = value;
|
---|
122 | OnViewTypeChanged();
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | public bool ShowBreadcrumbs {
|
---|
128 | get { return viewContextMenuStrip.ShowBreadcrumbsToolStripMenuItem.Checked; }
|
---|
129 | set { viewContextMenuStrip.ShowBreadcrumbsToolStripMenuItem.Checked = value; }
|
---|
130 | }
|
---|
131 |
|
---|
132 | private bool isOutmostViewHost;
|
---|
133 | public IEnumerable<IContent> Breadcrumbs { get { return breadcrumbControl.Breadcrumbs; } }
|
---|
134 |
|
---|
135 | protected override void SetEnabledStateOfControls() {
|
---|
136 | Enabled = Content != null;
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected override void OnContentChanged() {
|
---|
140 | viewContextMenuStrip.Item = Content;
|
---|
141 | //change ViewType if view of ViewType can not show content or is null
|
---|
142 | if (Content != null) {
|
---|
143 | if (!ViewCanShowContent(viewType, Content)) {
|
---|
144 | Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
|
---|
145 | if (cachedView != null && cachedView.GetType() == defaultViewType)
|
---|
146 | ActiveView = cachedView;
|
---|
147 | else if (defaultViewType != null)
|
---|
148 | ViewType = defaultViewType;
|
---|
149 | else if (viewContextMenuStrip.Items.Count > 0) // create first available view if no default view is available
|
---|
150 | ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
|
---|
151 | else {
|
---|
152 | ViewType = null;
|
---|
153 | ActiveView = null;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | if (ActiveView != null) ActiveView.Content = Content;
|
---|
157 | } else ActiveView = null;
|
---|
158 | UpdateLabels();
|
---|
159 | UpdateActiveMenuItem();
|
---|
160 | UpdateBreadcrumbControl();
|
---|
161 | }
|
---|
162 |
|
---|
163 | private void UpdateLabels() {
|
---|
164 | if (Content != null && viewContextMenuStrip.Items.Count > 0) {
|
---|
165 | messageLabel.Visible = false;
|
---|
166 | viewsLabel.Visible = viewsLabelVisible;
|
---|
167 | } else if (Content != null) {
|
---|
168 | messageLabel.Visible = true;
|
---|
169 | viewsLabel.Visible = false;
|
---|
170 | } else {
|
---|
171 | messageLabel.Visible = false;
|
---|
172 | viewsLabel.Visible = false;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | private void OnViewTypeChanged() {
|
---|
177 | if (viewType != null) {
|
---|
178 | if (!ViewCanShowContent(viewType, Content))
|
---|
179 | throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
180 | viewType, Content.GetType()));
|
---|
181 | IContentView view = MainFormManager.CreateView(viewType);
|
---|
182 | view.Locked = Locked;
|
---|
183 | view.ReadOnly = ReadOnly;
|
---|
184 | ActiveView = view; //necessary to allow the views to change the status of the viewhost
|
---|
185 | view.Content = Content;
|
---|
186 |
|
---|
187 | UpdateActiveMenuItem();
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | private void RegisterActiveViewEvents() {
|
---|
192 | activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
|
---|
193 | activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
|
---|
194 | activeView.Changed += new EventHandler(activeView_Changed);
|
---|
195 | }
|
---|
196 | private void DeregisterActiveViewEvents() {
|
---|
197 | activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
|
---|
198 | activeView.LockedChanged -= new EventHandler(activeView_LockedChanged);
|
---|
199 | activeView.Changed -= new EventHandler(activeView_Changed);
|
---|
200 | }
|
---|
201 | private void activeView_CaptionChanged(object sender, EventArgs e) {
|
---|
202 | Caption = activeView.Caption;
|
---|
203 | }
|
---|
204 | private void activeView_LockedChanged(object sender, EventArgs e) {
|
---|
205 | Locked = activeView.Locked;
|
---|
206 | configurationLabel.Enabled = !activeView.Locked;
|
---|
207 | }
|
---|
208 | private void activeView_Changed(object sender, EventArgs e) {
|
---|
209 | OnChanged();
|
---|
210 | }
|
---|
211 | private void ViewHost_VisibleChanged(object sender, EventArgs e) {
|
---|
212 | PerformOuterViewHostDetection();
|
---|
213 | }
|
---|
214 | private void viewContextMenuStrip_ShowBreadcrumbsChanged(object sender, EventArgs e) {
|
---|
215 | UpdateBreadcrumbControl();
|
---|
216 | AdjustActiveViewSize();
|
---|
217 | }
|
---|
218 |
|
---|
219 | protected override void OnSizeChanged(EventArgs e) {
|
---|
220 | //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
|
---|
221 | //not implemented with a panel to reduce the number of nested controls
|
---|
222 | //also cf. http://connect.microsoft.com/VisualStudio/feedback/details/98368/csc-incorrectly-allows-comparison-between-intptr-and-null
|
---|
223 | if (Handle != IntPtr.Zero)
|
---|
224 | this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
|
---|
225 | }
|
---|
226 | private void OnSizeChangedHelper(EventArgs e) {
|
---|
227 | base.OnSizeChanged(e);
|
---|
228 | viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
|
---|
229 | configurationLabel.Location = new Point(Width - configurationLabel.Margin.Right - configurationLabel.Width, viewsLabel.Bottom + viewsLabel.Margin.Bottom + configurationLabel.Margin.Top);
|
---|
230 | helpLabel.Location = new Point(Width - helpLabel.Margin.Right - helpLabel.Width, CalculateHelpLabelPosY());
|
---|
231 | }
|
---|
232 |
|
---|
233 | private int CalculateHelpLabelPosY() {
|
---|
234 | if (activeView != null && ViewAttribute.HasHelpResourcePath(activeView.GetType()) && !configurationLabel.Visible) {
|
---|
235 | return configurationLabel.Top;
|
---|
236 | }
|
---|
237 | return configurationLabel.Bottom + configurationLabel.Margin.Bottom + helpLabel.Margin.Top;
|
---|
238 | }
|
---|
239 |
|
---|
240 | #region forwarding of view events
|
---|
241 | internal protected override void OnShown(ViewShownEventArgs e) {
|
---|
242 | base.OnShown(e);
|
---|
243 | View view = ActiveView as View;
|
---|
244 | if (view != null)
|
---|
245 | view.OnShown(e);
|
---|
246 | }
|
---|
247 | internal protected override void OnHidden(EventArgs e) {
|
---|
248 | base.OnHidden(e);
|
---|
249 | View view = ActiveView as View;
|
---|
250 | if (view != null)
|
---|
251 | view.OnHidden(e);
|
---|
252 | }
|
---|
253 | internal protected override void OnClosing(FormClosingEventArgs e) {
|
---|
254 | base.OnClosing(e);
|
---|
255 | View view = ActiveView as View;
|
---|
256 | if (view != null)
|
---|
257 | view.OnClosing(e);
|
---|
258 | }
|
---|
259 | internal protected override void OnClosed(FormClosedEventArgs e) {
|
---|
260 | base.OnClosed(e);
|
---|
261 | View view = ActiveView as View;
|
---|
262 | if (view != null)
|
---|
263 | view.OnClosed(e);
|
---|
264 | }
|
---|
265 | #endregion
|
---|
266 |
|
---|
267 | #region GUI actions
|
---|
268 | private void UpdateActiveMenuItem() {
|
---|
269 | foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
|
---|
270 | if (item.Key == viewType) {
|
---|
271 | item.Value.Checked = true;
|
---|
272 | item.Value.Enabled = false;
|
---|
273 | } else {
|
---|
274 | item.Value.Checked = false;
|
---|
275 | item.Value.Enabled = true;
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | private void UpdateBreadcrumbControl() {
|
---|
281 | breadcrumbControl.Visible = ShowBreadcrumbs;
|
---|
282 | if (ShowBreadcrumbs)
|
---|
283 | UpdateBreadcrumbTrail(breadcrumbControl.Breadcrumbs, BuildBreadcrumbTrail());
|
---|
284 | }
|
---|
285 |
|
---|
286 | private bool ViewCanShowContent(Type viewType, object content) {
|
---|
287 | if (content == null) // every view can display null
|
---|
288 | return true;
|
---|
289 | if (viewType == null)
|
---|
290 | return false;
|
---|
291 | return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
|
---|
292 | }
|
---|
293 |
|
---|
294 | private void viewsLabel_DoubleClick(object sender, EventArgs e) {
|
---|
295 | IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
|
---|
296 | if (view != null) {
|
---|
297 | view.ReadOnly = ReadOnly;
|
---|
298 | view.Locked = Locked;
|
---|
299 | }
|
---|
300 | }
|
---|
301 | private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
|
---|
302 | Type viewType = (Type)e.ClickedItem.Tag;
|
---|
303 | ViewType = viewType;
|
---|
304 | }
|
---|
305 |
|
---|
306 | private bool startDragAndDrop;
|
---|
307 | private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
|
---|
308 | if (e.Button == MouseButtons.Right) {
|
---|
309 | Screen screen = Screen.FromControl(viewsLabel);
|
---|
310 | int rightBorder = viewsLabel.PointToScreen(viewsLabel.Location).X + viewContextMenuStrip.Width;
|
---|
311 | rightBorder = rightBorder - screen.Bounds.X; //pixel position on active screen
|
---|
312 |
|
---|
313 | if (rightBorder < screen.Bounds.Width)
|
---|
314 | viewContextMenuStrip.Show(viewsLabel, viewsLabel.Margin.Left, viewsLabel.Margin.Top);
|
---|
315 | else
|
---|
316 | viewContextMenuStrip.Show(screen.Bounds.X + screen.Bounds.Width - viewContextMenuStrip.Width, viewsLabel.PointToScreen(viewsLabel.Location).Y - viewsLabel.Margin.Top);
|
---|
317 | } else if (!Locked) {
|
---|
318 | startDragAndDrop = true;
|
---|
319 | viewsLabel.Capture = false;
|
---|
320 | viewsLabel.Focus();
|
---|
321 | }
|
---|
322 | }
|
---|
323 | private void viewsLabel_MouseLeave(object sender, EventArgs e) {
|
---|
324 | if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
|
---|
325 | DataObject data = new DataObject();
|
---|
326 | data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, Content);
|
---|
327 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
328 | } else
|
---|
329 | startDragAndDrop = false;
|
---|
330 | }
|
---|
331 |
|
---|
332 | private void configurationLabel_DoubleClick(object sender, MouseEventArgs e) {
|
---|
333 | ((IConfigureableView)ActiveView).ShowConfiguration();
|
---|
334 | }
|
---|
335 |
|
---|
336 | private void helpLabel_DoubleClick(object sender, EventArgs e) {
|
---|
337 | using (InfoBox dialog = new InfoBox("Help for " + ViewAttribute.GetViewName(ActiveView.GetType()), ViewAttribute.GetHelpResourcePath(ActiveView.GetType()), ActiveView)) {
|
---|
338 | dialog.ShowDialog(this);
|
---|
339 | }
|
---|
340 | }
|
---|
341 | #endregion
|
---|
342 |
|
---|
343 | #region Helpers
|
---|
344 | private void AdjustActiveViewSize() {
|
---|
345 | var view = activeView as View;
|
---|
346 | if (view == null) return;
|
---|
347 | int width = viewsLabelVisible ? Width - viewsLabel.Width - viewsLabel.Margin.Left - viewsLabel.Margin.Right : Width;
|
---|
348 | int height = ShowBreadcrumbs ? Height - viewsLabel.Height - viewsLabel.Margin.Top - viewsLabel.Margin.Bottom : Height;
|
---|
349 | view.Location = new Point(0, Height - height);
|
---|
350 | view.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
|
---|
351 | view.Size = new Size(width, height);
|
---|
352 | }
|
---|
353 |
|
---|
354 | private IEnumerable<IContent> BuildBreadcrumbTrail() {
|
---|
355 | var ll = new LinkedList<IContent>();
|
---|
356 | for (var control = (Control)this; control != null; control = control.Parent) {
|
---|
357 | var viewHost = control as ViewHost;
|
---|
358 | if (viewHost != null && viewHost.Content != null)
|
---|
359 | ll.AddFirst(viewHost.Content);
|
---|
360 | }
|
---|
361 | return ll;
|
---|
362 | }
|
---|
363 |
|
---|
364 | public void UpdateBreadcrumbTrail(IEnumerable<IContent> oldCrumbs, IEnumerable<IContent> newCrumbs) {
|
---|
365 | if (!newCrumbs.Any()) return;
|
---|
366 | var ll = new LinkedList<IContent>();
|
---|
367 | foreach (var c in oldCrumbs) {
|
---|
368 | if (c != newCrumbs.First())
|
---|
369 | ll.AddLast(c);
|
---|
370 | else break;
|
---|
371 | }
|
---|
372 | foreach (var c in newCrumbs)
|
---|
373 | ll.AddLast(c);
|
---|
374 | breadcrumbControl.Breadcrumbs = ll;
|
---|
375 | }
|
---|
376 |
|
---|
377 | private void PerformOuterViewHostDetection() {
|
---|
378 | var outmostViewHost = this;
|
---|
379 | for (var parent = Parent; parent != null; parent = parent.Parent) {
|
---|
380 | var vh = parent as ViewHost;
|
---|
381 | if (vh != null) outmostViewHost = vh;
|
---|
382 | }
|
---|
383 | isOutmostViewHost = outmostViewHost == this;
|
---|
384 | viewContextMenuStrip.ShowBreadcrumbsToolStripMenuItem.Checked = isOutmostViewHost;
|
---|
385 | }
|
---|
386 | #endregion
|
---|
387 | }
|
---|
388 | }
|
---|