[3437] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3437] | 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;
|
---|
[4510] | 24 | using System.Drawing;
|
---|
[3437] | 25 | using System.Linq;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.MainForm.WindowsForms {
|
---|
[3557] | 30 | [Content(typeof(IContent))]
|
---|
[3437] | 31 | public sealed partial class ViewHost : AsynchronousContentView {
|
---|
| 32 | public ViewHost() {
|
---|
| 33 | InitializeComponent();
|
---|
| 34 | startDragAndDrop = false;
|
---|
| 35 | viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
|
---|
[4011] | 36 |
|
---|
| 37 | viewType = null;
|
---|
[3437] | 38 | activeView = null;
|
---|
[3497] | 39 | Content = null;
|
---|
[4011] | 40 | messageLabel.Visible = false;
|
---|
| 41 | viewsLabel.Visible = false;
|
---|
[5012] | 42 | viewsLabelVisible = true;
|
---|
[3437] | 43 | }
|
---|
| 44 |
|
---|
[5012] | 45 | private bool viewsLabelVisible;
|
---|
| 46 | public bool ViewsLabelVisible {
|
---|
| 47 | get { return viewsLabelVisible; }
|
---|
| 48 | set {
|
---|
| 49 | if (viewsLabelVisible != value) {
|
---|
| 50 | viewsLabelVisible = value;
|
---|
| 51 | viewsLabel.Visible = value;
|
---|
| 52 | View view = activeView as View;
|
---|
| 53 | if (view != null) view.Dock = viewsLabelVisible ? DockStyle.None : DockStyle.Fill;
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[4510] | 58 | private IContentView cachedView;
|
---|
[3437] | 59 | private IContentView activeView;
|
---|
| 60 | public IContentView ActiveView {
|
---|
[4510] | 61 | get { return activeView; }
|
---|
[3437] | 62 | private set {
|
---|
| 63 | if (activeView != value) {
|
---|
| 64 | if (activeView != null) {
|
---|
[4510] | 65 | cachedView = activeView;
|
---|
[3437] | 66 | DeregisterActiveViewEvents();
|
---|
[4510] | 67 | View cached = cachedView as View;
|
---|
| 68 | if (cached != null) {
|
---|
| 69 | cached.OnHidden(EventArgs.Empty);
|
---|
| 70 | cached.Visible = false;
|
---|
[4299] | 71 | }
|
---|
[3437] | 72 | }
|
---|
[4521] | 73 |
|
---|
[3437] | 74 | activeView = value;
|
---|
[4521] | 75 |
|
---|
[3437] | 76 | if (activeView != null) {
|
---|
[5956] | 77 | #region dispose cachedView
|
---|
[4521] | 78 | if (activeView != cachedView) {
|
---|
| 79 | if (cachedView != null) cachedView.Content = null; //needed to deregister events
|
---|
| 80 | View cached = cachedView as View;
|
---|
| 81 | if (cached != null) {
|
---|
| 82 | Controls.Remove(cached);
|
---|
[6951] | 83 | cached.Dispose();
|
---|
[4521] | 84 | }
|
---|
| 85 | cachedView = null;
|
---|
[4510] | 86 | }
|
---|
[4521] | 87 | #endregion
|
---|
[4510] | 88 |
|
---|
[4465] | 89 | this.Caption = activeView.Caption;
|
---|
[4011] | 90 | viewType = activeView.GetType();
|
---|
[3437] | 91 | RegisterActiveViewEvents();
|
---|
| 92 | View view = activeView as View;
|
---|
[4299] | 93 | if (view != null) {
|
---|
[4510] | 94 | view.Visible = true;
|
---|
[5956] | 95 | if (ViewsLabelVisible) {
|
---|
| 96 | view.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
|
---|
| 97 | view.Size = new Size(Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
|
---|
| 98 | } else view.Dock = DockStyle.Fill;
|
---|
| 99 | if (!Controls.Contains((view))) Controls.Add(view);
|
---|
[3437] | 100 | view.OnShown(new ViewShownEventArgs(view, false));
|
---|
[4011] | 101 | }
|
---|
[4103] | 102 | } else viewType = null;
|
---|
[6342] | 103 | configurationLabel.Visible = activeView is IConfigureableView;
|
---|
| 104 | configurationLabel.Enabled = activeView != null && !activeView.Locked;
|
---|
[3437] | 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
[4011] | 108 |
|
---|
[3437] | 109 | private Type viewType;
|
---|
| 110 | public Type ViewType {
|
---|
[4510] | 111 | get { return viewType; }
|
---|
[3437] | 112 | set {
|
---|
[5278] | 113 | if (viewType != value) {
|
---|
[5318] | 114 | if (value == typeof(ViewHost))
|
---|
| 115 | throw new ArgumentException("Directly nested ViewHosts are not allowed.");
|
---|
[4011] | 116 | if (value != null && Content != null && !ViewCanShowContent(value, Content))
|
---|
[5318] | 117 | throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".", value, Content.GetType()));
|
---|
[5278] | 118 |
|
---|
[3437] | 119 | viewType = value;
|
---|
| 120 | OnViewTypeChanged();
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[3466] | 124 |
|
---|
[4510] | 125 | protected override void SetEnabledStateOfControls() {
|
---|
| 126 | Enabled = Content != null;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[3437] | 129 | protected override void OnContentChanged() {
|
---|
[3466] | 130 | viewContextMenuStrip.Item = Content;
|
---|
[4299] | 131 | //change ViewType if view of ViewType can not show content or is null
|
---|
| 132 | if (Content != null) {
|
---|
| 133 | if (!ViewCanShowContent(viewType, Content)) {
|
---|
| 134 | Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
|
---|
[4510] | 135 | if (cachedView != null && cachedView.GetType() == defaultViewType)
|
---|
| 136 | ActiveView = cachedView;
|
---|
| 137 | else if (defaultViewType != null)
|
---|
[4299] | 138 | ViewType = defaultViewType;
|
---|
| 139 | else if (viewContextMenuStrip.Items.Count > 0) // create first available view if no default view is available
|
---|
| 140 | ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
|
---|
[4314] | 141 | else {
|
---|
[4299] | 142 | ViewType = null;
|
---|
[4314] | 143 | ActiveView = null;
|
---|
| 144 | }
|
---|
[3466] | 145 | }
|
---|
[4344] | 146 | if (ActiveView != null) ActiveView.Content = Content;
|
---|
| 147 | } else ActiveView = null;
|
---|
[4299] | 148 | UpdateLabels();
|
---|
[4418] | 149 | UpdateActiveMenuItem();
|
---|
[4299] | 150 | }
|
---|
[4011] | 151 |
|
---|
[4299] | 152 | private void UpdateLabels() {
|
---|
[4011] | 153 | if (Content != null && viewContextMenuStrip.Items.Count > 0) {
|
---|
| 154 | messageLabel.Visible = false;
|
---|
[5012] | 155 | viewsLabel.Visible = viewsLabelVisible;
|
---|
[4011] | 156 | } else if (Content != null) {
|
---|
| 157 | messageLabel.Visible = true;
|
---|
| 158 | viewsLabel.Visible = false;
|
---|
[3437] | 159 | } else {
|
---|
[3664] | 160 | messageLabel.Visible = false;
|
---|
[3924] | 161 | viewsLabel.Visible = false;
|
---|
[3437] | 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | private void OnViewTypeChanged() {
|
---|
[4011] | 166 | if (viewType != null) {
|
---|
| 167 | if (!ViewCanShowContent(viewType, Content))
|
---|
| 168 | throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
| 169 | viewType, Content.GetType()));
|
---|
[4510] | 170 | IContentView view = MainFormManager.CreateView(viewType);
|
---|
| 171 | view.Locked = Locked;
|
---|
| 172 | view.ReadOnly = ReadOnly;
|
---|
[4299] | 173 | ActiveView = view; //necessary to allow the views to change the status of the viewhost
|
---|
| 174 | view.Content = Content;
|
---|
| 175 |
|
---|
[4011] | 176 | UpdateActiveMenuItem();
|
---|
[3655] | 177 | }
|
---|
[4011] | 178 | }
|
---|
[3437] | 179 |
|
---|
| 180 | private void RegisterActiveViewEvents() {
|
---|
| 181 | activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
|
---|
[4435] | 182 | activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
|
---|
[9363] | 183 | activeView.Changed += new EventHandler(activeView_Changed);
|
---|
[3437] | 184 | }
|
---|
| 185 | private void DeregisterActiveViewEvents() {
|
---|
[4511] | 186 | activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
|
---|
| 187 | activeView.LockedChanged -= new EventHandler(activeView_LockedChanged);
|
---|
[9363] | 188 | activeView.Changed -= new EventHandler(activeView_Changed);
|
---|
[3437] | 189 | }
|
---|
| 190 | private void activeView_CaptionChanged(object sender, EventArgs e) {
|
---|
[4510] | 191 | Caption = activeView.Caption;
|
---|
[3437] | 192 | }
|
---|
[4435] | 193 | private void activeView_LockedChanged(object sender, EventArgs e) {
|
---|
[4510] | 194 | Locked = activeView.Locked;
|
---|
[6342] | 195 | configurationLabel.Enabled = !activeView.Locked;
|
---|
[3437] | 196 | }
|
---|
[9363] | 197 | private void activeView_Changed(object sender, EventArgs e) {
|
---|
| 198 | OnChanged();
|
---|
| 199 | }
|
---|
[4011] | 200 |
|
---|
[4084] | 201 | protected override void OnSizeChanged(EventArgs e) {
|
---|
| 202 | //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
|
---|
| 203 | //not implemented with a panel to reduce the number of nested controls
|
---|
[9363] | 204 | //also cf. http://connect.microsoft.com/VisualStudio/feedback/details/98368/csc-incorrectly-allows-comparison-between-intptr-and-null
|
---|
| 205 | if (Handle != IntPtr.Zero)
|
---|
[4084] | 206 | this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
|
---|
| 207 | }
|
---|
| 208 | private void OnSizeChangedHelper(EventArgs e) {
|
---|
| 209 | base.OnSizeChanged(e);
|
---|
[4510] | 210 | viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
|
---|
[6342] | 211 | configurationLabel.Location = new Point(Width - configurationLabel.Margin.Right - configurationLabel.Width, viewsLabel.Bottom + viewsLabel.Margin.Bottom + configurationLabel.Margin.Top);
|
---|
[6951] | 212 |
|
---|
[4084] | 213 | }
|
---|
| 214 |
|
---|
[3437] | 215 | #region forwarding of view events
|
---|
| 216 | internal protected override void OnShown(ViewShownEventArgs e) {
|
---|
| 217 | base.OnShown(e);
|
---|
[4510] | 218 | View view = ActiveView as View;
|
---|
[3437] | 219 | if (view != null)
|
---|
| 220 | view.OnShown(e);
|
---|
| 221 | }
|
---|
| 222 | internal protected override void OnHidden(EventArgs e) {
|
---|
| 223 | base.OnHidden(e);
|
---|
[4510] | 224 | View view = ActiveView as View;
|
---|
[3437] | 225 | if (view != null)
|
---|
| 226 | view.OnHidden(e);
|
---|
| 227 | }
|
---|
| 228 | internal protected override void OnClosing(FormClosingEventArgs e) {
|
---|
| 229 | base.OnClosing(e);
|
---|
[4299] | 230 | View view = ActiveView as View;
|
---|
| 231 | if (view != null)
|
---|
[3437] | 232 | view.OnClosing(e);
|
---|
| 233 | }
|
---|
| 234 | internal protected override void OnClosed(FormClosedEventArgs e) {
|
---|
| 235 | base.OnClosed(e);
|
---|
[4299] | 236 | View view = ActiveView as View;
|
---|
| 237 | if (view != null)
|
---|
[3437] | 238 | view.OnClosed(e);
|
---|
| 239 | }
|
---|
| 240 | #endregion
|
---|
| 241 |
|
---|
| 242 | #region GUI actions
|
---|
| 243 | private void UpdateActiveMenuItem() {
|
---|
| 244 | foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
|
---|
| 245 | if (item.Key == viewType) {
|
---|
| 246 | item.Value.Checked = true;
|
---|
| 247 | item.Value.Enabled = false;
|
---|
| 248 | } else {
|
---|
| 249 | item.Value.Checked = false;
|
---|
| 250 | item.Value.Enabled = true;
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | private bool ViewCanShowContent(Type viewType, object content) {
|
---|
| 256 | if (content == null) // every view can display null
|
---|
| 257 | return true;
|
---|
| 258 | if (viewType == null)
|
---|
| 259 | return false;
|
---|
| 260 | return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | private void viewsLabel_DoubleClick(object sender, EventArgs e) {
|
---|
[3796] | 264 | IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
|
---|
[3670] | 265 | if (view != null) {
|
---|
| 266 | view.ReadOnly = this.ReadOnly;
|
---|
| 267 | view.Locked = this.Locked;
|
---|
| 268 | }
|
---|
[3437] | 269 | }
|
---|
| 270 | private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
|
---|
| 271 | Type viewType = (Type)e.ClickedItem.Tag;
|
---|
| 272 | ViewType = viewType;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | private bool startDragAndDrop;
|
---|
| 276 | private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
|
---|
[5462] | 277 | if (e.Button == System.Windows.Forms.MouseButtons.Right) {
|
---|
| 278 | Screen screen = Screen.FromControl(viewsLabel);
|
---|
| 279 | int rightBorder = viewsLabel.PointToScreen(viewsLabel.Location).X + viewContextMenuStrip.Width; //
|
---|
| 280 | rightBorder = rightBorder - screen.Bounds.X; //pixel position on active screen
|
---|
| 281 |
|
---|
| 282 | if (rightBorder < screen.Bounds.Width)
|
---|
| 283 | viewContextMenuStrip.Show(viewsLabel, viewsLabel.Margin.Left, viewsLabel.Margin.Top);
|
---|
| 284 | else
|
---|
| 285 | viewContextMenuStrip.Show(screen.Bounds.X + screen.Bounds.Width - viewContextMenuStrip.Width, viewsLabel.PointToScreen(viewsLabel.Location).Y - viewsLabel.Margin.Top);
|
---|
| 286 | } else if (!Locked) {
|
---|
[3437] | 287 | startDragAndDrop = true;
|
---|
| 288 | viewsLabel.Capture = false;
|
---|
[3497] | 289 | viewsLabel.Focus();
|
---|
[3437] | 290 | }
|
---|
| 291 | }
|
---|
| 292 | private void viewsLabel_MouseLeave(object sender, EventArgs e) {
|
---|
| 293 | if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
|
---|
| 294 | DataObject data = new DataObject();
|
---|
[5837] | 295 | data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, Content);
|
---|
[3437] | 296 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 297 | } else
|
---|
| 298 | startDragAndDrop = false;
|
---|
| 299 | }
|
---|
[6342] | 300 |
|
---|
[7244] | 301 | private void configurationLabel_DoubleClick(object sender, MouseEventArgs e) {
|
---|
[6342] | 302 | ((IConfigureableView)ActiveView).ShowConfiguration();
|
---|
| 303 | }
|
---|
[3437] | 304 | #endregion
|
---|
| 305 | }
|
---|
| 306 | }
|
---|