Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 6.5 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      this.readOnly = false;
34    }
35
36    public View(bool readOnly)
37      : this() {
38      this.readOnly = readOnly;
39    }
40
41    private string caption;
42    public string Caption {
43      get { return caption; }
44      set {
45        if (InvokeRequired) {
46          Action<string> action = delegate(string s) { this.Caption = s; };
47          Invoke(action, value);
48        } else {
49          if (value != caption) {
50            caption = value;
51            OnCaptionChanged();
52          }
53        }
54      }
55    }
56
57    private bool readOnly;
58    public virtual bool ReadOnly {
59      get { return this.readOnly; }
60      set {
61        if (InvokeRequired) {
62          Action<bool> action = delegate(bool b) { this.ReadOnly = b; };
63          Invoke(action, value);
64        } else {
65          if (value != readOnly) {
66            readOnly = value;
67            OnReadOnlyChanged();
68          }
69        }
70      }
71    }
72
73    private bool isShown;
74    public bool IsShown {
75      get { return this.isShown; }
76      private set { this.isShown = value; }
77    }
78
79    public new void Show() {
80      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
81      bool firstTimeShown = mainform.GetForm(this) == null;
82
83      this.IsShown = true;
84      mainform.ShowView(this);
85      if (firstTimeShown) {
86        Form form = mainform.GetForm(this);
87        form.FormClosed += new FormClosedEventHandler(OnClosedHelper);
88        form.FormClosing += new FormClosingEventHandler(OnClosingHelper);
89      }
90      this.OnShown(new ViewShownEventArgs(this, firstTimeShown));
91    }
92
93    public void Close() {
94      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
95      Form form = mainform.GetForm(this);
96      if (form != null) {
97        this.IsShown = false;
98        mainform.CloseView(this);
99      }
100    }
101
102    public void Close(CloseReason closeReason) {
103      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
104      Form form = mainform.GetForm(this);
105      if (form != null) {
106        this.IsShown = false;
107        mainform.CloseView(this, closeReason);
108      }
109    }
110
111    public new void Hide() {
112      this.IsShown = false;
113      MainFormManager.GetMainForm<MainForm>().HideView(this);
114      this.OnHidden(EventArgs.Empty);
115    }
116
117    public event EventHandler CaptionChanged;
118    protected virtual void OnCaptionChanged() {
119      if (InvokeRequired)
120        Invoke((MethodInvoker)OnCaptionChanged);
121      else {
122        EventHandler handler = CaptionChanged;
123        if (handler != null)
124          handler(this, EventArgs.Empty);
125      }
126    }
127    public event EventHandler ReadOnlyChanged;
128    protected virtual void OnReadOnlyChanged() {
129      if (InvokeRequired)
130        Invoke((MethodInvoker)OnReadOnlyChanged);
131      else {
132        EventHandler handler = ReadOnlyChanged;
133        if (handler != null)
134          handler(this, EventArgs.Empty);
135      }
136    }
137    public event EventHandler Changed;
138    protected virtual void OnChanged() {
139      if (InvokeRequired)
140        Invoke((MethodInvoker)OnChanged);
141      else {
142        EventHandler handler = Changed;
143        if (handler != null)
144          handler(this, EventArgs.Empty);
145      }
146    }
147
148    internal protected virtual void OnShown(ViewShownEventArgs e) {
149    }
150
151    internal protected virtual void OnHidden(EventArgs e) {
152    }
153
154    private CloseReason closeReason;
155    internal CloseReason CloseReason {
156      get { return this.closeReason; }
157      set { this.closeReason = value; }
158    }
159
160    internal void OnClosingHelper(object sender, FormClosingEventArgs e) {
161      FormClosingEventArgs eventArgs = new FormClosingEventArgs(this.closeReason, e.Cancel);
162      if (this.closeReason != CloseReason.None)
163        this.OnClosing(eventArgs);
164      else
165        this.OnClosing(e);
166
167      if (eventArgs.Cancel != e.Cancel)
168        e.Cancel = eventArgs.Cancel;
169      this.closeReason = CloseReason.None;
170    }
171
172    internal protected virtual void OnClosing(FormClosingEventArgs e) {
173    }
174
175    internal void OnClosedHelper(object sender, FormClosedEventArgs e) {
176      if (this.closeReason != CloseReason.None)
177        this.OnClosed(new FormClosedEventArgs(this.closeReason));
178      else
179        this.OnClosed(e);
180
181      Form form = (Form)sender;
182      form.FormClosed -= new FormClosedEventHandler(OnClosedHelper);
183      form.FormClosing -= new FormClosingEventHandler(OnClosingHelper);
184      this.closeReason = CloseReason.None;
185    }
186
187    internal protected virtual void OnClosed(FormClosedEventArgs e) {
188    }
189
190    private void View_Load(object sender, EventArgs e) {
191      if (!this.initialized && !this.DesignMode) {
192        this.OnInitialized(e);
193        this.initialized = true;
194      }
195    }
196
197    protected virtual void OnInitialized(EventArgs e) {
198    }
199
200    public new bool Enabled {
201      get { return base.Enabled; }
202      set {
203        SuspendRepaint();
204        base.Enabled = value;
205        ResumeRepaint(true);
206      }
207    }
208
209    public void SuspendRepaint() {
210      if (InvokeRequired)
211        Invoke((MethodInvoker)SuspendRepaint);
212      else
213        ((Control)this).SuspendRepaint();
214    }
215    public void ResumeRepaint(bool refresh) {
216      if (InvokeRequired)
217        Invoke((Action<bool>)ResumeRepaint, refresh);
218      else
219        ((Control)this).ResumeRepaint(refresh);
220    }
221  }
222}
Note: See TracBrowser for help on using the repository browser.