Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.MainForm.WindowsForms/3.3/MainForms/MainForm.cs @ 10103

Last change on this file since 10103 was 10103, checked in by jkarder, 10 years ago

#2116:

  • fixed issues within the outermost control detection
  • fixed visible property assignment of the breadcrumb control in ViewHost
File size: 23.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.PluginInfrastructure;
28using WeifenLuo.WinFormsUI.Docking;
29
30namespace HeuristicLab.MainForm.WindowsForms {
31  public partial class MainForm : Form, IMainForm {
32    private bool initialized;
33    private int appStartingCursors;
34    private int waitingCursors;
35
36    protected MainForm()
37      : base() {
38      InitializeComponent();
39      this.views = new Dictionary<IView, Form>();
40      this.userInterfaceItems = new List<IUserInterfaceItem>();
41      this.initialized = false;
42      this.showContentInViewHost = false;
43      appStartingCursors = 0;
44      waitingCursors = 0;
45    }
46
47    protected MainForm(Type userInterfaceItemType)
48      : this() {
49      this.userInterfaceItemType = userInterfaceItemType;
50    }
51
52    #region properties
53    private bool showContentInViewHost;
54    public bool ShowContentInViewHost {
55      get { return this.showContentInViewHost; }
56      set { this.showContentInViewHost = 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 {
119        EventHandler handler = ActiveViewChanged;
120        if (handler != null)
121          handler(this, EventArgs.Empty);
122      }
123    }
124
125    public event EventHandler<ViewEventArgs> ViewClosed;
126    protected virtual void OnViewClosed(IView view) {
127      if (InvokeRequired) Invoke((Action<IView>)OnViewClosed, view);
128      else {
129        EventHandler<ViewEventArgs> handler = ViewClosed;
130        if (handler != null)
131          handler(this, new ViewEventArgs(view));
132      }
133    }
134
135    public event EventHandler<ViewShownEventArgs> ViewShown;
136    protected virtual void OnViewShown(IView view, bool firstTimeShown) {
137      if (InvokeRequired) Invoke((Action<IView, bool>)OnViewShown, view, firstTimeShown);
138      else {
139        EventHandler<ViewShownEventArgs> handler = ViewShown;
140        if (handler != null)
141          handler(this, new ViewShownEventArgs(view, firstTimeShown));
142      }
143    }
144
145    public event EventHandler<ViewEventArgs> ViewHidden;
146    protected virtual void OnViewHidden(IView view) {
147      if (InvokeRequired) Invoke((Action<IView>)OnViewHidden, view);
148      else {
149        EventHandler<ViewEventArgs> handler = ViewHidden;
150        if (handler != null)
151          handler(this, new ViewEventArgs(view));
152      }
153    }
154
155    public event EventHandler Changed;
156    protected void OnChanged() {
157      if (InvokeRequired)
158        Invoke((MethodInvoker)OnChanged);
159      else {
160        EventHandler handler = Changed;
161        if (handler != null)
162          Changed(this, EventArgs.Empty);
163      }
164    }
165
166    private void MainFormBase_Load(object sender, EventArgs e) {
167      if (!DesignMode) {
168        MainFormManager.RegisterMainForm(this);
169        this.CreateGUI();
170        if (!this.initialized) {
171          this.initialized = true;
172          this.OnInitialized(EventArgs.Empty);
173        }
174      }
175    }
176
177    protected virtual void OnInitialized(EventArgs e) { }
178
179    public virtual void UpdateTitle() { }
180
181    private void FormActivated(object sender, EventArgs e) {
182      this.ActiveView = GetView((Form)sender);
183    }
184
185    protected override void OnFormClosing(FormClosingEventArgs e) {
186      foreach (KeyValuePair<IView, Form> pair in this.views) {
187        DockForm dockForm = pair.Value as DockForm;
188        View view = pair.Key as View;
189        if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
190          view.CloseReason = CloseReason.ApplicationExitCall;
191          view.OnClosingHelper(dockForm, e);
192        }
193      }
194      base.OnFormClosing(e);
195    }
196
197    protected override void OnFormClosed(FormClosedEventArgs e) {
198      foreach (KeyValuePair<IView, Form> pair in this.views.ToList()) {
199        DockForm dockForm = pair.Value as DockForm;
200        View view = pair.Key as View;
201        if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
202          view.CloseReason = CloseReason.ApplicationExitCall;
203          view.OnClosedHelper(dockForm, e);
204          dockForm.Close();
205        }
206      }
207      base.OnFormClosed(e);
208    }
209    #endregion
210
211    #region create, get, show, hide, close views
212    protected virtual Form CreateForm(IView view) {
213      throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
214    }
215
216    internal Form GetForm(IView view) {
217      if (views.ContainsKey(view))
218        return views[view];
219      return null;
220    }
221    protected IView GetView(Form form) {
222      return views.Where(x => x.Value == form).Single().Key;
223    }
224
225    public T GetOutermostControlOfType<T>(Control childControl) where T : Control {
226      if (childControl == null) throw new ArgumentNullException("childControl", "ChildControl cannot be null.");
227
228      T outermostControl = null;
229      for (var control = childControl; control != null; control = control.Parent) {
230        var c = control as T;
231        if (c != null) outermostControl = c;
232      }
233      return outermostControl;
234    }
235
236    public IContentView ShowContent(IContent content) {
237      if (content == null) throw new ArgumentNullException("Content cannot be null.");
238      Type viewType = MainFormManager.GetDefaultViewType(content.GetType());
239      if (viewType != null) return ShowContent(content, viewType);
240      return null;
241    }
242
243    public IContentView ShowContent<T>(T content, bool reuseExistingView, IEqualityComparer<T> comparer = null) where T : class, IContent {
244      if (content == null) throw new ArgumentNullException("Content cannot be null.");
245      if (!reuseExistingView) return ShowContent(content);
246
247      IContentView view = null;
248      if (comparer == null) view = Views.OfType<IContentView>().Where(v => (v.Content as T) == content).FirstOrDefault();
249      else view = Views.OfType<IContentView>().Where(v => comparer.Equals((v.Content as T), content)).FirstOrDefault();
250
251      if (view == null) view = ShowContent(content);
252      else view.Show();
253
254      return view;
255    }
256
257    public IContentView ShowContent(IContent content, Type viewType) {
258      if (InvokeRequired) return (IContentView)Invoke((Func<IContent, Type, IContentView>)ShowContent, content, viewType);
259      else {
260        if (content == null) throw new ArgumentNullException("Content cannot be null.");
261        if (viewType == null) throw new ArgumentNullException("ViewType cannot be null.");
262
263        IContentView view = null;
264        if (ShowContentInViewHost) {
265          ViewHost viewHost = new ViewHost();
266          viewHost.ViewType = viewType;
267          view = viewHost;
268
269        } else view = MainFormManager.CreateView(viewType);
270
271        view.Content = content;
272        view.Show();
273        return view;
274      }
275    }
276
277    public ViewHost ShowContentInOutermostViewHost(IContent content, Control childControl, bool requireHotlinking = false) {
278      if (content == null) throw new ArgumentNullException("content", "Content cannot be null.");
279      if (childControl == null) throw new ArgumentNullException("childControl", "ChildControl cannot be null.");
280
281      var outermostViewHost = GetOutermostControlOfType<ViewHost>(childControl);
282      if (outermostViewHost == null) return null;
283
284      if (!requireHotlinking || outermostViewHost.HotlinkingEnabled) {
285        var oldCrumbs = outermostViewHost.Breadcrumbs;
286        var newCrumbs = new LinkedList<IContent>();
287        for (var control = childControl; control != null; control = control.Parent) {
288          var vh = control as ViewHost;
289          if (vh != null && vh.Content != null)
290            newCrumbs.AddFirst(vh.Content);
291        }
292        bool showBreadcrumbs = outermostViewHost.ShowBreadcrumbs;
293        outermostViewHost.Content = null;
294        var viewType = MainFormManager.GetDefaultViewType(content.GetType());
295        outermostViewHost.ViewType = viewType;
296        outermostViewHost.Content = content;
297        outermostViewHost.UpdateBreadcrumbTrail(oldCrumbs, newCrumbs.Concat(new[] { content }));
298        outermostViewHost.ShowBreadcrumbs = showBreadcrumbs;
299      }
300      return outermostViewHost;
301    }
302
303    internal void ShowView(IView view) {
304      if (InvokeRequired) Invoke((Action<IView>)ShowView, view);
305      else {
306        Form form = GetForm(view);
307        bool firstTimeShown = form == null;
308        if (firstTimeShown) {
309          form = CreateForm(view);
310          form.Activated += new EventHandler(FormActivated);
311          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
312          view.Changed += new EventHandler(View_Changed);
313          views[view] = form;
314        }
315        this.ShowView(view, firstTimeShown);
316        this.OnViewShown(view, firstTimeShown);
317      }
318    }
319
320    private void View_Changed(object sender, EventArgs e) {
321      IView view = (IView)sender;
322      if (view == this.ActiveView)
323        this.OnActiveViewChanged();
324      this.OnChanged();
325    }
326
327    protected virtual void ShowView(IView view, bool firstTimeShown) {
328    }
329
330    internal void HideView(IView view) {
331      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
332      else {
333        this.Hide(view);
334        if (this.activeView == view)
335          this.ActiveView = null;
336        this.OnViewHidden(view);
337      }
338    }
339
340    protected virtual void Hide(IView view) {
341    }
342
343    private void ChildFormClosed(object sender, FormClosedEventArgs e) {
344      Form form = (Form)sender;
345      IView view = GetView(form);
346
347      view.Changed -= new EventHandler(View_Changed);
348      form.Activated -= new EventHandler(FormActivated);
349      form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
350
351      views.Remove(view);
352      this.OnViewClosed(view);
353      if (ActiveView == view)
354        ActiveView = null;
355    }
356
357    internal void CloseView(IView view) {
358      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
359      else if (views.ContainsKey(view)) {
360        this.views[view].Close();
361        this.OnViewClosed(view);
362      }
363    }
364
365    internal void CloseView(IView view, CloseReason closeReason) {
366      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
367      else if (views.ContainsKey(view)) {
368        ((View)view).CloseReason = closeReason;
369        this.CloseView(view);
370      }
371    }
372
373    public void CloseAllViews() {
374      foreach (IView view in views.Keys.ToArray())
375        CloseView(view);
376    }
377
378    public void CloseAllViews(CloseReason closeReason) {
379      foreach (IView view in views.Keys.ToArray())
380        CloseView(view, closeReason);
381    }
382    #endregion
383
384    #region progress views
385    private readonly Dictionary<IContent, IProgress> contentProgressLookup = new Dictionary<IContent, IProgress>();
386    private readonly Dictionary<Control, IProgress> viewProgressLookup = new Dictionary<Control, IProgress>();
387    private readonly List<ProgressView> progressViews = new List<ProgressView>();
388
389    /// <summary>
390    /// Adds a <see cref="ProgressView"/> to the <see cref="ContentView"/>s showing the specified content.
391    /// </summary>
392    public IProgress AddOperationProgressToContent(IContent content, string progressMessage, bool addToObjectGraphObjects = true) {
393      if (InvokeRequired) {
394        IProgress result = (IProgress)Invoke((Func<IContent, string, bool, IProgress>)AddOperationProgressToContent, content, progressMessage, addToObjectGraphObjects);
395        return result;
396      }
397      if (contentProgressLookup.ContainsKey(content))
398        throw new ArgumentException("A progress is already registered for the specified content.", "content");
399
400      var contentViews = views.Keys.OfType<ContentView>();
401      if (!contentViews.Any(v => v.Content == content))
402        throw new ArgumentException("The content is not displayed in a top-level view", "content");
403
404      if (addToObjectGraphObjects) {
405        var containedObjects = content.GetObjectGraphObjects();
406        contentViews = contentViews.Where(v => containedObjects.Contains(v.Content));
407      } else
408        contentViews = contentViews.Where(v => v.Content == content);
409
410      var progress = new Progress(progressMessage, ProgressState.Started);
411      foreach (var contentView in contentViews) {
412        progressViews.Add(new ProgressView(contentView, progress));
413      }
414
415      contentProgressLookup[content] = progress;
416      return progress;
417    }
418
419    /// <summary>
420    /// Adds a <see cref="ProgressView"/> to the specified view.
421    /// </summary>
422    public IProgress AddOperationProgressToView(Control control, string progressMessage) {
423      var progress = new Progress(progressMessage, ProgressState.Started);
424      AddOperationProgressToView(control, progress);
425      return progress;
426    }
427
428    public void AddOperationProgressToView(Control control, IProgress progress) {
429      if (InvokeRequired) {
430        Invoke((Action<Control, IProgress>)AddOperationProgressToView, control, progress);
431        return;
432      }
433      if (control == null) throw new ArgumentNullException("control", "The view must not be null.");
434      if (progress == null) throw new ArgumentNullException("progress", "The progress must not be null.");
435
436      IProgress oldProgress;
437      if (viewProgressLookup.TryGetValue(control, out oldProgress)) {
438        foreach (var progressView in progressViews.Where(v => v.Content == oldProgress).ToList()) {
439          progressView.Dispose();
440          progressViews.Remove(progressView);
441        }
442        viewProgressLookup.Remove(control);
443      }
444
445      progressViews.Add(new ProgressView(control, progress));
446      viewProgressLookup[control] = progress;
447    }
448
449    /// <summary>
450    /// Removes an existing <see cref="ProgressView"/> from the <see cref="ContentView"/>s showing the specified content.
451    /// </summary>
452    public void RemoveOperationProgressFromContent(IContent content, bool finishProgress = true) {
453      if (InvokeRequired) {
454        Invoke((Action<IContent, bool>)RemoveOperationProgressFromContent, content, finishProgress);
455        return;
456      }
457
458      IProgress progress;
459      if (!contentProgressLookup.TryGetValue(content, out progress))
460        throw new ArgumentException("No progress is registered for the specified content.", "content");
461
462      if (finishProgress) progress.Finish();
463      foreach (var progressView in progressViews.Where(v => v.Content == progress).ToList()) {
464        progressView.Dispose();
465        progressViews.Remove(progressView);
466      }
467      contentProgressLookup.Remove(content);
468
469    }
470
471    /// <summary>
472    /// Removes an existing <see cref="ProgressView"/> from the specified view.
473    /// </summary>
474    public void RemoveOperationProgressFromView(Control control, bool finishProgress = true) {
475      IProgress progress;
476      if (!viewProgressLookup.TryGetValue(control, out progress))
477        throw new ArgumentException("No progress is registered for the specified control.", "control");
478
479      if (finishProgress) progress.Finish();
480      foreach (var progressView in progressViews.Where(v => v.Content == progress).ToList()) {
481        progressView.Dispose();
482        progressViews.Remove(progressView);
483      }
484      viewProgressLookup.Remove(control);
485    }
486    #endregion
487
488    #region create menu and toolbar
489    private void CreateGUI() {
490      IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
491
492      IEnumerable<IPositionableUserInterfaceItem> toolStripMenuItems =
493        from mi in allUserInterfaceItems
494        where (mi is IPositionableUserInterfaceItem) &&
495              (mi is IMenuItem || mi is IMenuSeparatorItem)
496        orderby ((IPositionableUserInterfaceItem)mi).Position
497        select (IPositionableUserInterfaceItem)mi;
498
499      foreach (IPositionableUserInterfaceItem menuItem in toolStripMenuItems) {
500        if (menuItem is IMenuItem)
501          AddToolStripMenuItem((IMenuItem)menuItem);
502        else if (menuItem is IMenuSeparatorItem)
503          AddToolStripMenuItem((IMenuSeparatorItem)menuItem);
504      }
505
506      IEnumerable<IPositionableUserInterfaceItem> toolStripButtonItems =
507        from bi in allUserInterfaceItems
508        where (bi is IPositionableUserInterfaceItem) &&
509              (bi is IToolBarItem || bi is IToolBarSeparatorItem)
510        orderby ((IPositionableUserInterfaceItem)bi).Position
511        select (IPositionableUserInterfaceItem)bi;
512
513      foreach (IPositionableUserInterfaceItem toolStripButtonItem in toolStripButtonItems) {
514        if (toolStripButtonItem is IToolBarItem)
515          AddToolStripButtonItem((IToolBarItem)toolStripButtonItem);
516        else if (toolStripButtonItem is IToolBarSeparatorItem)
517          AddToolStripButtonItem((IToolBarSeparatorItem)toolStripButtonItem);
518      }
519
520      this.AdditionalCreationOfGuiElements();
521    }
522
523    protected virtual void AdditionalCreationOfGuiElements() {
524    }
525
526    private void AddToolStripMenuItem(IMenuItem menuItem) {
527      ToolStripMenuItem item = new ToolStripMenuItem();
528      this.SetToolStripItemProperties(item, menuItem);
529      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
530      if (menuItem is MenuItem) {
531        MenuItem menuItemBase = (MenuItem)menuItem;
532        menuItemBase.ToolStripItem = item;
533        item.ShortcutKeys = menuItemBase.ShortCutKeys;
534        item.DisplayStyle = menuItemBase.ToolStripItemDisplayStyle;
535      }
536    }
537
538    private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
539      this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
540    }
541
542    private void AddToolStripButtonItem(IToolBarItem buttonItem) {
543      ToolStripItem item = new ToolStripButton();
544      if (buttonItem is ToolBarItem) {
545        ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
546        if (buttonItemBase.IsDropDownButton)
547          item = new ToolStripDropDownButton();
548
549        item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
550        buttonItemBase.ToolStripItem = item;
551      }
552
553      this.SetToolStripItemProperties(item, buttonItem);
554      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
555    }
556
557    private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
558      this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
559    }
560
561    private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
562      ToolStripDropDownItem parent = null;
563      foreach (string s in structure) {
564        if (parentItems.ContainsKey(s))
565          parent = (ToolStripDropDownItem)parentItems[s];
566        else {
567          parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
568          parentItems.Add(parent);
569        }
570        parentItems = parent.DropDownItems;
571      }
572      parentItems.Add(item);
573    }
574
575    private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
576      toolStripItem.Name = userInterfaceItem.Name;
577      toolStripItem.Text = userInterfaceItem.Name;
578      toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
579      toolStripItem.Tag = userInterfaceItem;
580      toolStripItem.Image = userInterfaceItem.Image;
581      toolStripItem.Click += new EventHandler(ToolStripItemClicked);
582      this.userInterfaceItems.Add(userInterfaceItem);
583    }
584
585    private void ToolStripItemClicked(object sender, EventArgs e) {
586      System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
587      try {
588        ((IActionUserInterfaceItem)item.Tag).Execute();
589      } catch (Exception ex) {
590        ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, ex);
591      }
592    }
593    #endregion
594
595    #region Cursor Handling
596    public void SetAppStartingCursor() {
597      if (InvokeRequired)
598        Invoke(new Action(SetAppStartingCursor));
599      else {
600        appStartingCursors++;
601        SetCursor();
602      }
603    }
604    public void ResetAppStartingCursor() {
605      if (InvokeRequired)
606        Invoke(new Action(ResetAppStartingCursor));
607      else {
608        appStartingCursors--;
609        SetCursor();
610      }
611    }
612    public void SetWaitCursor() {
613      if (InvokeRequired)
614        Invoke(new Action(SetWaitCursor));
615      else {
616        waitingCursors++;
617        SetCursor();
618      }
619    }
620    public void ResetWaitCursor() {
621      if (InvokeRequired)
622        Invoke(new Action(ResetWaitCursor));
623      else {
624        waitingCursors--;
625        SetCursor();
626      }
627    }
628    private void SetCursor() {
629      if (waitingCursors > 0) Cursor = Cursors.WaitCursor;
630      else if (appStartingCursors > 0) Cursor = Cursors.AppStarting;
631      else Cursor = Cursors.Default;
632    }
633    #endregion
634  }
635}
Note: See TracBrowser for help on using the repository browser.