Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4201 was 4201, checked in by mkommend, 13 years ago

corrected setting of the ViewHost properties in the ShowContent method (ticket #1145)

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