Properly update the combo box selected text when the current item is changed, and...
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Application.cs
index 8c92d9c08a0a0ad5e843c56decb413b435f478e3..2c2d5bf931d3f08343f4a39947239bc0adacce93 100644 (file)
@@ -39,9 +39,7 @@ using System.IO;
 using System.Reflection;
 using System.Runtime.InteropServices;
 using System.Threading;
-#if NET_2_0
 using System.Text;
-#endif
 using System.Windows.Forms.VisualStyles;
 
 namespace System.Windows.Forms
@@ -148,10 +146,8 @@ namespace System.Windows.Forms
                private static readonly ArrayList message_filters = new ArrayList();
                private static readonly FormCollection forms = new FormCollection ();
 
-#if NET_2_0
                private static bool use_wait_cursor;
                private static ToolStrip keyboard_capture;
-#endif
                private static VisualStyleState visual_style_state = VisualStyleState.ClientAndNonClientAreasEnabled;
                static bool visual_styles_enabled;
 
@@ -160,76 +156,52 @@ namespace System.Windows.Forms
                        browser_embedded = false;
                }
 
-#if NET_2_0
                static Application ()
                {
                        // Attempt to load UIA support for winforms
                        // UIA support requires .NET 2.0
                        InitializeUIAutomation ();
                }
-#endif
 
                #region Private Methods
 
-#if NET_2_0
                private static void InitializeUIAutomation ()
                {
-                       // Initialize the UIAutomationWinforms FormListener
-                       // class, which signs up for the PreRun and FormAdded
-                       // events so that it can provide a11y support for MWF.
-                       string uia_winforms_assembly = "UIAutomationWinforms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f4ceacb585d99812";
-                       string listener_type_name = string.Empty;
-                       string init_method_name = "Initialize";
+                       // Initialize the UIAutomationWinforms Global class,
+                       // which create some listeners which subscribe to internal
+                       // MWF events so that it can provide a11y support for MWF
+                       const string UIA_WINFORMS_ASSEMBLY = 
+                         "UIAutomationWinforms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f4ceacb585d99812";
                        MethodInfo init_method;
-                       
                        Assembly mwf_providers = null;
                        try {
-                               mwf_providers = Assembly.Load (uia_winforms_assembly);
+                               mwf_providers = Assembly.Load (UIA_WINFORMS_ASSEMBLY);
                        } catch { }
                        
                        if (mwf_providers == null)
                                return;
-                       
+
+                       const string UIA_WINFORMS_TYPE     = "Mono.UIAutomation.Winforms.Global";
+                       const string UIA_WINFORMS_METHOD   = "Initialize";
                        try {
-                               //FormListener
-                               listener_type_name = "Mono.UIAutomation.Winforms.FormListener";
-                               Type listener_type = mwf_providers.GetType (listener_type_name, false);
-                               if (listener_type != null) {
-                                       init_method = listener_type.GetMethod (init_method_name, BindingFlags.Static | BindingFlags.Public);
-                                       if (init_method != null)
-                                               init_method.Invoke (null, new object [] {});
-                               }
-                               //ToolTipListener
-                               listener_type_name = "Mono.UIAutomation.Winforms.ToolTipListener";
-                               listener_type = mwf_providers.GetType (listener_type_name, false);
-                               if (listener_type != null) {
-                                       init_method = listener_type.GetMethod (init_method_name, BindingFlags.Static | BindingFlags.Public);
-                                       if (init_method != null)
-                                               init_method.Invoke (null, new object [] {});
-                               }
-                               //HelpProviderListener
-                               listener_type_name = "Mono.UIAutomation.Winforms.HelpProviderListener";
-                               listener_type = mwf_providers.GetType (listener_type_name, false);
-                               if (listener_type != null) {
-                                       init_method = listener_type.GetMethod (init_method_name, BindingFlags.Static | BindingFlags.Public);
-                                       if (init_method != null)
-                                               init_method.Invoke (null, new object [] {});
-                               }
-                               //ErrorProviderListener
-                               listener_type_name = "Mono.UIAutomation.Winforms.ErrorProviderListener";
-                               listener_type = mwf_providers.GetType (listener_type_name, false);
-                               if (listener_type != null) {
-                                       init_method = listener_type.GetMethod (init_method_name, BindingFlags.Static | BindingFlags.Public);
+                               Type global_type = mwf_providers.GetType (UIA_WINFORMS_TYPE, false);
+                               if (global_type != null) {
+                                       init_method = global_type.GetMethod (UIA_WINFORMS_METHOD, 
+                                                                            BindingFlags.Static | 
+                                                                            BindingFlags.Public);
                                        if (init_method != null)
                                                init_method.Invoke (null, new object [] {});
+                                       else
+                                               throw new Exception (String.Format ("Method {0} not found in type {1}.",
+                                                                                   UIA_WINFORMS_METHOD, UIA_WINFORMS_TYPE));
                                }
+                               else
+                                       throw new Exception (String.Format ("Type {0} not found in assembly {1}.",
+                                                                           UIA_WINFORMS_TYPE, UIA_WINFORMS_ASSEMBLY));
                        } catch (Exception ex) {
                                Console.Error.WriteLine ("Error setting up UIA: " + ex);
                        }
-
-
                }
-#endif
                
                internal static void CloseForms (Thread thread)
                {
@@ -453,7 +425,6 @@ namespace System.Windows.Forms
                        }
                }
 
-#if NET_2_0
                public static bool UseWaitCursor {
                        get {
                                return use_wait_cursor;
@@ -467,14 +438,8 @@ namespace System.Windows.Forms
                                }
                        }
                }
-#endif
-               
-#if NET_2_0
-               public
-#else
-               internal
-#endif
-               static bool RenderWithVisualStyles {
+
+               public static bool RenderWithVisualStyles {
                        get {
                                if (VisualStyleInformation.IsSupportedByOS) {
                                        if (!VisualStyleInformation.IsEnabledByUser)
@@ -490,12 +455,7 @@ namespace System.Windows.Forms
                        }
                }
 
-#if NET_2_0
-               public 
-#else
-               internal
-#endif
-               static VisualStyleState VisualStyleState {
+               public static VisualStyleState VisualStyleState {
                        get { return Application.visual_style_state; }
                        set { Application.visual_style_state = value; }
                }
@@ -527,11 +487,8 @@ namespace System.Windows.Forms
                        XplatUI.EnableThemes ();
                }
 
-#if NET_2_0
                [EditorBrowsable (EditorBrowsableState.Advanced)]
-               public
-#endif
-               static bool FilterMessage (ref Message message)
+               public static bool FilterMessage (ref Message message)
                {
                        lock (message_filters) {
                                for (int i = 0; i < message_filters.Count; i++) {
@@ -543,7 +500,6 @@ namespace System.Windows.Forms
                        return false;
                }
                
-#if NET_2_0
                //
                // If true, it uses GDI+, performance reasons were quoted
                //
@@ -658,19 +614,12 @@ namespace System.Windows.Forms
                        Application.Exit ();
                        Process.Start (procInfo);
                }
-#endif
 
                public static void Exit ()
                {
-#if NET_2_0
                        Exit (new CancelEventArgs ());
-#else
-                       XplatUI.PostQuitMessage (0);
-                       CloseForms (null);
-#endif
                }
 
-#if NET_2_0
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public static void Exit (CancelEventArgs e)
                {
@@ -694,7 +643,6 @@ namespace System.Windows.Forms
 
                        XplatUI.PostQuitMessage (0);
                }
-#endif
 
                public static void ExitThread()
                {
@@ -759,27 +707,25 @@ namespace System.Windows.Forms
                        Run (new ApplicationContext (mainForm));
                }
 
+               internal static void FirePreRun ()
+               {
+                       EventHandler handler = PreRun;
+                       if (handler != null)
+                               handler (null, EventArgs.Empty);
+               }
+
                public static void Run (ApplicationContext context)
                {
-#if NET_2_0
-                       // Signal that the Application loop is about to run.
-                       // This allows UIA to initialize a11y support for MWF
-                       // before the main loop begins.
-                       if (PreRun != null)
-                               PreRun (null, EventArgs.Empty);
                        // If a sync context hasn't been created by now, create
                        // a default one
                        if (SynchronizationContext.Current == null)
                                SynchronizationContext.SetSynchronizationContext (new SynchronizationContext ());
-#endif
                                
                        RunLoop (false, context);
                        
-#if NET_2_0
                        // Reset the sync context back to the default
                        if (SynchronizationContext.Current is WindowsFormsSynchronizationContext)
                                WindowsFormsSynchronizationContext.Uninstall ();
-#endif
                }
 
                private static void DisableFormsForModalLoop (Queue toplevels, ApplicationContext context)
@@ -919,12 +865,11 @@ namespace System.Windows.Forms
                                        Control c;
                                        c = Control.FromHandle(msg.hwnd);
 
-#if NET_2_0
                                        // If we have a control with keyboard capture (usually a *Strip)
                                        // give it the message, and then drop the message
                                        if (keyboard_capture != null) {
                                                // WM_SYSKEYUP does not make it into ProcessCmdKey, so do it here
-                                               if ((Msg)m.Msg == Msg.WM_SYSKEYUP)
+                                               if ((Msg)m.Msg == Msg.WM_SYSKEYDOWN)
                                                        if (m.WParam.ToInt32() == (int)Keys.Menu) {
                                                                keyboard_capture.GetTopLevelToolStrip ().Dismiss (ToolStripDropDownCloseReason.Keyboard);
                                                                continue;
@@ -948,20 +893,26 @@ namespace System.Windows.Forms
                                                                break;
                                                }
                                        }
-#endif
 
                                        if (((c != null) && c.PreProcessControlMessageInternal (ref m) != PreProcessControlState.MessageProcessed) ||
                                                (c == null)) {
                                                goto default;
                                        } 
                                        break;
-#if NET_2_0
+
                                case Msg.WM_LBUTTONDOWN:
                                case Msg.WM_MBUTTONDOWN:
                                case Msg.WM_RBUTTONDOWN:
                                        if (keyboard_capture != null) {
                                                Control c2 = Control.FromHandle (msg.hwnd);
-                                               
+
+                                               // the target is not a winforms control (an embedded control, perhaps), so
+                                               // release everything
+                                               if (c2 == null) {
+                                                       ToolStripManager.FireAppClicked ();
+                                                       goto default;
+                                               }
+
                                                // If we clicked a ToolStrip, we have to make sure it isn't
                                                // the one we are on, or any of its parents or children
                                                // If we clicked off the dropped down menu, release everything
@@ -981,7 +932,7 @@ namespace System.Windows.Forms
                                        }
                                        
                                        goto default;
-#endif
+
                                case Msg.WM_QUIT:
                                        quit = true; // make sure we exit
                                        break;
@@ -991,7 +942,11 @@ namespace System.Windows.Forms
                                        break;
                                }
 
-                               // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
+                               // If our Form doesn't have a handle anymore, it means it was destroyed and we need to *wait* for WM_QUIT.
+                               if ((context.MainForm != null) && (!context.MainForm.IsHandleCreated))
+                                       continue;
+
+                               // Handle exit, Form might have received WM_CLOSE and set 'closing' in response.
                                if ((context.MainForm != null) && (context.MainForm.closing || (Modal && !context.MainForm.Visible))) {
                                        if (!Modal) {
                                                XplatUI.PostQuitMessage (0);
@@ -1057,36 +1012,34 @@ namespace System.Windows.Forms
                public static event EventHandler ThreadExit;
                public static event ThreadExceptionEventHandler ThreadException;
                
-#if NET_2_0
                // These are used externally by the UIA framework
                internal static event EventHandler FormAdded;
                internal static event EventHandler PreRun;
 
+#pragma warning disable 0067
+               [MonoTODO]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public static event EventHandler EnterThreadModal;
 
+               [MonoTODO]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public static event EventHandler LeaveThreadModal;
-#endif
+#pragma warning restore 0067
 
                #endregion      // Events
 
                #region Public Delegates
 
-#if NET_2_0
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public delegate bool MessageLoopCallback ();
-#endif
 
                #endregion
                
                #region Internal Properties
-#if NET_2_0
                internal static ToolStrip KeyboardCapture {
                        get { return keyboard_capture; }
                        set { keyboard_capture = value; }
                }
-#endif
 
                internal static bool VisualStylesEnabled {
                        get { return visual_styles_enabled; }
@@ -1099,7 +1052,6 @@ namespace System.Windows.Forms
                {
                        lock (forms)
                                forms.Add (f);
-#if NET_2_0
                        // Signal that a Form has been added to this
                        // Application. Used by UIA to detect new Forms that
                        // need a11y support. This event may be fired even if
@@ -1107,7 +1059,6 @@ namespace System.Windows.Forms
                        // account for that when handling this signal.
                        if (FormAdded != null)
                                FormAdded (f, null);
-#endif
                }
                
                internal static void RemoveForm (Form f)
@@ -1116,7 +1067,6 @@ namespace System.Windows.Forms
                                forms.Remove (f);
                }
 
-#if NET_2_0
                private static bool ControlOnToolStrip (Control c)
                {
                        Control p = c.Parent;
@@ -1130,7 +1080,6 @@ namespace System.Windows.Forms
                        
                        return false;
                }
-#endif
 
                // Takes a starting path, appends company name, product name, and
                // product version.  If the directory doesn't exist, create it