#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HeuristicLab.PluginInfrastructure;
using System.Collections;
using WeifenLuo.WinFormsUI.Docking;
using HeuristicLab.Common;
namespace HeuristicLab.MainForm.WindowsForms {
public partial class MainForm : Form, IMainForm {
private bool initialized;
protected MainForm()
: base() {
InitializeComponent();
this.views = new Dictionary();
this.userInterfaceItems = new List();
this.initialized = false;
this.showContentInViewHost = false;
}
protected MainForm(Type userInterfaceItemType)
: this() {
this.userInterfaceItemType = userInterfaceItemType;
}
#region properties
private bool showContentInViewHost;
public bool ShowContentInViewHost {
get { return this.showContentInViewHost; }
set { this.showContentInViewHost = value; }
}
public string Title {
get { return this.Text; }
set {
if (InvokeRequired) {
Action action = delegate(string s) { this.Title = s; };
Invoke(action, value);
} else
this.Text = value;
}
}
public override Cursor Cursor {
get { return base.Cursor; }
set {
if (InvokeRequired) {
Action action = delegate(Cursor c) { this.Cursor = c; };
Invoke(action, value);
} else
base.Cursor = value;
}
}
private Type userInterfaceItemType;
public Type UserInterfaceItemType {
get { return this.userInterfaceItemType; }
}
private Dictionary views;
public IEnumerable Views {
get { return views.Keys; }
}
private IView activeView;
public IView ActiveView {
get { return this.activeView; }
protected set {
if (this.activeView != value) {
if (InvokeRequired) {
Action action = delegate(IView activeView) { this.ActiveView = activeView; };
Invoke(action, value);
} else {
this.activeView = value;
OnActiveViewChanged();
}
}
}
}
private List userInterfaceItems;
protected IEnumerable UserInterfaceItems {
get { return this.userInterfaceItems; }
}
#endregion
#region events
public event EventHandler ActiveViewChanged;
protected virtual void OnActiveViewChanged() {
if (InvokeRequired)
Invoke((MethodInvoker)OnActiveViewChanged);
else if (ActiveViewChanged != null)
ActiveViewChanged(this, EventArgs.Empty);
}
public event EventHandler ViewClosed;
protected virtual void OnViewClosed(IView view) {
if (InvokeRequired) Invoke((Action)OnViewClosed, view);
else {
EventHandler handler = ViewClosed;
if (handler != null)
handler(this, new ViewEventArgs(view));
}
}
public event EventHandler ViewShown;
protected virtual void OnViewShown(IView view, bool firstTimeShown) {
if (InvokeRequired) Invoke((Action)OnViewShown, view, firstTimeShown);
else {
EventHandler handler = ViewShown;
if (handler != null)
handler(this, new ViewShownEventArgs(view, firstTimeShown));
}
}
public event EventHandler ViewHidden;
protected virtual void OnViewHidden(IView view) {
if (InvokeRequired) Invoke((Action)OnViewHidden, view);
else {
EventHandler handler = ViewHidden;
if (handler != null)
handler(this, new ViewEventArgs(view));
}
}
public event EventHandler Changed;
protected void OnChanged() {
if (InvokeRequired)
Invoke((MethodInvoker)OnChanged);
else {
EventHandler handler = Changed;
if (handler != null)
Changed(this, EventArgs.Empty);
}
}
private void MainFormBase_Load(object sender, EventArgs e) {
if (!DesignMode) {
MainFormManager.RegisterMainForm(this);
if (!this.initialized) {
this.initialized = true;
this.OnInitialized(EventArgs.Empty);
}
this.CreateGUI();
}
}
protected virtual void OnInitialized(EventArgs e) {
}
private void FormActivated(object sender, EventArgs e) {
this.ActiveView = GetView((Form)sender);
}
protected override void OnFormClosing(FormClosingEventArgs e) {
foreach (KeyValuePair pair in this.views) {
DockForm dockForm = pair.Value as DockForm;
View view = pair.Key as View;
if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
view.CloseReason = CloseReason.ApplicationExitCall;
view.OnClosingHelper(dockForm, e);
}
}
base.OnFormClosing(e);
}
protected override void OnFormClosed(FormClosedEventArgs e) {
foreach (KeyValuePair pair in this.views.ToList()) {
DockForm dockForm = pair.Value as DockForm;
View view = pair.Key as View;
if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
view.CloseReason = CloseReason.ApplicationExitCall;
view.OnClosedHelper(dockForm, e);
dockForm.Close();
}
}
base.OnFormClosed(e);
}
#endregion
#region create, get, show, hide, close views
protected virtual Form CreateForm(IView view) {
throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
}
internal Form GetForm(IView view) {
if (views.ContainsKey(view))
return views[view];
return null;
}
protected IView GetView(Form form) {
return views.Where(x => x.Value == form).Single().Key;
}
public IContentView ShowContent(IContent content) {
IContentView view;
if (this.ShowContentInViewHost)
view = new ViewHost();
else
view = MainFormManager.CreateDefaultView(content.GetType());
if (view != null) {
view.Show();
view.Content = content;
}
return view;
}
internal void ShowView(IView view) {
if (InvokeRequired) Invoke((Action)ShowView, view);
else {
Form form = GetForm(view);
bool firstTimeShown = form == null;
if (firstTimeShown) {
form = CreateForm(view);
form.Activated += new EventHandler(FormActivated);
form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
view.Changed += new EventHandler(View_Changed);
views[view] = form;
}
this.ShowView(view, firstTimeShown);
this.OnViewShown(view, firstTimeShown);
}
}
private void View_Changed(object sender, EventArgs e) {
IView view = (IView)sender;
if (view == this.ActiveView)
this.OnActiveViewChanged();
this.OnChanged();
}
protected virtual void ShowView(IView view, bool firstTimeShown) {
}
internal void HideView(IView view) {
if (InvokeRequired) Invoke((Action)HideView, view);
else {
this.Hide(view);
if (this.activeView == view)
this.ActiveView = null;
this.OnViewHidden(view);
}
}
protected virtual void Hide(IView view) {
}
private void ChildFormClosed(object sender, FormClosedEventArgs e) {
Form form = (Form)sender;
IView view = GetView(form);
view.Changed -= new EventHandler(View_Changed);
form.Activated -= new EventHandler(FormActivated);
form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
views.Remove(view);
this.OnViewClosed(view);
if (ActiveView == view)
ActiveView = null;
}
internal void CloseView(IView view) {
if (InvokeRequired) Invoke((Action)CloseView, view);
else if (views.ContainsKey(view)) {
this.views[view].Close();
this.OnViewClosed(view);
}
}
internal void CloseView(IView view, CloseReason closeReason) {
if (InvokeRequired) Invoke((Action)CloseView, view);
else if (views.ContainsKey(view)) {
((View)view).CloseReason = closeReason;
this.CloseView(view);
}
}
public void CloseAllViews() {
foreach (IView view in views.Keys.ToArray())
CloseView(view);
}
public void CloseAllViews(CloseReason closeReason) {
foreach (IView view in views.Keys.ToArray())
CloseView(view, closeReason);
}
#endregion
#region create menu and toolbar
private void CreateGUI() {
IEnumerable