Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3403


Ignore:
Timestamp:
04/19/10 00:09:19 (15 years ago)
Author:
mkommend
Message:

enhanced viewHost (forwarding of view events, activeView property,...) (ticket #972)

Location:
trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/DockingMainForm.cs

    r3398 r3403  
    4545    }
    4646
    47     protected override void Show(IView view, bool firstTimeShown) {
    48       if (InvokeRequired) Invoke((Action<IView, bool>)Show, view, firstTimeShown);
     47    protected override void ShowView(IView view, bool firstTimeShown) {
     48      if (InvokeRequired) Invoke((Action<IView, bool>)ShowView, view, firstTimeShown);
    4949      else {
    50         base.Show(view, firstTimeShown);
     50        base.ShowView(view, firstTimeShown);
    5151        if (firstTimeShown)
    5252          ((DockForm)GetForm(view)).Show(dockPanel);
     
    5959      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
    6060      else {
    61         Form form = base.GetForm(view);
     61        Form form = this.GetForm(view);
    6262        if (form != null) {
    6363          ((DockForm)form).Hide();
     
    7272        ViewHost viewHost = new ViewHost(contentView);
    7373        form = new DockForm(viewHost);
    74       } else
     74        this.AddViewFormCombination(viewHost, form);
     75      } else {
    7576        form = new DockForm(view);
     77        this.AddViewFormCombination(view, form);
     78      }
    7679      return form;
    7780    }
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/MainForm.cs

    r3395 r3403  
    8989      get { return views.Keys; }
    9090    }
     91    protected void AddViewFormCombination(IView view, Form form) {
     92      this.views.Add(view, form);
     93      view.Changed += new EventHandler(ViewChanged);
     94    }
    9195
    9296    private IView activeView;
     
    124128    protected virtual void OnViewClosed(IView view) {
    125129      if (InvokeRequired) Invoke((Action<IView>)OnViewClosed, view);
    126       else if (this.ViewClosed != null) {
    127         this.ViewClosed(this, new ViewEventArgs(view));
     130      else {
     131        EventHandler<ViewEventArgs> handler = ViewClosed;
     132        if (handler != null)
     133          handler(this, new ViewEventArgs(view));
    128134      }
    129135    }
     
    132138    protected virtual void OnViewShown(IView view, bool firstTimeShown) {
    133139      if (InvokeRequired) Invoke((Action<IView, bool>)OnViewShown, view, firstTimeShown);
    134       else if (this.ViewShown != null) {
    135         this.ViewShown(this, new ViewShownEventArgs(view, firstTimeShown));
     140      else {
     141        EventHandler<ViewShownEventArgs> handler = ViewShown;
     142        if (handler != null)
     143          handler(this, new ViewShownEventArgs(view, firstTimeShown));
    136144      }
    137145    }
     
    140148    protected virtual void OnViewHidden(IView view) {
    141149      if (InvokeRequired) Invoke((Action<IView>)OnViewHidden, view);
    142       else if (this.ViewHidden != null) {
    143         this.ViewHidden(this, new ViewEventArgs(view));
     150      else {
     151        EventHandler<ViewEventArgs> handler = ViewHidden;
     152        if (handler != null)
     153          handler(this, new ViewEventArgs(view));
    144154      }
    145155    }
     
    149159      if (InvokeRequired)
    150160        Invoke((MethodInvoker)FireMainFormChanged);
    151       else if (Changed != null)
    152         Changed(this, EventArgs.Empty);
     161      else {
     162        EventHandler handler = Changed;
     163        if (handler != null)
     164          Changed(this, EventArgs.Empty);
     165      }
    153166    }
    154167
     
    172185
    173186    protected override void OnFormClosing(FormClosingEventArgs e) {
    174       foreach (KeyValuePair<IView,Form > pair in this.views) {
     187      foreach (KeyValuePair<IView, Form> pair in this.views) {
    175188        DockForm dockForm = pair.Value as DockForm;
    176189        View view = pair.Key as View;
     
    203216
    204217    internal Form GetForm(IView view) {
    205       if (views.ContainsKey(view))
    206         return views[view];
     218      IView internalView = GetView(view);
     219      if (internalView != null && views.ContainsKey(internalView))
     220        return views[internalView];
    207221      return null;
    208222    }
    209 
    210     internal IView GetView(Form form) {
     223    protected IView GetView(Form form) {
    211224      return views.Where(x => x.Value == form).Single().Key;
    212225    }
    213 
    214     internal void ShowView(IView view, bool firstTimeShown) {
    215       if (InvokeRequired) Invoke((Action<IView, bool>)ShowView, view, firstTimeShown);
    216       else {
    217         if (firstTimeShown) {
    218           Form form = CreateForm(view);
    219           view.Changed += new EventHandler(ViewChanged);
    220           this.views[view] = form;
     226    private IView GetView(IView view) {
     227      if (view == null || views.ContainsKey(view))
     228        return view;
     229      IView viewHost =
     230        (from ViewHost v in views.Keys.OfType<ViewHost>()
     231         where v.Views.Contains(((IContentView)view))
     232         select v).SingleOrDefault();
     233      return viewHost;
     234    }
     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 (form == null) {
     243          form = CreateForm(view);
    221244          form.Activated += new EventHandler(FormActivated);
    222245          form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
    223 
    224         }
    225         this.Show(view, firstTimeShown);
    226         this.OnViewShown(view, firstTimeShown);
     246        }
     247        IView internalView = GetView(form);
     248        this.ShowView(internalView, firstTimeShown);
     249        this.OnViewShown(internalView, firstTimeShown);
    227250      }
    228251    }
     
    234257    }
    235258
    236     protected virtual void Show(IView view, bool firstTimeShown) {
     259    protected virtual void ShowView(IView view, bool firstTimeShown) {
    237260    }
    238261
     
    240263      if (InvokeRequired) Invoke((Action<IView>)HideView, view);
    241264      else {
    242         if (this.views.ContainsKey(view)) {
    243           this.Hide(view);
    244           if (this.activeView == view)
     265        IView internalView = this.GetView(view);
     266        if (internalView != null && this.views.ContainsKey(internalView)) {
     267          this.Hide(internalView);
     268          if (this.activeView == internalView)
    245269            this.ActiveView = null;
    246           this.OnViewHidden(view);
     270          this.OnViewHidden(internalView);
    247271        }
    248272      }
     
    269293      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
    270294      else {
    271         if (this.views.ContainsKey(view)) {
    272           this.views[view].Close();
    273           this.OnViewClosed(view);
     295        IView internalView = GetView(view);
     296        if (internalView != null && this.views.ContainsKey(internalView)) {
     297          this.views[internalView].Close();
     298          this.OnViewClosed(internalView);
    274299        }
    275300      }
     
    279304      if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
    280305      else {
    281         if (this.views.ContainsKey(view)) {
    282           ((View)view).CloseReason = closeReason;
    283           this.CloseView(view);
     306        IView internalView = GetView(view);
     307        if (internalView != null && this.views.ContainsKey(internalView)) {
     308          ((View)internalView).CloseReason = closeReason;
     309          this.CloseView(internalView);
    284310        }
    285311      }
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/MultipleDocumentMainForm.cs

    r3398 r3403  
    5151    }
    5252
    53     protected override void Show(IView view, bool firstTimeShown) {
    54       if (InvokeRequired) Invoke((Action<IView, bool>)Show, view, firstTimeShown);
     53    protected override void ShowView(IView view, bool firstTimeShown) {
     54      if (InvokeRequired) Invoke((Action<IView, bool>)ShowView, view, firstTimeShown);
    5555      else {
    56         base.Show(view, firstTimeShown);
     56        base.ShowView(view, firstTimeShown);
    5757        if (firstTimeShown)
    5858          this.GetForm(view).Show();
     
    6868      else {
    6969        base.Hide(view);
    70         this.GetForm(view).Hide();
     70        Form form = this.GetForm(view);
     71        if (form != null) {
     72          form.Hide();
     73        }
    7174      }
    7275    }
     
    7881        ViewHost viewHost = new ViewHost(contentView);
    7982        form = new DocumentForm(viewHost);
    80       } else
     83        this.AddViewFormCombination(viewHost, form);
     84      } else {
    8185        form = new DocumentForm(view);
     86        this.AddViewFormCombination(view, form);
     87      }
    8288
    8389      form.MdiParent = this;
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/SingleDocumentMainForm.cs

    r3398 r3403  
    4545    }
    4646
    47     protected override void Show(IView view, bool firstTimeShown) {
    48       if (InvokeRequired) Invoke((Action<IView, bool>)Show, view, firstTimeShown);
     47    protected override void ShowView(IView view, bool firstTimeShown) {
     48      if (InvokeRequired) Invoke((Action<IView, bool>)ShowView, view, firstTimeShown);
    4949      else {
    50         base.Show(view, firstTimeShown);
     50        base.ShowView(view, firstTimeShown);
    5151        if (firstTimeShown)
    5252          this.GetForm(view).Show(this);
     
    6262      else {
    6363        base.Hide(view);
    64         this.GetForm(view).Hide();
     64        Form form = this.GetForm(view);
     65        if (form != null) {
     66          form.Hide();
     67        }
    6568      }
    6669    }
     
    7275        ViewHost viewHost = new ViewHost(contentView);
    7376        form = new DocumentForm(viewHost);
    74       } else
     77        this.AddViewFormCombination(viewHost, form);
     78      } else {
    7579        form = new DocumentForm(view);
     80        this.AddViewFormCombination(view, form);
     81      }
    7682
    7783      form.ShowInTaskbar = true;
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/View.cs

    r3398 r3403  
    8282
    8383      this.IsShown = true;
    84       mainform.ShowView(this, firstTimeShown);
     84      mainform.ShowView(this);
    8585      if (firstTimeShown) {
    8686        Form form = mainform.GetForm(this);
     
    146146    }
    147147
    148     protected virtual void OnShown(ViewShownEventArgs e) {
    149     }
    150 
    151     protected virtual void OnHidden(EventArgs e) {
     148    internal protected virtual void OnShown(ViewShownEventArgs e) {
     149    }
     150
     151    internal protected virtual void OnHidden(EventArgs e) {
    152152    }
    153153
     
    170170    }
    171171
    172     protected virtual void OnClosing(FormClosingEventArgs e) {
     172    internal protected virtual void OnClosing(FormClosingEventArgs e) {
    173173    }
    174174
     
    185185    }
    186186
    187     protected virtual void OnClosed(FormClosedEventArgs e) {
     187    internal protected virtual void OnClosed(FormClosedEventArgs e) {
    188188    }
    189189
     
    215215    public void ResumeRepaint(bool refresh) {
    216216      if (InvokeRequired)
    217         Invoke((Action<bool>)ResumeRepaint,refresh);
    218       else
    219       ((Control)this).ResumeRepaint(refresh);
     217        Invoke((Action<bool>)ResumeRepaint, refresh);
     218      else
     219        ((Control)this).ResumeRepaint(refresh);
    220220    }
    221221  }
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewHost.Designer.cs

    r3392 r3403  
    8484      this.viewsLabel.TabIndex = 0;
    8585      this.toolTip.SetToolTip(this.viewsLabel, "Double-click to open a new window of the current view.\r\nRight-click to change cur" +
    86               "rent view.");
     86              "rent view. \r\n Drag icon to copy or link content in another view.");
    8787      this.viewsLabel.MouseLeave += new System.EventHandler(this.viewsLabel_MouseLeave);
    8888      this.viewsLabel.DoubleClick += new System.EventHandler(this.viewsLabel_DoubleClick);
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewHost.cs

    r3398 r3403  
    2929  [Content(typeof(object))]
    3030  public sealed partial class ViewHost : AsynchronousContentView {
    31     private Dictionary<Type, IContentView> cachedViews;
    3231    public ViewHost() {
    3332      InitializeComponent();
     
    3736      startDragAndDrop = false;
    3837      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
     38      activeView = null;
    3939    }
    4040    public ViewHost(object content)
     
    4343    }
    4444
    45     public ViewHost(IContentView contentView) :this(){
     45    public ViewHost(IContentView contentView)
     46      : this() {
    4647      this.viewType = contentView.GetType();
    4748      this.Content = contentView.Content;
    4849      this.cachedViews.Add(contentView.GetType(), contentView);
    49       this.UpdateView();
    50     }
    51 
     50      this.activeView = contentView;
     51      this.RegisterActiveViewEvents();
     52      this.OnViewTypeChanged();
     53      this.ActiveViewChanged();
     54    }
     55
     56    private Dictionary<Type, IContentView> cachedViews;
     57    public IEnumerable<IContentView> Views {
     58      get { return cachedViews.Values; }
     59    }
     60
     61    private IContentView activeView;
     62    public IContentView ActiveView {
     63      get { return this.activeView; }
     64      private set {
     65        if (activeView != value) {
     66          if (activeView != null) {
     67            DeregisterActiveViewEvents();
     68            View view = activeView as View;
     69            if (view != null)
     70              view.OnHidden(EventArgs.Empty);
     71          }
     72          activeView = value;
     73          if (activeView != null) {
     74            RegisterActiveViewEvents();
     75            View view = activeView as View;
     76            if (view != null)
     77              view.OnShown(new ViewShownEventArgs(view,false));
     78          }
     79          ActiveViewChanged();
     80          OnViewTypeChanged();
     81        }
     82      }
     83    }
    5284    private Type viewType;
    5385    public Type ViewType {
     
    5991                                                      value, Content.GetType()));
    6092          viewType = value;
    61           UpdateView();
    62         }
    63       }
    64     }
    65 
     93          OnViewTypeChanged();
     94        }
     95      }
     96    }
    6697    public new object Content {
    6798      get { return base.Content; }
     
    107138            ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
    108139        }
     140
    109141        foreach (IContentView view in cachedViews.Values)
    110142          view.Content = this.Content;
     
    117149
    118150
    119     private void UpdateView() {
     151    private void OnViewTypeChanged() {
    120152      viewPanel.Controls.Clear();
    121 
    122153      if (viewType == null || Content == null)
    123154        return;
    124 
    125155      if (!ViewCanShowContent(viewType, Content))
    126156        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
     
    135165        cachedViews.Add(viewType, view);
    136166      }
    137       this.Caption = view.Caption;
    138       this.SaveEnabled = view.SaveEnabled;
     167      this.ActiveView = view;
    139168
    140169      Control control = (Control)view;
     
    144173    }
    145174
     175
     176
     177    private void RegisterActiveViewEvents() {
     178      activeView.Changed += new EventHandler(activeView_Changed);
     179      activeView.CaptionChanged += new EventHandler(activeView_CaptionChanged);
     180    }
     181    private void DeregisterActiveViewEvents() {
     182      activeView.Changed -= new EventHandler(activeView_Changed);
     183      activeView.CaptionChanged -= new EventHandler(activeView_CaptionChanged);
     184    }
     185    private void activeView_CaptionChanged(object sender, EventArgs e) {
     186      this.ActiveViewChanged();
     187    }
     188    private void activeView_Changed(object sender, EventArgs e) {
     189      this.ActiveViewChanged();
     190    }
     191    private void ActiveViewChanged() {
     192      if (ActiveView != null) {
     193        this.Caption = this.ActiveView.Caption;
     194        this.SaveEnabled = this.ActiveView.SaveEnabled;
     195      }
     196    }
     197
     198    #region forwarding of view events
     199    internal protected override void OnShown(ViewShownEventArgs e) {
     200      base.OnShown(e);
     201      View view = this.ActiveView as View;
     202      if (view != null)
     203        view.OnShown(e);
     204    }
     205
     206    internal protected override void OnHidden(EventArgs e) {
     207      base.OnHidden(e);
     208      View view = this.ActiveView as View;
     209      if (view != null)
     210        view.OnHidden(e);
     211    }
     212
     213    internal protected override void OnClosing(FormClosingEventArgs e) {
     214      base.OnClosing(e);
     215      foreach (View view in this.Views.OfType<View>())
     216        view.OnClosing(e);
     217    }
     218
     219    internal protected override void OnClosed(FormClosedEventArgs e) {
     220      base.OnClosed(e);
     221      foreach (View view in this.Views.OfType<View>())
     222        view.OnClosed(e);
     223    }
     224    #endregion
     225
     226    #region GUI actions
    146227    private void UpdateActiveMenuItem() {
    147228      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
     
    167248      MainFormManager.CreateView(viewType, Content, ReadOnly).Show();
    168249    }
    169 
    170250    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
    171251      Type viewType = (Type)e.ClickedItem.Tag;
     
    178258      viewsLabel.Capture = false;
    179259    }
    180 
    181260    private void viewsLabel_MouseLeave(object sender, EventArgs e) {
    182261      if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left && startDragAndDrop) {
     
    188267        startDragAndDrop = false;
    189268    }
     269    #endregion
    190270  }
    191271}
Note: See TracChangeset for help on using the changeset viewer.