Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/View.cs @ 3727

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

added Suspend- and ResumeRepaint calls in View and ContentView (ticket #972)

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