Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.MainForm.WindowsForms/3.3/View.cs @ 17275

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

corrected behavior of CheckedItemCollectionViews (ticket #893)

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