Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 13.7 KB
RevLine 
[2233]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2233]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
[2243]31using HeuristicLab.PluginInfrastructure;
[2696]32using System.Collections;
[3301]33using WeifenLuo.WinFormsUI.Docking;
[2243]34
[2426]35namespace HeuristicLab.MainForm.WindowsForms {
[2696]36  public partial class MainForm : Form, IMainForm {
37    private bool initialized;
[2541]38
[2696]39    protected MainForm()
[2233]40      : base() {
41      InitializeComponent();
[2443]42      this.views = new Dictionary<IView, Form>();
[2426]43      this.userInterfaceItems = new List<IUserInterfaceItem>();
[2541]44      this.initialized = false;
[3395]45      this.showViewsInViewHost = false;
[2268]46    }
47
[2696]48    protected MainForm(Type userInterfaceItemType)
[2268]49      : this() {
[2233]50      this.userInterfaceItemType = userInterfaceItemType;
51    }
52
[2696]53    #region properties
[3395]54    private bool showViewsInViewHost;
55    public bool ShowViewsInViewHost {
56      get { return this.showViewsInViewHost; }
57      set { this.showViewsInViewHost = value; }
[3394]58    }
59
[2233]60    public string Title {
[2243]61      get { return this.Text; }
[2256]62      set {
63        if (InvokeRequired) {
64          Action<string> action = delegate(string s) { this.Title = s; };
[2407]65          Invoke(action, value);
[2256]66        } else
67          this.Text = value;
68      }
[2233]69    }
70
[2350]71    public override Cursor Cursor {
[2338]72      get { return base.Cursor; }
73      set {
74        if (InvokeRequired) {
75          Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
[2407]76          Invoke(action, value);
[2338]77        } else
78          base.Cursor = value;
79      }
80    }
81
[2256]82    private Type userInterfaceItemType;
[2233]83    public Type UserInterfaceItemType {
84      get { return this.userInterfaceItemType; }
85    }
86
[2443]87    private Dictionary<IView, Form> views;
[2358]88    public IEnumerable<IView> Views {
[2443]89      get { return views.Keys; }
[2358]90    }
91
[2254]92    private IView activeView;
[2233]93    public IView ActiveView {
94      get { return this.activeView; }
[2254]95      protected set {
[2963]96        if (this.activeView != value) {
[2309]97          if (InvokeRequired) {
98            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
[2407]99            Invoke(action, value);
[2309]100          } else {
101            this.activeView = value;
102            OnActiveViewChanged();
103          }
[2254]104        }
105      }
[2233]106    }
107
[2426]108    private List<IUserInterfaceItem> userInterfaceItems;
109    protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
110      get { return this.userInterfaceItems; }
[2256]111    }
[2696]112    #endregion
[2254]113
[2696]114    #region events
[2254]115    public event EventHandler ActiveViewChanged;
116    protected virtual void OnActiveViewChanged() {
[2336]117      if (InvokeRequired)
118        Invoke((MethodInvoker)OnActiveViewChanged);
119      else if (ActiveViewChanged != null)
[2793]120        ActiveViewChanged(this, EventArgs.Empty);
[2254]121    }
122
[2548]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
[2426]147    public event EventHandler Changed;
[2703]148    protected void FireMainFormChanged() {
[2336]149      if (InvokeRequired)
[2696]150        Invoke((MethodInvoker)FireMainFormChanged);
[2426]151      else if (Changed != null)
[2793]152        Changed(this, EventArgs.Empty);
[2300]153    }
154
[2696]155    private void MainFormBase_Load(object sender, EventArgs e) {
156      if (!DesignMode) {
157        MainFormManager.RegisterMainForm(this);
158        if (!this.initialized) {
159          this.initialized = true;
[2793]160          this.OnInitialized(EventArgs.Empty);
[2696]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    }
[3301]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    }
[2696]197    #endregion
198
199    #region create, get, show, hide, close views
[2443]200    protected virtual Form CreateForm(IView view) {
[2963]201      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
[2443]202    }
203
[2696]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
[2704]214    internal void ShowView(IView view, bool firstTimeShown) {
215      if (InvokeRequired) Invoke((Action<IView, bool>)ShowView, view, firstTimeShown);
[2443]216      else {
[2696]217        if (firstTimeShown) {
[2443]218          Form form = CreateForm(view);
[2963]219          view.Changed += new EventHandler(ViewChanged);
[2696]220          this.views[view] = form;
[2443]221          form.Activated += new EventHandler(FormActivated);
222          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
[3301]223
[2548]224        }
[2696]225        this.Show(view, firstTimeShown);
226        this.OnViewShown(view, firstTimeShown);
[2306]227      }
[2233]228    }
[2297]229
[2963]230    private void ViewChanged(object sender, EventArgs e) {
231      IView view = (IView)sender;
232      if (view == this.ActiveView)
233        this.OnActiveViewChanged();
234    }
235
[2636]236    protected virtual void Show(IView view, bool firstTimeShown) {
[2548]237    }
238
[2696]239    internal void HideView(IView view) {
[2443]240      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
241      else {
[2548]242        if (this.views.ContainsKey(view)) {
243          this.Hide(view);
[2963]244          if (this.activeView == view)
[2723]245            this.ActiveView = null;
[2548]246          this.OnViewHidden(view);
247        }
[2443]248      }
[2305]249    }
250
[2548]251    protected virtual void Hide(IView view) {
252    }
253
[2963]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
[2696]268    internal void CloseView(IView view) {
[2443]269      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
270      else {
[2548]271        if (this.views.ContainsKey(view)) {
272          this.views[view].Close();
273          this.OnViewClosed(view);
274        }
[2544]275      }
276    }
277
[2696]278    internal void CloseView(IView view, CloseReason closeReason) {
[2544]279      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
280      else {
[2548]281        if (this.views.ContainsKey(view)) {
[2963]282          ((View)view).CloseReason = closeReason;
[2548]283          this.CloseView(view);
[2543]284        }
[2443]285      }
286    }
287
[2696]288    public void CloseAllViews() {
[2443]289      foreach (IView view in views.Keys.ToArray())
[2305]290        CloseView(view);
291    }
[2544]292
[2696]293    public void CloseAllViews(CloseReason closeReason) {
[2544]294      foreach (IView view in views.Keys.ToArray())
[2548]295        CloseView(view, closeReason);
[2544]296    }
[2443]297    #endregion
[2305]298
[2696]299    #region create menu and toolbar
300    private void CreateGUI() {
301      IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
[2443]302
[2696]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;
[2443]309
[2696]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      }
[2443]316
[2696]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;
[2443]323
[2696]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
[2761]331      this.AdditionalCreationOfGuiElements();
[2443]332    }
[2233]333
[2761]334    protected virtual void AdditionalCreationOfGuiElements() {
[2243]335    }
336
[2514]337    private void AddToolStripMenuItem(IMenuItem menuItem) {
[2696]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;
[2514]345      }
[2696]346      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
[2541]347    }
[2243]348
[2696]349    private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
350      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
351    }
352
[2514]353    private void AddToolStripButtonItem(IToolBarItem buttonItem) {
[2696]354      ToolStripItem item = new ToolStripButton();
355      if (buttonItem is ToolBarItem) {
356        ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
357        if (buttonItemBase.IsDropDownButton)
[2514]358          item = new ToolStripDropDownButton();
359
[2696]360        item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
361        buttonItemBase.ToolStripItem = item;
362      }
[2541]363
[2696]364      this.SetToolStripItemProperties(item, buttonItem);
[2426]365      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
[2243]366    }
367
[2696]368    private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
369      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
370    }
371
[2443]372    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
[2249]373      ToolStripDropDownItem parent = null;
[2426]374      foreach (string s in structure) {
375        if (parentItems.ContainsKey(s))
376          parent = (ToolStripDropDownItem)parentItems[s];
377        else {
[2443]378          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
[2426]379          parentItems.Add(parent);
380        }
[2249]381        parentItems = parent.DropDownItems;
382      }
[2426]383      parentItems.Add(item);
[2249]384    }
385
[2696]386    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
[2426]387      toolStripItem.Name = userInterfaceItem.Name;
388      toolStripItem.Text = userInterfaceItem.Name;
389      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
390      toolStripItem.Tag = userInterfaceItem;
391      toolStripItem.Image = userInterfaceItem.Image;
[2243]392      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
[2426]393      this.userInterfaceItems.Add(userInterfaceItem);
[2243]394    }
395
396    private void ToolStripItemClicked(object sender, EventArgs e) {
397      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
[2696]398      ((IActionUserInterfaceItem)item.Tag).Execute();
[2243]399    }
[2233]400    #endregion
401  }
402}
Note: See TracBrowser for help on using the repository browser.