Free cookie consent management tool by TermsFeed Policy Generator

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

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

enhanced viewHost (forwarding of view events, activeView property,...) (ticket #972)

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