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

Last change on this file since 3350 was 3350, checked in by mkommend, 12 years ago

implemented first version of View.ReadOnly and adapted some views to the new mechanism (ticket #973)

File size: 6.6 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, firstTimeShown);
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        foreach (Control control in this.Controls) {
136          IView view = control as IView;
137          if (view != null)
138            view.ReadOnly = this.readOnly;
139          ViewHost viewHost = control as ViewHost;
140          if (viewHost != null)
141            viewHost.ReadOnly = this.readOnly;
142        }
143      }
144    }
145    public event EventHandler Changed;
146    protected virtual void OnChanged() {
147      if (InvokeRequired)
148        Invoke((MethodInvoker)OnChanged);
149      else {
150        EventHandler handler = Changed;
151        if (handler != null)
152          handler(this, EventArgs.Empty);
153      }
154    }
155
156    protected virtual void OnShown(ViewShownEventArgs e) {
157    }
158
159    protected virtual void OnHidden(EventArgs e) {
160    }
161
162    private CloseReason closeReason;
163    internal CloseReason CloseReason {
164      get { return this.closeReason; }
165      set { this.closeReason = value; }
166    }
167
168    internal void OnClosingHelper(object sender, FormClosingEventArgs e) {
169      FormClosingEventArgs eventArgs = new FormClosingEventArgs(this.closeReason, e.Cancel);
170      if (this.closeReason != CloseReason.None)
171        this.OnClosing(eventArgs);
172      else
173        this.OnClosing(e);
174
175      if (eventArgs.Cancel != e.Cancel)
176        e.Cancel = eventArgs.Cancel;
177      this.closeReason = CloseReason.None;
178    }
179
180    protected virtual void OnClosing(FormClosingEventArgs e) {
181    }
182
183    internal void OnClosedHelper(object sender, FormClosedEventArgs e) {
184      if (this.closeReason != CloseReason.None)
185        this.OnClosed(new FormClosedEventArgs(this.closeReason));
186      else
187        this.OnClosed(e);
188
189      Form form = (Form)sender;
190      form.FormClosed -= new FormClosedEventHandler(OnClosedHelper);
191      form.FormClosing -= new FormClosingEventHandler(OnClosingHelper);
192      this.closeReason = CloseReason.None;
193    }
194
195    protected virtual void OnClosed(FormClosedEventArgs e) {
196    }
197
198    private void View_Load(object sender, EventArgs e) {
199      if (!this.initialized && !this.DesignMode) {
200        this.OnInitialized(e);
201        this.initialized = true;
202      }
203    }
204
205    protected virtual void OnInitialized(EventArgs e) {
206    }
207
208    public new bool Enabled {
209      get { return base.Enabled; }
210      set {
211        SuspendRepaint();
212        base.Enabled = value;
213        ResumeRepaint(true);
214      }
215    }
216
217    public void SuspendRepaint() {
218      ((Control)this).SuspendRepaint();
219    }
220    public void ResumeRepaint(bool refresh) {
221      ((Control)this).ResumeRepaint(refresh);
222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.