#region License Information /* HeuristicLab * Copyright (C) 2002-2008 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; namespace HeuristicLab.MainForm.WindowsForms { public partial class MainFormBase : Form, IMainForm { protected MainFormBase() : base() { InitializeComponent(); this.views = new List(); this.userInterfaceItems = new List(); MainFormManager.RegisterMainForm(this); } protected MainFormBase(Type userInterfaceItemType) : this() { this.userInterfaceItemType = userInterfaceItemType; CreateGUI(); OnActiveViewChanged(); FireMainFormChanged(); } #region IMainForm Members 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; } protected set { this.userInterfaceItemType = value; } } protected List views; public IEnumerable Views { get { return views; } } 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; } } public event EventHandler ActiveViewChanged; protected virtual void OnActiveViewChanged() { if (InvokeRequired) Invoke((MethodInvoker)OnActiveViewChanged); else if (ActiveViewChanged != null) ActiveViewChanged(this, new EventArgs()); } public event EventHandler Changed; public void FireMainFormChanged() { OnMainFormChanged(); } protected virtual void OnMainFormChanged() { if (InvokeRequired) Invoke((MethodInvoker)FireMainFormChanged); else if (Changed != null) Changed(this, new EventArgs()); } public virtual void ShowView(IView view) { if (!views.Contains(view)) { views.Add(view); } this.ActiveView = view; } public virtual void CloseView(IView view) { } public virtual void CloseAllViews() { foreach (IView view in views.ToArray()) CloseView(view); } protected virtual void ViewClosed(IView view) { views.Remove(view); if (ActiveView == view) ActiveView = null; } #endregion #region create menu and toolbar protected virtual void CreateGUI() { DiscoveryService ds = new DiscoveryService(); object[] items = ds.GetInstances(userInterfaceItemType); IEnumerable toolStripMenuItems = from mi in items where mi is MenuItemBase orderby ((MenuItemBase)mi).Position select (MenuItemBase)mi; foreach (MenuItemBase menuItem in toolStripMenuItems) AddToolStripMenuItem(menuItem); items = ds.GetInstances(userInterfaceItemType); IEnumerable toolStripButtonItems = from bi in items where bi is ToolBarItemBase orderby ((ToolBarItemBase)bi).Position select (ToolBarItemBase)bi; foreach (ToolBarItemBase toolStripButtonItem in toolStripButtonItems) AddToolStripButtonItem(toolStripButtonItem); } private void AddToolStripMenuItem(MenuItemBase menuItem) { ToolStripMenuItem item = new ToolStripMenuItem(); SetToolStripItemProperties(item, menuItem); menuItem.ToolStripItem = item; item.ShortcutKeys = menuItem.ShortCutKeys; item.DisplayStyle = menuItem.ToolStripItemDisplayStyle; this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items); } private void AddToolStripButtonItem(ToolBarItemBase buttonItem) { ToolStripItem item; if (buttonItem.IsDropDownButton) item = new ToolStripDropDownButton(); else item = new ToolStripButton(); SetToolStripItemProperties(item, buttonItem); item.DisplayStyle = buttonItem.ToolStripItemDisplayStyle; buttonItem.ToolStripItem = item; this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items); } private void InsertItem(IEnumerable structure, Type t,ToolStripItem item, ToolStripItemCollection parentItems) { ToolStripDropDownItem parent = null; foreach (string s in structure) { if (parentItems.ContainsKey(s)) parent = (ToolStripDropDownItem)parentItems[s]; else { parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s,null,null,s); ; parentItems.Add(parent); } parentItems = parent.DropDownItems; } parentItems.Add(item); } private void SetToolStripItemProperties(ToolStripItem toolStripItem, IUserInterfaceItem userInterfaceItem) { toolStripItem.Name = userInterfaceItem.Name; toolStripItem.Text = userInterfaceItem.Name; toolStripItem.ToolTipText = userInterfaceItem.ToolTipText; toolStripItem.Tag = userInterfaceItem; toolStripItem.Image = userInterfaceItem.Image; this.ActiveViewChanged += new EventHandler(userInterfaceItem.ActiveViewChanged); this.Changed += new EventHandler(userInterfaceItem.MainFormChanged); toolStripItem.Click += new EventHandler(ToolStripItemClicked); this.userInterfaceItems.Add(userInterfaceItem); } private void ToolStripItemClicked(object sender, EventArgs e) { System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender; ((IUserInterfaceItem)item.Tag).Execute(); } #endregion } }