Free cookie consent management tool by TermsFeed Policy Generator

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

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

added bug fixes to avoid CreateWindowHandle exceptions (ticket #972)

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