* DataGridViewElement.cs: Initialize state.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / NativeWindow.cs
index f3fcf50fae12d12ae6fcb39ce84bccdd1159703b..5c023edc5de4ca76e9dd872742e20c2754e7b885 100644 (file)
 using System.Runtime.Remoting;
 using System.Runtime.InteropServices;
 using System.Runtime.CompilerServices;
+using System.Threading;
 using System.Collections;
 using System.Diagnostics;
 
 namespace System.Windows.Forms
 {
-       public class NativeWindow : MarshalByRefObject {
+       public class NativeWindow : MarshalByRefObject
+#if NET_2_0
+               , IWin32Window
+#endif
+       {
                internal IntPtr                 window_handle;
                static internal Hashtable       window_collection = new Hashtable();
 
+               [ThreadStatic]
+               static NativeWindow WindowCreating;
+
                #region Public Constructors
                public NativeWindow()
                {
@@ -88,9 +96,8 @@ namespace System.Windows.Forms
                public void AssignHandle(IntPtr handle)
                {
                        lock (window_collection) {
-                               if (window_handle != IntPtr.Zero) {
+                               if (window_handle != IntPtr.Zero)
                                        window_collection.Remove(window_handle);
-                               }
                                window_handle=handle;
                                window_collection.Add(window_handle, this);
                        }
@@ -100,14 +107,19 @@ namespace System.Windows.Forms
                public virtual void CreateHandle(CreateParams create_params)
                {
                        if (create_params != null) {
+                               WindowCreating = this;
+
                                window_handle=XplatUI.CreateWindow(create_params);
 
+                               WindowCreating = null;
+
                                if (window_handle != IntPtr.Zero) {
                                        lock (window_collection) {
-                                               window_collection.Add(window_handle, this);
+                                               window_collection[window_handle] = this;
                                        }
                                }
                        }
+
                }
 
                public void DefWndProc(ref Message m)
@@ -156,8 +168,13 @@ namespace System.Windows.Forms
                {
                        Message         m = new Message();
                        NativeWindow    window = null;
+                                       
+#if debug
+                       Console.WriteLine("NativeWindow.cs ({0}, {1}, {2}, {3}): result {4}", hWnd, msg, wParam, lParam, m.Result);
+#endif
+
 
-                       try {
+                       //try {
                                lock (window_collection) {
                                        window = (NativeWindow)window_collection[hWnd];
                                }
@@ -169,17 +186,30 @@ namespace System.Windows.Forms
 
                                if (window != null)
                                        window.WndProc(ref m);
+                               else if (WindowCreating != null) {
+                                       // we need to do this AssignHandle here instead of relying on
+                                       // Control.WndProc to do it, because subclasses can override
+                                       // WndProc, install their own WM_CREATE block, and look at
+                                       // this.Handle, and it needs to be set.  Otherwise, we end up
+                                       // recursively creating windows and emitting WM_CREATE.
+                                       window = WindowCreating;
+                                       WindowCreating = null;
+                                       if (window.window_handle == IntPtr.Zero)
+                                               window.AssignHandle (hWnd);
+
+                                       window.WndProc (ref m);
+                               }
                                else
                                        m.Result=XplatUI.DefWndProc(ref m);
-                       }
-                       catch (Exception ex) {
-#if !ExternalExceptionHandler                          
-                               if (window != null)
-                                       window.OnThreadException(ex);
-#else
-                               throw;
-#endif
-                       }
+//            }
+//            catch (Exception ex) {
+//#if !ExternalExceptionHandler                                
+//                if (window != null)
+//                    window.OnThreadException(ex);
+//#else
+//                throw;
+//#endif
+//            }
 
                        #if debug
                                Console.WriteLine("NativeWindow.cs: Message {0}, result {1}", msg, m.Result);