Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3301 was 3301, checked in by mkommend, 14 years ago

implemented fix for ticket #966

File size: 5.3 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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.Windows.Forms;
24
[2426]25namespace HeuristicLab.MainForm.WindowsForms {
[2696]26  public partial class View : UserControl, IView {
[2509]27    private bool initialized;
[2696]28    public View() {
[2233]29      InitializeComponent();
[2509]30      this.initialized = false;
[2711]31      this.isShown = false;
[2543]32      this.closeReason = CloseReason.None;
[2]33    }
[2456]34
[2]35    private string myCaption;
36    public string Caption {
37      get { return myCaption; }
38      set {
[2548]39        if (InvokeRequired) {
40          Action<string> action = delegate(string s) { this.Caption = s; };
41          Invoke(action, value);
42        } else {
43          if (value != myCaption) {
44            myCaption = value;
45            OnCaptionChanged();
46          }
[2]47        }
48      }
49    }
50
[2711]51    private bool isShown;
52    public bool IsShown {
53      get { return this.isShown; }
[2723]54      private set { this.isShown = value; }
[2711]55    }
56
[2704]57    public new void Show() {
[2696]58      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
59      bool firstTimeShown = mainform.GetForm(this) == null;
60
[2723]61      this.IsShown = true;
62      mainform.ShowView(this, firstTimeShown);
[2696]63      if (firstTimeShown) {
64        Form form = mainform.GetForm(this);
65        form.FormClosed += new FormClosedEventHandler(OnClosedHelper);
66        form.FormClosing += new FormClosingEventHandler(OnClosingHelper);
67      }
[2723]68      this.OnShown(new ViewShownEventArgs(this, firstTimeShown));
[2696]69    }
70
71    public void Close() {
72      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
73      Form form = mainform.GetForm(this);
[2723]74      if (form != null) {
75        this.IsShown = false;
[2696]76        mainform.CloseView(this);
[2723]77      }
[2696]78    }
79
80    public void Close(CloseReason closeReason) {
81      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
82      Form form = mainform.GetForm(this);
[2723]83      if (form != null) {
84        this.IsShown = false;
85        mainform.CloseView(this, closeReason);
86      }
[2696]87    }
88
[2704]89    public new void Hide() {
[2723]90      this.IsShown = false;
[2696]91      MainFormManager.GetMainForm<MainForm>().HideView(this);
[2793]92      this.OnHidden(EventArgs.Empty);
[2696]93    }
94
[2]95    public event EventHandler CaptionChanged;
96    protected virtual void OnCaptionChanged() {
97      if (CaptionChanged != null)
[2793]98        CaptionChanged(this, EventArgs.Empty);
[2]99    }
100
[2426]101    public event EventHandler Changed;
102    protected virtual void OnChanged() {
[2548]103      if (InvokeRequired)
104        Invoke((MethodInvoker)OnChanged);
105      else if (Changed != null)
[2793]106        Changed(this, EventArgs.Empty);
[2254]107    }
108
[2696]109    protected virtual void OnShown(ViewShownEventArgs e) {
[2]110    }
[2266]111
[2696]112    protected virtual void OnHidden(EventArgs e) {
113    }
114
[2962]115    private CloseReason closeReason;
116    internal CloseReason CloseReason {
117      get { return this.closeReason; }
118      set { this.closeReason = value; }
119    }
120
[3301]121    internal void OnClosingHelper(object sender, FormClosingEventArgs e) {
122      FormClosingEventArgs eventArgs = new FormClosingEventArgs(this.closeReason, e.Cancel);
[2543]123      if (this.closeReason != CloseReason.None)
[3301]124        this.OnClosing(eventArgs);
[2543]125      else
[2696]126        this.OnClosing(e);
[2548]127
[3301]128      if (eventArgs.Cancel != e.Cancel)
129        e.Cancel = eventArgs.Cancel;
[2543]130      this.closeReason = CloseReason.None;
[2532]131    }
132
[2696]133    protected virtual void OnClosing(FormClosingEventArgs e) {
[2543]134    }
135
[3301]136    internal void OnClosedHelper(object sender, FormClosedEventArgs e) {
[2696]137      if (this.closeReason != CloseReason.None)
138        this.OnClosed(new FormClosedEventArgs(this.closeReason));
139      else
140        this.OnClosed(e);
141
142      Form form = (Form)sender;
143      form.FormClosed -= new FormClosedEventHandler(OnClosedHelper);
144      form.FormClosing -= new FormClosingEventHandler(OnClosingHelper);
145      this.closeReason = CloseReason.None;
[2266]146    }
[2509]147
[2696]148    protected virtual void OnClosed(FormClosedEventArgs e) {
149    }
[2509]150
[2813]151    private void View_Load(object sender, EventArgs e) {
[2509]152      if (!this.initialized && !this.DesignMode) {
[2696]153        this.OnInitialized(e);
[2509]154        this.initialized = true;
155      }
156    }
[2696]157
158    protected virtual void OnInitialized(EventArgs e) {
159    }
[3177]160
161    public new bool Enabled {
162      get { return base.Enabled; }
163      set {
164        SuspendRepaint();
165        base.Enabled = value;
166        ResumeRepaint(true);
167      }
168    }
169
170    public void SuspendRepaint() {
171      ((Control)this).SuspendRepaint();
172    }
173    public void ResumeRepaint(bool refresh) {
174      ((Control)this).ResumeRepaint(refresh);
175    }
[2]176  }
177}
Note: See TracBrowser for help on using the repository browser.