Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/MainForm.cs @ 3580

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

changed logic of showing new views (ticket #972)

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