[3437] | 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;
|
---|
[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;
|
---|
[3437] | 42 | }
|
---|
| 43 |
|
---|
[4510] | 44 | private IContentView cachedView;
|
---|
[3437] | 45 | private IContentView activeView;
|
---|
| 46 | public IContentView ActiveView {
|
---|
[4510] | 47 | get { return activeView; }
|
---|
[3437] | 48 | private set {
|
---|
| 49 | if (activeView != value) {
|
---|
| 50 | if (activeView != null) {
|
---|
[4510] | 51 | cachedView = activeView;
|
---|
[3437] | 52 | DeregisterActiveViewEvents();
|
---|
[4510] | 53 | View cached = cachedView as View;
|
---|
| 54 | if (cached != null) {
|
---|
| 55 | cached.OnHidden(EventArgs.Empty);
|
---|
| 56 | cached.Visible = false;
|
---|
[4299] | 57 | }
|
---|
[3437] | 58 | }
|
---|
[4521] | 59 |
|
---|
[3437] | 60 | activeView = value;
|
---|
[4521] | 61 |
|
---|
[3437] | 62 | if (activeView != null) {
|
---|
[4521] | 63 | #region disposed cachedView
|
---|
| 64 | if (activeView != cachedView) {
|
---|
| 65 | if (cachedView != null) cachedView.Content = null; //needed to deregister events
|
---|
| 66 | View cached = cachedView as View;
|
---|
| 67 | if (cached != null) {
|
---|
| 68 | Controls.Remove(cached);
|
---|
| 69 | cached.Dispose();
|
---|
| 70 | }
|
---|
| 71 | cachedView = null;
|
---|
[4510] | 72 | }
|
---|
[4521] | 73 | #endregion
|
---|
[4510] | 74 |
|
---|
[4465] | 75 | this.Caption = activeView.Caption;
|
---|
[4011] | 76 | viewType = activeView.GetType();
|
---|
[3437] | 77 | RegisterActiveViewEvents();
|
---|
| 78 | View view = activeView as View;
|
---|
[4299] | 79 | if (view != null) {
|
---|
[4510] | 80 | view.Visible = true;
|
---|
[4435] | 81 | view.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
|
---|
[4510] | 82 | view.Size = new Size(Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
|
---|
[3437] | 83 | view.OnShown(new ViewShownEventArgs(view, false));
|
---|
[4510] | 84 | if (!Controls.Contains((view))) Controls.Add(view);
|
---|
[4011] | 85 | }
|
---|
[4103] | 86 | } else viewType = null;
|
---|
[3437] | 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
[4011] | 90 |
|
---|
[3437] | 91 | private Type viewType;
|
---|
| 92 | public Type ViewType {
|
---|
[4510] | 93 | get { return viewType; }
|
---|
[3437] | 94 | set {
|
---|
| 95 | if (viewType != value) {
|
---|
[4011] | 96 | if (value != null && Content != null && !ViewCanShowContent(value, Content))
|
---|
[3437] | 97 | throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
| 98 | value, Content.GetType()));
|
---|
| 99 | viewType = value;
|
---|
| 100 | OnViewTypeChanged();
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
[3466] | 104 |
|
---|
[4510] | 105 | protected override void SetEnabledStateOfControls() {
|
---|
| 106 | Enabled = Content != null;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[3437] | 109 | protected override void OnContentChanged() {
|
---|
[3466] | 110 | viewContextMenuStrip.Item = Content;
|
---|
[4299] | 111 | //change ViewType if view of ViewType can not show content or is null
|
---|
| 112 | if (Content != null) {
|
---|
| 113 | if (!ViewCanShowContent(viewType, Content)) {
|
---|
| 114 | Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
|
---|
[4510] | 115 | if (cachedView != null && cachedView.GetType() == defaultViewType)
|
---|
| 116 | ActiveView = cachedView;
|
---|
| 117 | else if (defaultViewType != null)
|
---|
[4299] | 118 | ViewType = defaultViewType;
|
---|
| 119 | else if (viewContextMenuStrip.Items.Count > 0) // create first available view if no default view is available
|
---|
| 120 | ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
|
---|
[4314] | 121 | else {
|
---|
[4299] | 122 | ViewType = null;
|
---|
[4314] | 123 | ActiveView = null;
|
---|
| 124 | }
|
---|
[3466] | 125 | }
|
---|
[4344] | 126 | if (ActiveView != null) ActiveView.Content = Content;
|
---|
| 127 | } else ActiveView = null;
|
---|
[4299] | 128 | UpdateLabels();
|
---|
[4418] | 129 | UpdateActiveMenuItem();
|
---|
[4299] | 130 | }
|
---|
[4011] | 131 |
|
---|
[4299] | 132 | private void UpdateLabels() {
|
---|
[4011] | 133 | if (Content != null && viewContextMenuStrip.Items.Count > 0) {
|
---|
| 134 | messageLabel.Visible = false;
|
---|
| 135 | viewsLabel.Visible = true;
|
---|
| 136 | } else if (Content != null) {
|
---|
| 137 | messageLabel.Visible = true;
|
---|
| 138 | viewsLabel.Visible = false;
|
---|
[3437] | 139 | } else {
|
---|
[3664] | 140 | messageLabel.Visible = false;
|
---|
[3924] | 141 | viewsLabel.Visible = false;
|
---|
[3437] | 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | private void OnViewTypeChanged() {
|
---|
[4011] | 146 | if (viewType != null) {
|
---|
| 147 | if (!ViewCanShowContent(viewType, Content))
|
---|
| 148 | throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
|
---|
| 149 | viewType, Content.GetType()));
|
---|
[4510] | 150 | IContentView view = MainFormManager.CreateView(viewType);
|
---|
| 151 | view.Locked = Locked;
|
---|
| 152 | view.ReadOnly = ReadOnly;
|
---|
[4299] | 153 | ActiveView = view; //necessary to allow the views to change the status of the viewhost
|
---|
| 154 | view.Content = Content;
|
---|
| 155 |
|
---|
[4011] | 156 | UpdateActiveMenuItem();
|
---|
[3655] | 157 | }
|
---|
[4011] | 158 | }
|
---|
[3437] | 159 |
|
---|
| 160 | private void RegisterActiveViewEvents() {
|
---|
| 161 | activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
|
---|
[4435] | 162 | activeView.LockedChanged += new EventHandler(activeView_LockedChanged);
|
---|
[3437] | 163 | }
|
---|
| 164 | private void DeregisterActiveViewEvents() {
|
---|
[4511] | 165 | activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
|
---|
| 166 | activeView.LockedChanged -= new EventHandler(activeView_LockedChanged);
|
---|
[3437] | 167 | }
|
---|
| 168 | private void activeView_CaptionChanged(object sender, EventArgs e) {
|
---|
[4510] | 169 | Caption = activeView.Caption;
|
---|
[3437] | 170 | }
|
---|
[4435] | 171 | private void activeView_LockedChanged(object sender, EventArgs e) {
|
---|
[4510] | 172 | Locked = activeView.Locked;
|
---|
[3437] | 173 | }
|
---|
[4011] | 174 |
|
---|
[4084] | 175 | protected override void OnSizeChanged(EventArgs e) {
|
---|
| 176 | //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
|
---|
| 177 | //not implemented with a panel to reduce the number of nested controls
|
---|
[4510] | 178 | if (Handle != null)
|
---|
[4084] | 179 | this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
|
---|
| 180 | }
|
---|
| 181 | private void OnSizeChangedHelper(EventArgs e) {
|
---|
| 182 | base.OnSizeChanged(e);
|
---|
[4510] | 183 | viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
|
---|
[4084] | 184 | }
|
---|
| 185 |
|
---|
[3437] | 186 | #region forwarding of view events
|
---|
| 187 | internal protected override void OnShown(ViewShownEventArgs e) {
|
---|
| 188 | base.OnShown(e);
|
---|
[4510] | 189 | View view = ActiveView as View;
|
---|
[3437] | 190 | if (view != null)
|
---|
| 191 | view.OnShown(e);
|
---|
| 192 | }
|
---|
| 193 | internal protected override void OnHidden(EventArgs e) {
|
---|
| 194 | base.OnHidden(e);
|
---|
[4510] | 195 | View view = ActiveView as View;
|
---|
[3437] | 196 | if (view != null)
|
---|
| 197 | view.OnHidden(e);
|
---|
| 198 | }
|
---|
| 199 | internal protected override void OnClosing(FormClosingEventArgs e) {
|
---|
| 200 | base.OnClosing(e);
|
---|
[4299] | 201 | View view = ActiveView as View;
|
---|
| 202 | if (view != null)
|
---|
[3437] | 203 | view.OnClosing(e);
|
---|
| 204 | }
|
---|
| 205 | internal protected override void OnClosed(FormClosedEventArgs e) {
|
---|
| 206 | base.OnClosed(e);
|
---|
[4299] | 207 | View view = ActiveView as View;
|
---|
| 208 | if (view != null)
|
---|
[3437] | 209 | view.OnClosed(e);
|
---|
| 210 | }
|
---|
| 211 | #endregion
|
---|
| 212 |
|
---|
| 213 | #region GUI actions
|
---|
| 214 | private void UpdateActiveMenuItem() {
|
---|
| 215 | foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
|
---|
| 216 | if (item.Key == viewType) {
|
---|
| 217 | item.Value.Checked = true;
|
---|
| 218 | item.Value.Enabled = false;
|
---|
| 219 | } else {
|
---|
| 220 | item.Value.Checked = false;
|
---|
| 221 | item.Value.Enabled = true;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | private bool ViewCanShowContent(Type viewType, object content) {
|
---|
| 227 | if (content == null) // every view can display null
|
---|
| 228 | return true;
|
---|
| 229 | if (viewType == null)
|
---|
| 230 | return false;
|
---|
| 231 | return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | private void viewsLabel_DoubleClick(object sender, EventArgs e) {
|
---|
[3796] | 235 | IContentView view = MainFormManager.MainForm.ShowContent(this.Content, this.ViewType);
|
---|
[3670] | 236 | if (view != null) {
|
---|
| 237 | view.ReadOnly = this.ReadOnly;
|
---|
| 238 | view.Locked = this.Locked;
|
---|
| 239 | }
|
---|
[3437] | 240 | }
|
---|
| 241 | private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
|
---|
| 242 | Type viewType = (Type)e.ClickedItem.Tag;
|
---|
| 243 | ViewType = viewType;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | private bool startDragAndDrop;
|
---|
| 247 | private void viewsLabel_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 248 | if (!Locked) {
|
---|
| 249 | startDragAndDrop = true;
|
---|
| 250 | viewsLabel.Capture = false;
|
---|
[3497] | 251 | viewsLabel.Focus();
|
---|
[3437] | 252 | }
|
---|
| 253 | }
|
---|
| 254 | private void viewsLabel_MouseLeave(object sender, EventArgs e) {
|
---|
| 255 | if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
|
---|
| 256 | DataObject data = new DataObject();
|
---|
| 257 | data.SetData("Type", Content.GetType());
|
---|
| 258 | data.SetData("Value", Content);
|
---|
| 259 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 260 | } else
|
---|
| 261 | startDragAndDrop = false;
|
---|
| 262 | }
|
---|
| 263 | #endregion
|
---|
| 264 | }
|
---|
| 265 | }
|
---|