Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2793 was 2793, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • replaced new EventArgs() by EventArgs.Empty
File size: 4.8 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.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Xml;
29using System.Windows.Forms;
30
31namespace HeuristicLab.MainForm.WindowsForms {
32  public partial class View : UserControl, IView {
33    private bool initialized;
34    public View() {
35      InitializeComponent();
36      this.initialized = false;
37      this.isShown = false;
38      this.closeReason = CloseReason.None;
39    }
40
41    private string myCaption;
42    public string Caption {
43      get { return myCaption; }
44      set {
45        if (InvokeRequired) {
46          Action<string> action = delegate(string s) { this.Caption = s; };
47          Invoke(action, value);
48        } else {
49          if (value != myCaption) {
50            myCaption = value;
51            OnCaptionChanged();
52          }
53        }
54      }
55    }
56
57    private bool isShown;
58    public bool IsShown {
59      get { return this.isShown; }
60      private set { this.isShown = value; }
61    }
62
63    public new void Show() {
64      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
65      bool firstTimeShown = mainform.GetForm(this) == null;
66
67      this.IsShown = true;
68      mainform.ShowView(this, firstTimeShown);
69      if (firstTimeShown) {
70        Form form = mainform.GetForm(this);
71        form.FormClosed += new FormClosedEventHandler(OnClosedHelper);
72        form.FormClosing += new FormClosingEventHandler(OnClosingHelper);
73      }
74      this.OnShown(new ViewShownEventArgs(this, firstTimeShown));
75    }
76
77    public void Close() {
78      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
79      Form form = mainform.GetForm(this);
80      if (form != null) {
81        this.IsShown = false;
82        mainform.CloseView(this);
83      }
84    }
85
86    public void Close(CloseReason closeReason) {
87      MainForm mainform = MainFormManager.GetMainForm<MainForm>();
88      Form form = mainform.GetForm(this);
89      if (form != null) {
90        this.IsShown = false;
91        mainform.CloseView(this, closeReason);
92      }
93    }
94
95    public new void Hide() {
96      this.IsShown = false;
97      MainFormManager.GetMainForm<MainForm>().HideView(this);
98      this.OnHidden(EventArgs.Empty);
99    }
100
101    public event EventHandler CaptionChanged;
102    protected virtual void OnCaptionChanged() {
103      if (CaptionChanged != null)
104        CaptionChanged(this, EventArgs.Empty);
105    }
106
107    public event EventHandler Changed;
108    protected virtual void OnChanged() {
109      if (InvokeRequired)
110        Invoke((MethodInvoker)OnChanged);
111      else if (Changed != null)
112        Changed(this, EventArgs.Empty);
113    }
114
115    protected virtual void OnShown(ViewShownEventArgs e) {
116    }
117
118    protected virtual void OnHidden(EventArgs e) {
119    }
120
121    internal CloseReason closeReason;
122    internal void OnClosingHelper(object sender, FormClosingEventArgs e) {
123      if (this.closeReason != CloseReason.None)
124        this.OnClosing(new FormClosingEventArgs(this.closeReason, e.Cancel));
125      else
126        this.OnClosing(e);
127
128      this.closeReason = CloseReason.None;
129    }
130
131    protected virtual void OnClosing(FormClosingEventArgs e) {
132    }
133
134    internal void OnClosedHelper(object sender, FormClosedEventArgs e) {
135      if (this.closeReason != CloseReason.None)
136        this.OnClosed(new FormClosedEventArgs(this.closeReason));
137      else
138        this.OnClosed(e);
139
140      Form form = (Form)sender;
141      form.FormClosed -= new FormClosedEventHandler(OnClosedHelper);
142      form.FormClosing -= new FormClosingEventHandler(OnClosingHelper);
143      this.closeReason = CloseReason.None;
144    }
145
146    protected virtual void OnClosed(FormClosedEventArgs e) {
147    }
148
149    private void ViewBase_Load(object sender, EventArgs e) {
150      if (!this.initialized && !this.DesignMode) {
151        this.OnInitialized(e);
152        this.initialized = true;
153      }
154    }
155
156    protected virtual void OnInitialized(EventArgs e) {
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.