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