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.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 | using System.Collections;
|
---|
33 | using WeifenLuo.WinFormsUI.Docking;
|
---|
34 | using HeuristicLab.Common;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.MainForm.WindowsForms {
|
---|
37 | public partial class MainForm : Form, IMainForm {
|
---|
38 | private bool initialized;
|
---|
39 |
|
---|
40 | protected MainForm()
|
---|
41 | : base() {
|
---|
42 | InitializeComponent();
|
---|
43 | this.views = new Dictionary<IView, Form>();
|
---|
44 | this.userInterfaceItems = new List<IUserInterfaceItem>();
|
---|
45 | this.initialized = false;
|
---|
46 | this.showContentInViewHost = false;
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected MainForm(Type userInterfaceItemType)
|
---|
50 | : this() {
|
---|
51 | this.userInterfaceItemType = userInterfaceItemType;
|
---|
52 | }
|
---|
53 |
|
---|
54 | #region properties
|
---|
55 | private bool showContentInViewHost;
|
---|
56 | public bool ShowContentInViewHost {
|
---|
57 | get { return this.showContentInViewHost; }
|
---|
58 | set { this.showContentInViewHost = value; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public string Title {
|
---|
62 | get { return this.Text; }
|
---|
63 | set {
|
---|
64 | if (InvokeRequired) {
|
---|
65 | Action<string> action = delegate(string s) { this.Title = s; };
|
---|
66 | Invoke(action, value);
|
---|
67 | } else
|
---|
68 | this.Text = value;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override Cursor Cursor {
|
---|
73 | get { return base.Cursor; }
|
---|
74 | set {
|
---|
75 | if (InvokeRequired) {
|
---|
76 | Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
|
---|
77 | Invoke(action, value);
|
---|
78 | } else
|
---|
79 | base.Cursor = value;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | private Type userInterfaceItemType;
|
---|
84 | public Type UserInterfaceItemType {
|
---|
85 | get { return this.userInterfaceItemType; }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private Dictionary<IView, Form> views;
|
---|
89 | public IEnumerable<IView> Views {
|
---|
90 | get { return views.Keys; }
|
---|
91 | }
|
---|
92 |
|
---|
93 | private IView activeView;
|
---|
94 | public IView ActiveView {
|
---|
95 | get { return this.activeView; }
|
---|
96 | protected set {
|
---|
97 | if (this.activeView != value) {
|
---|
98 | if (InvokeRequired) {
|
---|
99 | Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
|
---|
100 | Invoke(action, value);
|
---|
101 | } else {
|
---|
102 | this.activeView = value;
|
---|
103 | OnActiveViewChanged();
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | private List<IUserInterfaceItem> userInterfaceItems;
|
---|
110 | protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
|
---|
111 | get { return this.userInterfaceItems; }
|
---|
112 | }
|
---|
113 | #endregion
|
---|
114 |
|
---|
115 | #region events
|
---|
116 | public event EventHandler ActiveViewChanged;
|
---|
117 | protected virtual void OnActiveViewChanged() {
|
---|
118 | if (InvokeRequired)
|
---|
119 | Invoke((MethodInvoker)OnActiveViewChanged);
|
---|
120 | else if (ActiveViewChanged != null)
|
---|
121 | ActiveViewChanged(this, EventArgs.Empty);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public event EventHandler<ViewEventArgs> ViewClosed;
|
---|
125 | protected virtual void OnViewClosed(IView view) {
|
---|
126 | if (InvokeRequired) Invoke((Action<IView>)OnViewClosed, view);
|
---|
127 | else {
|
---|
128 | EventHandler<ViewEventArgs> handler = ViewClosed;
|
---|
129 | if (handler != null)
|
---|
130 | handler(this, new ViewEventArgs(view));
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | public event EventHandler<ViewShownEventArgs> ViewShown;
|
---|
135 | protected virtual void OnViewShown(IView view, bool firstTimeShown) {
|
---|
136 | if (InvokeRequired) Invoke((Action<IView, bool>)OnViewShown, view, firstTimeShown);
|
---|
137 | else {
|
---|
138 | EventHandler<ViewShownEventArgs> handler = ViewShown;
|
---|
139 | if (handler != null)
|
---|
140 | handler(this, new ViewShownEventArgs(view, firstTimeShown));
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | public event EventHandler<ViewEventArgs> ViewHidden;
|
---|
145 | protected virtual void OnViewHidden(IView view) {
|
---|
146 | if (InvokeRequired) Invoke((Action<IView>)OnViewHidden, view);
|
---|
147 | else {
|
---|
148 | EventHandler<ViewEventArgs> handler = ViewHidden;
|
---|
149 | if (handler != null)
|
---|
150 | handler(this, new ViewEventArgs(view));
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | public event EventHandler Changed;
|
---|
155 | protected void OnChanged() {
|
---|
156 | if (InvokeRequired)
|
---|
157 | Invoke((MethodInvoker)OnChanged);
|
---|
158 | else {
|
---|
159 | EventHandler handler = Changed;
|
---|
160 | if (handler != null)
|
---|
161 | Changed(this, EventArgs.Empty);
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | private void MainFormBase_Load(object sender, EventArgs e) {
|
---|
166 | if (!DesignMode) {
|
---|
167 | MainFormManager.RegisterMainForm(this);
|
---|
168 | if (!this.initialized) {
|
---|
169 | this.initialized = true;
|
---|
170 | this.OnInitialized(EventArgs.Empty);
|
---|
171 | }
|
---|
172 | this.CreateGUI();
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | protected virtual void OnInitialized(EventArgs e) {
|
---|
177 | }
|
---|
178 |
|
---|
179 | private void FormActivated(object sender, EventArgs e) {
|
---|
180 | this.ActiveView = GetView((Form)sender);
|
---|
181 | }
|
---|
182 |
|
---|
183 | protected override void OnFormClosing(FormClosingEventArgs e) {
|
---|
184 | foreach (KeyValuePair<IView, Form> pair in this.views) {
|
---|
185 | DockForm dockForm = pair.Value as DockForm;
|
---|
186 | View view = pair.Key as View;
|
---|
187 | if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
|
---|
188 | view.CloseReason = CloseReason.ApplicationExitCall;
|
---|
189 | view.OnClosingHelper(dockForm, e);
|
---|
190 | }
|
---|
191 | }
|
---|
192 | base.OnFormClosing(e);
|
---|
193 | }
|
---|
194 |
|
---|
195 | protected override void OnFormClosed(FormClosedEventArgs e) {
|
---|
196 | foreach (KeyValuePair<IView, Form> pair in this.views.ToList()) {
|
---|
197 | DockForm dockForm = pair.Value as DockForm;
|
---|
198 | View view = pair.Key as View;
|
---|
199 | if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
|
---|
200 | view.CloseReason = CloseReason.ApplicationExitCall;
|
---|
201 | view.OnClosedHelper(dockForm, e);
|
---|
202 | dockForm.Close();
|
---|
203 | }
|
---|
204 | }
|
---|
205 | base.OnFormClosed(e);
|
---|
206 | }
|
---|
207 | #endregion
|
---|
208 |
|
---|
209 | #region create, get, show, hide, close views
|
---|
210 | protected virtual Form CreateForm(IView view) {
|
---|
211 | throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
|
---|
212 | }
|
---|
213 |
|
---|
214 | internal Form GetForm(IView view) {
|
---|
215 | if (views.ContainsKey(view))
|
---|
216 | return views[view];
|
---|
217 | return null;
|
---|
218 | }
|
---|
219 | protected IView GetView(Form form) {
|
---|
220 | return views.Where(x => x.Value == form).Single().Key;
|
---|
221 | }
|
---|
222 |
|
---|
223 | public IContentView ShowContent(IContent content) {
|
---|
224 | Type viewType = MainFormManager.GetDefaultViewType(content.GetType());
|
---|
225 | if (viewType != null)
|
---|
226 | return ShowContent(content, viewType);
|
---|
227 | return null;
|
---|
228 | }
|
---|
229 |
|
---|
230 | internal IContentView ShowContent(IContent content, Type viewType) {
|
---|
231 | if (viewType == null)
|
---|
232 | throw new ArgumentNullException("Could not create view of type null");
|
---|
233 |
|
---|
234 | IContentView view;
|
---|
235 | if (this.ShowContentInViewHost) {
|
---|
236 | ViewHost viewHost = new ViewHost();
|
---|
237 | viewHost.ViewType = viewType;
|
---|
238 | view = viewHost;
|
---|
239 | } else
|
---|
240 | view = MainFormManager.CreateView(viewType);
|
---|
241 |
|
---|
242 | if (view != null) {
|
---|
243 | view.Show();
|
---|
244 | view.Content = content;
|
---|
245 | }
|
---|
246 | return view;
|
---|
247 | }
|
---|
248 |
|
---|
249 | internal void ShowView(IView view) {
|
---|
250 | if (InvokeRequired) Invoke((Action<IView>)ShowView, view);
|
---|
251 | else {
|
---|
252 | Form form = GetForm(view);
|
---|
253 | bool firstTimeShown = form == null;
|
---|
254 | if (firstTimeShown) {
|
---|
255 | form = CreateForm(view);
|
---|
256 | form.Activated += new EventHandler(FormActivated);
|
---|
257 | form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
|
---|
258 | view.Changed += new EventHandler(View_Changed);
|
---|
259 | views[view] = form;
|
---|
260 | }
|
---|
261 | this.ShowView(view, firstTimeShown);
|
---|
262 | this.OnViewShown(view, firstTimeShown);
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | private void View_Changed(object sender, EventArgs e) {
|
---|
267 | IView view = (IView)sender;
|
---|
268 | if (view == this.ActiveView)
|
---|
269 | this.OnActiveViewChanged();
|
---|
270 | this.OnChanged();
|
---|
271 | }
|
---|
272 |
|
---|
273 | protected virtual void ShowView(IView view, bool firstTimeShown) {
|
---|
274 | }
|
---|
275 |
|
---|
276 | internal void HideView(IView view) {
|
---|
277 | if (InvokeRequired) Invoke((Action<IView>)HideView, view);
|
---|
278 | else {
|
---|
279 | this.Hide(view);
|
---|
280 | if (this.activeView == view)
|
---|
281 | this.ActiveView = null;
|
---|
282 | this.OnViewHidden(view);
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | protected virtual void Hide(IView view) {
|
---|
287 | }
|
---|
288 |
|
---|
289 | private void ChildFormClosed(object sender, FormClosedEventArgs e) {
|
---|
290 | Form form = (Form)sender;
|
---|
291 | IView view = GetView(form);
|
---|
292 |
|
---|
293 | view.Changed -= new EventHandler(View_Changed);
|
---|
294 | form.Activated -= new EventHandler(FormActivated);
|
---|
295 | form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
|
---|
296 |
|
---|
297 | views.Remove(view);
|
---|
298 | this.OnViewClosed(view);
|
---|
299 | if (ActiveView == view)
|
---|
300 | ActiveView = null;
|
---|
301 | }
|
---|
302 |
|
---|
303 | internal void CloseView(IView view) {
|
---|
304 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
305 | else if (views.ContainsKey(view)) {
|
---|
306 | this.views[view].Close();
|
---|
307 | this.OnViewClosed(view);
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | internal void CloseView(IView view, CloseReason closeReason) {
|
---|
312 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
313 | else if (views.ContainsKey(view)) {
|
---|
314 | ((View)view).CloseReason = closeReason;
|
---|
315 | this.CloseView(view);
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | public void CloseAllViews() {
|
---|
320 | foreach (IView view in views.Keys.ToArray())
|
---|
321 | CloseView(view);
|
---|
322 | }
|
---|
323 |
|
---|
324 | public void CloseAllViews(CloseReason closeReason) {
|
---|
325 | foreach (IView view in views.Keys.ToArray())
|
---|
326 | CloseView(view, closeReason);
|
---|
327 | }
|
---|
328 | #endregion
|
---|
329 |
|
---|
330 | #region create menu and toolbar
|
---|
331 | private void CreateGUI() {
|
---|
332 | IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
|
---|
333 |
|
---|
334 | IEnumerable<IPositionableUserInterfaceItem> toolStripMenuItems =
|
---|
335 | from mi in allUserInterfaceItems
|
---|
336 | where (mi is IPositionableUserInterfaceItem) &&
|
---|
337 | (mi is IMenuItem || mi is IMenuSeparatorItem)
|
---|
338 | orderby ((IPositionableUserInterfaceItem)mi).Position
|
---|
339 | select (IPositionableUserInterfaceItem)mi;
|
---|
340 |
|
---|
341 | foreach (IPositionableUserInterfaceItem menuItem in toolStripMenuItems) {
|
---|
342 | if (menuItem is IMenuItem)
|
---|
343 | AddToolStripMenuItem((IMenuItem)menuItem);
|
---|
344 | else if (menuItem is IMenuSeparatorItem)
|
---|
345 | AddToolStripMenuItem((IMenuSeparatorItem)menuItem);
|
---|
346 | }
|
---|
347 |
|
---|
348 | IEnumerable<IPositionableUserInterfaceItem> toolStripButtonItems =
|
---|
349 | from bi in allUserInterfaceItems
|
---|
350 | where (bi is IPositionableUserInterfaceItem) &&
|
---|
351 | (bi is IToolBarItem || bi is IToolBarSeparatorItem)
|
---|
352 | orderby ((IPositionableUserInterfaceItem)bi).Position
|
---|
353 | select (IPositionableUserInterfaceItem)bi;
|
---|
354 |
|
---|
355 | foreach (IPositionableUserInterfaceItem toolStripButtonItem in toolStripButtonItems) {
|
---|
356 | if (toolStripButtonItem is IToolBarItem)
|
---|
357 | AddToolStripButtonItem((IToolBarItem)toolStripButtonItem);
|
---|
358 | else if (toolStripButtonItem is IToolBarSeparatorItem)
|
---|
359 | AddToolStripButtonItem((IToolBarSeparatorItem)toolStripButtonItem);
|
---|
360 | }
|
---|
361 |
|
---|
362 | this.AdditionalCreationOfGuiElements();
|
---|
363 | }
|
---|
364 |
|
---|
365 | protected virtual void AdditionalCreationOfGuiElements() {
|
---|
366 | }
|
---|
367 |
|
---|
368 | private void AddToolStripMenuItem(IMenuItem menuItem) {
|
---|
369 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
370 | this.SetToolStripItemProperties(item, menuItem);
|
---|
371 | if (menuItem is MenuItem) {
|
---|
372 | MenuItem menuItemBase = (MenuItem)menuItem;
|
---|
373 | menuItemBase.ToolStripItem = item;
|
---|
374 | item.ShortcutKeys = menuItemBase.ShortCutKeys;
|
---|
375 | item.DisplayStyle = menuItemBase.ToolStripItemDisplayStyle;
|
---|
376 | }
|
---|
377 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
|
---|
378 | }
|
---|
379 |
|
---|
380 | private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
|
---|
381 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
|
---|
382 | }
|
---|
383 |
|
---|
384 | private void AddToolStripButtonItem(IToolBarItem buttonItem) {
|
---|
385 | ToolStripItem item = new ToolStripButton();
|
---|
386 | if (buttonItem is ToolBarItem) {
|
---|
387 | ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
|
---|
388 | if (buttonItemBase.IsDropDownButton)
|
---|
389 | item = new ToolStripDropDownButton();
|
---|
390 |
|
---|
391 | item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
|
---|
392 | buttonItemBase.ToolStripItem = item;
|
---|
393 | }
|
---|
394 |
|
---|
395 | this.SetToolStripItemProperties(item, buttonItem);
|
---|
396 | this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
|
---|
397 | }
|
---|
398 |
|
---|
399 | private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
|
---|
400 | this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
|
---|
401 | }
|
---|
402 |
|
---|
403 | private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
|
---|
404 | ToolStripDropDownItem parent = null;
|
---|
405 | foreach (string s in structure) {
|
---|
406 | if (parentItems.ContainsKey(s))
|
---|
407 | parent = (ToolStripDropDownItem)parentItems[s];
|
---|
408 | else {
|
---|
409 | parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
|
---|
410 | parentItems.Add(parent);
|
---|
411 | }
|
---|
412 | parentItems = parent.DropDownItems;
|
---|
413 | }
|
---|
414 | parentItems.Add(item);
|
---|
415 | }
|
---|
416 |
|
---|
417 | private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
|
---|
418 | toolStripItem.Name = userInterfaceItem.Name;
|
---|
419 | toolStripItem.Text = userInterfaceItem.Name;
|
---|
420 | toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
|
---|
421 | toolStripItem.Tag = userInterfaceItem;
|
---|
422 | toolStripItem.Image = userInterfaceItem.Image;
|
---|
423 | toolStripItem.Click += new EventHandler(ToolStripItemClicked);
|
---|
424 | this.userInterfaceItems.Add(userInterfaceItem);
|
---|
425 | }
|
---|
426 |
|
---|
427 | private void ToolStripItemClicked(object sender, EventArgs e) {
|
---|
428 | System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
|
---|
429 | ((IActionUserInterfaceItem)item.Tag).Execute();
|
---|
430 | }
|
---|
431 | #endregion
|
---|
432 | }
|
---|
433 | }
|
---|