Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/MainForm.cs @ 4375

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

Added InvokeRequired check to ShowContent method (ticket #1154).

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