Merge pull request #3600 from henricm/fix-win-network-info-tests
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / Application.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004 - 2006 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //      Daniel Nauck    (dna(at)mono-project(dot)de)
25 //
26
27 // COMPLETE
28
29 #undef DebugRunLoop
30
31 using Microsoft.Win32;
32 using System;
33 using System.Drawing;
34 using System.ComponentModel;
35 using System.Collections;
36 using System.Diagnostics;
37 using System.Globalization;
38 using System.IO;
39 using System.Reflection;
40 using System.Runtime.InteropServices;
41 using System.Threading;
42 using System.Text;
43 using System.Windows.Forms.VisualStyles;
44
45 namespace System.Windows.Forms
46 {
47         public sealed class Application
48         {
49                 internal class MWFThread
50                 {
51                         #region Fields
52
53                         private ApplicationContext context;
54                         private bool messageloop_started;
55                         private bool handling_exception;
56                         private int thread_id;
57
58                         private static readonly Hashtable threads = new Hashtable();
59
60                         #endregion      // Fields
61
62                         #region Constructors
63
64                         private MWFThread()
65                         {
66                         }
67
68                         #endregion      // Constructors
69
70                         #region Properties
71
72                         public ApplicationContext Context {
73                                 get { return context; }
74                                 set { context = value; }
75                         }
76
77                         public bool MessageLoop {
78                                 get { return messageloop_started; }
79                                 set { messageloop_started = value; }
80                         }
81
82                         public bool HandlingException {
83                                 get { return handling_exception; }
84                                 set { handling_exception = value; }
85                         }
86
87                         public static int LoopCount {
88                                 get {
89                                         lock (threads) {
90                                                 int loops = 0;
91
92                                                 foreach (MWFThread thread in threads.Values) {
93                                                         if (thread.messageloop_started)
94                                                                 loops++;
95                                                 }
96
97                                                 return loops;
98                                         }
99                                 }
100                         }
101
102                         public static MWFThread Current {
103                                 get {
104                                         MWFThread thread = null;
105
106                                         lock (threads) {
107                                                 thread = (MWFThread) threads [Thread.CurrentThread.GetHashCode ()];
108                                                 if (thread == null) {
109                                                         thread = new MWFThread();
110                                                         thread.thread_id = Thread.CurrentThread.GetHashCode ();
111                                                         threads [thread.thread_id] = thread;
112                                                 }
113                                         }
114
115                                         return thread;
116                                 }
117                         }
118
119                         #endregion      // Properties
120
121                         #region Methods
122
123                         public void Exit ()
124                         {
125                                 if (context != null)
126                                         context.ExitThread();
127                                 context = null;
128
129                                 if (Application.ThreadExit != null)
130                                         Application.ThreadExit(null, EventArgs.Empty);
131
132                                 if (LoopCount == 0) {
133                                         if (Application.ApplicationExit != null)
134                                                 Application.ApplicationExit (null, EventArgs.Empty);
135                                 }
136
137                                 ((MWFThread) threads [thread_id]).MessageLoop = false;
138                         }
139
140                         #endregion      // Methods
141                 }
142
143                 private static bool browser_embedded;
144                 private static InputLanguage input_language = InputLanguage.CurrentInputLanguage;
145                 private static string safe_caption_format = "{1} - {0} - {2}";
146                 private static readonly ArrayList message_filters = new ArrayList();
147                 private static readonly FormCollection forms = new FormCollection ();
148
149                 private static bool use_wait_cursor;
150                 private static ToolStrip keyboard_capture;
151                 private static VisualStyleState visual_style_state = VisualStyleState.ClientAndNonClientAreasEnabled;
152                 static bool visual_styles_enabled;
153
154                 private Application ()
155                 {
156                         browser_embedded = false;
157                 }
158
159                 static Application ()
160                 {
161                         // Attempt to load UIA support for winforms
162                         // UIA support requires .NET 2.0
163                         InitializeUIAutomation ();
164                 }
165
166                 #region Private Methods
167
168                 private static void InitializeUIAutomation ()
169                 {
170                         // Initialize the UIAutomationWinforms Global class,
171                         // which create some listeners which subscribe to internal
172                         // MWF events so that it can provide a11y support for MWF
173                         const string UIA_WINFORMS_ASSEMBLY = 
174                           "UIAutomationWinforms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f4ceacb585d99812";
175                         MethodInfo init_method;
176                         Assembly mwf_providers = null;
177                         try {
178                                 mwf_providers = Assembly.Load (UIA_WINFORMS_ASSEMBLY);
179                         } catch { }
180                         
181                         if (mwf_providers == null)
182                                 return;
183
184                         const string UIA_WINFORMS_TYPE     = "Mono.UIAutomation.Winforms.Global";
185                         const string UIA_WINFORMS_METHOD   = "Initialize";
186                         try {
187                                 Type global_type = mwf_providers.GetType (UIA_WINFORMS_TYPE, false);
188                                 if (global_type != null) {
189                                         init_method = global_type.GetMethod (UIA_WINFORMS_METHOD, 
190                                                                              BindingFlags.Static | 
191                                                                              BindingFlags.Public);
192                                         if (init_method != null)
193                                                 init_method.Invoke (null, new object [] {});
194                                         else
195                                                 throw new Exception (String.Format ("Method {0} not found in type {1}.",
196                                                                                     UIA_WINFORMS_METHOD, UIA_WINFORMS_TYPE));
197                                 }
198                                 else
199                                         throw new Exception (String.Format ("Type {0} not found in assembly {1}.",
200                                                                             UIA_WINFORMS_TYPE, UIA_WINFORMS_ASSEMBLY));
201                         } catch (Exception ex) {
202                                 Console.Error.WriteLine ("Error setting up UIA: " + ex);
203                         }
204                 }
205                 
206                 internal static void CloseForms (Thread thread)
207                 {
208                         #if DebugRunLoop
209                                 Console.WriteLine("   CloseForms({0}) called", thread);
210                         #endif
211
212                         ArrayList forms_to_close = new ArrayList ();
213
214                         lock (forms) {
215                                 foreach (Form f in forms) {
216                                         if (thread == null || thread == f.creator_thread)
217                                                 forms_to_close.Add (f);
218                                 }
219
220                                 foreach (Form f in forms_to_close) {
221                                         #if DebugRunLoop
222                                                 Console.WriteLine("      Closing form {0}", f);
223                                         #endif
224                                         f.Dispose ();
225                                 }
226                         }
227                 }
228
229                 #endregion      // Private methods
230
231                 #region Public Static Properties
232
233                 public static bool AllowQuit {
234                         get {
235                                 return !browser_embedded;
236                         }
237                 }
238
239                 public static string CommonAppDataPath {
240                         get {
241                                 return CreateDataPath (Environment.GetFolderPath (Environment.SpecialFolder.CommonApplicationData));
242                         }
243                 }
244
245                 public static RegistryKey CommonAppDataRegistry {
246                         get {
247                                 string key = string.Format ("Software\\{0}\\{1}\\{2}", CompanyName, ProductName, ProductVersion);
248
249                                 return Registry.LocalMachine.CreateSubKey (key);
250                         }
251                 }
252
253                 public static string CompanyName {
254                         get {
255                                 string company = string.Empty;
256
257                                 Assembly assembly = Assembly.GetEntryAssembly ();
258                                 
259                                 if (assembly == null)
260                                         assembly = Assembly.GetCallingAssembly ();
261
262                                 AssemblyCompanyAttribute[] attrs = (AssemblyCompanyAttribute[])
263                                         assembly.GetCustomAttributes (typeof(AssemblyCompanyAttribute), true);
264                                 if (attrs != null && attrs.Length > 0)
265                                         company = attrs [0].Company;
266
267                                 // If there is no [AssemblyCompany], return the outermost namespace
268                                 // on Main ()
269                                 if (company == null || company.Length == 0)
270                                         if (assembly.EntryPoint != null) {
271                                                 company = assembly.EntryPoint.DeclaringType.Namespace;
272
273                                                 if (company != null) {
274                                                         int firstDot = company.IndexOf ('.');
275                                                         if (firstDot >= 0)
276                                                                 company = company.Substring (0, firstDot);
277                                                 }
278                                         }
279
280                                 // If that doesn't work, return the name of class containing Main ()
281                                 if (company == null || company.Length == 0)
282                                         if (assembly.EntryPoint != null)
283                                                 company = assembly.EntryPoint.DeclaringType.FullName;
284                                 
285                                 return company;
286                         }
287                 }
288
289                 public static CultureInfo CurrentCulture {
290                         get {
291                                 return Thread.CurrentThread.CurrentUICulture;
292                         }
293                         set {
294                                 Thread.CurrentThread.CurrentUICulture = value;
295                         }
296                 }
297
298                 public static InputLanguage CurrentInputLanguage {
299                         get {
300                                 return input_language;
301                         }
302                         set {
303                                 input_language=value;
304                         }
305                 }
306
307                 public static string ExecutablePath {
308                         get {
309                                 return Path.GetFullPath (Environment.GetCommandLineArgs ()[0]);
310                         }
311                 }
312
313                 public static string LocalUserAppDataPath {
314                         get {
315                                 return CreateDataPath (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData));
316                         }
317                 }
318
319                 public static bool MessageLoop {
320                         get {
321                                 return MWFThread.Current.MessageLoop;
322                         }
323                 }
324
325                 public static string ProductName {
326                         get {
327                                 string name = string.Empty;
328                                 
329                                 Assembly assembly = Assembly.GetEntryAssembly ();
330                                 
331                                 if (assembly == null)
332                                         assembly = Assembly.GetCallingAssembly ();
333
334                                 AssemblyProductAttribute[] attrs = (AssemblyProductAttribute[])
335                                         assembly.GetCustomAttributes (typeof(AssemblyProductAttribute), true);
336
337                                 if (attrs != null && attrs.Length > 0)
338                                         name = attrs [0].Product;
339
340                                 // If there is no [AssemblyProduct], .NET returns the name of 
341                                 // the innermost namespace and if that fails, resorts to the 
342                                 // name of the class containing Main ()
343                                 if (name == null || name.Length == 0)
344                                         if (assembly.EntryPoint != null) {
345                                                 name = assembly.EntryPoint.DeclaringType.Namespace;
346
347                                                 if (name != null) {
348                                                         int lastDot = name.LastIndexOf ('.');
349                                                         if (lastDot >= 0 && lastDot < name.Length - 1)
350                                                                 name = name.Substring (lastDot + 1);
351                                                 }
352
353                                                 if (name == null || name.Length == 0)
354                                                         name = assembly.EntryPoint.DeclaringType.FullName;
355                                         }
356
357                                 return name;
358                         }
359                 }
360
361                 public static string ProductVersion {
362                         get {
363                                 String version = string.Empty;
364
365                                 Assembly assembly = Assembly.GetEntryAssembly ();
366                                 
367                                 if (assembly == null)
368                                         assembly = Assembly.GetCallingAssembly ();
369
370                                 AssemblyInformationalVersionAttribute infoVersion =
371                                         Attribute.GetCustomAttribute (assembly,
372                                         typeof (AssemblyInformationalVersionAttribute))
373                                         as AssemblyInformationalVersionAttribute;
374                                         
375                                 if (infoVersion != null)
376                                         version = infoVersion.InformationalVersion;
377
378                                 // If [AssemblyFileVersion] is present it is used
379                                 // before resorting to assembly version
380                                 if (version == null || version.Length == 0) {
381                                         AssemblyFileVersionAttribute fileVersion =
382                                                 Attribute.GetCustomAttribute (assembly,
383                                                 typeof (AssemblyFileVersionAttribute))
384                                                 as AssemblyFileVersionAttribute;
385                                         if (fileVersion != null)
386                                                 version = fileVersion.Version;
387                                 }
388
389                                 // If neither [AssemblyInformationalVersionAttribute]
390                                 // nor [AssemblyFileVersion] are present, then use
391                                 // the assembly version
392                                 if (version == null || version.Length == 0)
393                                         version = assembly.GetName ().Version.ToString ();
394
395                                 return version;
396                         }
397                 }
398
399                 public static string SafeTopLevelCaptionFormat {
400                         get {
401                                 return safe_caption_format;
402                         }
403                         set {
404                                 safe_caption_format = value;
405                         }
406                 }
407
408                 public static string StartupPath {
409                         get {
410                                 return Path.GetDirectoryName (Application.ExecutablePath);
411                         }
412                 }
413
414                 public static string UserAppDataPath {
415                         get {
416                                 return CreateDataPath (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData));
417                         }
418                 }
419
420                 public static RegistryKey UserAppDataRegistry {
421                         get {
422                                 string key = string.Format ("Software\\{0}\\{1}\\{2}", CompanyName, ProductName, ProductVersion);
423                                 
424                                 return Registry.CurrentUser.CreateSubKey (key);
425                         }
426                 }
427
428                 public static bool UseWaitCursor {
429                         get {
430                                 return use_wait_cursor;
431                         }
432                         set {
433                                 use_wait_cursor = value;
434                                 if (use_wait_cursor) {
435                                         foreach (Form form in OpenForms) {
436                                                 form.Cursor = Cursors.WaitCursor;
437                                         }
438                                 }
439                         }
440                 }
441
442                 public static bool RenderWithVisualStyles {
443                         get {
444                                 if (VisualStyleInformation.IsSupportedByOS) {
445                                         if (!VisualStyleInformation.IsEnabledByUser)
446                                                 return false;
447                                         if (!XplatUI.ThemesEnabled)
448                                                 return false;
449                                         if (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled)
450                                                 return true;
451                                         if (Application.VisualStyleState == VisualStyleState.ClientAreaEnabled)
452                                                 return true;
453                                 }
454                                 return false;
455                         }
456                 }
457
458                 public static VisualStyleState VisualStyleState {
459                         get { return Application.visual_style_state; }
460                         set { Application.visual_style_state = value; }
461                 }
462
463                 #endregion
464
465                 #region Public Static Methods
466
467                 public static void AddMessageFilter (IMessageFilter value)
468                 {
469                         lock (message_filters) {
470                                 message_filters.Add (value);
471                         }
472                 }
473
474                 internal static void AddKeyFilter (IKeyFilter value)
475                 {
476                         XplatUI.AddKeyFilter (value);
477                 }
478
479                 public static void DoEvents ()
480                 {
481                         XplatUI.DoEvents ();
482                 }
483
484                 public static void EnableVisualStyles ()
485                 {
486                         visual_styles_enabled = true;
487                         XplatUI.EnableThemes ();
488                 }
489
490                 [EditorBrowsable (EditorBrowsableState.Advanced)]
491                 public static bool FilterMessage (ref Message message)
492                 {
493                         lock (message_filters) {
494                                 for (int i = 0; i < message_filters.Count; i++) {
495                                         IMessageFilter filter = (IMessageFilter) message_filters[i];
496                                         if (filter.PreFilterMessage (ref message))
497                                                 return true;
498                                 }
499                         }
500                         return false;
501                 }
502                 
503                 //
504                 // If true, it uses GDI+, performance reasons were quoted
505                 //
506                 static internal bool use_compatible_text_rendering = true;
507                 
508                 public static void SetCompatibleTextRenderingDefault (bool defaultValue)
509                 {
510                         use_compatible_text_rendering = defaultValue;
511                 }
512
513                 public static FormCollection OpenForms {
514                         get {
515                                 return forms;
516                         }
517                 }
518                 
519                 [MonoNotSupported ("Only applies when Winforms is being hosted by an unmanaged app.")]
520                 [EditorBrowsable (EditorBrowsableState.Advanced)]
521                 public static void RegisterMessageLoop (MessageLoopCallback callback)
522                 {
523                 }
524
525                 [MonoNotSupported ("Empty stub.")]
526                 public static bool SetSuspendState (PowerState state, bool force, bool disableWakeEvent)
527                 {
528                         return false;
529                 }
530
531                 [MonoNotSupported ("Empty stub.")]
532                 public static void SetUnhandledExceptionMode (UnhandledExceptionMode mode)
533                 {
534                         //FIXME: a stub to fill
535                 }
536
537                 [MonoNotSupported ("Empty stub.")]
538                 public static void SetUnhandledExceptionMode (UnhandledExceptionMode mode, bool threadScope)
539                 {
540                         //FIXME: a stub to fill
541                 }
542
543                 [MonoNotSupported ("Only applies when Winforms is being hosted by an unmanaged app.")]
544                 [EditorBrowsable (EditorBrowsableState.Advanced)]
545                 public static void UnregisterMessageLoop ()
546                 {
547                 }
548
549                 [EditorBrowsable (EditorBrowsableState.Advanced)]
550                 public static void RaiseIdle (EventArgs e)
551                 {
552                         XplatUI.RaiseIdle (e);
553                 }
554                 
555                 public static void Restart ()
556                 {
557                         //FIXME: ClickOnce stuff using the Update or UpdateAsync methods.
558                         //FIXME: SecurityPermission: Restart () requires IsUnrestricted permission.
559
560                         if (Assembly.GetEntryAssembly () == null)
561                                 throw new NotSupportedException ("The method 'Restart' is not supported by this application type.");
562
563                         string mono_path = MonoToolsLocator.Mono;
564
565                         //Get command line arguments
566                         StringBuilder argsBuilder = new StringBuilder ();
567                         string[] args = Environment.GetCommandLineArgs ();
568                         for (int i = 0; i < args.Length; i++)
569                         {
570                                 argsBuilder.Append (string.Format ("\"{0}\" ", args[i]));
571                         }
572                         string arguments = argsBuilder.ToString ();
573                         ProcessStartInfo procInfo = Process.GetCurrentProcess ().StartInfo;
574
575                         if (mono_path == null) { //it is .NET on Windows
576                                 procInfo.FileName = args[0];
577                                 procInfo.Arguments = arguments.Remove (0, args[0].Length + 3); //1 space and 2 quotes
578                         }
579                         else {
580                                 procInfo.Arguments = arguments;
581                                 procInfo.FileName = mono_path;
582                         }
583
584                         procInfo.WorkingDirectory = Environment.CurrentDirectory;
585
586                         Application.Exit ();
587                         Process.Start (procInfo);
588                 }
589
590                 public static void Exit ()
591                 {
592                         Exit (new CancelEventArgs ());
593                 }
594
595                 [EditorBrowsable (EditorBrowsableState.Advanced)]
596                 public static void Exit (CancelEventArgs e)
597                 {
598                         ArrayList forms_to_close;
599                         
600                         lock (forms) {
601                                 forms_to_close = new ArrayList (forms);
602
603                                 foreach (Form f in forms_to_close) {
604                                         // Give each form a chance to cancel the Application.Exit
605                                         e.Cancel = f.FireClosingEvents (CloseReason.ApplicationExitCall, false);
606
607                                         if (e.Cancel)
608                                                 return;
609
610                                         f.suppress_closing_events = true;
611                                         f.Close ();
612                                         f.Dispose ();
613                                 }
614                         }
615
616                         XplatUI.PostQuitMessage (0);
617                 }
618
619                 public static void ExitThread()
620                 {
621                         CloseForms(Thread.CurrentThread);
622                         // this might not be right - need to investigate (somehow) if a WM_QUIT message is generated here
623                         XplatUI.PostQuitMessage(0);
624                 }
625
626                 public static ApartmentState OleRequired ()
627                 {
628                         //throw new NotImplementedException("OLE Not supported by this System.Windows.Forms implementation");
629                         return ApartmentState.Unknown;
630                 }
631
632                 public static void OnThreadException (Exception t)
633                 {
634                         if (MWFThread.Current.HandlingException) {
635                                 /* we're already handling an exception and we got
636                                    another one?  print it out and exit, this means
637                                    we've got a runtime/SWF bug. */
638                                 Console.WriteLine (t);
639                                 // Don't use Application.Exit here, since it may cause a stack overflow
640                                 // in certain cases. It's however hard to reproduce since it seems to 
641                                 // be depending on when the GC kicks in.
642                                 Environment.Exit(1);
643                         }
644
645                         try {
646                                 MWFThread.Current.HandlingException = true;
647
648                                 if (Application.ThreadException != null) {
649                                         Application.ThreadException(null, new ThreadExceptionEventArgs(t));
650                                         return;
651                                 }
652
653                                 if (SystemInformation.UserInteractive) {
654                                         Form form = new ThreadExceptionDialog (t);
655                                         form.ShowDialog ();
656                                 } else {
657                                         Console.WriteLine (t.ToString ());
658                                         Application.Exit ();
659                                 }
660                         } finally {
661                                 MWFThread.Current.HandlingException = false;
662                         }
663                 }
664
665                 public static void RemoveMessageFilter (IMessageFilter value)
666                 {
667                         lock (message_filters) {
668                                 message_filters.Remove (value);
669                         }
670                 }
671
672                 public static void Run ()
673                 {
674                         Run (new ApplicationContext ());
675                 }
676
677                 public static void Run (Form mainForm)
678                 {
679                         Run (new ApplicationContext (mainForm));
680                 }
681
682                 internal static void FirePreRun ()
683                 {
684                         EventHandler handler = PreRun;
685                         if (handler != null)
686                                 handler (null, EventArgs.Empty);
687                 }
688
689                 public static void Run (ApplicationContext context)
690                 {
691                         // If a sync context hasn't been created by now, create
692                         // a default one
693                         if (SynchronizationContext.Current == null)
694                                 SynchronizationContext.SetSynchronizationContext (new SynchronizationContext ());
695                                 
696                         RunLoop (false, context);
697                         
698                         // Reset the sync context back to the default
699                         if (SynchronizationContext.Current is WindowsFormsSynchronizationContext)
700                                 WindowsFormsSynchronizationContext.Uninstall ();
701                 }
702
703                 private static void DisableFormsForModalLoop (Queue toplevels, ApplicationContext context)
704                 {
705                         Form f;
706
707                         lock (forms) {
708                                 IEnumerator control = forms.GetEnumerator ();
709
710                                 while (control.MoveNext ()) {
711                                         f = (Form)control.Current;
712
713                                         // Don't disable the main form.
714                                         if (f == context.MainForm) {
715                                                 continue;
716                                         }
717
718                                         // Don't disable any children of the main form.
719                                         // These do not have to be MDI children.
720                                         Control current = f;
721                                         bool is_child_of_main = false; ;
722
723                                         do {
724                                                 if (current.Parent == context.MainForm) {
725                                                         is_child_of_main = true;
726                                                         break;
727                                                 }
728                                                 current = current.Parent;
729                                         } while (current != null);
730
731                                         if (is_child_of_main)
732                                                 continue;
733
734                                         // Disable the rest
735                                         if (f.IsHandleCreated && XplatUI.IsEnabled (f.Handle)) {
736 #if DebugRunLoop
737                                                 Console.WriteLine("      Disabling form {0}", f);
738 #endif
739                                                 XplatUI.EnableWindow (f.Handle, false);
740                                                 toplevels.Enqueue (f);
741                                         }
742                                 }
743                         }
744                                 
745                 }
746                 
747                 
748                 private static void EnableFormsForModalLoop (Queue toplevels, ApplicationContext context)
749                 {
750                         while (toplevels.Count > 0) {
751 #if DebugRunLoop
752                                 Console.WriteLine("      Re-Enabling form form {0}", toplevels.Peek());
753 #endif
754                                 Form c = (Form) toplevels.Dequeue ();
755                                 if (c.IsHandleCreated) {
756                                         XplatUI.EnableWindow (c.window.Handle, true);
757                                         context.MainForm = c;
758                                 }
759                         }
760 #if DebugRunLoop
761                         Console.WriteLine("   Done with the re-enable");
762 #endif
763                 }
764
765                 internal static void RunLoop (bool Modal, ApplicationContext context)
766                 {
767                         Queue           toplevels;
768                         MSG             msg;
769                         Object          queue_id;
770                         MWFThread       thread;
771                         ApplicationContext previous_thread_context;
772                         
773                         thread = MWFThread.Current;
774
775                         /*
776                          * There is a NotWorking test for this, but since we are using this method both for Form.ShowDialog as for ApplicationContexts we'll
777                          * fail on nested ShowDialogs, so disable the check for the moment.
778                          */
779                         //if (thread.MessageLoop) {
780                         //        throw new InvalidOperationException ("Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.");
781                         //}
782
783                         msg = new MSG();
784
785                         if (context == null)
786                                 context = new ApplicationContext();
787                 
788                         previous_thread_context = thread.Context;
789                         thread.Context = context;
790
791                         if (context.MainForm != null) {
792                                 context.MainForm.is_modal = Modal;
793                                 context.MainForm.context = context;
794                                 context.MainForm.closing = false;
795                                 context.MainForm.Visible = true;        // Cannot use Show() or scaling gets confused by menus
796                                 // XXX the above line can be used to close the form. another problem with our handling of Show/Activate.
797                                 if (context.MainForm != null)
798                                         context.MainForm.Activate();
799                         }
800
801                         #if DebugRunLoop
802                                 Console.WriteLine("Entering RunLoop(Modal={0}, Form={1})", Modal, context.MainForm != null ? context.MainForm.ToString() : "NULL");
803                         #endif
804
805                         if (Modal) {
806                                 toplevels = new Queue ();
807                                 DisableFormsForModalLoop (toplevels, context);
808                                 
809                                 // FIXME - need activate?
810                                 /* make sure the MainForm is enabled */
811                                 if (context.MainForm != null) {
812                                         XplatUI.EnableWindow (context.MainForm.Handle, true);
813                                         XplatUI.SetModal(context.MainForm.Handle, true);
814                                 }
815                         } else {
816                                 toplevels = null;
817                         }
818
819                         queue_id = XplatUI.StartLoop(Thread.CurrentThread);
820                         thread.MessageLoop = true;
821
822                         bool quit = false;
823
824                         while (!quit && XplatUI.GetMessage(queue_id, ref msg, IntPtr.Zero, 0, 0)) {
825                                 Message m = Message.Create(msg.hwnd, (int)msg.message, msg.wParam, msg.lParam);
826                                 
827                                 if (Application.FilterMessage (ref m))
828                                         continue;
829                                         
830                                 switch((Msg)msg.message) {
831                                 case Msg.WM_KEYDOWN:
832                                 case Msg.WM_SYSKEYDOWN:
833                                 case Msg.WM_CHAR:
834                                 case Msg.WM_SYSCHAR:
835                                 case Msg.WM_KEYUP:
836                                 case Msg.WM_SYSKEYUP:
837                                         Control c;
838                                         c = Control.FromHandle(msg.hwnd);
839
840                                         // If we have a control with keyboard capture (usually a *Strip)
841                                         // give it the message, and then drop the message
842                                         if (keyboard_capture != null) {
843                                                 // WM_SYSKEYUP does not make it into ProcessCmdKey, so do it here
844                                                 if ((Msg)m.Msg == Msg.WM_SYSKEYDOWN)
845                                                         if (m.WParam.ToInt32() == (int)Keys.Menu) {
846                                                                 keyboard_capture.GetTopLevelToolStrip ().Dismiss (ToolStripDropDownCloseReason.Keyboard);
847                                                                 continue;
848                                                         }
849
850                                                 m.HWnd = keyboard_capture.Handle;
851
852                                                 switch (keyboard_capture.PreProcessControlMessageInternal (ref m)) {
853                                                         case PreProcessControlState.MessageProcessed:
854                                                                 continue;
855                                                         case PreProcessControlState.MessageNeeded:
856                                                         case PreProcessControlState.MessageNotNeeded:
857                                                                 if (((m.Msg == (int)Msg.WM_KEYDOWN || m.Msg == (int)Msg.WM_CHAR) && !keyboard_capture.ProcessControlMnemonic ((char)m.WParam))) {
858                                                                         if (c == null || !ControlOnToolStrip (c))
859                                                                                 continue;
860                                                                         else
861                                                                                 m.HWnd = msg.hwnd;
862                                                                 } else
863                                                                         continue;
864                                                                 
865                                                                 break;
866                                                 }
867                                         }
868
869                                         if (((c != null) && c.PreProcessControlMessageInternal (ref m) != PreProcessControlState.MessageProcessed) ||
870                                                 (c == null)) {
871                                                 goto default;
872                                         } 
873                                         break;
874
875                                 case Msg.WM_LBUTTONDOWN:
876                                 case Msg.WM_MBUTTONDOWN:
877                                 case Msg.WM_RBUTTONDOWN:
878                                         if (keyboard_capture != null) {
879                                                 Control c2 = Control.FromHandle (msg.hwnd);
880
881                                                 // the target is not a winforms control (an embedded control, perhaps), so
882                                                 // release everything
883                                                 if (c2 == null) {
884                                                         ToolStripManager.FireAppClicked ();
885                                                         goto default;
886                                                 }
887
888                                                 // If we clicked a ToolStrip, we have to make sure it isn't
889                                                 // the one we are on, or any of its parents or children
890                                                 // If we clicked off the dropped down menu, release everything
891                                                 if (c2 is ToolStrip) {
892                                                         if ((c2 as ToolStrip).GetTopLevelToolStrip () != keyboard_capture.GetTopLevelToolStrip ())
893                                                                 ToolStripManager.FireAppClicked ();
894                                                 } else {
895                                                         if (c2.Parent != null)
896                                                                 if (c2.Parent is ToolStripDropDownMenu)
897                                                                         if ((c2.Parent as ToolStripDropDownMenu).GetTopLevelToolStrip () == keyboard_capture.GetTopLevelToolStrip ())
898                                                                                 goto default;
899                                                         if (c2.TopLevelControl == null)
900                                                                 goto default;
901                                                                 
902                                                         ToolStripManager.FireAppClicked ();
903                                                 }
904                                         }
905                                         
906                                         goto default;
907
908                                 case Msg.WM_QUIT:
909                                         quit = true; // make sure we exit
910                                         break;
911                                 default:
912                                         XplatUI.TranslateMessage (ref msg);
913                                         XplatUI.DispatchMessage (ref msg);
914                                         break;
915                                 }
916
917                                 // If our Form doesn't have a handle anymore, it means it was destroyed and we need to *wait* for WM_QUIT.
918                                 if ((context.MainForm != null) && (!context.MainForm.IsHandleCreated))
919                                         continue;
920
921                                 // Handle exit, Form might have received WM_CLOSE and set 'closing' in response.
922                                 if ((context.MainForm != null) && (context.MainForm.closing || (Modal && !context.MainForm.Visible))) {
923                                         if (!Modal) {
924                                                 XplatUI.PostQuitMessage (0);
925                                         } else {
926                                                 break;
927                                         }
928                                 }
929                         }
930                         #if DebugRunLoop
931                                 Console.WriteLine ("   RunLoop loop left");
932                         #endif
933
934                         thread.MessageLoop = false;
935                         XplatUI.EndLoop (Thread.CurrentThread);
936
937                         if (Modal) {
938                                 Form old = context.MainForm;
939
940                                 context.MainForm = null;
941
942                                 EnableFormsForModalLoop (toplevels, context);
943                                 
944                                 if (old != null && old.IsHandleCreated) {
945                                         XplatUI.SetModal (old.Handle, false);
946                                 }
947                                 #if DebugRunLoop
948                                         Console.WriteLine ("   Done with the SetModal");
949                                 #endif
950                                 old.RaiseCloseEvents (true, false);
951                                 old.is_modal = false;
952                         }
953
954                         #if DebugRunLoop
955                                 Console.WriteLine ("Leaving RunLoop(Modal={0}, Form={1})", Modal, context.MainForm != null ? context.MainForm.ToString() : "NULL");
956                         #endif
957
958                         if (context.MainForm != null) {
959                                 context.MainForm.context = null;
960                                 context.MainForm = null;
961                         }
962
963                         thread.Context = previous_thread_context;
964
965                         if (!Modal)
966                                 thread.Exit();
967                 }
968
969                 #endregion      // Public Static Methods
970
971                 #region Events
972
973                 public static event EventHandler ApplicationExit;
974
975                 public static event EventHandler Idle {
976                         add {
977                                 XplatUI.Idle += value;
978                         }
979                         remove {
980                                 XplatUI.Idle -= value;
981                         }
982                 }
983
984                 public static event EventHandler ThreadExit;
985                 public static event ThreadExceptionEventHandler ThreadException;
986                 
987                 // These are used externally by the UIA framework
988                 internal static event EventHandler FormAdded;
989                 internal static event EventHandler PreRun;
990
991 #pragma warning disable 0067
992                 [MonoTODO]
993                 [EditorBrowsable (EditorBrowsableState.Advanced)]
994                 public static event EventHandler EnterThreadModal;
995
996                 [MonoTODO]
997                 [EditorBrowsable (EditorBrowsableState.Advanced)]
998                 public static event EventHandler LeaveThreadModal;
999 #pragma warning restore 0067
1000
1001                 #endregion      // Events
1002
1003                 #region Public Delegates
1004
1005                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1006                 public delegate bool MessageLoopCallback ();
1007
1008                 #endregion
1009                 
1010                 #region Internal Properties
1011                 internal static ToolStrip KeyboardCapture {
1012                         get { return keyboard_capture; }
1013                         set { keyboard_capture = value; }
1014                 }
1015
1016                 internal static bool VisualStylesEnabled {
1017                         get { return visual_styles_enabled; }
1018                 }
1019                 #endregion
1020                 
1021                 #region Internal Methods
1022
1023                 internal static void AddForm (Form f)
1024                 {
1025                         lock (forms)
1026                                 forms.Add (f);
1027                         // Signal that a Form has been added to this
1028                         // Application. Used by UIA to detect new Forms that
1029                         // need a11y support. This event may be fired even if
1030                         // the form has already been added, so clients should
1031                         // account for that when handling this signal.
1032                         if (FormAdded != null)
1033                                 FormAdded (f, null);
1034                 }
1035                 
1036                 internal static void RemoveForm (Form f)
1037                 {
1038                         lock (forms)
1039                                 forms.Remove (f);
1040                 }
1041
1042                 private static bool ControlOnToolStrip (Control c)
1043                 {
1044                         Control p = c.Parent;
1045                         
1046                         while (p != null) {
1047                                 if (p is ToolStrip)
1048                                         return true;
1049                                         
1050                                 p = p.Parent;
1051                         }
1052                         
1053                         return false;
1054                 }
1055
1056                 // Takes a starting path, appends company name, product name, and
1057                 // product version.  If the directory doesn't exist, create it
1058                 private static string CreateDataPath (string basePath)
1059                 {
1060                         string path;
1061
1062                         path = Path.Combine (basePath, CompanyName);
1063                         path = Path.Combine (path, ProductName);
1064                         path = Path.Combine (path, ProductVersion);
1065
1066                         if (!Directory.Exists (path))
1067                                 Directory.CreateDirectory (path);
1068                         
1069                         return path;
1070                 }
1071                 #endregion
1072         }
1073 }