Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added a call to the SetEnabledStateOfControls method in the OnInitialized method (ticked #1194).

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