1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Data;
|
---|
26 | using System.Drawing;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 |
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.MainForm.WindowsForms {
|
---|
34 | public partial class MainFormBase : Form, IMainForm {
|
---|
35 |
|
---|
36 | private bool initialized;
|
---|
37 | protected MainFormBase()
|
---|
38 | : base() {
|
---|
39 | InitializeComponent();
|
---|
40 | this.views = new Dictionary<IView, Form>();
|
---|
41 | this.userInterfaceItems = new List<IUserInterfaceItem>();
|
---|
42 | this.initialized = false;
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected MainFormBase(Type userInterfaceItemType)
|
---|
46 | : this() {
|
---|
47 | this.userInterfaceItemType = userInterfaceItemType;
|
---|
48 | CreateGUI();
|
---|
49 | }
|
---|
50 |
|
---|
51 | private void MainFormBase_Load(object sender, EventArgs e) {
|
---|
52 | if (!DesignMode) {
|
---|
53 | MainFormManager.RegisterMainForm(this);
|
---|
54 | if (!this.initialized) {
|
---|
55 | this.initialized = true;
|
---|
56 | if (this.Initialized != null)
|
---|
57 | this.Initialized(this, new EventArgs());
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public event EventHandler Initialized;
|
---|
63 |
|
---|
64 | #region IMainForm Members
|
---|
65 | public string Title {
|
---|
66 | get { return this.Text; }
|
---|
67 | set {
|
---|
68 | if (InvokeRequired) {
|
---|
69 | Action<string> action = delegate(string s) { this.Title = s; };
|
---|
70 | Invoke(action, value);
|
---|
71 | } else
|
---|
72 | this.Text = value;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override Cursor Cursor {
|
---|
77 | get { return base.Cursor; }
|
---|
78 | set {
|
---|
79 | if (InvokeRequired) {
|
---|
80 | Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
|
---|
81 | Invoke(action, value);
|
---|
82 | } else
|
---|
83 | base.Cursor = value;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | private Type userInterfaceItemType;
|
---|
88 | public Type UserInterfaceItemType {
|
---|
89 | get { return this.userInterfaceItemType; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | private Dictionary<IView, Form> views;
|
---|
93 | public IEnumerable<IView> Views {
|
---|
94 | get { return views.Keys; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public Form GetForm(IView view) {
|
---|
98 | if (views.ContainsKey(view))
|
---|
99 | return views[view];
|
---|
100 | return null;
|
---|
101 | }
|
---|
102 |
|
---|
103 | private IView activeView;
|
---|
104 | public IView ActiveView {
|
---|
105 | get { return this.activeView; }
|
---|
106 | protected set {
|
---|
107 | if (this.activeView != value) {
|
---|
108 | if (InvokeRequired) {
|
---|
109 | Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
|
---|
110 | Invoke(action, value);
|
---|
111 | } else {
|
---|
112 | this.activeView = value;
|
---|
113 | OnActiveViewChanged();
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | private List<IUserInterfaceItem> userInterfaceItems;
|
---|
120 | protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
|
---|
121 | get { return this.userInterfaceItems; }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public event EventHandler ActiveViewChanged;
|
---|
125 | protected virtual void OnActiveViewChanged() {
|
---|
126 | if (InvokeRequired)
|
---|
127 | Invoke((MethodInvoker)OnActiveViewChanged);
|
---|
128 | else if (ActiveViewChanged != null)
|
---|
129 | ActiveViewChanged(this, new EventArgs());
|
---|
130 | }
|
---|
131 |
|
---|
132 | public event EventHandler<ViewEventArgs> ViewClosed;
|
---|
133 | protected virtual void OnViewClosed(IView view) {
|
---|
134 | if (InvokeRequired) Invoke((Action<IView>)OnViewClosed, view);
|
---|
135 | else if (this.ViewClosed != null) {
|
---|
136 | this.ViewClosed(this, new ViewEventArgs(view));
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | public event EventHandler<ViewShownEventArgs> ViewShown;
|
---|
141 | protected virtual void OnViewShown(IView view, bool firstTimeShown) {
|
---|
142 | if (InvokeRequired) Invoke((Action<IView, bool>)OnViewShown, view, firstTimeShown);
|
---|
143 | else if (this.ViewShown != null) {
|
---|
144 | this.ViewShown(this, new ViewShownEventArgs(view, firstTimeShown));
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | public event EventHandler<ViewEventArgs> ViewHidden;
|
---|
149 | protected virtual void OnViewHidden(IView view) {
|
---|
150 | if (InvokeRequired) Invoke((Action<IView>)OnViewHidden, view);
|
---|
151 | else if (this.ViewHidden != null) {
|
---|
152 | this.ViewHidden(this, new ViewEventArgs(view));
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | public event EventHandler Changed;
|
---|
157 | public void FireMainFormChanged() {
|
---|
158 | OnMainFormChanged();
|
---|
159 | }
|
---|
160 | protected virtual void OnMainFormChanged() {
|
---|
161 | if (InvokeRequired)
|
---|
162 | Invoke((MethodInvoker)OnMainFormChanged);
|
---|
163 | else if (Changed != null)
|
---|
164 | Changed(this, new EventArgs());
|
---|
165 | }
|
---|
166 |
|
---|
167 | protected virtual Form CreateForm(IView view) {
|
---|
168 | throw new NotImplementedException("CreateForm must be implemented in subclasses of MainFormBase.");
|
---|
169 | }
|
---|
170 |
|
---|
171 | public void ShowView(IView view) {
|
---|
172 | if (InvokeRequired) Invoke((Action<IView>)ShowView, view);
|
---|
173 | else {
|
---|
174 | if (!views.Keys.Contains(view)) {
|
---|
175 | Form form = CreateForm(view);
|
---|
176 | views[view] = form;
|
---|
177 | form.Activated += new EventHandler(FormActivated);
|
---|
178 | form.FormClosing += new FormClosingEventHandler(view.OnClosing);
|
---|
179 | form.FormClosing += new FormClosingEventHandler(((ViewBase)view).OnClosingHelper);
|
---|
180 | form.FormClosed += new FormClosedEventHandler(view.OnClosed);
|
---|
181 | form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
|
---|
182 | foreach (IUserInterfaceItem item in UserInterfaceItems)
|
---|
183 | view.Changed += new EventHandler(item.ViewChanged);
|
---|
184 | this.Show(view, true);
|
---|
185 | this.OnViewShown(view, true);
|
---|
186 | } else {
|
---|
187 | this.Show(view, false);
|
---|
188 | this.OnViewShown(view, false);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | protected virtual void Show(IView view, bool firstTimeShown) {
|
---|
194 | }
|
---|
195 |
|
---|
196 | public void HideView(IView view) {
|
---|
197 | if (InvokeRequired) Invoke((Action<IView>)HideView, view);
|
---|
198 | else {
|
---|
199 | if (this.views.ContainsKey(view)) {
|
---|
200 | this.Hide(view);
|
---|
201 | this.OnViewHidden(view);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | protected virtual void Hide(IView view) {
|
---|
207 | }
|
---|
208 |
|
---|
209 | public void CloseView(IView view) {
|
---|
210 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
211 | else {
|
---|
212 | if (this.views.ContainsKey(view)) {
|
---|
213 | this.views[view].Close();
|
---|
214 | this.OnViewClosed(view);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | public void CloseView(IView view, CloseReason closeReason) {
|
---|
220 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
221 | else {
|
---|
222 | if (this.views.ContainsKey(view)) {
|
---|
223 | ((ViewBase)view).closeReason = closeReason;
|
---|
224 | this.CloseView(view);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | public virtual void CloseAllViews() {
|
---|
230 | foreach (IView view in views.Keys.ToArray())
|
---|
231 | CloseView(view);
|
---|
232 | }
|
---|
233 |
|
---|
234 | public virtual void CloseAllViews(CloseReason closeReason) {
|
---|
235 | foreach (IView view in views.Keys.ToArray())
|
---|
236 | CloseView(view, closeReason);
|
---|
237 | }
|
---|
238 | #endregion
|
---|
239 |
|
---|
240 | #region events
|
---|
241 | private void ChildFormClosed(object sender, FormClosedEventArgs e) {
|
---|
242 | Form form = (Form)sender;
|
---|
243 | IView view = GetViewForForm(form);
|
---|
244 |
|
---|
245 | form.Activated -= new EventHandler(FormActivated);
|
---|
246 | form.FormClosing -= new FormClosingEventHandler(view.OnClosing);
|
---|
247 | form.FormClosing -= new FormClosingEventHandler(((ViewBase)view).OnClosingHelper);
|
---|
248 | form.FormClosed -= new FormClosedEventHandler(view.OnClosed);
|
---|
249 | form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
|
---|
250 | foreach (IUserInterfaceItem item in UserInterfaceItems)
|
---|
251 | view.Changed -= new EventHandler(item.ViewChanged);
|
---|
252 |
|
---|
253 | views.Remove(view);
|
---|
254 | this.OnViewClosed(view);
|
---|
255 | if (ActiveView == view)
|
---|
256 | ActiveView = null;
|
---|
257 | }
|
---|
258 |
|
---|
259 | private void FormActivated(object sender, EventArgs e) {
|
---|
260 | this.ActiveView = GetViewForForm((Form)sender);
|
---|
261 | }
|
---|
262 |
|
---|
263 | private IView GetViewForForm(Form form) {
|
---|
264 | return views.Where(x => x.Value == form).Single().Key;
|
---|
265 | }
|
---|
266 | #endregion
|
---|
267 |
|
---|
268 | #region create menu and toolbar
|
---|
269 | protected virtual void CreateGUI() {
|
---|
270 | IEnumerable<IMenuItem> toolStripMenuItems =
|
---|
271 | from mi in ApplicationManager.Manager.GetInstances(userInterfaceItemType)
|
---|
272 | where mi is IMenuItem
|
---|
273 | orderby ((IMenuItem)mi).Position
|
---|
274 | select (IMenuItem)mi;
|
---|
275 | foreach (IMenuItem menuItem in toolStripMenuItems)
|
---|
276 | AddToolStripMenuItem(menuItem);
|
---|
277 |
|
---|
278 | IEnumerable<IToolBarItem> toolStripButtonItems =
|
---|
279 | from bi in ApplicationManager.Manager.GetInstances(userInterfaceItemType)
|
---|
280 | where bi is IToolBarItem
|
---|
281 | orderby ((IToolBarItem)bi).Position
|
---|
282 | select (IToolBarItem)bi;
|
---|
283 | foreach (IToolBarItem toolStripButtonItem in toolStripButtonItems)
|
---|
284 | AddToolStripButtonItem(toolStripButtonItem);
|
---|
285 | }
|
---|
286 |
|
---|
287 | private void AddToolStripMenuItem(IMenuItem menuItem) {
|
---|
288 | if (menuItem is MenuItemBase) {
|
---|
289 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
290 | SetToolStripItemProperties(item, menuItem);
|
---|
291 | ((MenuItemBase)menuItem).ToolStripItem = item;
|
---|
292 | item.ShortcutKeys = ((MenuItemBase)menuItem).ShortCutKeys;
|
---|
293 | item.DisplayStyle = ((MenuItemBase)menuItem).ToolStripItemDisplayStyle;
|
---|
294 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
|
---|
295 | } else if (menuItem is MenuSeparatorItemBase) {
|
---|
296 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | private void AddToolStripButtonItem(IToolBarItem buttonItem) {
|
---|
301 | ToolStripItem item = null;
|
---|
302 |
|
---|
303 | if (buttonItem is ToolBarItemBase) {
|
---|
304 | if (((ToolBarItemBase)buttonItem).IsDropDownButton)
|
---|
305 | item = new ToolStripDropDownButton();
|
---|
306 | else
|
---|
307 | item = new ToolStripButton();
|
---|
308 |
|
---|
309 | SetToolStripItemProperties(item, buttonItem);
|
---|
310 | item.DisplayStyle = ((ToolBarItemBase)buttonItem).ToolStripItemDisplayStyle;
|
---|
311 | ((ToolBarItemBase)buttonItem).ToolStripItem = item;
|
---|
312 | } else if (buttonItem is IToolBarSeparatorItem)
|
---|
313 | item = new ToolStripSeparator();
|
---|
314 |
|
---|
315 | this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
|
---|
316 | }
|
---|
317 |
|
---|
318 | private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
|
---|
319 | ToolStripDropDownItem parent = null;
|
---|
320 | foreach (string s in structure) {
|
---|
321 | if (parentItems.ContainsKey(s))
|
---|
322 | parent = (ToolStripDropDownItem)parentItems[s];
|
---|
323 | else {
|
---|
324 | parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
|
---|
325 | parentItems.Add(parent);
|
---|
326 | }
|
---|
327 | parentItems = parent.DropDownItems;
|
---|
328 | }
|
---|
329 | parentItems.Add(item);
|
---|
330 | }
|
---|
331 |
|
---|
332 | private void SetToolStripItemProperties(ToolStripItem toolStripItem, IUserInterfaceItem userInterfaceItem) {
|
---|
333 | toolStripItem.Name = userInterfaceItem.Name;
|
---|
334 | toolStripItem.Text = userInterfaceItem.Name;
|
---|
335 | toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
|
---|
336 | toolStripItem.Tag = userInterfaceItem;
|
---|
337 | toolStripItem.Image = userInterfaceItem.Image;
|
---|
338 | this.ActiveViewChanged += new EventHandler(userInterfaceItem.ActiveViewChanged);
|
---|
339 | this.Changed += new EventHandler(userInterfaceItem.MainFormChanged);
|
---|
340 | this.Initialized += new EventHandler(userInterfaceItem.MainFormInitialized);
|
---|
341 | toolStripItem.Click += new EventHandler(ToolStripItemClicked);
|
---|
342 | this.userInterfaceItems.Add(userInterfaceItem);
|
---|
343 | }
|
---|
344 |
|
---|
345 | private void ToolStripItemClicked(object sender, EventArgs e) {
|
---|
346 | System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
|
---|
347 | ((IUserInterfaceItem)item.Tag).Execute();
|
---|
348 | }
|
---|
349 | #endregion
|
---|
350 | }
|
---|
351 | }
|
---|