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(ViewChanged);
|
---|
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 FireMainFormChanged() {
|
---|
159 | if (InvokeRequired)
|
---|
160 | Invoke((MethodInvoker)FireMainFormChanged);
|
---|
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 ViewChanged(object sender, EventArgs e) {
|
---|
257 | IView view = (IView)sender;
|
---|
258 | if (view == this.ActiveView)
|
---|
259 | this.OnActiveViewChanged();
|
---|
260 | }
|
---|
261 |
|
---|
262 | protected virtual void ShowView(IView view, bool firstTimeShown) {
|
---|
263 | }
|
---|
264 |
|
---|
265 | internal void HideView(IView view) {
|
---|
266 | if (InvokeRequired) Invoke((Action<IView>)HideView, view);
|
---|
267 | else {
|
---|
268 | IView internalView = this.GetView(view);
|
---|
269 | if (internalView != null && this.views.ContainsKey(internalView)) {
|
---|
270 | this.Hide(internalView);
|
---|
271 | if (this.activeView == internalView)
|
---|
272 | this.ActiveView = null;
|
---|
273 | this.OnViewHidden(internalView);
|
---|
274 | }
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | protected virtual void Hide(IView view) {
|
---|
279 | }
|
---|
280 |
|
---|
281 | private void ChildFormClosed(object sender, FormClosedEventArgs e) {
|
---|
282 | Form form = (Form)sender;
|
---|
283 | IView view = GetView(form);
|
---|
284 |
|
---|
285 | view.Changed -= new EventHandler(ViewChanged);
|
---|
286 | form.Activated -= new EventHandler(FormActivated);
|
---|
287 | form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
|
---|
288 |
|
---|
289 | views.Remove(view);
|
---|
290 | this.OnViewClosed(view);
|
---|
291 | if (ActiveView == view)
|
---|
292 | ActiveView = null;
|
---|
293 | }
|
---|
294 |
|
---|
295 | internal void CloseView(IView view) {
|
---|
296 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
297 | else {
|
---|
298 | IView internalView = GetView(view);
|
---|
299 | if (internalView != null && this.views.ContainsKey(internalView)) {
|
---|
300 | this.views[internalView].Close();
|
---|
301 | this.OnViewClosed(internalView);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | internal void CloseView(IView view, CloseReason closeReason) {
|
---|
307 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
308 | else {
|
---|
309 | IView internalView = GetView(view);
|
---|
310 | if (internalView != null && this.views.ContainsKey(internalView)) {
|
---|
311 | ((View)internalView).CloseReason = closeReason;
|
---|
312 | this.CloseView(internalView);
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | public void CloseAllViews() {
|
---|
318 | foreach (IView view in views.Keys.ToArray())
|
---|
319 | CloseView(view);
|
---|
320 | }
|
---|
321 |
|
---|
322 | public void CloseAllViews(CloseReason closeReason) {
|
---|
323 | foreach (IView view in views.Keys.ToArray())
|
---|
324 | CloseView(view, closeReason);
|
---|
325 | }
|
---|
326 | #endregion
|
---|
327 |
|
---|
328 | #region create menu and toolbar
|
---|
329 | private void CreateGUI() {
|
---|
330 | IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
|
---|
331 |
|
---|
332 | IEnumerable<IPositionableUserInterfaceItem> toolStripMenuItems =
|
---|
333 | from mi in allUserInterfaceItems
|
---|
334 | where (mi is IPositionableUserInterfaceItem) &&
|
---|
335 | (mi is IMenuItem || mi is IMenuSeparatorItem)
|
---|
336 | orderby ((IPositionableUserInterfaceItem)mi).Position
|
---|
337 | select (IPositionableUserInterfaceItem)mi;
|
---|
338 |
|
---|
339 | foreach (IPositionableUserInterfaceItem menuItem in toolStripMenuItems) {
|
---|
340 | if (menuItem is IMenuItem)
|
---|
341 | AddToolStripMenuItem((IMenuItem)menuItem);
|
---|
342 | else if (menuItem is IMenuSeparatorItem)
|
---|
343 | AddToolStripMenuItem((IMenuSeparatorItem)menuItem);
|
---|
344 | }
|
---|
345 |
|
---|
346 | IEnumerable<IPositionableUserInterfaceItem> toolStripButtonItems =
|
---|
347 | from bi in allUserInterfaceItems
|
---|
348 | where (bi is IPositionableUserInterfaceItem) &&
|
---|
349 | (bi is IToolBarItem || bi is IToolBarSeparatorItem)
|
---|
350 | orderby ((IPositionableUserInterfaceItem)bi).Position
|
---|
351 | select (IPositionableUserInterfaceItem)bi;
|
---|
352 |
|
---|
353 | foreach (IPositionableUserInterfaceItem toolStripButtonItem in toolStripButtonItems) {
|
---|
354 | if (toolStripButtonItem is IToolBarItem)
|
---|
355 | AddToolStripButtonItem((IToolBarItem)toolStripButtonItem);
|
---|
356 | else if (toolStripButtonItem is IToolBarSeparatorItem)
|
---|
357 | AddToolStripButtonItem((IToolBarSeparatorItem)toolStripButtonItem);
|
---|
358 | }
|
---|
359 |
|
---|
360 | this.AdditionalCreationOfGuiElements();
|
---|
361 | }
|
---|
362 |
|
---|
363 | protected virtual void AdditionalCreationOfGuiElements() {
|
---|
364 | }
|
---|
365 |
|
---|
366 | private void AddToolStripMenuItem(IMenuItem menuItem) {
|
---|
367 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
368 | this.SetToolStripItemProperties(item, menuItem);
|
---|
369 | if (menuItem is MenuItem) {
|
---|
370 | MenuItem menuItemBase = (MenuItem)menuItem;
|
---|
371 | menuItemBase.ToolStripItem = item;
|
---|
372 | item.ShortcutKeys = menuItemBase.ShortCutKeys;
|
---|
373 | item.DisplayStyle = menuItemBase.ToolStripItemDisplayStyle;
|
---|
374 | }
|
---|
375 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
|
---|
376 | }
|
---|
377 |
|
---|
378 | private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
|
---|
379 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
|
---|
380 | }
|
---|
381 |
|
---|
382 | private void AddToolStripButtonItem(IToolBarItem buttonItem) {
|
---|
383 | ToolStripItem item = new ToolStripButton();
|
---|
384 | if (buttonItem is ToolBarItem) {
|
---|
385 | ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
|
---|
386 | if (buttonItemBase.IsDropDownButton)
|
---|
387 | item = new ToolStripDropDownButton();
|
---|
388 |
|
---|
389 | item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
|
---|
390 | buttonItemBase.ToolStripItem = item;
|
---|
391 | }
|
---|
392 |
|
---|
393 | this.SetToolStripItemProperties(item, buttonItem);
|
---|
394 | this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
|
---|
395 | }
|
---|
396 |
|
---|
397 | private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
|
---|
398 | this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
|
---|
399 | }
|
---|
400 |
|
---|
401 | private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
|
---|
402 | ToolStripDropDownItem parent = null;
|
---|
403 | foreach (string s in structure) {
|
---|
404 | if (parentItems.ContainsKey(s))
|
---|
405 | parent = (ToolStripDropDownItem)parentItems[s];
|
---|
406 | else {
|
---|
407 | parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
|
---|
408 | parentItems.Add(parent);
|
---|
409 | }
|
---|
410 | parentItems = parent.DropDownItems;
|
---|
411 | }
|
---|
412 | parentItems.Add(item);
|
---|
413 | }
|
---|
414 |
|
---|
415 | private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
|
---|
416 | toolStripItem.Name = userInterfaceItem.Name;
|
---|
417 | toolStripItem.Text = userInterfaceItem.Name;
|
---|
418 | toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
|
---|
419 | toolStripItem.Tag = userInterfaceItem;
|
---|
420 | toolStripItem.Image = userInterfaceItem.Image;
|
---|
421 | toolStripItem.Click += new EventHandler(ToolStripItemClicked);
|
---|
422 | this.userInterfaceItems.Add(userInterfaceItem);
|
---|
423 | }
|
---|
424 |
|
---|
425 | private void ToolStripItemClicked(object sender, EventArgs e) {
|
---|
426 | System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
|
---|
427 | ((IActionUserInterfaceItem)item.Tag).Execute();
|
---|
428 | }
|
---|
429 | #endregion
|
---|
430 | }
|
---|
431 | }
|
---|