Free cookie consent management tool by TermsFeed Policy Generator

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

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

fixed cross threading issues in ViewHost (ticket #972)

File size: 6.5 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;
23using System.Windows.Forms;
24
[2426]25namespace HeuristicLab.MainForm.WindowsForms {
[2696]26  public partial class View : UserControl, IView {
[2509]27    private bool initialized;
[2696]28    public View() {
[2233]29      InitializeComponent();
[2509]30      this.initialized = false;
[2711]31      this.isShown = false;
[2543]32      this.closeReason = CloseReason.None;
[3350]33      this.readOnly = false;
[2]34    }
[2456]35
[3350]36    public View(bool readOnly)
37      : this() {
38      this.readOnly = readOnly;
39    }
40
41    private string caption;
[2]42    public string Caption {
[3350]43      get { return caption; }
[2]44      set {
[2548]45        if (InvokeRequired) {
46          Action<string> action = delegate(string s) { this.Caption = s; };
47          Invoke(action, value);
48        } else {
[3350]49          if (value != caption) {
50            caption = value;
[2548]51            OnCaptionChanged();
52          }
[2]53        }
54      }
55    }
56
[3350]57    private bool readOnly;
58    public virtual bool ReadOnly {
59      get { return this.readOnly; }
[3398]60      set {
[3350]61        if (InvokeRequired) {
62          Action<bool> action = delegate(bool b) { this.ReadOnly = b; };
63          Invoke(action, value);
64        } else {
65          if (value != readOnly) {
66            readOnly = value;
67            OnReadOnlyChanged();
68          }
69        }
70      }
71    }
72
[2711]73    private bool isShown;
74    public bool IsShown {
75      get { return this.isShown; }
[2723]76      private set { this.isShown = value; }
[2711]77    }
78
[2704]79    public new void Show() {
[2696]80      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
81      bool firstTimeShown = mainform.GetForm(this) == null;
82
[2723]83      this.IsShown = true;
84      mainform.ShowView(this, firstTimeShown);
[2696]85      if (firstTimeShown) {
86        Form form = mainform.GetForm(this);
87        form.FormClosed += new FormClosedEventHandler(OnClosedHelper);
88        form.FormClosing += new FormClosingEventHandler(OnClosingHelper);
89      }
[2723]90      this.OnShown(new ViewShownEventArgs(this, firstTimeShown));
[2696]91    }
92
93    public void Close() {
94      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
95      Form form = mainform.GetForm(this);
[2723]96      if (form != null) {
97        this.IsShown = false;
[2696]98        mainform.CloseView(this);
[2723]99      }
[2696]100    }
101
102    public void Close(CloseReason closeReason) {
103      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
104      Form form = mainform.GetForm(this);
[2723]105      if (form != null) {
106        this.IsShown = false;
107        mainform.CloseView(this, closeReason);
108      }
[2696]109    }
110
[2704]111    public new void Hide() {
[2723]112      this.IsShown = false;
[2696]113      MainFormManager.GetMainForm<MainForm>().HideView(this);
[2793]114      this.OnHidden(EventArgs.Empty);
[2696]115    }
116
[2]117    public event EventHandler CaptionChanged;
118    protected virtual void OnCaptionChanged() {
[3350]119      if (InvokeRequired)
120        Invoke((MethodInvoker)OnCaptionChanged);
121      else {
122        EventHandler handler = CaptionChanged;
123        if (handler != null)
124          handler(this, EventArgs.Empty);
125      }
[2]126    }
[3350]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    }
[2426]137    public event EventHandler Changed;
138    protected virtual void OnChanged() {
[2548]139      if (InvokeRequired)
140        Invoke((MethodInvoker)OnChanged);
[3350]141      else {
142        EventHandler handler = Changed;
143        if (handler != null)
144          handler(this, EventArgs.Empty);
145      }
[2254]146    }
147
[2696]148    protected virtual void OnShown(ViewShownEventArgs e) {
[2]149    }
[2266]150
[2696]151    protected virtual void OnHidden(EventArgs e) {
152    }
153
[2962]154    private CloseReason closeReason;
155    internal CloseReason CloseReason {
156      get { return this.closeReason; }
157      set { this.closeReason = value; }
158    }
159
[3301]160    internal void OnClosingHelper(object sender, FormClosingEventArgs e) {
161      FormClosingEventArgs eventArgs = new FormClosingEventArgs(this.closeReason, e.Cancel);
[2543]162      if (this.closeReason != CloseReason.None)
[3301]163        this.OnClosing(eventArgs);
[2543]164      else
[2696]165        this.OnClosing(e);
[2548]166
[3301]167      if (eventArgs.Cancel != e.Cancel)
168        e.Cancel = eventArgs.Cancel;
[2543]169      this.closeReason = CloseReason.None;
[2532]170    }
171
[2696]172    protected virtual void OnClosing(FormClosingEventArgs e) {
[2543]173    }
174
[3301]175    internal void OnClosedHelper(object sender, FormClosedEventArgs e) {
[2696]176      if (this.closeReason != CloseReason.None)
177        this.OnClosed(new FormClosedEventArgs(this.closeReason));
178      else
179        this.OnClosed(e);
180
181      Form form = (Form)sender;
182      form.FormClosed -= new FormClosedEventHandler(OnClosedHelper);
183      form.FormClosing -= new FormClosingEventHandler(OnClosingHelper);
184      this.closeReason = CloseReason.None;
[2266]185    }
[2509]186
[2696]187    protected virtual void OnClosed(FormClosedEventArgs e) {
188    }
[2509]189
[2813]190    private void View_Load(object sender, EventArgs e) {
[2509]191      if (!this.initialized && !this.DesignMode) {
[2696]192        this.OnInitialized(e);
[2509]193        this.initialized = true;
194      }
195    }
[2696]196
197    protected virtual void OnInitialized(EventArgs e) {
198    }
[3177]199
200    public new bool Enabled {
201      get { return base.Enabled; }
202      set {
203        SuspendRepaint();
204        base.Enabled = value;
205        ResumeRepaint(true);
206      }
207    }
208
209    public void SuspendRepaint() {
[3398]210      if (InvokeRequired)
211        Invoke((MethodInvoker)SuspendRepaint);
212      else
213        ((Control)this).SuspendRepaint();
[3177]214    }
215    public void ResumeRepaint(bool refresh) {
[3398]216      if (InvokeRequired)
217        Invoke((Action<bool>)ResumeRepaint,refresh);
218      else
[3177]219      ((Control)this).ResumeRepaint(refresh);
220    }
[2]221  }
222}
Note: See TracBrowser for help on using the repository browser.