Free cookie consent management tool by TermsFeed Policy Generator

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

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

corrected behaviour of the ActiveViewChangedEvent (ticket #897)

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