[2233] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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.Data;
|
---|
| 26 | using System.Drawing;
|
---|
| 27 | using System.Linq;
|
---|
| 28 | using System.Text;
|
---|
| 29 | using System.Windows.Forms;
|
---|
| 30 |
|
---|
[2243] | 31 | using HeuristicLab.PluginInfrastructure;
|
---|
[2696] | 32 | using System.Collections;
|
---|
[2243] | 33 |
|
---|
[2426] | 34 | namespace HeuristicLab.MainForm.WindowsForms {
|
---|
[2696] | 35 | public partial class MainForm : Form, IMainForm {
|
---|
| 36 | private bool initialized;
|
---|
[2541] | 37 |
|
---|
[2696] | 38 | protected MainForm()
|
---|
[2233] | 39 | : base() {
|
---|
| 40 | InitializeComponent();
|
---|
[2443] | 41 | this.views = new Dictionary<IView, Form>();
|
---|
[2426] | 42 | this.userInterfaceItems = new List<IUserInterfaceItem>();
|
---|
[2541] | 43 | this.initialized = false;
|
---|
[2268] | 44 | }
|
---|
| 45 |
|
---|
[2696] | 46 | protected MainForm(Type userInterfaceItemType)
|
---|
[2268] | 47 | : this() {
|
---|
[2233] | 48 | this.userInterfaceItemType = userInterfaceItemType;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[2696] | 51 | #region properties
|
---|
[2233] | 52 | public string Title {
|
---|
[2243] | 53 | get { return this.Text; }
|
---|
[2256] | 54 | set {
|
---|
| 55 | if (InvokeRequired) {
|
---|
| 56 | Action<string> action = delegate(string s) { this.Title = s; };
|
---|
[2407] | 57 | Invoke(action, value);
|
---|
[2256] | 58 | } else
|
---|
| 59 | this.Text = value;
|
---|
| 60 | }
|
---|
[2233] | 61 | }
|
---|
| 62 |
|
---|
[2350] | 63 | public override Cursor Cursor {
|
---|
[2338] | 64 | get { return base.Cursor; }
|
---|
| 65 | set {
|
---|
| 66 | if (InvokeRequired) {
|
---|
| 67 | Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
|
---|
[2407] | 68 | Invoke(action, value);
|
---|
[2338] | 69 | } else
|
---|
| 70 | base.Cursor = value;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[2256] | 74 | private Type userInterfaceItemType;
|
---|
[2233] | 75 | public Type UserInterfaceItemType {
|
---|
| 76 | get { return this.userInterfaceItemType; }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[2443] | 79 | private Dictionary<IView, Form> views;
|
---|
[2358] | 80 | public IEnumerable<IView> Views {
|
---|
[2443] | 81 | get { return views.Keys; }
|
---|
[2358] | 82 | }
|
---|
| 83 |
|
---|
[2254] | 84 | private IView activeView;
|
---|
[2233] | 85 | public IView ActiveView {
|
---|
| 86 | get { return this.activeView; }
|
---|
[2254] | 87 | protected set {
|
---|
| 88 | if (this.activeView != value) {
|
---|
[2309] | 89 | if (InvokeRequired) {
|
---|
| 90 | Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
|
---|
[2407] | 91 | Invoke(action, value);
|
---|
[2309] | 92 | } else {
|
---|
| 93 | this.activeView = value;
|
---|
| 94 | OnActiveViewChanged();
|
---|
| 95 | }
|
---|
[2254] | 96 | }
|
---|
| 97 | }
|
---|
[2233] | 98 | }
|
---|
| 99 |
|
---|
[2426] | 100 | private List<IUserInterfaceItem> userInterfaceItems;
|
---|
| 101 | protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
|
---|
| 102 | get { return this.userInterfaceItems; }
|
---|
[2256] | 103 | }
|
---|
[2696] | 104 | #endregion
|
---|
[2254] | 105 |
|
---|
[2696] | 106 | #region events
|
---|
[2254] | 107 | public event EventHandler ActiveViewChanged;
|
---|
| 108 | protected virtual void OnActiveViewChanged() {
|
---|
[2336] | 109 | if (InvokeRequired)
|
---|
| 110 | Invoke((MethodInvoker)OnActiveViewChanged);
|
---|
| 111 | else if (ActiveViewChanged != null)
|
---|
[2256] | 112 | ActiveViewChanged(this, new EventArgs());
|
---|
[2254] | 113 | }
|
---|
| 114 |
|
---|
[2548] | 115 | public event EventHandler<ViewEventArgs> ViewClosed;
|
---|
| 116 | protected virtual void OnViewClosed(IView view) {
|
---|
| 117 | if (InvokeRequired) Invoke((Action<IView>)OnViewClosed, view);
|
---|
| 118 | else if (this.ViewClosed != null) {
|
---|
| 119 | this.ViewClosed(this, new ViewEventArgs(view));
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | public event EventHandler<ViewShownEventArgs> ViewShown;
|
---|
| 124 | protected virtual void OnViewShown(IView view, bool firstTimeShown) {
|
---|
| 125 | if (InvokeRequired) Invoke((Action<IView, bool>)OnViewShown, view, firstTimeShown);
|
---|
| 126 | else if (this.ViewShown != null) {
|
---|
| 127 | this.ViewShown(this, new ViewShownEventArgs(view, firstTimeShown));
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public event EventHandler<ViewEventArgs> ViewHidden;
|
---|
| 132 | protected virtual void OnViewHidden(IView view) {
|
---|
| 133 | if (InvokeRequired) Invoke((Action<IView>)OnViewHidden, view);
|
---|
| 134 | else if (this.ViewHidden != null) {
|
---|
| 135 | this.ViewHidden(this, new ViewEventArgs(view));
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[2426] | 139 | public event EventHandler Changed;
|
---|
[2703] | 140 | protected void FireMainFormChanged() {
|
---|
[2336] | 141 | if (InvokeRequired)
|
---|
[2696] | 142 | Invoke((MethodInvoker)FireMainFormChanged);
|
---|
[2426] | 143 | else if (Changed != null)
|
---|
| 144 | Changed(this, new EventArgs());
|
---|
[2300] | 145 | }
|
---|
| 146 |
|
---|
[2696] | 147 | private void MainFormBase_Load(object sender, EventArgs e) {
|
---|
| 148 | if (!DesignMode) {
|
---|
| 149 | MainFormManager.RegisterMainForm(this);
|
---|
| 150 | if (!this.initialized) {
|
---|
| 151 | this.initialized = true;
|
---|
| 152 | this.OnInitialized(new EventArgs());
|
---|
| 153 | }
|
---|
| 154 | this.CreateGUI();
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | protected virtual void OnInitialized(EventArgs e) {
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | private void ChildFormClosed(object sender, FormClosedEventArgs e) {
|
---|
| 162 | Form form = (Form)sender;
|
---|
| 163 | IView view = GetView(form);
|
---|
| 164 |
|
---|
| 165 | form.Activated -= new EventHandler(FormActivated);
|
---|
| 166 | form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
|
---|
| 167 |
|
---|
| 168 | views.Remove(view);
|
---|
| 169 | this.OnViewClosed(view);
|
---|
| 170 | if (ActiveView == view)
|
---|
| 171 | ActiveView = null;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | private void FormActivated(object sender, EventArgs e) {
|
---|
| 175 | this.ActiveView = GetView((Form)sender);
|
---|
| 176 | }
|
---|
| 177 | #endregion
|
---|
| 178 |
|
---|
| 179 | #region create, get, show, hide, close views
|
---|
[2443] | 180 | protected virtual Form CreateForm(IView view) {
|
---|
| 181 | throw new NotImplementedException("CreateForm must be implemented in subclasses of MainFormBase.");
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[2696] | 184 | internal Form GetForm(IView view) {
|
---|
| 185 | if (views.ContainsKey(view))
|
---|
| 186 | return views[view];
|
---|
| 187 | return null;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | internal IView GetView(Form form) {
|
---|
| 191 | return views.Where(x => x.Value == form).Single().Key;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[2704] | 194 | internal void ShowView(IView view, bool firstTimeShown) {
|
---|
| 195 | if (InvokeRequired) Invoke((Action<IView, bool>)ShowView, view, firstTimeShown);
|
---|
[2443] | 196 | else {
|
---|
[2696] | 197 | if (firstTimeShown) {
|
---|
[2443] | 198 | Form form = CreateForm(view);
|
---|
[2696] | 199 | this.views[view] = form;
|
---|
[2443] | 200 | form.Activated += new EventHandler(FormActivated);
|
---|
| 201 | form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
|
---|
[2548] | 202 | }
|
---|
[2696] | 203 | this.Show(view, firstTimeShown);
|
---|
| 204 | this.OnViewShown(view, firstTimeShown);
|
---|
[2306] | 205 | }
|
---|
[2233] | 206 | }
|
---|
[2297] | 207 |
|
---|
[2636] | 208 | protected virtual void Show(IView view, bool firstTimeShown) {
|
---|
[2548] | 209 | }
|
---|
| 210 |
|
---|
[2696] | 211 | internal void HideView(IView view) {
|
---|
[2443] | 212 | if (InvokeRequired) Invoke((Action<IView>)HideView, view);
|
---|
| 213 | else {
|
---|
[2548] | 214 | if (this.views.ContainsKey(view)) {
|
---|
| 215 | this.Hide(view);
|
---|
| 216 | this.OnViewHidden(view);
|
---|
| 217 | }
|
---|
[2443] | 218 | }
|
---|
[2305] | 219 | }
|
---|
| 220 |
|
---|
[2548] | 221 | protected virtual void Hide(IView view) {
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[2696] | 224 | internal void CloseView(IView view) {
|
---|
[2443] | 225 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
| 226 | else {
|
---|
[2548] | 227 | if (this.views.ContainsKey(view)) {
|
---|
| 228 | this.views[view].Close();
|
---|
| 229 | this.OnViewClosed(view);
|
---|
| 230 | }
|
---|
[2544] | 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[2696] | 234 | internal void CloseView(IView view, CloseReason closeReason) {
|
---|
[2544] | 235 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
| 236 | else {
|
---|
[2548] | 237 | if (this.views.ContainsKey(view)) {
|
---|
[2696] | 238 | ((View)view).closeReason = closeReason;
|
---|
[2548] | 239 | this.CloseView(view);
|
---|
[2543] | 240 | }
|
---|
[2443] | 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[2696] | 244 | public void CloseAllViews() {
|
---|
[2443] | 245 | foreach (IView view in views.Keys.ToArray())
|
---|
[2305] | 246 | CloseView(view);
|
---|
| 247 | }
|
---|
[2544] | 248 |
|
---|
[2696] | 249 | public void CloseAllViews(CloseReason closeReason) {
|
---|
[2544] | 250 | foreach (IView view in views.Keys.ToArray())
|
---|
[2548] | 251 | CloseView(view, closeReason);
|
---|
[2544] | 252 | }
|
---|
[2443] | 253 | #endregion
|
---|
[2305] | 254 |
|
---|
[2696] | 255 | #region create menu and toolbar
|
---|
| 256 | private void CreateGUI() {
|
---|
| 257 | IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
|
---|
[2443] | 258 |
|
---|
[2696] | 259 | IEnumerable<IPositionableUserInterfaceItem> toolStripMenuItems =
|
---|
| 260 | from mi in allUserInterfaceItems
|
---|
| 261 | where (mi is IPositionableUserInterfaceItem) &&
|
---|
| 262 | (mi is IMenuItem || mi is IMenuSeparatorItem)
|
---|
| 263 | orderby ((IPositionableUserInterfaceItem)mi).Position
|
---|
| 264 | select (IPositionableUserInterfaceItem)mi;
|
---|
[2443] | 265 |
|
---|
[2696] | 266 | foreach (IPositionableUserInterfaceItem menuItem in toolStripMenuItems) {
|
---|
| 267 | if (menuItem is IMenuItem)
|
---|
| 268 | AddToolStripMenuItem((IMenuItem)menuItem);
|
---|
| 269 | else if (menuItem is IMenuSeparatorItem)
|
---|
| 270 | AddToolStripMenuItem((IMenuSeparatorItem)menuItem);
|
---|
| 271 | }
|
---|
[2443] | 272 |
|
---|
[2696] | 273 | IEnumerable<IPositionableUserInterfaceItem> toolStripButtonItems =
|
---|
| 274 | from bi in allUserInterfaceItems
|
---|
| 275 | where (bi is IPositionableUserInterfaceItem) &&
|
---|
| 276 | (bi is IToolBarItem || bi is IToolBarSeparatorItem)
|
---|
| 277 | orderby ((IPositionableUserInterfaceItem)bi).Position
|
---|
| 278 | select (IPositionableUserInterfaceItem)bi;
|
---|
[2443] | 279 |
|
---|
[2696] | 280 | foreach (IPositionableUserInterfaceItem toolStripButtonItem in toolStripButtonItems) {
|
---|
| 281 | if (toolStripButtonItem is IToolBarItem)
|
---|
| 282 | AddToolStripButtonItem((IToolBarItem)toolStripButtonItem);
|
---|
| 283 | else if (toolStripButtonItem is IToolBarSeparatorItem)
|
---|
| 284 | AddToolStripButtonItem((IToolBarSeparatorItem)toolStripButtonItem);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | this.AdditionalCreationOfGUIElements();
|
---|
[2443] | 288 | }
|
---|
[2233] | 289 |
|
---|
[2696] | 290 | protected virtual void AdditionalCreationOfGUIElements() {
|
---|
[2243] | 291 | }
|
---|
| 292 |
|
---|
[2514] | 293 | private void AddToolStripMenuItem(IMenuItem menuItem) {
|
---|
[2696] | 294 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
| 295 | this.SetToolStripItemProperties(item, menuItem);
|
---|
| 296 | if (menuItem is MenuItem) {
|
---|
| 297 | MenuItem menuItemBase = (MenuItem)menuItem;
|
---|
| 298 | menuItemBase.ToolStripItem = item;
|
---|
| 299 | item.ShortcutKeys = menuItemBase.ShortCutKeys;
|
---|
| 300 | item.DisplayStyle = menuItemBase.ToolStripItemDisplayStyle;
|
---|
[2514] | 301 | }
|
---|
[2696] | 302 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
|
---|
[2541] | 303 | }
|
---|
[2243] | 304 |
|
---|
[2696] | 305 | private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
|
---|
| 306 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
|
---|
| 307 | }
|
---|
| 308 |
|
---|
[2514] | 309 | private void AddToolStripButtonItem(IToolBarItem buttonItem) {
|
---|
[2696] | 310 | ToolStripItem item = new ToolStripButton();
|
---|
| 311 | if (buttonItem is ToolBarItem) {
|
---|
| 312 | ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
|
---|
| 313 | if (buttonItemBase.IsDropDownButton)
|
---|
[2514] | 314 | item = new ToolStripDropDownButton();
|
---|
| 315 |
|
---|
[2696] | 316 | item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
|
---|
| 317 | buttonItemBase.ToolStripItem = item;
|
---|
| 318 | }
|
---|
[2541] | 319 |
|
---|
[2696] | 320 | this.SetToolStripItemProperties(item, buttonItem);
|
---|
[2426] | 321 | this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
|
---|
[2243] | 322 | }
|
---|
| 323 |
|
---|
[2696] | 324 | private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
|
---|
| 325 | this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[2443] | 328 | private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
|
---|
[2249] | 329 | ToolStripDropDownItem parent = null;
|
---|
[2426] | 330 | foreach (string s in structure) {
|
---|
| 331 | if (parentItems.ContainsKey(s))
|
---|
| 332 | parent = (ToolStripDropDownItem)parentItems[s];
|
---|
| 333 | else {
|
---|
[2443] | 334 | parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
|
---|
[2426] | 335 | parentItems.Add(parent);
|
---|
| 336 | }
|
---|
[2249] | 337 | parentItems = parent.DropDownItems;
|
---|
| 338 | }
|
---|
[2426] | 339 | parentItems.Add(item);
|
---|
[2249] | 340 | }
|
---|
| 341 |
|
---|
[2696] | 342 | private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
|
---|
[2426] | 343 | toolStripItem.Name = userInterfaceItem.Name;
|
---|
| 344 | toolStripItem.Text = userInterfaceItem.Name;
|
---|
| 345 | toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
|
---|
| 346 | toolStripItem.Tag = userInterfaceItem;
|
---|
| 347 | toolStripItem.Image = userInterfaceItem.Image;
|
---|
[2243] | 348 | toolStripItem.Click += new EventHandler(ToolStripItemClicked);
|
---|
[2426] | 349 | this.userInterfaceItems.Add(userInterfaceItem);
|
---|
[2243] | 350 | }
|
---|
| 351 |
|
---|
| 352 | private void ToolStripItemClicked(object sender, EventArgs e) {
|
---|
| 353 | System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
|
---|
[2696] | 354 | ((IActionUserInterfaceItem)item.Tag).Execute();
|
---|
[2243] | 355 | }
|
---|
[2233] | 356 | #endregion
|
---|
| 357 | }
|
---|
| 358 | }
|
---|