Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.MainForm.WindowsForms/3.3/Views/View.cs @ 10377

Last change on this file since 10377 was 9920, checked in by ascheibe, 11 years ago

#2100 moved help system from View to ViewHost

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