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