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

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

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

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.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    protected void AddViewFormCombination(IView view, Form form) {
92      this.views.Add(view, form);
93      view.Changed += new EventHandler(ViewChanged);
94    }
95
96    private IView activeView;
97    public IView ActiveView {
98      get { return this.activeView; }
99      protected set {
100        if (this.activeView != value) {
101          if (InvokeRequired) {
102            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
103            Invoke(action, value);
104          } else {
105            this.activeView = value;
106            OnActiveViewChanged();
107          }
108        }
109      }
110    }
111
112    private List<IUserInterfaceItem> userInterfaceItems;
113    protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
114      get { return this.userInterfaceItems; }
115    }
116    #endregion
117
118    #region events
119    public event EventHandler ActiveViewChanged;
120    protected virtual void OnActiveViewChanged() {
121      if (InvokeRequired)
122        Invoke((MethodInvoker)OnActiveViewChanged);
123      else if (ActiveViewChanged != null)
124        ActiveViewChanged(this, EventArgs.Empty);
125    }
126
127    public event EventHandler<ViewEventArgs> ViewClosed;
128    protected virtual void OnViewClosed(IView view) {
129      if (InvokeRequired) Invoke((Action<IView>)OnViewClosed, view);
130      else {
131        EventHandler<ViewEventArgs> handler = ViewClosed;
132        if (handler != null)
133          handler(this, new ViewEventArgs(view));
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);
140      else {
141        EventHandler<ViewShownEventArgs> handler = ViewShown;
142        if (handler != null)
143          handler(this, new ViewShownEventArgs(view, firstTimeShown));
144      }
145    }
146
147    public event EventHandler<ViewEventArgs> ViewHidden;
148    protected virtual void OnViewHidden(IView view) {
149      if (InvokeRequired) Invoke((Action<IView>)OnViewHidden, view);
150      else {
151        EventHandler<ViewEventArgs> handler = ViewHidden;
152        if (handler != null)
153          handler(this, new ViewEventArgs(view));
154      }
155    }
156
157    public event EventHandler Changed;
158    protected void FireMainFormChanged() {
159      if (InvokeRequired)
160        Invoke((MethodInvoker)FireMainFormChanged);
161      else {
162        EventHandler handler = Changed;
163        if (handler != null)
164          Changed(this, EventArgs.Empty);
165      }
166    }
167
168    private void MainFormBase_Load(object sender, EventArgs e) {
169      if (!DesignMode) {
170        MainFormManager.RegisterMainForm(this);
171        if (!this.initialized) {
172          this.initialized = true;
173          this.OnInitialized(EventArgs.Empty);
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    }
185
186    protected override void OnFormClosing(FormClosingEventArgs e) {
187      foreach (KeyValuePair<IView, Form> pair in this.views) {
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    }
210    #endregion
211
212    #region create, get, show, hide, close views
213    protected virtual Form CreateForm(IView view) {
214      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
215    }
216
217    internal Form GetForm(IView view) {
218      IView internalView = GetView(view);
219      if (internalView != null && views.ContainsKey(internalView))
220        return views[internalView];
221      return null;
222    }
223    protected IView GetView(Form form) {
224      return views.Where(x => x.Value == form).Single().Key;
225    }
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    }
235
236
237    internal void ShowView(IView view) {
238      if (InvokeRequired) Invoke((Action<IView>)ShowView, view);
239      else {
240        Form form = GetForm(view);
241        bool firstTimeShown = form == null;
242        if (form == null) {
243          form = CreateForm(view);
244          form.Activated += new EventHandler(FormActivated);
245          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
246        }
247        IView internalView = GetView(form);
248        this.ShowView(internalView, firstTimeShown);
249        this.OnViewShown(internalView, firstTimeShown);
250      }
251    }
252
253    private void ViewChanged(object sender, EventArgs e) {
254      IView view = (IView)sender;
255      if (view == this.ActiveView)
256        this.OnActiveViewChanged();
257    }
258
259    protected virtual void ShowView(IView view, bool firstTimeShown) {
260    }
261
262    internal void HideView(IView view) {
263      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
264      else {
265        IView internalView = this.GetView(view);
266        if (internalView != null && this.views.ContainsKey(internalView)) {
267          this.Hide(internalView);
268          if (this.activeView == internalView)
269            this.ActiveView = null;
270          this.OnViewHidden(internalView);
271        }
272      }
273    }
274
275    protected virtual void Hide(IView view) {
276    }
277
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
292    internal void CloseView(IView view) {
293      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
294      else {
295        IView internalView = GetView(view);
296        if (internalView != null && this.views.ContainsKey(internalView)) {
297          this.views[internalView].Close();
298          this.OnViewClosed(internalView);
299        }
300      }
301    }
302
303    internal void CloseView(IView view, CloseReason closeReason) {
304      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
305      else {
306        IView internalView = GetView(view);
307        if (internalView != null && this.views.ContainsKey(internalView)) {
308          ((View)internalView).CloseReason = closeReason;
309          this.CloseView(internalView);
310        }
311      }
312    }
313
314    public void CloseAllViews() {
315      foreach (IView view in views.Keys.ToArray())
316        CloseView(view);
317    }
318
319    public void CloseAllViews(CloseReason closeReason) {
320      foreach (IView view in views.Keys.ToArray())
321        CloseView(view, closeReason);
322    }
323    #endregion
324
325    #region create menu and toolbar
326    private void CreateGUI() {
327      IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
328
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;
335
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      }
342
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;
349
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
357      this.AdditionalCreationOfGuiElements();
358    }
359
360    protected virtual void AdditionalCreationOfGuiElements() {
361    }
362
363    private void AddToolStripMenuItem(IMenuItem menuItem) {
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;
371      }
372      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
373    }
374
375    private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
376      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
377    }
378
379    private void AddToolStripButtonItem(IToolBarItem buttonItem) {
380      ToolStripItem item = new ToolStripButton();
381      if (buttonItem is ToolBarItem) {
382        ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
383        if (buttonItemBase.IsDropDownButton)
384          item = new ToolStripDropDownButton();
385
386        item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
387        buttonItemBase.ToolStripItem = item;
388      }
389
390      this.SetToolStripItemProperties(item, buttonItem);
391      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
392    }
393
394    private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
395      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
396    }
397
398    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
399      ToolStripDropDownItem parent = null;
400      foreach (string s in structure) {
401        if (parentItems.ContainsKey(s))
402          parent = (ToolStripDropDownItem)parentItems[s];
403        else {
404          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
405          parentItems.Add(parent);
406        }
407        parentItems = parent.DropDownItems;
408      }
409      parentItems.Add(item);
410    }
411
412    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
413      toolStripItem.Name = userInterfaceItem.Name;
414      toolStripItem.Text = userInterfaceItem.Name;
415      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
416      toolStripItem.Tag = userInterfaceItem;
417      toolStripItem.Image = userInterfaceItem.Image;
418      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
419      this.userInterfaceItems.Add(userInterfaceItem);
420    }
421
422    private void ToolStripItemClicked(object sender, EventArgs e) {
423      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
424      ((IActionUserInterfaceItem)item.Tag).Execute();
425    }
426    #endregion
427  }
428}
Note: See TracBrowser for help on using the repository browser.