Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4144 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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