Free cookie consent management tool by TermsFeed Policy Generator

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

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

corrected bug in OnClosing of views (ticket #982)

File size: 7.8 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.Linq;
24using System.Windows.Forms;
25using System.Reflection;
26using System.ComponentModel;
27
28namespace HeuristicLab.MainForm.WindowsForms {
29  public partial class View : UserControl, IView {
30    private bool initialized;
31    public View() {
32      InitializeComponent();
33      this.initialized = false;
34      this.isShown = false;
35      this.closeReason = CloseReason.None;
36      this.readOnly = false;
37    }
38
39    public View(bool readOnly)
40      : this() {
41      this.readOnly = readOnly;
42    }
43
44    private string caption;
45    public string Caption {
46      get { return caption; }
47      set {
48        if (InvokeRequired) {
49          Action<string> action = delegate(string s) { this.Caption = s; };
50          Invoke(action, value);
51        } else {
52          if (value != caption) {
53            caption = value;
54            OnCaptionChanged();
55          }
56        }
57      }
58    }
59
60    private bool readOnly;
61    public virtual bool ReadOnly {
62      get { return this.readOnly; }
63      set {
64        if (InvokeRequired) {
65          Action<bool> action = delegate(bool b) { this.ReadOnly = b; };
66          Invoke(action, value);
67        } else {
68          if (value != readOnly) {
69            readOnly = value;
70            PropertyInfo prop = typeof(IView).GetProperty("ReadOnly");
71            PropagateStateChanges(this, typeof(IView), prop);
72            OnReadOnlyChanged();
73          }
74        }
75      }
76    }
77
78    private bool isShown;
79    public bool IsShown {
80      get { return this.isShown; }
81      private set { this.isShown = value; }
82    }
83
84    public new void Show() {
85      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
86      bool firstTimeShown = mainform.GetForm(this) == null;
87
88      this.IsShown = true;
89      mainform.ShowView(this);
90      if (firstTimeShown) {
91        Form form = mainform.GetForm(this);
92        form.FormClosed += new FormClosedEventHandler(OnClosedHelper);
93        form.FormClosing += new FormClosingEventHandler(OnClosingHelper);
94      }
95      this.OnShown(new ViewShownEventArgs(this, firstTimeShown));
96    }
97
98    public void Close() {
99      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
100      Form form = mainform.GetForm(this);
101      if (form != null) {
102        this.IsShown = false;
103        mainform.CloseView(this);
104      }
105    }
106
107    public void Close(CloseReason closeReason) {
108      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
109      Form form = mainform.GetForm(this);
110      if (form != null) {
111        this.IsShown = false;
112        mainform.CloseView(this, closeReason);
113      }
114    }
115
116    public new void Hide() {
117      this.IsShown = false;
118      MainFormManager.GetMainForm<MainForm>().HideView(this);
119      this.OnHidden(EventArgs.Empty);
120    }
121
122    public event EventHandler CaptionChanged;
123    protected virtual void OnCaptionChanged() {
124      if (InvokeRequired)
125        Invoke((MethodInvoker)OnCaptionChanged);
126      else {
127        EventHandler handler = CaptionChanged;
128        if (handler != null)
129          handler(this, EventArgs.Empty);
130      }
131    }
132    public event EventHandler ReadOnlyChanged;
133    protected virtual void OnReadOnlyChanged() {
134      if (InvokeRequired)
135        Invoke((MethodInvoker)OnReadOnlyChanged);
136      else {
137        EventHandler handler = ReadOnlyChanged;
138        if (handler != null)
139          handler(this, EventArgs.Empty);
140      }
141    }
142    protected void PropagateStateChanges(Control control, Type type, PropertyInfo propertyInfo) {
143      if (!type.GetProperties().Contains(propertyInfo))
144        throw new ArgumentException("The specified type " + type + "implement the property " + propertyInfo.Name + ".");
145      if (!type.IsAssignableFrom(this.GetType()))
146        throw new ArgumentException("The specified type " + type + "must be the same or a base class / interface of this object.");
147      if (!propertyInfo.CanWrite)
148        throw new ArgumentException("The specified property " + propertyInfo.Name + " must have a setter.");
149
150      foreach (Control c in control.Controls) {
151        Type controlType = c.GetType();
152        PropertyInfo controlPropertyInfo = controlType.GetProperty(propertyInfo.Name, propertyInfo.PropertyType);
153        if (type.IsAssignableFrom(controlType) && controlPropertyInfo != null) {
154          var thisValue = propertyInfo.GetValue(this, null);
155          controlPropertyInfo.SetValue(c, thisValue, null);
156        } else
157          PropagateStateChanges(c, type, propertyInfo);
158      }
159    }
160    public event EventHandler Changed;
161    protected virtual void OnChanged() {
162      if (InvokeRequired)
163        Invoke((MethodInvoker)OnChanged);
164      else {
165        EventHandler handler = Changed;
166        if (handler != null)
167          handler(this, EventArgs.Empty);
168      }
169    }
170
171    internal protected virtual void OnShown(ViewShownEventArgs e) {
172    }
173
174    internal protected virtual void OnHidden(EventArgs e) {
175    }
176
177    private CloseReason closeReason;
178    internal CloseReason CloseReason {
179      get { return this.closeReason; }
180      set { this.closeReason = value; }
181    }
182
183    internal void OnClosingHelper(object sender, FormClosingEventArgs e) {
184      FormClosingEventArgs eventArgs = new FormClosingEventArgs(this.closeReason, e.Cancel);
185      if (this.closeReason != CloseReason.None) {
186        this.OnClosing(eventArgs);
187        if (eventArgs.Cancel != e.Cancel)
188          e.Cancel = eventArgs.Cancel;
189      } else
190        this.OnClosing(e);
191      this.closeReason = CloseReason.None;
192    }
193
194    internal protected virtual void OnClosing(FormClosingEventArgs e) {
195    }
196
197    internal void OnClosedHelper(object sender, FormClosedEventArgs e) {
198      if (this.closeReason != CloseReason.None)
199        this.OnClosed(new FormClosedEventArgs(this.closeReason));
200      else
201        this.OnClosed(e);
202
203      Form form = (Form)sender;
204      form.FormClosed -= new FormClosedEventHandler(OnClosedHelper);
205      form.FormClosing -= new FormClosingEventHandler(OnClosingHelper);
206      this.closeReason = CloseReason.None;
207    }
208
209    internal protected virtual void OnClosed(FormClosedEventArgs e) {
210    }
211
212    private void View_Load(object sender, EventArgs e) {
213      if (!this.initialized && !this.DesignMode) {
214        this.OnInitialized(e);
215        this.initialized = true;
216      }
217    }
218
219    protected virtual void OnInitialized(EventArgs e) {
220    }
221
222    public new bool Enabled {
223      get { return base.Enabled; }
224      set {
225        SuspendRepaint();
226        base.Enabled = value;
227        ResumeRepaint(true);
228      }
229    }
230
231    public void SuspendRepaint() {
232      if (InvokeRequired)
233        Invoke((MethodInvoker)SuspendRepaint);
234      else
235        ((Control)this).SuspendRepaint();
236    }
237    public void ResumeRepaint(bool refresh) {
238      if (InvokeRequired)
239        Invoke((Action<bool>)ResumeRepaint, refresh);
240      else
241        ((Control)this).ResumeRepaint(refresh);
242    }
243  }
244}
Note: See TracBrowser for help on using the repository browser.