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

Last change on this file since 3395 was 3395, checked in by mkommend, 11 years ago

renamed MainForm.ShowViewsInViewHost and removed ShowInViewHost from ViewAttribute (ticket #972)

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