Free cookie consent management tool by TermsFeed Policy Generator

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

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

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