Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.MainForm.WindowsForms/3.3/Views/View.cs @ 14542

Last change on this file since 14542 was 14542, checked in by gkronber, 7 years ago

#2650: merged r14504:14533 from trunk to branch

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