Free cookie consent management tool by TermsFeed Policy Generator

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

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

changed detection of design mode in MainFormBase (ticket #771)

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