Free cookie consent management tool by TermsFeed Policy Generator

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

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

changed CloseReason to FormOwnerClosing if IView.Close method is used (ticket #771)

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