Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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