[3437] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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) {
|
---|
[4521] | 77 | #region disposed cachedView
|
---|
| 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);
|
---|
| 83 | cached.Dispose();
|
---|
| 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;
|
---|
[4435] | 95 | view.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
|
---|
[4510] | 96 | view.Size = new Size(Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
|
---|
[5012] | 97 | view.Dock = viewsLabelVisible ? DockStyle.None : DockStyle.Fill;
|
---|
[3437] | 98 | view.OnShown(new ViewShownEventArgs(view, false));
|
---|
[4510] | 99 | if (!Controls.Contains((view))) Controls.Add(view);
|
---|
[4011] | 100 | }
|
---|
[4103] | 101 | } else viewType = null;
|
---|
[3437] | 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
[4011] | 105 |
|
---|
[3437] | 106 | private Type viewType;
|
---|
| 107 | public Type ViewType {
|
---|
[4510] | 108 | get { return viewType; }
|
---|
[3437] | 109 | set {
|
---|
[5278] | 110 | if (viewType != value) {
|
---|
[5318] | 111 | if (value == typeof(ViewHost))
|
---|
| 112 | throw new ArgumentException("Directly nested ViewHosts are not allowed.");
|
---|
[4011] | 113 | if (value != null && Content != null && !ViewCanShowContent(value, Content))
|
---|
[5318] | 114 | throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".", value, Content.GetType()));
|
---|
[5278] | 115 |
|
---|
[3437] | 116 | viewType = value;
|
---|
| 117 | OnViewTypeChanged();
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
[3466] | 121 |
|
---|
[4510] | 122 | protected override void SetEnabledStateOfControls() {
|
---|
| 123 | Enabled = Content != null;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[3437] | 126 | protected override void OnContentChanged() {
|
---|
[3466] | 127 | viewContextMenuStrip.Item = Content;
|
---|
[4299] | 128 | //change ViewType if view of ViewType can not show content or is null
|
---|
| 129 | if (Content != null) {
|
---|
| 130 | if (!ViewCanShowContent(viewType, Content)) {
|
---|
| 131 | Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
|
---|
[4510] | 132 | if (cachedView != null && cachedView.GetType() == defaultViewType)
|
---|
| 133 | ActiveView = cachedView;
|
---|
| 134 | else if (defaultViewType != null)
|
---|
[4299] | 135 | ViewType = defaultViewType;
|
---|
| 136 | else if (viewContextMenuStrip.Items.Count > 0) // create first available view if no default view is available
|
---|
| 137 | ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
|
---|
[4314] | 138 | else {
|
---|
[4299] | 139 | ViewType = null;
|
---|
[4314] | 140 | ActiveView = null;
|
---|
| 141 | }
|
---|
[3466] | 142 | }
|
---|
[4344] | 143 | if (ActiveView != null) ActiveView.Content = Content;
|
---|
| 144 | } else ActiveView = null;
|
---|
[4299] | 145 | UpdateLabels();
|
---|
[4418] | 146 | UpdateActiveMenuItem();
|
---|
[4299] | 147 | }
|
---|
[4011] | 148 |
|
---|
[4299] | 149 | private void UpdateLabels() {
|
---|
[4011] | 150 | if (Content != null && viewContextMenuStrip.Items.Count > 0) {
|
---|
| 151 | messageLabel.Visible = false;
|
---|
[5012] | 152 | viewsLabel.Visible = viewsLabelVisible;
|
---|
[4011] | 153 | } else if (Content != null) {
|
---|
| 154 | messageLabel.Visible = true;
|
---|
| 155 | viewsLabel.Visible = false;
|
---|
[3437] | 156 | } else {
|
---|
[3664] | 157 | messageLabel.Visible = false;
|
---|
[3924] | 158 | viewsLabel.Visible = false;
|
---|
[3437] | 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | private void OnViewTypeChanged() {
|
---|
[4011] | 163 | if (viewType != null) {
|
---|
| 164 | if (!ViewCanShowContent(viewType, Content))
|
---|
| 165 | throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
| 166 | viewType, Content.GetType()));
|
---|
[4510] | 167 | IContentView view = MainFormManager.CreateView(viewType);
|
---|
| 168 | view.Locked = Locked;
|
---|
| 169 | view.ReadOnly = ReadOnly;
|
---|
[4299] | 170 | ActiveView = view; //necessary to allow the views to change the status of the viewhost
|
---|
| 171 | view.Content = Content;
|
---|
| 172 |
|
---|
[4011] | 173 | UpdateActiveMenuItem();
|
---|
[3655] | 174 | }
|
---|
[4011] | 175 | }
|
---|
[3437] | 176 |
|
---|
| 177 | private void RegisterActiveViewEvents() {
|
---|
| 178 | activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
|
---|
[4435] | 179 | activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
|
---|
[3437] | 180 | }
|
---|
| 181 | private void DeregisterActiveViewEvents() {
|
---|
[4511] | 182 | activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
|
---|
| 183 | activeView.LockedChanged -= new EventHandler(activeView_LockedChanged);
|
---|
[3437] | 184 | }
|
---|
| 185 | private void activeView_CaptionChanged(object sender, EventArgs e) {
|
---|
[4510] | 186 | Caption = activeView.Caption;
|
---|
[3437] | 187 | }
|
---|
[4435] | 188 | private void activeView_LockedChanged(object sender, EventArgs e) {
|
---|
[4510] | 189 | Locked = activeView.Locked;
|
---|
[3437] | 190 | }
|
---|
[4011] | 191 |
|
---|
[4084] | 192 | protected override void OnSizeChanged(EventArgs e) {
|
---|
| 193 | //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
|
---|
| 194 | //not implemented with a panel to reduce the number of nested controls
|
---|
[4510] | 195 | if (Handle != null)
|
---|
[4084] | 196 | this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
|
---|
| 197 | }
|
---|
| 198 | private void OnSizeChangedHelper(EventArgs e) {
|
---|
| 199 | base.OnSizeChanged(e);
|
---|
[4510] | 200 | viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
|
---|
[4084] | 201 | }
|
---|
| 202 |
|
---|
[3437] | 203 | #region forwarding of view events
|
---|
| 204 | internal protected override void OnShown(ViewShownEventArgs e) {
|
---|
| 205 | base.OnShown(e);
|
---|
[4510] | 206 | View view = ActiveView as View;
|
---|
[3437] | 207 | if (view != null)
|
---|
| 208 | view.OnShown(e);
|
---|
| 209 | }
|
---|
| 210 | internal protected override void OnHidden(EventArgs e) {
|
---|
| 211 | base.OnHidden(e);
|
---|
[4510] | 212 | View view = ActiveView as View;
|
---|
[3437] | 213 | if (view != null)
|
---|
| 214 | view.OnHidden(e);
|
---|
| 215 | }
|
---|
| 216 | internal protected override void OnClosing(FormClosingEventArgs e) {
|
---|
| 217 | base.OnClosing(e);
|
---|
[4299] | 218 | View view = ActiveView as View;
|
---|
| 219 | if (view != null)
|
---|
[3437] | 220 | view.OnClosing(e);
|
---|
| 221 | }
|
---|
| 222 | internal protected override void OnClosed(FormClosedEventArgs e) {
|
---|
| 223 | base.OnClosed(e);
|
---|
[4299] | 224 | View view = ActiveView as View;
|
---|
| 225 | if (view != null)
|
---|
[3437] | 226 | view.OnClosed(e);
|
---|
| 227 | }
|
---|
| 228 | #endregion
|
---|
| 229 |
|
---|
| 230 | #region GUI actions
|
---|
| 231 | private void UpdateActiveMenuItem() {
|
---|
| 232 | foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
|
---|
| 233 | if (item.Key == viewType) {
|
---|
| 234 | item.Value.Checked = true;
|
---|
| 235 | item.Value.Enabled = false;
|
---|
| 236 | } else {
|
---|
| 237 | item.Value.Checked = false;
|
---|
| 238 | item.Value.Enabled = true;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | private bool ViewCanShowContent(Type viewType, object content) {
|
---|
| 244 | if (content == null) // every view can display null
|
---|
| 245 | return true;
|
---|
| 246 | if (viewType == null)
|
---|
| 247 | return false;
|
---|
| 248 | return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | private void viewsLabel_DoubleClick(object sender, EventArgs e) {
|
---|
[3796] | 252 | IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
|
---|
[3670] | 253 | if (view != null) {
|
---|
| 254 | view.ReadOnly = this.ReadOnly;
|
---|
| 255 | view.Locked = this.Locked;
|
---|
| 256 | }
|
---|
[3437] | 257 | }
|
---|
| 258 | private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
|
---|
| 259 | Type viewType = (Type)e.ClickedItem.Tag;
|
---|
| 260 | ViewType = viewType;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | private bool startDragAndDrop;
|
---|
| 264 | private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
|
---|
[5462] | 265 | if (e.Button == System.Windows.Forms.MouseButtons.Right) {
|
---|
| 266 | Screen screen = Screen.FromControl(viewsLabel);
|
---|
| 267 | int rightBorder = viewsLabel.PointToScreen(viewsLabel.Location).X + viewContextMenuStrip.Width; //
|
---|
| 268 | rightBorder = rightBorder - screen.Bounds.X; //pixel position on active screen
|
---|
| 269 |
|
---|
| 270 | if (rightBorder < screen.Bounds.Width)
|
---|
| 271 | viewContextMenuStrip.Show(viewsLabel, viewsLabel.Margin.Left, viewsLabel.Margin.Top);
|
---|
| 272 | else
|
---|
| 273 | viewContextMenuStrip.Show(screen.Bounds.X + screen.Bounds.Width - viewContextMenuStrip.Width, viewsLabel.PointToScreen(viewsLabel.Location).Y - viewsLabel.Margin.Top);
|
---|
| 274 | } else if (!Locked) {
|
---|
[3437] | 275 | startDragAndDrop = true;
|
---|
| 276 | viewsLabel.Capture = false;
|
---|
[3497] | 277 | viewsLabel.Focus();
|
---|
[3437] | 278 | }
|
---|
| 279 | }
|
---|
| 280 | private void viewsLabel_MouseLeave(object sender, EventArgs e) {
|
---|
| 281 | if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
|
---|
| 282 | DataObject data = new DataObject();
|
---|
[5744] | 283 | data.SetData("HeuristicLab", Content);
|
---|
[3437] | 284 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 285 | } else
|
---|
| 286 | startDragAndDrop = false;
|
---|
| 287 | }
|
---|
| 288 | #endregion
|
---|
| 289 | }
|
---|
| 290 | }
|
---|