Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3301 was 3301, checked in by mkommend, 14 years ago

implemented fix for ticket #966

File size: 13.5 KB
RevLine 
[2233]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2233]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
[2243]31using HeuristicLab.PluginInfrastructure;
[2696]32using System.Collections;
[3301]33using WeifenLuo.WinFormsUI.Docking;
[2243]34
[2426]35namespace HeuristicLab.MainForm.WindowsForms {
[2696]36  public partial class MainForm : Form, IMainForm {
37    private bool initialized;
[2541]38
[2696]39    protected MainForm()
[2233]40      : base() {
41      InitializeComponent();
[2443]42      this.views = new Dictionary<IView, Form>();
[2426]43      this.userInterfaceItems = new List<IUserInterfaceItem>();
[2541]44      this.initialized = false;
[2268]45    }
46
[2696]47    protected MainForm(Type userInterfaceItemType)
[2268]48      : this() {
[2233]49      this.userInterfaceItemType = userInterfaceItemType;
50    }
51
[2696]52    #region properties
[2233]53    public string Title {
[2243]54      get { return this.Text; }
[2256]55      set {
56        if (InvokeRequired) {
57          Action<string> action = delegate(string s) { this.Title = s; };
[2407]58          Invoke(action, value);
[2256]59        } else
60          this.Text = value;
61      }
[2233]62    }
63
[2350]64    public override Cursor Cursor {
[2338]65      get { return base.Cursor; }
66      set {
67        if (InvokeRequired) {
68          Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
[2407]69          Invoke(action, value);
[2338]70        } else
71          base.Cursor = value;
72      }
73    }
74
[2256]75    private Type userInterfaceItemType;
[2233]76    public Type UserInterfaceItemType {
77      get { return this.userInterfaceItemType; }
78    }
79
[2443]80    private Dictionary<IView, Form> views;
[2358]81    public IEnumerable<IView> Views {
[2443]82      get { return views.Keys; }
[2358]83    }
84
[2254]85    private IView activeView;
[2233]86    public IView ActiveView {
87      get { return this.activeView; }
[2254]88      protected set {
[2963]89        if (this.activeView != value) {
[2309]90          if (InvokeRequired) {
91            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
[2407]92            Invoke(action, value);
[2309]93          } else {
94            this.activeView = value;
95            OnActiveViewChanged();
96          }
[2254]97        }
98      }
[2233]99    }
100
[2426]101    private List<IUserInterfaceItem> userInterfaceItems;
102    protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
103      get { return this.userInterfaceItems; }
[2256]104    }
[2696]105    #endregion
[2254]106
[2696]107    #region events
[2254]108    public event EventHandler ActiveViewChanged;
109    protected virtual void OnActiveViewChanged() {
[2336]110      if (InvokeRequired)
111        Invoke((MethodInvoker)OnActiveViewChanged);
112      else if (ActiveViewChanged != null)
[2793]113        ActiveViewChanged(this, EventArgs.Empty);
[2254]114    }
115
[2548]116    public event EventHandler<ViewEventArgs> ViewClosed;
117    protected virtual void OnViewClosed(IView view) {
118      if (InvokeRequired) Invoke((Action<IView>)OnViewClosed, view);
119      else if (this.ViewClosed != null) {
120        this.ViewClosed(this, new ViewEventArgs(view));
121      }
122    }
123
124    public event EventHandler<ViewShownEventArgs> ViewShown;
125    protected virtual void OnViewShown(IView view, bool firstTimeShown) {
126      if (InvokeRequired) Invoke((Action<IView, bool>)OnViewShown, view, firstTimeShown);
127      else if (this.ViewShown != null) {
128        this.ViewShown(this, new ViewShownEventArgs(view, firstTimeShown));
129      }
130    }
131
132    public event EventHandler<ViewEventArgs> ViewHidden;
133    protected virtual void OnViewHidden(IView view) {
134      if (InvokeRequired) Invoke((Action<IView>)OnViewHidden, view);
135      else if (this.ViewHidden != null) {
136        this.ViewHidden(this, new ViewEventArgs(view));
137      }
138    }
139
[2426]140    public event EventHandler Changed;
[2703]141    protected void FireMainFormChanged() {
[2336]142      if (InvokeRequired)
[2696]143        Invoke((MethodInvoker)FireMainFormChanged);
[2426]144      else if (Changed != null)
[2793]145        Changed(this, EventArgs.Empty);
[2300]146    }
147
[2696]148    private void MainFormBase_Load(object sender, EventArgs e) {
149      if (!DesignMode) {
150        MainFormManager.RegisterMainForm(this);
151        if (!this.initialized) {
152          this.initialized = true;
[2793]153          this.OnInitialized(EventArgs.Empty);
[2696]154        }
155        this.CreateGUI();
156      }
157    }
158
159    protected virtual void OnInitialized(EventArgs e) {
160    }
161
162    private void FormActivated(object sender, EventArgs e) {
163      this.ActiveView = GetView((Form)sender);
164    }
[3301]165
166    protected override void OnFormClosing(FormClosingEventArgs e) {
167      foreach (KeyValuePair<IView,Form > pair in this.views) {
168        DockForm dockForm = pair.Value as DockForm;
169        View view = pair.Key as View;
170        if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
171          view.CloseReason = CloseReason.ApplicationExitCall;
172          view.OnClosingHelper(dockForm, e);
173        }
174      }
175      base.OnFormClosing(e);
176    }
177
178    protected override void OnFormClosed(FormClosedEventArgs e) {
179      foreach (KeyValuePair<IView, Form> pair in this.views.ToList()) {
180        DockForm dockForm = pair.Value as DockForm;
181        View view = pair.Key as View;
182        if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
183          view.CloseReason = CloseReason.ApplicationExitCall;
184          view.OnClosedHelper(dockForm, e);
185          dockForm.Close();
186        }
187      }
188      base.OnFormClosed(e);
189    }
[2696]190    #endregion
191
192    #region create, get, show, hide, close views
[2443]193    protected virtual Form CreateForm(IView view) {
[2963]194      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
[2443]195    }
196
[2696]197    internal Form GetForm(IView view) {
198      if (views.ContainsKey(view))
199        return views[view];
200      return null;
201    }
202
203    internal IView GetView(Form form) {
204      return views.Where(x => x.Value == form).Single().Key;
205    }
206
[2704]207    internal void ShowView(IView view, bool firstTimeShown) {
208      if (InvokeRequired) Invoke((Action<IView, bool>)ShowView, view, firstTimeShown);
[2443]209      else {
[2696]210        if (firstTimeShown) {
[2443]211          Form form = CreateForm(view);
[2963]212          view.Changed += new EventHandler(ViewChanged);
[2696]213          this.views[view] = form;
[2443]214          form.Activated += new EventHandler(FormActivated);
215          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
[3301]216
[2548]217        }
[2696]218        this.Show(view, firstTimeShown);
219        this.OnViewShown(view, firstTimeShown);
[2306]220      }
[2233]221    }
[2297]222
[2963]223    private void ViewChanged(object sender, EventArgs e) {
224      IView view = (IView)sender;
225      if (view == this.ActiveView)
226        this.OnActiveViewChanged();
227    }
228
[2636]229    protected virtual void Show(IView view, bool firstTimeShown) {
[2548]230    }
231
[2696]232    internal void HideView(IView view) {
[2443]233      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
234      else {
[2548]235        if (this.views.ContainsKey(view)) {
236          this.Hide(view);
[2963]237          if (this.activeView == view)
[2723]238            this.ActiveView = null;
[2548]239          this.OnViewHidden(view);
240        }
[2443]241      }
[2305]242    }
243
[2548]244    protected virtual void Hide(IView view) {
245    }
246
[2963]247    private void ChildFormClosed(object sender, FormClosedEventArgs e) {
248      Form form = (Form)sender;
249      IView view = GetView(form);
250
251      view.Changed -= new EventHandler(ViewChanged);
252      form.Activated -= new EventHandler(FormActivated);
253      form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
254
255      views.Remove(view);
256      this.OnViewClosed(view);
257      if (ActiveView == view)
258        ActiveView = null;
259    }
260
[2696]261    internal void CloseView(IView view) {
[2443]262      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
263      else {
[2548]264        if (this.views.ContainsKey(view)) {
265          this.views[view].Close();
266          this.OnViewClosed(view);
267        }
[2544]268      }
269    }
270
[2696]271    internal void CloseView(IView view, CloseReason closeReason) {
[2544]272      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
273      else {
[2548]274        if (this.views.ContainsKey(view)) {
[2963]275          ((View)view).CloseReason = closeReason;
[2548]276          this.CloseView(view);
[2543]277        }
[2443]278      }
279    }
280
[2696]281    public void CloseAllViews() {
[2443]282      foreach (IView view in views.Keys.ToArray())
[2305]283        CloseView(view);
284    }
[2544]285
[2696]286    public void CloseAllViews(CloseReason closeReason) {
[2544]287      foreach (IView view in views.Keys.ToArray())
[2548]288        CloseView(view, closeReason);
[2544]289    }
[2443]290    #endregion
[2305]291
[2696]292    #region create menu and toolbar
293    private void CreateGUI() {
294      IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
[2443]295
[2696]296      IEnumerable<IPositionableUserInterfaceItem> toolStripMenuItems =
297        from mi in allUserInterfaceItems
298        where (mi is IPositionableUserInterfaceItem) &&
299              (mi is IMenuItem || mi is IMenuSeparatorItem)
300        orderby ((IPositionableUserInterfaceItem)mi).Position
301        select (IPositionableUserInterfaceItem)mi;
[2443]302
[2696]303      foreach (IPositionableUserInterfaceItem menuItem in toolStripMenuItems) {
304        if (menuItem is IMenuItem)
305          AddToolStripMenuItem((IMenuItem)menuItem);
306        else if (menuItem is IMenuSeparatorItem)
307          AddToolStripMenuItem((IMenuSeparatorItem)menuItem);
308      }
[2443]309
[2696]310      IEnumerable<IPositionableUserInterfaceItem> toolStripButtonItems =
311        from bi in allUserInterfaceItems
312        where (bi is IPositionableUserInterfaceItem) &&
313              (bi is IToolBarItem || bi is IToolBarSeparatorItem)
314        orderby ((IPositionableUserInterfaceItem)bi).Position
315        select (IPositionableUserInterfaceItem)bi;
[2443]316
[2696]317      foreach (IPositionableUserInterfaceItem toolStripButtonItem in toolStripButtonItems) {
318        if (toolStripButtonItem is IToolBarItem)
319          AddToolStripButtonItem((IToolBarItem)toolStripButtonItem);
320        else if (toolStripButtonItem is IToolBarSeparatorItem)
321          AddToolStripButtonItem((IToolBarSeparatorItem)toolStripButtonItem);
322      }
323
[2761]324      this.AdditionalCreationOfGuiElements();
[2443]325    }
[2233]326
[2761]327    protected virtual void AdditionalCreationOfGuiElements() {
[2243]328    }
329
[2514]330    private void AddToolStripMenuItem(IMenuItem menuItem) {
[2696]331      ToolStripMenuItem item = new ToolStripMenuItem();
332      this.SetToolStripItemProperties(item, menuItem);
333      if (menuItem is MenuItem) {
334        MenuItem menuItemBase = (MenuItem)menuItem;
335        menuItemBase.ToolStripItem = item;
336        item.ShortcutKeys = menuItemBase.ShortCutKeys;
337        item.DisplayStyle = menuItemBase.ToolStripItemDisplayStyle;
[2514]338      }
[2696]339      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
[2541]340    }
[2243]341
[2696]342    private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
343      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
344    }
345
[2514]346    private void AddToolStripButtonItem(IToolBarItem buttonItem) {
[2696]347      ToolStripItem item = new ToolStripButton();
348      if (buttonItem is ToolBarItem) {
349        ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
350        if (buttonItemBase.IsDropDownButton)
[2514]351          item = new ToolStripDropDownButton();
352
[2696]353        item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
354        buttonItemBase.ToolStripItem = item;
355      }
[2541]356
[2696]357      this.SetToolStripItemProperties(item, buttonItem);
[2426]358      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
[2243]359    }
360
[2696]361    private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
362      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
363    }
364
[2443]365    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
[2249]366      ToolStripDropDownItem parent = null;
[2426]367      foreach (string s in structure) {
368        if (parentItems.ContainsKey(s))
369          parent = (ToolStripDropDownItem)parentItems[s];
370        else {
[2443]371          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
[2426]372          parentItems.Add(parent);
373        }
[2249]374        parentItems = parent.DropDownItems;
375      }
[2426]376      parentItems.Add(item);
[2249]377    }
378
[2696]379    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
[2426]380      toolStripItem.Name = userInterfaceItem.Name;
381      toolStripItem.Text = userInterfaceItem.Name;
382      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
383      toolStripItem.Tag = userInterfaceItem;
384      toolStripItem.Image = userInterfaceItem.Image;
[2243]385      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
[2426]386      this.userInterfaceItems.Add(userInterfaceItem);
[2243]387    }
388
389    private void ToolStripItemClicked(object sender, EventArgs e) {
390      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
[2696]391      ((IActionUserInterfaceItem)item.Tag).Execute();
[2243]392    }
[2233]393    #endregion
394  }
395}
Note: See TracBrowser for help on using the repository browser.