Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MessageService.cs « MonoDevelop.Ide « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dd6b82f1fc327c066d66ac55867d3d85d383d0c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
//
// MessageService.cs
//
// Author:
//   Mike Krüger <mkrueger@novell.com>
//
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using MonoDevelop.Components;
using MonoDevelop.Core;
using MonoDevelop.Components.Extensions;
using MonoDevelop.Ide.Gui;
using System.Threading.Tasks;
using MonoDevelop.Ide.WelcomePage;

#if MAC
using AppKit;
using MonoDevelop.Components.Mac;
#endif

namespace MonoDevelop.Ide
{
	public class AlertButtonEventArgs : EventArgs
	{
		public AlertButton Button {
			get;
			private set;
		}

		public bool CloseDialog {
			get;
			set;
		}

		public AlertButtonEventArgs (AlertButton button, bool closeDialog)
		{
			Button = button;
			CloseDialog = closeDialog;
		}

		public AlertButtonEventArgs (AlertButton button) : this (button, true)
		{
		}
	}

	public class AlertButton 
	{
		public static AlertButton Ok      = new AlertButton (Gtk.Stock.Ok, true);
		public static AlertButton Yes     = new AlertButton (Gtk.Stock.Yes, true);
		public static AlertButton No      = new AlertButton (Gtk.Stock.No, true);
		public static AlertButton Close   = new AlertButton (Gtk.Stock.Close, true);
		public static AlertButton Cancel  = new AlertButton (Gtk.Stock.Cancel, true);
		public static AlertButton Delete  = new AlertButton (Gtk.Stock.Delete, true);
		public static AlertButton Remove  = new AlertButton (Gtk.Stock.Remove, true);
		public static AlertButton Clear   = new AlertButton (Gtk.Stock.Clear, true);
		public static AlertButton Reload  = new AlertButton (GettextCatalog.GetString ("_Reload"), Gtk.Stock.Refresh);
		public static AlertButton Revert  = new AlertButton (Gtk.Stock.RevertToSaved, true );
		public static AlertButton Copy    = new AlertButton (Gtk.Stock.Copy, true);
		public static AlertButton Move    = new AlertButton (GettextCatalog.GetString ("_Move"));
		public static AlertButton Save    = new AlertButton (Gtk.Stock.Save, true);
		public static AlertButton SaveAs  = new AlertButton (Gtk.Stock.SaveAs, true);
		public static AlertButton CloseWithoutSave = new AlertButton (GettextCatalog.GetString ("Close _without Saving"));
		public static AlertButton BuildWithoutSave = new AlertButton (GettextCatalog.GetString ("Build _without Saving"));
		public static AlertButton Discard = new AlertButton (GettextCatalog.GetString ("D_iscard"));
		public static AlertButton Stop    = new AlertButton (Gtk.Stock.Stop, true);
		public static AlertButton Proceed = new AlertButton (GettextCatalog.GetString ("_Proceed"));
		public static AlertButton Replace = new AlertButton (GettextCatalog.GetString ("_Replace"));

		public static AlertButton OverwriteFile = new AlertButton (GettextCatalog.GetString ("_Overwrite file"));
		public static AlertButton AddExistingFile = new AlertButton (GettextCatalog.GetString ("Add existing file"));
		public static AlertButton MakeWriteable = new AlertButton (GettextCatalog.GetString ("Make Writeable"));

		
		public string Label { get; set; }
		public string Icon { get; set; }
		public bool IsStockButton { get; set; }
		
		public AlertButton (string label, string icon)
		{
			this.Label = label;
			this.Icon = icon;
		}
		
		public AlertButton (string label) : this (label, null)
		{
		}
		
		public AlertButton (string label, bool isStockButton) : this (label)
		{
			this.IsStockButton = isStockButton;
		}
	}
	
	public class AlertOption
	{
		internal AlertOption (string id, string text)
		{
			this.Id = id;
			this.Text = text;
		}

		public string Id { get; private set; }
		public string Text { get; private set; }
		public bool Value { get; set; }
	}
	
	//all methods are synchronously invoked on the GUI thread, except those which take GTK# objects as arguments
	public static class MessageService
	{
		static Window defaultRootWindow;
		public static Window RootWindow {
			get {
				if (WelcomePageService.WelcomeWindowVisible && !IdeApp.Workbench.RootWindow.Visible)
					return WelcomePageService.WelcomeWindow;
				return defaultRootWindow;
			}
			internal set {
				defaultRootWindow = value;
			}
		}

		#region ShowError
		public static void ShowError (string primaryText)
		{
			ShowError ((Window)null, primaryText);
		}

		public static void ShowError (string primaryText, Exception ex)
		{
			ShowError ((Window)null, primaryText, null, ex);
		}

		public static void ShowError (string primaryText, string secondaryText)
		{
			ShowError ((Window)null, primaryText, secondaryText, null);
		}

		public static void ShowError (string primaryText, string secondaryText, Exception ex)
		{
			ShowError ((Window)null, primaryText, secondaryText, ex);
		}

		public static void ShowError (Window parent, string primaryText)
		{
			ShowError (parent, primaryText, null, null);
		}

		public static void ShowError (Window parent, string primaryText, string secondaryText)
		{
			ShowError (parent, primaryText, secondaryText, null);
		}

		public static void ShowError (Window parent, string primaryText, string secondaryText, Exception ex)
		{
			ShowError (parent, primaryText, secondaryText, ex, true, AlertButton.Ok);
		}

		internal static AlertButton ShowError (Window parent, string primaryText, string secondaryText, Exception ex, bool logError, params AlertButton[] buttons)
		{
			if (logError) {
				string msg = string.IsNullOrEmpty (secondaryText) ? primaryText : primaryText + ". " + secondaryText;
				LoggingService.LogError (msg, ex);
			}

			if (string.IsNullOrEmpty (secondaryText) && (ex != null))
				secondaryText = ErrorHelper.GetErrorMessage (ex);

			return GenericAlert (parent, MonoDevelop.Ide.Gui.Stock.Error, primaryText, secondaryText, buttons);
		}

		internal static void ShowFatalError (string primaryText, string secondaryText, Exception ex)
		{
			string msg = string.IsNullOrEmpty (secondaryText) ? primaryText : primaryText + ". " + secondaryText;
			LoggingService.LogFatalError (msg, ex);
			GenericAlert (null, MonoDevelop.Ide.Gui.Stock.Error, primaryText, secondaryText, AlertButton.Ok);
		}
		#endregion
		
		#region ShowWarning
		public static void ShowWarning (string primaryText)
		{
			ShowWarning ((Window)null, primaryText);
		}
		public static void ShowWarning (Window parent, string primaryText)
		{
			ShowWarning (parent, primaryText, null);
		}
		public static void ShowWarning (string primaryText, string secondaryText)
		{
			ShowWarning ((Window)null, primaryText, secondaryText);
		}
		public static void ShowWarning (Window parent, string primaryText, string secondaryText)
		{
			GenericAlert (parent, MonoDevelop.Ide.Gui.Stock.Warning, primaryText, secondaryText, AlertButton.Ok);
		}
		#endregion
		
		#region ShowMessage
		public static void ShowMessage (string primaryText)
		{
			ShowMessage ((Window)null, primaryText);
		}
		public static void ShowMessage (Window parent, string primaryText)
		{
			ShowMessage (parent, primaryText, null);
		}
		public static void ShowMessage (string primaryText, string secondaryText)
		{
			ShowMessage ((Window)null, primaryText, secondaryText);
		}
		public static void ShowMessage (Window parent, string primaryText, string secondaryText)
		{
			GenericAlert (parent, MonoDevelop.Ide.Gui.Stock.Information, primaryText, secondaryText, AlertButton.Ok);
		}
		#endregion
		
		#region Confirm
		public static bool Confirm (string primaryText, AlertButton button)
		{
			return Confirm (primaryText, null, button);
		}
		
		public static bool Confirm (string primaryText, string secondaryText, AlertButton button)
		{
			return GenericAlert (MonoDevelop.Ide.Gui.Stock.Question, primaryText, secondaryText, AlertButton.Cancel, button) == button;
		}
		public static bool Confirm (string primaryText, AlertButton button, bool confirmIsDefault)
		{
			return Confirm (primaryText, null, button, confirmIsDefault);
		}
		
		public static bool Confirm (string primaryText, string secondaryText, AlertButton button, bool confirmIsDefault)
		{
			return GenericAlert (MonoDevelop.Ide.Gui.Stock.Question, primaryText, secondaryText, confirmIsDefault ? 0 : 1, AlertButton.Cancel, button) == button;
		}
		
		public static bool Confirm (ConfirmationMessage message)
		{
			return messageService.GenericAlert (null, message) == message.ConfirmButton;
		}
		#endregion
		
		#region AskQuestion
		public static AlertButton AskQuestion (string primaryText, params AlertButton[] buttons)
		{
			return AskQuestion (primaryText, null, buttons);
		}
		
		public static AlertButton AskQuestion (string primaryText, string secondaryText, params AlertButton[] buttons)
		{
			return GenericAlert (MonoDevelop.Ide.Gui.Stock.Question, primaryText, secondaryText, buttons);
		}
		public static AlertButton AskQuestion (string primaryText, int defaultButton, params AlertButton[] buttons)
		{
			return AskQuestion (primaryText, null, defaultButton, buttons);
		}
		
		public static AlertButton AskQuestion (string primaryText, string secondaryText, int defaultButton, params AlertButton[] buttons)
		{
			return GenericAlert (MonoDevelop.Ide.Gui.Stock.Question, primaryText, secondaryText, defaultButton, buttons);
		}
		
		public static AlertButton AskQuestion (QuestionMessage message)
		{
			return messageService.GenericAlert (null, message);
		}
		
		#endregion
		
		/// <summary>
		/// Places, runs and destroys a transient dialog.
		/// </summary>
		public static int ShowCustomDialog (Dialog dialog)
		{
			return ShowCustomDialog (dialog, null);
		}
		
		public static int ShowCustomDialog (Dialog dlg, Window parent)
		{
			Gtk.Dialog dialog = dlg;
			try {
				return RunCustomDialog (dlg, parent);
			} finally {
				dialog?.Destroy ();
			}
		}
		
		public static int RunCustomDialog (Dialog dialog)
		{
			return RunCustomDialog (dialog, null);
		}
		
		/// <summary>
		/// Places and runs a transient dialog. Does not destroy it, so values can be retrieved from its widgets.
		/// </summary>
		public static int RunCustomDialog (Dialog dlg, Window parent)
		{
			// if dialog is modal, make sure it's parented on any existing modal dialog
			Gtk.Dialog dialog = dlg;
			if (dialog.Modal) {
				parent = IdeServices.DesktopService.GetParentForModalWindow ();
			}

			//ensure the dialog has a parent
			if (parent == null) {
				if (dialog.TransientFor != null)
					parent = dialog.TransientFor;
				else
					parent = IdeServices.DesktopService.GetFocusedTopLevelWindow ();
			}

			//TODO: use native parenting API for native windows
			if (parent?.nativeWidget is Gtk.Window) {
				dialog.TransientFor = parent;
				dialog.DestroyWithParent = true;
			}

			MonoDevelop.Components.IdeTheme.ApplyTheme (dialog);

			if (dialog.Title == null)
				dialog.Title = BrandingService.ApplicationName;

			#if MAC
			Runtime.RunInMainThread (() => {
				// If there is a native NSWindow model window running, we need
				// to show the new dialog over that window.
				if (NSApplication.SharedApplication.ModalWindow != null || parent?.nativeWidget is NSWindow) {
					if (dialog.Modal) {
						EventHandler shownHandler = null;
						shownHandler = (s, e) => {
							ShowCustomModalDialog (dialog, parent);
							dialog.Shown -= shownHandler;
						};
						dialog.Shown += shownHandler;
					} else {
						// If parent is a native NSWindow, run the dialog modally anyway
						ShowCustomModalDialog (dialog, parent);
					}
				} else {
					PlaceDialog (dialog, parent);
				}
			}).Wait ();
			#endif

			var initialRootWindow = Xwt.MessageDialog.RootWindow;
			try {
				Xwt.MessageDialog.RootWindow = Xwt.Toolkit.CurrentEngine.WrapWindow (dialog);
				IdeApp.DisableIdleActions ();
				int result = GtkWorkarounds.RunDialogWithNotification (dialog);
				// Focus parent window once the dialog is ran, as focus gets lost
				if (parent != null) {
					IdeServices.DesktopService.FocusWindow (parent);
				}

				return result;
			} finally {
				Xwt.MessageDialog.RootWindow = initialRootWindow;
				IdeApp.EnableIdleActions ();
			}
		}

		#if MAC

		static void ShowCustomModalDialog (Gtk.Window dialog, Window parent)
		{
			CenterWindow (dialog, parent);

			var nsdialog = GtkMacInterop.GetNSWindow (dialog);
			// Make the GTK window modal WRT the current modal NSWindow

			NSWindow nativeParent = null;
			try {
				nativeParent = parent;
				nativeParent.AddChildWindow (nsdialog, NSWindowOrderingMode.Above);
			} catch (Exception ex) {
				LoggingService.LogInternalError ("Failed to get the native parent window", ex);
			}
			var s = NSApplication.SharedApplication.BeginModalSession (nsdialog);
			void unrealizer (object sender, EventArgs e)
			{
				if (nativeParent != null) {
					nativeParent.RemoveChildWindow (nsdialog);
				}
				NSApplication.SharedApplication.EndModalSession (s);
				dialog.Unrealized -= unrealizer;
			}
			dialog.Unrealized += unrealizer;

		}
		#endif
		
		/// <summary>
		/// Positions a dialog relative to its parent on platforms where default placement is known to be poor.
		/// </summary>
		public static void PlaceDialog (Window childControl, Window parent)
		{
			//HACK: this is a workaround for broken automatic window placement on Mac
			if (!Platform.IsMac)
				return;

			if (parent == null) {
				if (childControl.nativeWidget is Gtk.Window gtkChild) {
					if (gtkChild.Modal)
						parent = IdeServices.DesktopService.GetParentForModalWindow ();
				}
			}

			CenterWindow (childControl, parent);
		}

		/// <summary>Centers a window relative to its parent.</summary>
		static void CenterWindow (Window childControl, Window parentControl)
		{
			var gtkChild = childControl?.nativeWidget as Gtk.Window;
			var gtkParent = parentControl?.nativeWidget as Gtk.Window;
#if MAC
			var nsChild = childControl?.nativeWidget as NSWindow;
			var nsParent = parentControl?.nativeWidget as NSWindow ?? parentControl;

			if (nsChild != null) {
				if (nsParent == null || !nsParent.IsVisible) {
					nsChild.Center ();
				} else {
					int x = (int) (nsParent.Frame.Left + (nsParent.Frame.Width - nsChild.Frame.Width) / 2);
					int y = (int) (nsParent.Frame.Top + (nsParent.Frame.Height - nsChild.Frame.Height) / 2);
					nsChild.SetFrameOrigin (new CoreGraphics.CGPoint (x, y));
				}

				return;
			}
#endif
			if (gtkChild != null) {
				gtkChild.Show ();
				int x, y;
				gtkChild.GetSize (out var w, out var h);
				if (gtkParent != null) {
					gtkParent.GetSize (out var winw, out var winh);
					gtkParent.GetPosition (out var winx, out var winy);
					x = Math.Max (0, (winw - w) / 2) + winx;
					y = Math.Max (0, (winh - h) / 2) + winy;
					gtkChild.Move (x, y);
#if MAC
				} else if (nsParent != null) {
					x = (int) (nsParent.Frame.Left + (nsParent.Frame.Width - w) / 2);
					y = (int) (nsParent.Frame.Top + (nsParent.Frame.Height - h) / 2);
					nsChild = GtkMacInterop.GetNSWindow (gtkChild);
					if (nsChild != null) {
						nsChild.SetFrameOrigin (new CoreGraphics.CGPoint (x, y));
					} else {
						gtkChild.Move (x, y);
					}
#endif
				} else {
#if MAC
					// There is no parent, so just center the dialog
					nsChild = GtkMacInterop.GetNSWindow (gtkChild);
					if (nsChild != null) {
						nsChild.Center ();
					}
#else
					gtkChild.SetPosition (Gtk.WindowPosition.Center);
#endif
				}
			}
		}
		
		public static AlertButton GenericAlert (string icon, string primaryText, string secondaryText, params AlertButton[] buttons)
		{
			return GenericAlert ((Window)null, icon, primaryText, secondaryText, buttons.Length - 1, buttons);
		}

		public static AlertButton GenericAlert (Window parent, string icon, string primaryText, string secondaryText, params AlertButton[] buttons)
		{
			return GenericAlert (parent, icon, primaryText, secondaryText, buttons.Length - 1, buttons);
		}
		
		public static AlertButton GenericAlert (string icon, string primaryText, string secondaryText, int defaultButton,
			params AlertButton[] buttons)
		{
			return GenericAlert ((Window)null, icon, primaryText, secondaryText, defaultButton, CancellationToken.None, buttons);
		}

		public static AlertButton GenericAlert (Window parent, string icon, string primaryText, string secondaryText, int defaultButton,
			params AlertButton[] buttons)
		{
			return GenericAlert (parent, icon, primaryText, secondaryText, defaultButton, CancellationToken.None, buttons);
		}

		public static AlertButton GenericAlert (string icon, string primaryText, string secondaryText, int defaultButton,
			CancellationToken cancellationToken,
			params AlertButton[] buttons)
		{
			return GenericAlert ((Window)null, icon, primaryText, secondaryText, defaultButton, cancellationToken, buttons);
		}
		
		public static AlertButton GenericAlert (Window parent, string icon, string primaryText, string secondaryText, int defaultButton,
			CancellationToken cancellationToken,
			params AlertButton[] buttons)
		{
			var message = new GenericMessage (primaryText, secondaryText, cancellationToken) {
				Icon = icon,
				DefaultButton = defaultButton,
			};
			foreach (AlertButton but in buttons)
				message.Buttons.Add (but);
			
			return messageService.GenericAlert (parent, message);
		}

		public static AlertButton GenericAlert (GenericMessage message)
		{
			return GenericAlert ((Window)null, message);
		}
		
		public static AlertButton GenericAlert (Window parent, GenericMessage message)
		{
			return messageService.GenericAlert (parent, message);
		}

		public static async Task<T> ExecuteTaskAndShowWaitDialog<T> (Task<T> task, string waitMessage, CancellationTokenSource cts)
		{
			bool taskFinished = false;
			var dontExitMethodUntilDialogClosed = new TaskCompletionSource<bool> ();
			var delayTask = Task.Delay (1000);//Don't show wait dialog immediately, wait 1 sec before showing
			var finishedTask = await Task.WhenAny (delayTask, task).ConfigureAwait (false);
			if (finishedTask == task)//If task finished before delayTask, great return value and never display dialog
				return task.Result;
			//cancelDialog is used to close dialog when task is finished
			var cancelDialog = new CancellationTokenSource ();
			Gtk.Application.Invoke ((o, args) => {
				if (cancelDialog.Token.IsCancellationRequested)
					return;
				var gm = new GenericMessage (waitMessage, null, cancelDialog.Token);
				gm.Buttons.Add (AlertButton.Cancel);
				gm.DefaultButton = 0;
				GenericAlert (gm);
				dontExitMethodUntilDialogClosed.SetResult (true);
				if (!taskFinished) {
					//Don't cancel if task finished already, we closed dialog via cancelDialog.Cancel ();
					//caller of this method might reuse this cts for other tasks
					cts.Cancel ();
				}
			});
			try {
				await task.ConfigureAwait (false);
			} finally {
				taskFinished = true;
				cancelDialog.Cancel ();
			}
			await dontExitMethodUntilDialogClosed.Task.ConfigureAwait (false);
			return task.Result;
		}
		
		public static string GetTextResponse (string question, string caption, string initialValue)
		{
			return GetTextResponse ((Window)null, question, caption, initialValue, false);
		}

		public static string GetTextResponse (Window parent, string question, string caption, string initialValue)
		{
			return GetTextResponse (parent, question, caption, initialValue, false);
		}

		public static string GetPassword (string question, string caption)
		{
			return GetTextResponse ((Window)null, question, caption, string.Empty, true);
		}

		public static string GetPassword (Window parent, string question, string caption)
		{
			return GetTextResponse (parent, question, caption, string.Empty, true);
		}

		static string GetTextResponse (Window parent, string question, string caption, string initialValue, bool isPassword)
		{
			return messageService.GetTextResponse (parent, question, caption, initialValue, isPassword);
		}
		
#region Internal GUI object
		static InternalMessageService mso;
		static InternalMessageService messageService
		{
			get {
				if (mso == null)
					mso = new InternalMessageService ();
				return mso;
			}
		}
		
		//The real GTK# code is wrapped in a GuiSyncObject to make calls synchronous on the GUI thread
		class InternalMessageService : GuiSyncObject
		{
			public AlertButton GenericAlert (Window parent, MessageDescription message)
			{
				var dialog = new AlertDialog (message) {
					TransientFor = parent ?? IdeServices.DesktopService.GetFocusedTopLevelWindow ()
				};
				return dialog.Run ();
			}
			
			public string GetTextResponse (Window parent, string question, string caption, string initialValue, bool isPassword)
			{
				var dialog = new TextQuestionDialog {
					Question = question,
					Caption = caption,
					Value = initialValue,
					IsPassword = isPassword,
				};
				if (dialog.Run ())
					return dialog.Value;
				return null;
			}
		}
#endregion
	}
	
	public class MessageDescription
	{
		internal MessageDescription () : this (CancellationToken.None)
		{
		}
		
		internal MessageDescription (CancellationToken cancellationToken)
		{
			DefaultButton = -1;
			Buttons = new List<AlertButton> ();
			Options = new List<AlertOption> ();
			CancellationToken = cancellationToken;
		}
		
		internal IList<AlertButton> Buttons { get; private set; }
		internal IList<AlertOption> Options { get; private set; }
		
		internal AlertButton ApplyToAllButton { get; set; }
		
		public string Icon { get; set; }
		
		public string Text { get; set; }
		public string SecondaryText { get; set; }
		public bool AllowApplyToAll { get; set; }
		public int DefaultButton { get; set; }
		public CancellationToken CancellationToken { get; private set; }
		public bool UseMarkup { get; set; }

		public event EventHandler<AlertButtonEventArgs> AlertButtonClicked;

		internal bool NotifyClicked (AlertButton button)
		{
			var args = new AlertButtonEventArgs (button);
			if (AlertButtonClicked != null)
				AlertButtonClicked (this, args);
			return args.CloseDialog;
		}

		public void AddOption (string id, string text, bool setByDefault)
		{
			Options.Add (new AlertOption (id, text) { Value = setByDefault });
		}
		
		public bool GetOptionValue (string id)
		{
			foreach (var op in Options)
				if (op.Id == id)
					return op.Value;
			throw new ArgumentException ("Invalid option id");
		}
		
		public void SetOptionValue (string id, bool value)
		{
			foreach (var op in Options) {
				if (op.Id == id) {
					op.Value = value;
					return;
				}
			}
			throw new ArgumentException ("Invalid option id");
		}
	}
	
	public sealed class GenericMessage: MessageDescription
	{
		public GenericMessage () : base (CancellationToken.None)
		{
		}
		
		public GenericMessage (string text) : this () 
		{
			Text = text;
		}
		
		public GenericMessage (string text, string secondaryText) : this (text)
		{
			SecondaryText = secondaryText;
		}

		public GenericMessage (string text, string secondaryText, CancellationToken cancellationToken)
			: base (cancellationToken)
		{
			Text = text;
			SecondaryText = secondaryText;
		}
		
		public new IList<AlertButton> Buttons {
			get { return base.Buttons; }
		}
	}
	
	
	public sealed class QuestionMessage: MessageDescription
	{
		public QuestionMessage ()
		{
			Icon = MonoDevelop.Ide.Gui.Stock.Question;
		}
		
		public QuestionMessage (string text): this ()
		{
			Text = text;
		}
		
		public QuestionMessage (string text, string secondaryText): this (text)
		{
			SecondaryText = secondaryText;
		}
		
		public new IList<AlertButton> Buttons {
			get { return base.Buttons; }
		}
	}
	
	public sealed class ConfirmationMessage: MessageDescription
	{
		AlertButton confirmButton;
		
		public ConfirmationMessage ()
		{
			Icon = MonoDevelop.Ide.Gui.Stock.Question;
			Buttons.Add (AlertButton.Cancel);
		}
		
		public ConfirmationMessage (AlertButton button): this ()
		{
			ConfirmButton = button;
		}
		
		public ConfirmationMessage (string primaryText, AlertButton button): this (button)
		{
			Text = primaryText;
		}
		
		public ConfirmationMessage (string primaryText, string secondaryText, AlertButton button): this (primaryText, button)
		{
			SecondaryText = secondaryText;
		}
		
		public AlertButton ConfirmButton {
			get { return confirmButton; }
			set {
				if (Buttons.Count == 2)
					Buttons.RemoveAt (1);
				Buttons.Add (value);
				confirmButton = value;
			}
		}
		
		public bool ConfirmIsDefault {
			get {
				return DefaultButton == 1;
			}
			set {
				if (value)
					DefaultButton = 1;
				else
					DefaultButton = 0;
			}
		}
	}
}