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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
32using System.Collections;
33using WeifenLuo.WinFormsUI.Docking;
34
35namespace HeuristicLab.MainForm.WindowsForms {
36  public partial class MainForm : Form, IMainForm {
37    private bool initialized;
38
39    protected MainForm()
40      : base() {
41      InitializeComponent();
42      this.views = new Dictionary<IView, Form>();
43      this.userInterfaceItems = new List<IUserInterfaceItem>();
44      this.initialized = false;
45    }
46
47    protected MainForm(Type userInterfaceItemType)
48      : this() {
49      this.userInterfaceItemType = userInterfaceItemType;
50    }
51
52    #region properties
53    public string Title {
54      get { return this.Text; }
55      set {
56        if (InvokeRequired) {
57          Action<string> action = delegate(string s) { this.Title = s; };
58          Invoke(action, value);
59        } else
60          this.Text = value;
61      }
62    }
63
64    public override Cursor Cursor {
65      get { return base.Cursor; }
66      set {
67        if (InvokeRequired) {
68          Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
69          Invoke(action, value);
70        } else
71          base.Cursor = value;
72      }
73    }
74
75    private Type userInterfaceItemType;
76    public Type UserInterfaceItemType {
77      get { return this.userInterfaceItemType; }
78    }
79
80    private Dictionary<IView, Form> views;
81    public IEnumerable<IView> Views {
82      get { return views.Keys; }
83    }
84
85    private IView activeView;
86    public IView ActiveView {
87      get { return this.activeView; }
88      protected set {
89        if (this.activeView != value) {
90          if (InvokeRequired) {
91            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
92            Invoke(action, value);
93          } else {
94            this.activeView = value;
95            OnActiveViewChanged();
96          }
97        }
98      }
99    }
100
101    private List<IUserInterfaceItem> userInterfaceItems;
102    protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
103      get { return this.userInterfaceItems; }
104    }
105    #endregion
106
107    #region events
108    public event EventHandler ActiveViewChanged;
109    protected virtual void OnActiveViewChanged() {
110      if (InvokeRequired)
111        Invoke((MethodInvoker)OnActiveViewChanged);
112      else if (ActiveViewChanged != null)
113        ActiveViewChanged(this, EventArgs.Empty);
114    }
115
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
140    public event EventHandler Changed;
141    protected void FireMainFormChanged() {
142      if (InvokeRequired)
143        Invoke((MethodInvoker)FireMainFormChanged);
144      else if (Changed != null)
145        Changed(this, EventArgs.Empty);
146    }
147
148    private void MainFormBase_Load(object sender, EventArgs e) {
149      if (!DesignMode) {
150        MainFormManager.RegisterMainForm(this);
151        if (!this.initialized) {
152          this.initialized = true;
153          this.OnInitialized(EventArgs.Empty);
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    }
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    }
190    #endregion
191
192    #region create, get, show, hide, close views
193    protected virtual Form CreateForm(IView view) {
194      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
195    }
196
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
207    internal void ShowView(IView view, bool firstTimeShown) {
208      if (InvokeRequired) Invoke((Action<IView, bool>)ShowView, view, firstTimeShown);
209      else {
210        if (firstTimeShown) {
211          Form form = CreateForm(view);
212          view.Changed += new EventHandler(ViewChanged);
213          this.views[view] = form;
214          form.Activated += new EventHandler(FormActivated);
215          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
216
217        }
218        this.Show(view, firstTimeShown);
219        this.OnViewShown(view, firstTimeShown);
220      }
221    }
222
223    private void ViewChanged(object sender, EventArgs e) {
224      IView view = (IView)sender;
225      if (view == this.ActiveView)
226        this.OnActiveViewChanged();
227    }
228
229    protected virtual void Show(IView view, bool firstTimeShown) {
230    }
231
232    internal void HideView(IView view) {
233      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
234      else {
235        if (this.views.ContainsKey(view)) {
236          this.Hide(view);
237          if (this.activeView == view)
238            this.ActiveView = null;
239          this.OnViewHidden(view);
240        }
241      }
242    }
243
244    protected virtual void Hide(IView view) {
245    }
246
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
261    internal void CloseView(IView view) {
262      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
263      else {
264        if (this.views.ContainsKey(view)) {
265          this.views[view].Close();
266          this.OnViewClosed(view);
267        }
268      }
269    }
270
271    internal void CloseView(IView view, CloseReason closeReason) {
272      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
273      else {
274        if (this.views.ContainsKey(view)) {
275          ((View)view).CloseReason = closeReason;
276          this.CloseView(view);
277        }
278      }
279    }
280
281    public void CloseAllViews() {
282      foreach (IView view in views.Keys.ToArray())
283        CloseView(view);
284    }
285
286    public void CloseAllViews(CloseReason closeReason) {
287      foreach (IView view in views.Keys.ToArray())
288        CloseView(view, closeReason);
289    }
290    #endregion
291
292    #region create menu and toolbar
293    private void CreateGUI() {
294      IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
295
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;
302
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      }
309
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;
316
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
324      this.AdditionalCreationOfGuiElements();
325    }
326
327    protected virtual void AdditionalCreationOfGuiElements() {
328    }
329
330    private void AddToolStripMenuItem(IMenuItem menuItem) {
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;
338      }
339      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
340    }
341
342    private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
343      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
344    }
345
346    private void AddToolStripButtonItem(IToolBarItem buttonItem) {
347      ToolStripItem item = new ToolStripButton();
348      if (buttonItem is ToolBarItem) {
349        ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
350        if (buttonItemBase.IsDropDownButton)
351          item = new ToolStripDropDownButton();
352
353        item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
354        buttonItemBase.ToolStripItem = item;
355      }
356
357      this.SetToolStripItemProperties(item, buttonItem);
358      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
359    }
360
361    private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
362      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
363    }
364
365    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
366      ToolStripDropDownItem parent = null;
367      foreach (string s in structure) {
368        if (parentItems.ContainsKey(s))
369          parent = (ToolStripDropDownItem)parentItems[s];
370        else {
371          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
372          parentItems.Add(parent);
373        }
374        parentItems = parent.DropDownItems;
375      }
376      parentItems.Add(item);
377    }
378
379    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
380      toolStripItem.Name = userInterfaceItem.Name;
381      toolStripItem.Text = userInterfaceItem.Name;
382      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
383      toolStripItem.Tag = userInterfaceItem;
384      toolStripItem.Image = userInterfaceItem.Image;
385      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
386      this.userInterfaceItems.Add(userInterfaceItem);
387    }
388
389    private void ToolStripItemClicked(object sender, EventArgs e) {
390      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
391      ((IActionUserInterfaceItem)item.Tag).Execute();
392    }
393    #endregion
394  }
395}
Note: See TracBrowser for help on using the repository browser.