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, 14 years ago

adapted mainforms to have a ShowInViewHost property (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    }
46
47    protected MainForm(Type userInterfaceItemType)
48      : this() {
49      this.userInterfaceItemType = userInterfaceItemType;
50    }
51
52    #region properties
53    private bool showInViewHost;
54    public bool ShowInViewHost {
55      get { return this.showInViewHost; }
56      set { this.showInViewHost = value; }
57    }
58
59    public string Title {
60      get { return this.Text; }
61      set {
62        if (InvokeRequired) {
63          Action<string> action = delegate(string s) { this.Title = s; };
64          Invoke(action, value);
65        } else
66          this.Text = value;
67      }
68    }
69
70    public override Cursor Cursor {
71      get { return base.Cursor; }
72      set {
73        if (InvokeRequired) {
74          Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
75          Invoke(action, value);
76        } else
77          base.Cursor = value;
78      }
79    }
80
81    private Type userInterfaceItemType;
82    public Type UserInterfaceItemType {
83      get { return this.userInterfaceItemType; }
84    }
85
86    private Dictionary<IView, Form> views;
87    public IEnumerable<IView> Views {
88      get { return views.Keys; }
89    }
90
91    private IView activeView;
92    public IView ActiveView {
93      get { return this.activeView; }
94      protected set {
95        if (this.activeView != value) {
96          if (InvokeRequired) {
97            Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
98            Invoke(action, value);
99          } else {
100            this.activeView = value;
101            OnActiveViewChanged();
102          }
103        }
104      }
105    }
106
107    private List<IUserInterfaceItem> userInterfaceItems;
108    protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
109      get { return this.userInterfaceItems; }
110    }
111    #endregion
112
113    #region events
114    public event EventHandler ActiveViewChanged;
115    protected virtual void OnActiveViewChanged() {
116      if (InvokeRequired)
117        Invoke((MethodInvoker)OnActiveViewChanged);
118      else if (ActiveViewChanged != null)
119        ActiveViewChanged(this, EventArgs.Empty);
120    }
121
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
146    public event EventHandler Changed;
147    protected void FireMainFormChanged() {
148      if (InvokeRequired)
149        Invoke((MethodInvoker)FireMainFormChanged);
150      else if (Changed != null)
151        Changed(this, EventArgs.Empty);
152    }
153
154    private void MainFormBase_Load(object sender, EventArgs e) {
155      if (!DesignMode) {
156        MainFormManager.RegisterMainForm(this);
157        if (!this.initialized) {
158          this.initialized = true;
159          this.OnInitialized(EventArgs.Empty);
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    }
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    }
196    #endregion
197
198    #region create, get, show, hide, close views
199    protected virtual Form CreateForm(IView view) {
200      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
201    }
202
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
213    internal void ShowView(IView view, bool firstTimeShown) {
214      if (InvokeRequired) Invoke((Action<IView, bool>)ShowView, view, firstTimeShown);
215      else {
216        if (firstTimeShown) {
217          Form form = CreateForm(view);
218          view.Changed += new EventHandler(ViewChanged);
219          this.views[view] = form;
220          form.Activated += new EventHandler(FormActivated);
221          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
222
223        }
224        this.Show(view, firstTimeShown);
225        this.OnViewShown(view, firstTimeShown);
226      }
227    }
228
229    private void ViewChanged(object sender, EventArgs e) {
230      IView view = (IView)sender;
231      if (view == this.ActiveView)
232        this.OnActiveViewChanged();
233    }
234
235    protected virtual void Show(IView view, bool firstTimeShown) {
236    }
237
238    internal void HideView(IView view) {
239      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
240      else {
241        if (this.views.ContainsKey(view)) {
242          this.Hide(view);
243          if (this.activeView == view)
244            this.ActiveView = null;
245          this.OnViewHidden(view);
246        }
247      }
248    }
249
250    protected virtual void Hide(IView view) {
251    }
252
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
267    internal void CloseView(IView view) {
268      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
269      else {
270        if (this.views.ContainsKey(view)) {
271          this.views[view].Close();
272          this.OnViewClosed(view);
273        }
274      }
275    }
276
277    internal void CloseView(IView view, CloseReason closeReason) {
278      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
279      else {
280        if (this.views.ContainsKey(view)) {
281          ((View)view).CloseReason = closeReason;
282          this.CloseView(view);
283        }
284      }
285    }
286
287    public void CloseAllViews() {
288      foreach (IView view in views.Keys.ToArray())
289        CloseView(view);
290    }
291
292    public void CloseAllViews(CloseReason closeReason) {
293      foreach (IView view in views.Keys.ToArray())
294        CloseView(view, closeReason);
295    }
296    #endregion
297
298    #region create menu and toolbar
299    private void CreateGUI() {
300      IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
301
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;
308
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      }
315
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;
322
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
330      this.AdditionalCreationOfGuiElements();
331    }
332
333    protected virtual void AdditionalCreationOfGuiElements() {
334    }
335
336    private void AddToolStripMenuItem(IMenuItem menuItem) {
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;
344      }
345      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
346    }
347
348    private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
349      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
350    }
351
352    private void AddToolStripButtonItem(IToolBarItem buttonItem) {
353      ToolStripItem item = new ToolStripButton();
354      if (buttonItem is ToolBarItem) {
355        ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
356        if (buttonItemBase.IsDropDownButton)
357          item = new ToolStripDropDownButton();
358
359        item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
360        buttonItemBase.ToolStripItem = item;
361      }
362
363      this.SetToolStripItemProperties(item, buttonItem);
364      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
365    }
366
367    private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
368      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
369    }
370
371    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
372      ToolStripDropDownItem parent = null;
373      foreach (string s in structure) {
374        if (parentItems.ContainsKey(s))
375          parent = (ToolStripDropDownItem)parentItems[s];
376        else {
377          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
378          parentItems.Add(parent);
379        }
380        parentItems = parent.DropDownItems;
381      }
382      parentItems.Add(item);
383    }
384
385    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
386      toolStripItem.Name = userInterfaceItem.Name;
387      toolStripItem.Text = userInterfaceItem.Name;
388      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
389      toolStripItem.Tag = userInterfaceItem;
390      toolStripItem.Image = userInterfaceItem.Image;
391      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
392      this.userInterfaceItems.Add(userInterfaceItem);
393    }
394
395    private void ToolStripItemClicked(object sender, EventArgs e) {
396      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
397      ((IActionUserInterfaceItem)item.Tag).Execute();
398    }
399    #endregion
400  }
401}
Note: See TracBrowser for help on using the repository browser.