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