[3437] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3437] | 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.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
[4068] | 26 | using HeuristicLab.Common;
|
---|
[3437] | 27 | using HeuristicLab.PluginInfrastructure;
|
---|
| 28 | using WeifenLuo.WinFormsUI.Docking;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.MainForm.WindowsForms {
|
---|
| 31 | public partial class MainForm : Form, IMainForm {
|
---|
| 32 | private bool initialized;
|
---|
[6827] | 33 | private int appStartingCursors;
|
---|
| 34 | private int waitingCursors;
|
---|
[3437] | 35 |
|
---|
| 36 | protected MainForm()
|
---|
| 37 | : base() {
|
---|
| 38 | InitializeComponent();
|
---|
| 39 | this.views = new Dictionary<IView, Form>();
|
---|
| 40 | this.userInterfaceItems = new List<IUserInterfaceItem>();
|
---|
| 41 | this.initialized = false;
|
---|
[3557] | 42 | this.showContentInViewHost = false;
|
---|
[6827] | 43 | appStartingCursors = 0;
|
---|
| 44 | waitingCursors = 0;
|
---|
[3437] | 45 | }
|
---|
| 46 |
|
---|
| 47 | protected MainForm(Type userInterfaceItemType)
|
---|
| 48 | : this() {
|
---|
| 49 | this.userInterfaceItemType = userInterfaceItemType;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | #region properties
|
---|
[3557] | 53 | private bool showContentInViewHost;
|
---|
| 54 | public bool ShowContentInViewHost {
|
---|
| 55 | get { return this.showContentInViewHost; }
|
---|
| 56 | set { this.showContentInViewHost = value; }
|
---|
[3437] | 57 | }
|
---|
| 58 |
|
---|
| 59 | public string Title {
|
---|
| 60 | get { return this.Text; }
|
---|
| 61 | set {
|
---|
| 62 | if (InvokeRequired) {
|
---|
| 63 | Action<string> action = delegate(string s) { this.Title = s; };
|
---|
| 64 | Invoke(action, value);
|
---|
| 65 | } else
|
---|
| 66 | this.Text = value;
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public override Cursor Cursor {
|
---|
| 71 | get { return base.Cursor; }
|
---|
| 72 | set {
|
---|
| 73 | if (InvokeRequired) {
|
---|
| 74 | Action<Cursor> action = delegate(Cursor c) { this.Cursor = c; };
|
---|
| 75 | Invoke(action, value);
|
---|
| 76 | } else
|
---|
| 77 | base.Cursor = value;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | private Type userInterfaceItemType;
|
---|
| 82 | public Type UserInterfaceItemType {
|
---|
| 83 | get { return this.userInterfaceItemType; }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | private Dictionary<IView, Form> views;
|
---|
| 87 | public IEnumerable<IView> Views {
|
---|
| 88 | get { return views.Keys; }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private IView activeView;
|
---|
| 92 | public IView ActiveView {
|
---|
| 93 | get { return this.activeView; }
|
---|
| 94 | protected set {
|
---|
| 95 | if (this.activeView != value) {
|
---|
| 96 | if (InvokeRequired) {
|
---|
| 97 | Action<IView> action = delegate(IView activeView) { this.ActiveView = activeView; };
|
---|
| 98 | Invoke(action, value);
|
---|
| 99 | } else {
|
---|
| 100 | this.activeView = value;
|
---|
| 101 | OnActiveViewChanged();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | private List<IUserInterfaceItem> userInterfaceItems;
|
---|
| 108 | protected IEnumerable<IUserInterfaceItem> UserInterfaceItems {
|
---|
| 109 | get { return this.userInterfaceItems; }
|
---|
| 110 | }
|
---|
| 111 | #endregion
|
---|
| 112 |
|
---|
| 113 | #region events
|
---|
| 114 | public event EventHandler ActiveViewChanged;
|
---|
| 115 | protected virtual void OnActiveViewChanged() {
|
---|
| 116 | if (InvokeRequired)
|
---|
| 117 | Invoke((MethodInvoker)OnActiveViewChanged);
|
---|
[4234] | 118 | else {
|
---|
| 119 | EventHandler handler = ActiveViewChanged;
|
---|
| 120 | if (handler != null)
|
---|
| 121 | handler(this, EventArgs.Empty);
|
---|
| 122 | }
|
---|
[3437] | 123 | }
|
---|
| 124 |
|
---|
| 125 | public event EventHandler<ViewEventArgs> ViewClosed;
|
---|
| 126 | protected virtual void OnViewClosed(IView view) {
|
---|
| 127 | if (InvokeRequired) Invoke((Action<IView>)OnViewClosed, view);
|
---|
| 128 | else {
|
---|
| 129 | EventHandler<ViewEventArgs> handler = ViewClosed;
|
---|
| 130 | if (handler != null)
|
---|
| 131 | handler(this, new ViewEventArgs(view));
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | public event EventHandler<ViewShownEventArgs> ViewShown;
|
---|
| 136 | protected virtual void OnViewShown(IView view, bool firstTimeShown) {
|
---|
| 137 | if (InvokeRequired) Invoke((Action<IView, bool>)OnViewShown, view, firstTimeShown);
|
---|
| 138 | else {
|
---|
| 139 | EventHandler<ViewShownEventArgs> handler = ViewShown;
|
---|
| 140 | if (handler != null)
|
---|
| 141 | handler(this, new ViewShownEventArgs(view, firstTimeShown));
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | public event EventHandler<ViewEventArgs> ViewHidden;
|
---|
| 146 | protected virtual void OnViewHidden(IView view) {
|
---|
| 147 | if (InvokeRequired) Invoke((Action<IView>)OnViewHidden, view);
|
---|
| 148 | else {
|
---|
| 149 | EventHandler<ViewEventArgs> handler = ViewHidden;
|
---|
| 150 | if (handler != null)
|
---|
| 151 | handler(this, new ViewEventArgs(view));
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | public event EventHandler Changed;
|
---|
[3488] | 156 | protected void OnChanged() {
|
---|
[3437] | 157 | if (InvokeRequired)
|
---|
[3488] | 158 | Invoke((MethodInvoker)OnChanged);
|
---|
[3437] | 159 | else {
|
---|
| 160 | EventHandler handler = Changed;
|
---|
| 161 | if (handler != null)
|
---|
| 162 | Changed(this, EventArgs.Empty);
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | private void MainFormBase_Load(object sender, EventArgs e) {
|
---|
| 167 | if (!DesignMode) {
|
---|
| 168 | MainFormManager.RegisterMainForm(this);
|
---|
[3794] | 169 | this.CreateGUI();
|
---|
[3437] | 170 | if (!this.initialized) {
|
---|
| 171 | this.initialized = true;
|
---|
| 172 | this.OnInitialized(EventArgs.Empty);
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[7013] | 177 | protected virtual void OnInitialized(EventArgs e) { }
|
---|
[3437] | 178 |
|
---|
[7013] | 179 | public virtual void UpdateTitle() { }
|
---|
| 180 |
|
---|
[3437] | 181 | private void FormActivated(object sender, EventArgs e) {
|
---|
| 182 | this.ActiveView = GetView((Form)sender);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | protected override void OnFormClosing(FormClosingEventArgs e) {
|
---|
| 186 | foreach (KeyValuePair<IView, Form> pair in this.views) {
|
---|
| 187 | DockForm dockForm = pair.Value as DockForm;
|
---|
| 188 | View view = pair.Key as View;
|
---|
| 189 | if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
|
---|
| 190 | view.CloseReason = CloseReason.ApplicationExitCall;
|
---|
| 191 | view.OnClosingHelper(dockForm, e);
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | base.OnFormClosing(e);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | protected override void OnFormClosed(FormClosedEventArgs e) {
|
---|
| 198 | foreach (KeyValuePair<IView, Form> pair in this.views.ToList()) {
|
---|
| 199 | DockForm dockForm = pair.Value as DockForm;
|
---|
| 200 | View view = pair.Key as View;
|
---|
| 201 | if (view != null && dockForm != null && dockForm.DockState != DockState.Document) {
|
---|
| 202 | view.CloseReason = CloseReason.ApplicationExitCall;
|
---|
| 203 | view.OnClosedHelper(dockForm, e);
|
---|
| 204 | dockForm.Close();
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | base.OnFormClosed(e);
|
---|
| 208 | }
|
---|
| 209 | #endregion
|
---|
| 210 |
|
---|
| 211 | #region create, get, show, hide, close views
|
---|
| 212 | protected virtual Form CreateForm(IView view) {
|
---|
| 213 | throw new NotImplementedException("CreateForm must be implemented in subclasses of MainForm.");
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[13627] | 216 | protected internal Form GetForm(IView view) {
|
---|
[3557] | 217 | if (views.ContainsKey(view))
|
---|
| 218 | return views[view];
|
---|
[3437] | 219 | return null;
|
---|
| 220 | }
|
---|
| 221 | protected IView GetView(Form form) {
|
---|
| 222 | return views.Where(x => x.Value == form).Single().Key;
|
---|
| 223 | }
|
---|
[3557] | 224 |
|
---|
| 225 | public IContentView ShowContent(IContent content) {
|
---|
[5270] | 226 | if (content == null) throw new ArgumentNullException("Content cannot be null.");
|
---|
[3670] | 227 | Type viewType = MainFormManager.GetDefaultViewType(content.GetType());
|
---|
[5270] | 228 | if (viewType != null) return ShowContent(content, viewType);
|
---|
[3670] | 229 | return null;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[15371] | 232 | public IContentView ShowContent<T>(T content, bool reuseExistingView, IEqualityComparer<T> comparer = null) where T : class, IContent {
|
---|
[5270] | 233 | if (content == null) throw new ArgumentNullException("Content cannot be null.");
|
---|
| 234 | if (!reuseExistingView) return ShowContent(content);
|
---|
| 235 |
|
---|
| 236 | IContentView view = null;
|
---|
| 237 | if (comparer == null) view = Views.OfType<IContentView>().Where(v => (v.Content as T) == content).FirstOrDefault();
|
---|
| 238 | else view = Views.OfType<IContentView>().Where(v => comparer.Equals((v.Content as T), content)).FirstOrDefault();
|
---|
| 239 |
|
---|
| 240 | if (view == null) view = ShowContent(content);
|
---|
| 241 | else view.Show();
|
---|
| 242 |
|
---|
| 243 | return view;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[3796] | 246 | public IContentView ShowContent(IContent content, Type viewType) {
|
---|
[4234] | 247 | if (InvokeRequired) return (IContentView)Invoke((Func<IContent, Type, IContentView>)ShowContent, content, viewType);
|
---|
| 248 | else {
|
---|
[5270] | 249 | if (content == null) throw new ArgumentNullException("Content cannot be null.");
|
---|
| 250 | if (viewType == null) throw new ArgumentNullException("ViewType cannot be null.");
|
---|
[3670] | 251 |
|
---|
[5270] | 252 | IContentView view = null;
|
---|
| 253 | if (ShowContentInViewHost) {
|
---|
[4234] | 254 | ViewHost viewHost = new ViewHost();
|
---|
| 255 | viewHost.ViewType = viewType;
|
---|
| 256 | view = viewHost;
|
---|
[3557] | 257 |
|
---|
[5270] | 258 | } else view = MainFormManager.CreateView(viewType);
|
---|
| 259 |
|
---|
[4234] | 260 | view.Content = content;
|
---|
| 261 | view.Show();
|
---|
| 262 | return view;
|
---|
| 263 | }
|
---|
[3437] | 264 | }
|
---|
| 265 |
|
---|
| 266 | internal void ShowView(IView view) {
|
---|
| 267 | if (InvokeRequired) Invoke((Action<IView>)ShowView, view);
|
---|
| 268 | else {
|
---|
| 269 | Form form = GetForm(view);
|
---|
| 270 | bool firstTimeShown = form == null;
|
---|
[3557] | 271 | if (firstTimeShown) {
|
---|
[3437] | 272 | form = CreateForm(view);
|
---|
| 273 | form.Activated += new EventHandler(FormActivated);
|
---|
| 274 | form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
|
---|
[3557] | 275 | view.Changed += new EventHandler(View_Changed);
|
---|
| 276 | views[view] = form;
|
---|
[3437] | 277 | }
|
---|
[3557] | 278 | this.ShowView(view, firstTimeShown);
|
---|
| 279 | this.OnViewShown(view, firstTimeShown);
|
---|
[3437] | 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[3488] | 283 | private void View_Changed(object sender, EventArgs e) {
|
---|
[3437] | 284 | IView view = (IView)sender;
|
---|
| 285 | if (view == this.ActiveView)
|
---|
| 286 | this.OnActiveViewChanged();
|
---|
[3488] | 287 | this.OnChanged();
|
---|
[3437] | 288 | }
|
---|
| 289 |
|
---|
| 290 | protected virtual void ShowView(IView view, bool firstTimeShown) {
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | internal void HideView(IView view) {
|
---|
| 294 | if (InvokeRequired) Invoke((Action<IView>)HideView, view);
|
---|
| 295 | else {
|
---|
[3557] | 296 | this.Hide(view);
|
---|
| 297 | if (this.activeView == view)
|
---|
| 298 | this.ActiveView = null;
|
---|
| 299 | this.OnViewHidden(view);
|
---|
[3437] | 300 | }
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | protected virtual void Hide(IView view) {
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | private void ChildFormClosed(object sender, FormClosedEventArgs e) {
|
---|
| 307 | Form form = (Form)sender;
|
---|
| 308 | IView view = GetView(form);
|
---|
| 309 |
|
---|
[3488] | 310 | view.Changed -= new EventHandler(View_Changed);
|
---|
[3437] | 311 | form.Activated -= new EventHandler(FormActivated);
|
---|
| 312 | form.FormClosed -= new FormClosedEventHandler(ChildFormClosed);
|
---|
| 313 |
|
---|
| 314 | views.Remove(view);
|
---|
| 315 | this.OnViewClosed(view);
|
---|
| 316 | if (ActiveView == view)
|
---|
| 317 | ActiveView = null;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | internal void CloseView(IView view) {
|
---|
| 321 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
[3557] | 322 | else if (views.ContainsKey(view)) {
|
---|
| 323 | this.views[view].Close();
|
---|
| 324 | this.OnViewClosed(view);
|
---|
[3437] | 325 | }
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | internal void CloseView(IView view, CloseReason closeReason) {
|
---|
| 329 | if (InvokeRequired) Invoke((Action<IView>)CloseView, view);
|
---|
[3557] | 330 | else if (views.ContainsKey(view)) {
|
---|
| 331 | ((View)view).CloseReason = closeReason;
|
---|
| 332 | this.CloseView(view);
|
---|
[3437] | 333 | }
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | public void CloseAllViews() {
|
---|
| 337 | foreach (IView view in views.Keys.ToArray())
|
---|
| 338 | CloseView(view);
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | public void CloseAllViews(CloseReason closeReason) {
|
---|
| 342 | foreach (IView view in views.Keys.ToArray())
|
---|
| 343 | CloseView(view, closeReason);
|
---|
| 344 | }
|
---|
[9893] | 345 | #endregion
|
---|
[9849] | 346 |
|
---|
[9893] | 347 | #region progress views
|
---|
| 348 | private readonly Dictionary<IContent, IProgress> contentProgressLookup = new Dictionary<IContent, IProgress>();
|
---|
[9896] | 349 | private readonly Dictionary<Control, IProgress> viewProgressLookup = new Dictionary<Control, IProgress>();
|
---|
[9893] | 350 | private readonly List<ProgressView> progressViews = new List<ProgressView>();
|
---|
| 351 |
|
---|
[9849] | 352 | /// <summary>
|
---|
| 353 | /// Adds a <see cref="ProgressView"/> to the <see cref="ContentView"/>s showing the specified content.
|
---|
| 354 | /// </summary>
|
---|
[15371] | 355 | public IProgress AddOperationProgressToContent(IContent content, string progressMessage, double progressValue = -1, bool addToObjectGraphObjects = true) {
|
---|
[9905] | 356 | if (InvokeRequired) {
|
---|
[15371] | 357 | IProgress result = (IProgress)Invoke((Func<IContent, string, double, bool, IProgress>)AddOperationProgressToContent, content, progressMessage, progressValue, addToObjectGraphObjects);
|
---|
[9905] | 358 | return result;
|
---|
| 359 | }
|
---|
[9849] | 360 | if (contentProgressLookup.ContainsKey(content))
|
---|
| 361 | throw new ArgumentException("A progress is already registered for the specified content.", "content");
|
---|
| 362 |
|
---|
[9893] | 363 | var contentViews = views.Keys.OfType<ContentView>();
|
---|
| 364 | if (!contentViews.Any(v => v.Content == content))
|
---|
| 365 | throw new ArgumentException("The content is not displayed in a top-level view", "content");
|
---|
| 366 |
|
---|
[9849] | 367 | if (addToObjectGraphObjects) {
|
---|
| 368 | var containedObjects = content.GetObjectGraphObjects();
|
---|
[9893] | 369 | contentViews = contentViews.Where(v => containedObjects.Contains(v.Content));
|
---|
[9849] | 370 | } else
|
---|
[9893] | 371 | contentViews = contentViews.Where(v => v.Content == content);
|
---|
[9849] | 372 |
|
---|
[15371] | 373 | var progress = new Progress();
|
---|
| 374 | progress.Start(progressMessage, progressValue);
|
---|
[9893] | 375 | foreach (var contentView in contentViews) {
|
---|
[9896] | 376 | progressViews.Add(new ProgressView(contentView, progress));
|
---|
[9893] | 377 | }
|
---|
[9849] | 378 |
|
---|
| 379 | contentProgressLookup[content] = progress;
|
---|
[9894] | 380 | return progress;
|
---|
[9849] | 381 | }
|
---|
| 382 |
|
---|
| 383 | /// <summary>
|
---|
| 384 | /// Adds a <see cref="ProgressView"/> to the specified view.
|
---|
| 385 | /// </summary>
|
---|
[9900] | 386 | public IProgress AddOperationProgressToView(Control control, string progressMessage) {
|
---|
[15371] | 387 | var progress = new Progress();
|
---|
| 388 | progress.Start(progressMessage);
|
---|
[9900] | 389 | AddOperationProgressToView(control, progress);
|
---|
[9894] | 390 | return progress;
|
---|
| 391 | }
|
---|
[9849] | 392 |
|
---|
[9900] | 393 | public void AddOperationProgressToView(Control control, IProgress progress) {
|
---|
[9905] | 394 | if (InvokeRequired) {
|
---|
| 395 | Invoke((Action<Control, IProgress>)AddOperationProgressToView, control, progress);
|
---|
| 396 | return;
|
---|
| 397 | }
|
---|
[9900] | 398 | if (control == null) throw new ArgumentNullException("control", "The view must not be null.");
|
---|
[9894] | 399 | if (progress == null) throw new ArgumentNullException("progress", "The progress must not be null.");
|
---|
| 400 |
|
---|
| 401 | IProgress oldProgress;
|
---|
[9900] | 402 | if (viewProgressLookup.TryGetValue(control, out oldProgress)) {
|
---|
[9894] | 403 | foreach (var progressView in progressViews.Where(v => v.Content == oldProgress).ToList()) {
|
---|
| 404 | progressView.Dispose();
|
---|
| 405 | progressViews.Remove(progressView);
|
---|
| 406 | }
|
---|
[9900] | 407 | viewProgressLookup.Remove(control);
|
---|
[9894] | 408 | }
|
---|
| 409 |
|
---|
[9900] | 410 | progressViews.Add(new ProgressView(control, progress));
|
---|
| 411 | viewProgressLookup[control] = progress;
|
---|
[9849] | 412 | }
|
---|
| 413 |
|
---|
| 414 | /// <summary>
|
---|
| 415 | /// Removes an existing <see cref="ProgressView"/> from the <see cref="ContentView"/>s showing the specified content.
|
---|
| 416 | /// </summary>
|
---|
[9894] | 417 | public void RemoveOperationProgressFromContent(IContent content, bool finishProgress = true) {
|
---|
[9905] | 418 | if (InvokeRequired) {
|
---|
| 419 | Invoke((Action<IContent, bool>)RemoveOperationProgressFromContent, content, finishProgress);
|
---|
| 420 | return;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
[9849] | 423 | IProgress progress;
|
---|
| 424 | if (!contentProgressLookup.TryGetValue(content, out progress))
|
---|
| 425 | throw new ArgumentException("No progress is registered for the specified content.", "content");
|
---|
| 426 |
|
---|
[9894] | 427 | if (finishProgress) progress.Finish();
|
---|
[9893] | 428 | foreach (var progressView in progressViews.Where(v => v.Content == progress).ToList()) {
|
---|
| 429 | progressView.Dispose();
|
---|
| 430 | progressViews.Remove(progressView);
|
---|
| 431 | }
|
---|
[9849] | 432 | contentProgressLookup.Remove(content);
|
---|
[9905] | 433 |
|
---|
[9849] | 434 | }
|
---|
| 435 |
|
---|
| 436 | /// <summary>
|
---|
| 437 | /// Removes an existing <see cref="ProgressView"/> from the specified view.
|
---|
| 438 | /// </summary>
|
---|
[9900] | 439 | public void RemoveOperationProgressFromView(Control control, bool finishProgress = true) {
|
---|
[10575] | 440 | if (InvokeRequired) {
|
---|
| 441 | Invoke((Action<Control, bool>)RemoveOperationProgressFromView, control, finishProgress);
|
---|
| 442 | return;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[9849] | 445 | IProgress progress;
|
---|
[9900] | 446 | if (!viewProgressLookup.TryGetValue(control, out progress))
|
---|
| 447 | throw new ArgumentException("No progress is registered for the specified control.", "control");
|
---|
[9849] | 448 |
|
---|
[9894] | 449 | if (finishProgress) progress.Finish();
|
---|
[9893] | 450 | foreach (var progressView in progressViews.Where(v => v.Content == progress).ToList()) {
|
---|
| 451 | progressView.Dispose();
|
---|
| 452 | progressViews.Remove(progressView);
|
---|
| 453 | }
|
---|
[9900] | 454 | viewProgressLookup.Remove(control);
|
---|
[9849] | 455 | }
|
---|
[3437] | 456 | #endregion
|
---|
| 457 |
|
---|
| 458 | #region create menu and toolbar
|
---|
| 459 | private void CreateGUI() {
|
---|
[13621] | 460 | if (userInterfaceItemType != null) {
|
---|
| 461 | IEnumerable<object> allUserInterfaceItems = ApplicationManager.Manager.GetInstances(userInterfaceItemType);
|
---|
[3437] | 462 |
|
---|
[13621] | 463 | IEnumerable<IPositionableUserInterfaceItem> toolStripMenuItems =
|
---|
| 464 | from mi in allUserInterfaceItems
|
---|
| 465 | where (mi is IPositionableUserInterfaceItem) &&
|
---|
| 466 | (mi is IMenuItem || mi is IMenuSeparatorItem)
|
---|
| 467 | orderby ((IPositionableUserInterfaceItem)mi).Position
|
---|
| 468 | select (IPositionableUserInterfaceItem)mi;
|
---|
[3437] | 469 |
|
---|
[13621] | 470 | foreach (IPositionableUserInterfaceItem menuItem in toolStripMenuItems) {
|
---|
| 471 | if (menuItem is IMenuItem)
|
---|
| 472 | AddToolStripMenuItem((IMenuItem)menuItem);
|
---|
| 473 | else if (menuItem is IMenuSeparatorItem)
|
---|
| 474 | AddToolStripMenuItem((IMenuSeparatorItem)menuItem);
|
---|
| 475 | }
|
---|
[3437] | 476 |
|
---|
[13621] | 477 | IEnumerable<IPositionableUserInterfaceItem> toolStripButtonItems =
|
---|
| 478 | from bi in allUserInterfaceItems
|
---|
| 479 | where (bi is IPositionableUserInterfaceItem) &&
|
---|
| 480 | (bi is IToolBarItem || bi is IToolBarSeparatorItem)
|
---|
| 481 | orderby ((IPositionableUserInterfaceItem)bi).Position
|
---|
| 482 | select (IPositionableUserInterfaceItem)bi;
|
---|
[3437] | 483 |
|
---|
[13621] | 484 | foreach (IPositionableUserInterfaceItem toolStripButtonItem in toolStripButtonItems) {
|
---|
| 485 | if (toolStripButtonItem is IToolBarItem)
|
---|
| 486 | AddToolStripButtonItem((IToolBarItem)toolStripButtonItem);
|
---|
| 487 | else if (toolStripButtonItem is IToolBarSeparatorItem)
|
---|
| 488 | AddToolStripButtonItem((IToolBarSeparatorItem)toolStripButtonItem);
|
---|
| 489 | }
|
---|
| 490 |
|
---|
[3437] | 491 | }
|
---|
[13621] | 492 | this.AdditionalCreationOfGuiElements();
|
---|
[3437] | 493 |
|
---|
[13621] | 494 | if (menuStrip.Items.Count == 0) menuStrip.Visible = false;
|
---|
| 495 | if (toolStrip.Items.Count == 0) toolStrip.Visible = false;
|
---|
[3437] | 496 | }
|
---|
| 497 |
|
---|
| 498 | protected virtual void AdditionalCreationOfGuiElements() {
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | private void AddToolStripMenuItem(IMenuItem menuItem) {
|
---|
| 502 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
| 503 | this.SetToolStripItemProperties(item, menuItem);
|
---|
[9626] | 504 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), item, menuStrip.Items);
|
---|
[3437] | 505 | if (menuItem is MenuItem) {
|
---|
| 506 | MenuItem menuItemBase = (MenuItem)menuItem;
|
---|
| 507 | menuItemBase.ToolStripItem = item;
|
---|
| 508 | item.ShortcutKeys = menuItemBase.ShortCutKeys;
|
---|
| 509 | item.DisplayStyle = menuItemBase.ToolStripItemDisplayStyle;
|
---|
| 510 | }
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | private void AddToolStripMenuItem(IMenuSeparatorItem menuItem) {
|
---|
| 514 | this.InsertItem(menuItem.Structure, typeof(ToolStripMenuItem), new ToolStripSeparator(), menuStrip.Items);
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | private void AddToolStripButtonItem(IToolBarItem buttonItem) {
|
---|
| 518 | ToolStripItem item = new ToolStripButton();
|
---|
| 519 | if (buttonItem is ToolBarItem) {
|
---|
| 520 | ToolBarItem buttonItemBase = (ToolBarItem)buttonItem;
|
---|
| 521 | if (buttonItemBase.IsDropDownButton)
|
---|
| 522 | item = new ToolStripDropDownButton();
|
---|
| 523 |
|
---|
| 524 | item.DisplayStyle = buttonItemBase.ToolStripItemDisplayStyle;
|
---|
| 525 | buttonItemBase.ToolStripItem = item;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | this.SetToolStripItemProperties(item, buttonItem);
|
---|
| 529 | this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), item, toolStrip.Items);
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | private void AddToolStripButtonItem(IToolBarSeparatorItem buttonItem) {
|
---|
| 533 | this.InsertItem(buttonItem.Structure, typeof(ToolStripDropDownButton), new ToolStripSeparator(), toolStrip.Items);
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 | private void InsertItem(IEnumerable<string> structure, Type t, ToolStripItem item, ToolStripItemCollection parentItems) {
|
---|
| 537 | ToolStripDropDownItem parent = null;
|
---|
| 538 | foreach (string s in structure) {
|
---|
| 539 | if (parentItems.ContainsKey(s))
|
---|
| 540 | parent = (ToolStripDropDownItem)parentItems[s];
|
---|
| 541 | else {
|
---|
| 542 | parent = (ToolStripDropDownItem)Activator.CreateInstance(t, s, null, null, s); ;
|
---|
| 543 | parentItems.Add(parent);
|
---|
| 544 | }
|
---|
| 545 | parentItems = parent.DropDownItems;
|
---|
| 546 | }
|
---|
| 547 | parentItems.Add(item);
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | private void SetToolStripItemProperties(ToolStripItem toolStripItem, IActionUserInterfaceItem userInterfaceItem) {
|
---|
| 551 | toolStripItem.Name = userInterfaceItem.Name;
|
---|
| 552 | toolStripItem.Text = userInterfaceItem.Name;
|
---|
| 553 | toolStripItem.ToolTipText = userInterfaceItem.ToolTipText;
|
---|
| 554 | toolStripItem.Tag = userInterfaceItem;
|
---|
| 555 | toolStripItem.Image = userInterfaceItem.Image;
|
---|
| 556 | toolStripItem.Click += new EventHandler(ToolStripItemClicked);
|
---|
| 557 | this.userInterfaceItems.Add(userInterfaceItem);
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | private void ToolStripItemClicked(object sender, EventArgs e) {
|
---|
| 561 | System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)sender;
|
---|
[5237] | 562 | try {
|
---|
| 563 | ((IActionUserInterfaceItem)item.Tag).Execute();
|
---|
[13621] | 564 | }
|
---|
| 565 | catch (Exception ex) {
|
---|
[5237] | 566 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, ex);
|
---|
| 567 | }
|
---|
[3437] | 568 | }
|
---|
| 569 | #endregion
|
---|
[6827] | 570 |
|
---|
| 571 | #region Cursor Handling
|
---|
| 572 | public void SetAppStartingCursor() {
|
---|
| 573 | if (InvokeRequired)
|
---|
| 574 | Invoke(new Action(SetAppStartingCursor));
|
---|
| 575 | else {
|
---|
| 576 | appStartingCursors++;
|
---|
| 577 | SetCursor();
|
---|
| 578 | }
|
---|
| 579 | }
|
---|
| 580 | public void ResetAppStartingCursor() {
|
---|
| 581 | if (InvokeRequired)
|
---|
| 582 | Invoke(new Action(ResetAppStartingCursor));
|
---|
| 583 | else {
|
---|
| 584 | appStartingCursors--;
|
---|
| 585 | SetCursor();
|
---|
| 586 | }
|
---|
| 587 | }
|
---|
| 588 | public void SetWaitCursor() {
|
---|
| 589 | if (InvokeRequired)
|
---|
| 590 | Invoke(new Action(SetWaitCursor));
|
---|
| 591 | else {
|
---|
| 592 | waitingCursors++;
|
---|
| 593 | SetCursor();
|
---|
| 594 | }
|
---|
| 595 | }
|
---|
| 596 | public void ResetWaitCursor() {
|
---|
| 597 | if (InvokeRequired)
|
---|
| 598 | Invoke(new Action(ResetWaitCursor));
|
---|
| 599 | else {
|
---|
| 600 | waitingCursors--;
|
---|
| 601 | SetCursor();
|
---|
| 602 | }
|
---|
| 603 | }
|
---|
| 604 | private void SetCursor() {
|
---|
| 605 | if (waitingCursors > 0) Cursor = Cursors.WaitCursor;
|
---|
| 606 | else if (appStartingCursors > 0) Cursor = Cursors.AppStarting;
|
---|
| 607 | else Cursor = Cursors.Default;
|
---|
| 608 | }
|
---|
| 609 | #endregion
|
---|
[3437] | 610 | }
|
---|
| 611 | }
|
---|