Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/View.cs @ 3416

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

implemented ContentViews and propagation of view state changes (ticket #982)

File size: 7.8 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[3416]23using System.Linq;
[2]24using System.Windows.Forms;
[3416]25using System.Reflection;
26using System.ComponentModel;
[2]27
[2426]28namespace HeuristicLab.MainForm.WindowsForms {
[2696]29  public partial class View : UserControl, IView {
[2509]30    private bool initialized;
[2696]31    public View() {
[2233]32      InitializeComponent();
[2509]33      this.initialized = false;
[2711]34      this.isShown = false;
[2543]35      this.closeReason = CloseReason.None;
[3350]36      this.readOnly = false;
[2]37    }
[2456]38
[3350]39    public View(bool readOnly)
40      : this() {
41      this.readOnly = readOnly;
42    }
43
44    private string caption;
[2]45    public string Caption {
[3350]46      get { return caption; }
[2]47      set {
[2548]48        if (InvokeRequired) {
49          Action<string> action = delegate(string s) { this.Caption = s; };
50          Invoke(action, value);
51        } else {
[3350]52          if (value != caption) {
53            caption = value;
[2548]54            OnCaptionChanged();
55          }
[2]56        }
57      }
58    }
59
[3350]60    private bool readOnly;
61    public virtual bool ReadOnly {
62      get { return this.readOnly; }
[3398]63      set {
[3350]64        if (InvokeRequired) {
65          Action<bool> action = delegate(bool b) { this.ReadOnly = b; };
66          Invoke(action, value);
67        } else {
68          if (value != readOnly) {
69            readOnly = value;
[3416]70            PropertyInfo prop = typeof(IView).GetProperty("ReadOnly");
71            PropagateStateChanges(this, typeof(IView), prop);
[3350]72            OnReadOnlyChanged();
73          }
74        }
75      }
76    }
77
[2711]78    private bool isShown;
79    public bool IsShown {
80      get { return this.isShown; }
[2723]81      private set { this.isShown = value; }
[2711]82    }
83
[2704]84    public new void Show() {
[2696]85      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
86      bool firstTimeShown = mainform.GetForm(this) == null;
87
[2723]88      this.IsShown = true;
[3403]89      mainform.ShowView(this);
[2696]90      if (firstTimeShown) {
91        Form form = mainform.GetForm(this);
92        form.FormClosed += new FormClosedEventHandler(OnClosedHelper);
93        form.FormClosing += new FormClosingEventHandler(OnClosingHelper);
94      }
[2723]95      this.OnShown(new ViewShownEventArgs(this, firstTimeShown));
[2696]96    }
97
98    public void Close() {
99      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
100      Form form = mainform.GetForm(this);
[2723]101      if (form != null) {
102        this.IsShown = false;
[2696]103        mainform.CloseView(this);
[2723]104      }
[2696]105    }
106
107    public void Close(CloseReason closeReason) {
108      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
109      Form form = mainform.GetForm(this);
[2723]110      if (form != null) {
111        this.IsShown = false;
112        mainform.CloseView(this, closeReason);
113      }
[2696]114    }
115
[2704]116    public new void Hide() {
[2723]117      this.IsShown = false;
[2696]118      MainFormManager.GetMainForm<MainForm>().HideView(this);
[2793]119      this.OnHidden(EventArgs.Empty);
[2696]120    }
121
[2]122    public event EventHandler CaptionChanged;
123    protected virtual void OnCaptionChanged() {
[3350]124      if (InvokeRequired)
125        Invoke((MethodInvoker)OnCaptionChanged);
126      else {
127        EventHandler handler = CaptionChanged;
128        if (handler != null)
129          handler(this, EventArgs.Empty);
130      }
[2]131    }
[3350]132    public event EventHandler ReadOnlyChanged;
133    protected virtual void OnReadOnlyChanged() {
134      if (InvokeRequired)
135        Invoke((MethodInvoker)OnReadOnlyChanged);
136      else {
137        EventHandler handler = ReadOnlyChanged;
138        if (handler != null)
139          handler(this, EventArgs.Empty);
140      }
141    }
[3416]142    protected void PropagateStateChanges(Control control, Type type, PropertyInfo propertyInfo) {
143      if (!type.GetProperties().Contains(propertyInfo))
144        throw new ArgumentException("The specified type " + type + "implement the property " + propertyInfo.Name + ".");
145      if (!type.IsAssignableFrom(this.GetType()))
146        throw new ArgumentException("The specified type " + type + "must be the same or a base class / interface of this object.");
147      if (!propertyInfo.CanWrite)
148        throw new ArgumentException("The specified property " + propertyInfo.Name + " must have a setter.");
149
150      foreach (Control c in control.Controls) {
151        Type controlType = c.GetType();
152        PropertyInfo controlPropertyInfo = controlType.GetProperty(propertyInfo.Name, propertyInfo.PropertyType);
153        if (type.IsAssignableFrom(controlType) && controlPropertyInfo!= null) {
154          var thisValue = propertyInfo.GetValue(this, null);
155          controlPropertyInfo.SetValue(c, thisValue, null);
156        } else
157          PropagateStateChanges(c, type, propertyInfo);
158      }
159    }
[2426]160    public event EventHandler Changed;
161    protected virtual void OnChanged() {
[2548]162      if (InvokeRequired)
163        Invoke((MethodInvoker)OnChanged);
[3350]164      else {
165        EventHandler handler = Changed;
166        if (handler != null)
167          handler(this, EventArgs.Empty);
168      }
[2254]169    }
170
[3403]171    internal protected virtual void OnShown(ViewShownEventArgs e) {
[2]172    }
[2266]173
[3403]174    internal protected virtual void OnHidden(EventArgs e) {
[2696]175    }
176
[2962]177    private CloseReason closeReason;
178    internal CloseReason CloseReason {
179      get { return this.closeReason; }
180      set { this.closeReason = value; }
181    }
182
[3301]183    internal void OnClosingHelper(object sender, FormClosingEventArgs e) {
184      FormClosingEventArgs eventArgs = new FormClosingEventArgs(this.closeReason, e.Cancel);
[2543]185      if (this.closeReason != CloseReason.None)
[3301]186        this.OnClosing(eventArgs);
[2543]187      else
[2696]188        this.OnClosing(e);
[2548]189
[3301]190      if (eventArgs.Cancel != e.Cancel)
191        e.Cancel = eventArgs.Cancel;
[2543]192      this.closeReason = CloseReason.None;
[2532]193    }
194
[3403]195    internal protected virtual void OnClosing(FormClosingEventArgs e) {
[2543]196    }
197
[3301]198    internal void OnClosedHelper(object sender, FormClosedEventArgs e) {
[2696]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;
[2266]208    }
[2509]209
[3403]210    internal protected virtual void OnClosed(FormClosedEventArgs e) {
[2696]211    }
[2509]212
[2813]213    private void View_Load(object sender, EventArgs e) {
[2509]214      if (!this.initialized && !this.DesignMode) {
[2696]215        this.OnInitialized(e);
[2509]216        this.initialized = true;
217      }
218    }
[2696]219
220    protected virtual void OnInitialized(EventArgs e) {
221    }
[3177]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() {
[3398]233      if (InvokeRequired)
234        Invoke((MethodInvoker)SuspendRepaint);
235      else
236        ((Control)this).SuspendRepaint();
[3177]237    }
238    public void ResumeRepaint(bool refresh) {
[3398]239      if (InvokeRequired)
[3403]240        Invoke((Action<bool>)ResumeRepaint, refresh);
[3398]241      else
[3403]242        ((Control)this).ResumeRepaint(refresh);
[3177]243    }
[2]244  }
245}
Note: See TracBrowser for help on using the repository browser.