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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Reflection;
|
---|
25 | using System.Windows.Forms;
|
---|
26 |
|
---|
27 | namespace 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 | if (ViewAttribute.HasHelpResourcePath((this.GetType())))
|
---|
42 | helpLabel.Visible = true;
|
---|
43 | }
|
---|
44 |
|
---|
45 | private string caption;
|
---|
46 | public string Caption {
|
---|
47 | get { return caption; }
|
---|
48 | set {
|
---|
49 | if (InvokeRequired) {
|
---|
50 | Action<string> action = delegate(string s) { this.Caption = s; };
|
---|
51 | Invoke(action, value);
|
---|
52 | } else {
|
---|
53 | if (value != caption) {
|
---|
54 | caption = value;
|
---|
55 | OnCaptionChanged();
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private bool readOnly;
|
---|
62 | public virtual bool ReadOnly {
|
---|
63 | get { return this.readOnly; }
|
---|
64 | set {
|
---|
65 | if (InvokeRequired) {
|
---|
66 | Action<bool> action = delegate(bool b) { this.ReadOnly = b; };
|
---|
67 | Invoke(action, value);
|
---|
68 | } else {
|
---|
69 | if (value != readOnly) {
|
---|
70 | this.readOnly = value;
|
---|
71 | OnReadOnlyChanged();
|
---|
72 | PropertyInfo prop = typeof(IView).GetProperty("ReadOnly");
|
---|
73 | PropagateStateChanges(this, typeof(IView), prop);
|
---|
74 | SetEnabledStateOfControls();
|
---|
75 | OnChanged();
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public new bool Enabled {
|
---|
82 | get { return base.Enabled; }
|
---|
83 | set {
|
---|
84 | if (base.Enabled != value) {
|
---|
85 | this.SuspendRepaint();
|
---|
86 | base.Enabled = value;
|
---|
87 | this.ResumeRepaint(true);
|
---|
88 | OnChanged();
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | bool IView.Enabled {
|
---|
94 | get { return Enabled; }
|
---|
95 | set { Enabled = value; }
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected override void OnEnabledChanged(EventArgs e) {
|
---|
99 | base.OnEnabledChanged(e);
|
---|
100 | if (Enabled) SetEnabledStateOfControls();
|
---|
101 | }
|
---|
102 |
|
---|
103 | /// <summary>
|
---|
104 | /// This method is called if the ReadyOnly property of the View changes to update the controls of the view.
|
---|
105 | /// </summary>
|
---|
106 | protected virtual void SetEnabledStateOfControls() {
|
---|
107 | }
|
---|
108 |
|
---|
109 | private bool isShown;
|
---|
110 | public bool IsShown {
|
---|
111 | get { return this.isShown; }
|
---|
112 | private set { this.isShown = value; }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public new void Show() {
|
---|
116 | MainForm mainform = MainFormManager.GetMainForm<MainForm>();
|
---|
117 | bool firstTimeShown = mainform.GetForm(this) == null;
|
---|
118 |
|
---|
119 | this.IsShown = true;
|
---|
120 | mainform.ShowView(this);
|
---|
121 | if (firstTimeShown) {
|
---|
122 | Form form = mainform.GetForm(this);
|
---|
123 | form.FormClosed += new FormClosedEventHandler(OnClosedHelper);
|
---|
124 | form.FormClosing += new FormClosingEventHandler(OnClosingHelper);
|
---|
125 | }
|
---|
126 | this.OnShown(new ViewShownEventArgs(this, firstTimeShown));
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void Close() {
|
---|
130 | MainForm mainform = MainFormManager.GetMainForm<MainForm>();
|
---|
131 | Form form = mainform.GetForm(this);
|
---|
132 | if (form != null) {
|
---|
133 | this.IsShown = false;
|
---|
134 | mainform.CloseView(this);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | public void Close(CloseReason closeReason) {
|
---|
139 | MainForm mainform = MainFormManager.GetMainForm<MainForm>();
|
---|
140 | Form form = mainform.GetForm(this);
|
---|
141 | if (form != null) {
|
---|
142 | this.IsShown = false;
|
---|
143 | mainform.CloseView(this, closeReason);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public new void Hide() {
|
---|
148 | this.IsShown = false;
|
---|
149 | MainFormManager.GetMainForm<MainForm>().HideView(this);
|
---|
150 | this.OnHidden(EventArgs.Empty);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public event EventHandler CaptionChanged;
|
---|
154 | protected virtual void OnCaptionChanged() {
|
---|
155 | if (InvokeRequired)
|
---|
156 | Invoke((MethodInvoker)OnCaptionChanged);
|
---|
157 | else {
|
---|
158 | EventHandler handler = CaptionChanged;
|
---|
159 | if (handler != null)
|
---|
160 | handler(this, EventArgs.Empty);
|
---|
161 | }
|
---|
162 | }
|
---|
163 | public event EventHandler ReadOnlyChanged;
|
---|
164 | protected virtual void OnReadOnlyChanged() {
|
---|
165 | if (InvokeRequired)
|
---|
166 | Invoke((MethodInvoker)OnReadOnlyChanged);
|
---|
167 | else {
|
---|
168 | EventHandler handler = ReadOnlyChanged;
|
---|
169 | if (handler != null)
|
---|
170 | handler(this, EventArgs.Empty);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | protected virtual void PropagateStateChanges(Control control, Type type, PropertyInfo propertyInfo) {
|
---|
174 | if (!type.GetProperties().Contains(propertyInfo))
|
---|
175 | throw new ArgumentException("The specified type " + type + "implement the property " + propertyInfo.Name + ".");
|
---|
176 | if (!type.IsAssignableFrom(this.GetType()))
|
---|
177 | throw new ArgumentException("The specified type " + type + "must be the same or a base class / interface of this object.");
|
---|
178 | if (!propertyInfo.CanWrite)
|
---|
179 | throw new ArgumentException("The specified property " + propertyInfo.Name + " must have a setter.");
|
---|
180 |
|
---|
181 | foreach (Control c in control.Controls) {
|
---|
182 | Type controlType = c.GetType();
|
---|
183 | PropertyInfo controlPropertyInfo = controlType.GetProperty(propertyInfo.Name, propertyInfo.PropertyType);
|
---|
184 | if (type.IsAssignableFrom(controlType) && controlPropertyInfo != null) {
|
---|
185 | var thisValue = propertyInfo.GetValue(this, null);
|
---|
186 | controlPropertyInfo.SetValue(c, thisValue, null);
|
---|
187 | } else PropagateStateChanges(c, type, propertyInfo);
|
---|
188 | }
|
---|
189 | }
|
---|
190 | public event EventHandler Changed;
|
---|
191 | protected virtual void OnChanged() {
|
---|
192 | if (InvokeRequired)
|
---|
193 | Invoke((MethodInvoker)OnChanged);
|
---|
194 | else {
|
---|
195 | EventHandler handler = Changed;
|
---|
196 | if (handler != null)
|
---|
197 | handler(this, EventArgs.Empty);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | internal protected virtual void OnShown(ViewShownEventArgs e) {
|
---|
202 | }
|
---|
203 |
|
---|
204 | internal protected virtual void OnHidden(EventArgs e) {
|
---|
205 | }
|
---|
206 |
|
---|
207 | private CloseReason closeReason;
|
---|
208 | internal CloseReason CloseReason {
|
---|
209 | get { return this.closeReason; }
|
---|
210 | set { this.closeReason = value; }
|
---|
211 | }
|
---|
212 |
|
---|
213 | internal void OnClosingHelper(object sender, FormClosingEventArgs e) {
|
---|
214 | FormClosingEventArgs eventArgs = new FormClosingEventArgs(this.closeReason, e.Cancel);
|
---|
215 | if (this.closeReason != CloseReason.None) {
|
---|
216 | this.OnClosing(eventArgs);
|
---|
217 | if (eventArgs.Cancel != e.Cancel)
|
---|
218 | e.Cancel = eventArgs.Cancel;
|
---|
219 | } else
|
---|
220 | this.OnClosing(e);
|
---|
221 | this.closeReason = CloseReason.None;
|
---|
222 | }
|
---|
223 |
|
---|
224 | internal protected virtual void OnClosing(FormClosingEventArgs e) {
|
---|
225 | }
|
---|
226 |
|
---|
227 | internal void OnClosedHelper(object sender, FormClosedEventArgs e) {
|
---|
228 | if (this.closeReason != CloseReason.None)
|
---|
229 | this.OnClosed(new FormClosedEventArgs(this.closeReason));
|
---|
230 | else
|
---|
231 | this.OnClosed(e);
|
---|
232 |
|
---|
233 | Form form = (Form)sender;
|
---|
234 | form.FormClosed -= new FormClosedEventHandler(OnClosedHelper);
|
---|
235 | form.FormClosing -= new FormClosingEventHandler(OnClosingHelper);
|
---|
236 | this.closeReason = CloseReason.None;
|
---|
237 | }
|
---|
238 |
|
---|
239 | internal protected virtual void OnClosed(FormClosedEventArgs e) {
|
---|
240 | }
|
---|
241 |
|
---|
242 | private void View_Load(object sender, EventArgs e) {
|
---|
243 | if (!this.initialized && !this.DesignMode) {
|
---|
244 | this.OnInitialized(e);
|
---|
245 | this.initialized = true;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | protected virtual void OnInitialized(EventArgs e) {
|
---|
250 | SetEnabledStateOfControls();
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | public void SuspendRepaint() {
|
---|
255 | if (InvokeRequired)
|
---|
256 | Invoke((MethodInvoker)SuspendRepaint);
|
---|
257 | else
|
---|
258 | ((Control)this).SuspendRepaint();
|
---|
259 | }
|
---|
260 | public void ResumeRepaint(bool refresh) {
|
---|
261 | if (InvokeRequired)
|
---|
262 | Invoke((Action<bool>)ResumeRepaint, refresh);
|
---|
263 | else
|
---|
264 | ((Control)this).ResumeRepaint(refresh);
|
---|
265 | }
|
---|
266 |
|
---|
267 | protected void helpLabel_DoubleClick(object sender, EventArgs e) {
|
---|
268 | using (InfoBox dialog = new InfoBox("Help for " + ViewAttribute.GetViewName(this.GetType()), ViewAttribute.GetHelpResourcePath(this.GetType()), this)) {
|
---|
269 | dialog.ShowDialog(this);
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|