Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/26/10 23:56:25 (14 years ago)
Author:
mkommend
Message:

Added cachedView member and refactored source code (ticket #1211).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/ViewHost.cs

    r4465 r4510  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
    2425using System.Linq;
    2526using System.Windows.Forms;
     
    4142    }
    4243
     44    private IContentView cachedView;
    4345    private IContentView activeView;
    4446    public IContentView ActiveView {
    45       get { return this.activeView; }
     47      get { return activeView; }
    4648      private set {
    4749        if (activeView != value) {
    4850          if (activeView != null) {
     51            cachedView = activeView;
    4952            DeregisterActiveViewEvents();
    50             View view = activeView as View;
    51             if (view != null) {
    52               view.OnHidden(EventArgs.Empty);
    53               Controls.Remove(view);
    54               view.Dispose();
     53            View cached = cachedView as View;
     54            if (cached != null) {
     55              cached.OnHidden(EventArgs.Empty);
     56              cached.Visible = false;
    5557            }
    5658          }
    5759          activeView = value;
    5860          if (activeView != null) {
     61            View cached = cachedView as View;
     62            if (cached != null && activeView != cached) {
     63              Controls.Remove(cached);
     64              cached.Dispose();
     65            }
     66            cachedView = null;
     67
    5968            this.Caption = activeView.Caption;
    6069            viewType = activeView.GetType();
     
    6271            View view = activeView as View;
    6372            if (view != null) {
     73              view.Visible = true;
    6474              view.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
    65               view.Size = new System.Drawing.Size(this.Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
     75              view.Size = new Size(Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
    6676              view.OnShown(new ViewShownEventArgs(view, false));
    67               Controls.Add(view);
     77              if (!Controls.Contains((view))) Controls.Add(view);
    6878            }
    6979          } else viewType = null;
     
    7181      }
    7282    }
    73     private Control ActiveViewControl {
    74       get { return ActiveView as Control; }
    75     }
    7683
    7784    private Type viewType;
    7885    public Type ViewType {
    79       get { return this.viewType; }
     86      get { return viewType; }
    8087      set {
    8188        if (viewType != value) {
     
    8996    }
    9097
     98    protected override void SetEnabledStateOfControls() {
     99      Enabled = Content != null;
     100    }
     101
    91102    protected override void OnContentChanged() {
    92103      viewContextMenuStrip.Item = Content;
     
    95106        if (!ViewCanShowContent(viewType, Content)) {
    96107          Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
    97           if (defaultViewType != null)
     108          if (cachedView != null && cachedView.GetType() == defaultViewType)
     109            ActiveView = cachedView;
     110          else if (defaultViewType != null)
    98111            ViewType = defaultViewType;
    99112          else if (viewContextMenuStrip.Items.Count > 0)  // create first available view if no default view is available
     
    128141          throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
    129142                                                            viewType, Content.GetType()));
    130         IContentView view;
    131         view = MainFormManager.CreateView(viewType);
    132         view.Locked = this.Locked;
    133         view.ReadOnly = this.ReadOnly;
     143        IContentView view = MainFormManager.CreateView(viewType);
     144        view.Locked = Locked;
     145        view.ReadOnly = ReadOnly;
    134146        ActiveView = view; //necessary to allow the views to change the status of the viewhost
    135147        view.Content = Content;
     
    148160    }
    149161    private void activeView_CaptionChanged(object sender, EventArgs e) {
    150       this.Caption = activeView.Caption;
     162      Caption = activeView.Caption;
    151163    }
    152164    private void activeView_LockedChanged(object sender, EventArgs e) {
    153       this.Locked = activeView.Locked;
     165      Locked = activeView.Locked;
    154166    }
    155167
     
    157169      //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
    158170      //not implemented with a panel to reduce the number of nested controls
    159       if (this.Handle != null)
     171      if (Handle != null)
    160172        this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
    161173    }
    162174    private void OnSizeChangedHelper(EventArgs e) {
    163175      base.OnSizeChanged(e);
    164       this.viewsLabel.Location = new System.Drawing.Point(this.Width - this.viewsLabel.Margin.Right - this.viewsLabel.Width, this.viewsLabel.Margin.Top);
     176      viewsLabel.Location = new Point(Width - viewsLabel.Margin.Right - viewsLabel.Width, viewsLabel.Margin.Top);
    165177    }
    166178
     
    168180    internal protected override void OnShown(ViewShownEventArgs e) {
    169181      base.OnShown(e);
    170       View view = this.ActiveView as View;
     182      View view = ActiveView as View;
    171183      if (view != null)
    172184        view.OnShown(e);
     
    174186    internal protected override void OnHidden(EventArgs e) {
    175187      base.OnHidden(e);
    176       View view = this.ActiveView as View;
     188      View view = ActiveView as View;
    177189      if (view != null)
    178190        view.OnHidden(e);
Note: See TracChangeset for help on using the changeset viewer.