Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

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