Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3714 was 3557, checked in by mkommend, 15 years ago

changed logic of showing new views (ticket #972)

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