Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/MainFormBase.cs @ 2529

Last change on this file since 2529 was 2514, checked in by mkommend, 15 years ago

added ability to generate separators in between menu- and toolbaritems (ticket #771)

File size: 10.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.MainForm.WindowsForms {
34  public partial class MainFormBase : Form, IMainForm {
35    protected MainFormBase()
36      : base() {
37      InitializeComponent();
38      this.views = new Dictionary<IView, Form>();
39      this.userInterfaceItems = new List<IUserInterfaceItem>();
40    }
41
42    protected MainFormBase(Type userInterfaceItemType)
43      : this() {
44      this.userInterfaceItemType = userInterfaceItemType;
45      CreateGUI();
46    }
47
48    private void MainFormBase_Load(object sender, EventArgs e) {
49      if (!DesignMode)
50        MainFormManager.RegisterMainForm(this);
51    }
52
53    #region IMainForm Members
54    public string Title {
55      get { return this.Text; }
56      set {
57        if (InvokeRequired) {
58          Action<string> action = delegate(string s) { this.Title = s; };
59          Invoke(action, value);
60        } else
61          this.Text = value;
62      }
63    }
64
65    public override Cursor Cursor {
66      get { return base.Cursor; }
67      set {
68        if (InvokeRequired) {
69          Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
70          Invoke(action, value);
71        } else
72          base.Cursor = value;
73      }
74    }
75
76    private Type userInterfaceItemType;
77    public Type UserInterfaceItemType {
78      get { return this.userInterfaceItemType; }
79    }
80
81    private Dictionary<IView, Form> views;
82    public IEnumerable<IView> Views {
83      get { return views.Keys; }
84    }
85
86    public Form GetForm(IView view) {
87      if (views.ContainsKey(view))
88        return views[view];
89      return null;
90    }
91
92    private IView activeView;
93    public IView ActiveView {
94      get { return this.activeView; }
95      protected set {
96        if (this.activeView != value) {
97          if (InvokeRequired) {
98            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
99            Invoke(action, value);
100          } else {
101            this.activeView = value;
102            OnActiveViewChanged();
103          }
104        }
105      }
106    }
107
108    private List<IUserInterfaceItem> userInterfaceItems;
109    protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
110      get { return this.userInterfaceItems; }
111    }
112
113    public event EventHandler ActiveViewChanged;
114    protected virtual void OnActiveViewChanged() {
115      if (InvokeRequired)
116        Invoke((MethodInvoker)OnActiveViewChanged);
117      else if (ActiveViewChanged != null)
118        ActiveViewChanged(this, new EventArgs());
119    }
120
121    public event EventHandler Changed;
122    public void FireMainFormChanged() {
123      OnMainFormChanged();
124    }
125    protected virtual void OnMainFormChanged() {
126      if (InvokeRequired)
127        Invoke((MethodInvoker)OnMainFormChanged);
128      else if (Changed != null)
129        Changed(this, new EventArgs());
130    }
131
132    protected virtual Form CreateForm(IView view) {
133      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainFormBase.");
134    }
135
136    public virtual bool ShowView(IView view) {
137      if (InvokeRequired) return (bool)Invoke((Func<IView, bool>)ShowView, view);
138      else {
139        if (!views.Keys.Contains(view)) {
140          Form form = CreateForm(view);
141          views[view] = form;
142          form.Activated += new EventHandler(FormActivated);
143          form.GotFocus += new EventHandler(FormActivated);
144          form.FormClosing += new FormClosingEventHandler(view.OnClosing);
145          form.FormClosed += new FormClosedEventHandler(view.OnClosed);
146          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
147          foreach (IUserInterfaceItem item in UserInterfaceItems)
148            view.Changed += new EventHandler(item.ViewChanged);
149          return true;
150        } else
151          return false;
152      }
153    }
154
155    public virtual void HideView(IView view) {
156      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
157      else {
158        if (views.ContainsKey(view))
159          views[view].Hide();
160      }
161    }
162
163    public void CloseView(IView view) {
164      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
165      else {
166        if (views.ContainsKey(view))
167          views[view].Close();
168      }
169    }
170
171    public virtual void CloseAllViews() {
172      foreach (IView view in views.Keys.ToArray())
173        CloseView(view);
174    }
175    #endregion
176
177    #region events
178    private void ChildFormClosed(object sender, FormClosedEventArgs e) {
179      Form form = (Form)sender;
180      IView view = GetViewForForm(form);
181
182      form.Activated -= new EventHandler(FormActivated);
183      form.GotFocus -= new EventHandler(FormActivated);
184      form.FormClosing -= new FormClosingEventHandler(view.OnClosing);
185      form.FormClosed -= new FormClosedEventHandler(view.OnClosed);
186      form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
187      foreach (IUserInterfaceItem item in UserInterfaceItems)
188        view.Changed -= new EventHandler(item.ViewChanged);
189
190      views.Remove(view);
191      if (ActiveView == view)
192        ActiveView = null;
193    }
194
195    private void FormActivated(object sender, EventArgs e) {
196      this.ActiveView = GetViewForForm((Form)sender);
197    }
198
199    private IView GetViewForForm(Form form) {
200      return views.Where(x => x.Value == form).Single().Key;
201    }
202    #endregion
203
204    #region create menu and toolbar
205    protected virtual void CreateGUI() {
206      DiscoveryService ds = new DiscoveryService();
207
208      object[] items = ds.GetInstances(userInterfaceItemType);
209      IEnumerable<IMenuItem> toolStripMenuItems =
210        from mi in items
211        where mi is IMenuItem
212        orderby ((IMenuItem)mi).Position
213        select (IMenuItem)mi;
214      foreach (IMenuItem menuItem in toolStripMenuItems)
215        AddToolStripMenuItem(menuItem);
216
217      items = ds.GetInstances(userInterfaceItemType);
218      IEnumerable<IToolBarItem> toolStripButtonItems =
219        from bi in items
220        where bi is IToolBarItem
221        orderby ((IToolBarItem)bi).Position
222        select (IToolBarItem)bi;
223      foreach (IToolBarItem toolStripButtonItem in toolStripButtonItems)
224        AddToolStripButtonItem(toolStripButtonItem);
225    }
226
227    private void AddToolStripMenuItem(IMenuItem menuItem) {
228      if (menuItem is MenuItemBase) {
229        ToolStripMenuItem item = new ToolStripMenuItem();
230        SetToolStripItemProperties(item, menuItem);
231        ((MenuItemBase)menuItem).ToolStripItem = item;
232        item.ShortcutKeys = ((MenuItemBase)menuItem).ShortCutKeys;
233        item.DisplayStyle = ((MenuItemBase)menuItem).ToolStripItemDisplayStyle;
234        this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
235      } else if (menuItem is MenuSeparatorItemBase) {
236        this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
237      }
238     }
239
240    private void AddToolStripButtonItem(IToolBarItem buttonItem) {
241      ToolStripItem item = null;
242
243      if (buttonItem is ToolBarItemBase) {
244        if (((ToolBarItemBase)buttonItem).IsDropDownButton)
245          item = new ToolStripDropDownButton();
246        else
247          item = new ToolStripButton();
248
249        SetToolStripItemProperties(item, buttonItem);
250        item.DisplayStyle = ((ToolBarItemBase)buttonItem).ToolStripItemDisplayStyle;
251        ((ToolBarItemBase)buttonItem).ToolStripItem = item;
252      } else if (buttonItem is IToolBarSeparatorItem)
253        item = new ToolStripSeparator();
254     
255      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
256    }
257
258    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
259      ToolStripDropDownItem parent = null;
260      foreach (string s in structure) {
261        if (parentItems.ContainsKey(s))
262          parent = (ToolStripDropDownItem)parentItems[s];
263        else {
264          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
265          parentItems.Add(parent);
266        }
267        parentItems = parent.DropDownItems;
268      }
269      parentItems.Add(item);
270    }
271
272    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IUserInterfaceItem userInterfaceItem) {
273      toolStripItem.Name = userInterfaceItem.Name;
274      toolStripItem.Text = userInterfaceItem.Name;
275      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
276      toolStripItem.Tag = userInterfaceItem;
277      toolStripItem.Image = userInterfaceItem.Image;
278      this.ActiveViewChanged += new EventHandler(userInterfaceItem.ActiveViewChanged);
279      this.Changed += new EventHandler(userInterfaceItem.MainFormChanged);
280      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
281      this.userInterfaceItems.Add(userInterfaceItem);
282    }
283
284    private void ToolStripItemClicked(object sender, EventArgs e) {
285      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
286      ((IUserInterfaceItem)item.Tag).Execute();
287    }
288    #endregion
289  }
290}
Note: See TracBrowser for help on using the repository browser.