Free cookie consent management tool by TermsFeed Policy Generator

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

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

implemented fix for ticket #966

File size: 5.3 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.Windows.Forms;
24
25namespace HeuristicLab.MainForm.WindowsForms {
26  public partial class View : UserControl, IView {
27    private bool initialized;
28    public View() {
29      InitializeComponent();
30      this.initialized = false;
31      this.isShown = false;
32      this.closeReason = CloseReason.None;
33    }
34
35    private string myCaption;
36    public string Caption {
37      get { return myCaption; }
38      set {
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          }
47        }
48      }
49    }
50
51    private bool isShown;
52    public bool IsShown {
53      get { return this.isShown; }
54      private set { this.isShown = value; }
55    }
56
57    public new void Show() {
58      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
59      bool firstTimeShown = mainform.GetForm(this) == null;
60
61      this.IsShown = true;
62      mainform.ShowView(this, firstTimeShown);
63      if (firstTimeShown) {
64        Form form = mainform.GetForm(this);
65        form.FormClosed += new FormClosedEventHandler(OnClosedHelper);
66        form.FormClosing += new FormClosingEventHandler(OnClosingHelper);
67      }
68      this.OnShown(new ViewShownEventArgs(this, firstTimeShown));
69    }
70
71    public void Close() {
72      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
73      Form form = mainform.GetForm(this);
74      if (form != null) {
75        this.IsShown = false;
76        mainform.CloseView(this);
77      }
78    }
79
80    public void Close(CloseReason closeReason) {
81      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
82      Form form = mainform.GetForm(this);
83      if (form != null) {
84        this.IsShown = false;
85        mainform.CloseView(this, closeReason);
86      }
87    }
88
89    public new void Hide() {
90      this.IsShown = false;
91      MainFormManager.GetMainForm<MainForm>().HideView(this);
92      this.OnHidden(EventArgs.Empty);
93    }
94
95    public event EventHandler CaptionChanged;
96    protected virtual void OnCaptionChanged() {
97      if (CaptionChanged != null)
98        CaptionChanged(this, EventArgs.Empty);
99    }
100
101    public event EventHandler Changed;
102    protected virtual void OnChanged() {
103      if (InvokeRequired)
104        Invoke((MethodInvoker)OnChanged);
105      else if (Changed != null)
106        Changed(this, EventArgs.Empty);
107    }
108
109    protected virtual void OnShown(ViewShownEventArgs e) {
110    }
111
112    protected virtual void OnHidden(EventArgs e) {
113    }
114
115    private CloseReason closeReason;
116    internal CloseReason CloseReason {
117      get { return this.closeReason; }
118      set { this.closeReason = value; }
119    }
120
121    internal void OnClosingHelper(object sender, FormClosingEventArgs e) {
122      FormClosingEventArgs eventArgs = new FormClosingEventArgs(this.closeReason, e.Cancel);
123      if (this.closeReason != CloseReason.None)
124        this.OnClosing(eventArgs);
125      else
126        this.OnClosing(e);
127
128      if (eventArgs.Cancel != e.Cancel)
129        e.Cancel = eventArgs.Cancel;
130      this.closeReason = CloseReason.None;
131    }
132
133    protected virtual void OnClosing(FormClosingEventArgs e) {
134    }
135
136    internal void OnClosedHelper(object sender, FormClosedEventArgs e) {
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;
146    }
147
148    protected virtual void OnClosed(FormClosedEventArgs e) {
149    }
150
151    private void View_Load(object sender, EventArgs e) {
152      if (!this.initialized && !this.DesignMode) {
153        this.OnInitialized(e);
154        this.initialized = true;
155      }
156    }
157
158    protected virtual void OnInitialized(EventArgs e) {
159    }
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    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.