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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2006 Novell, Inc.
23 // Peter Dennis Bartok pbartok@novell.com
29 //#define ExternalExceptionHandler
31 using System.Runtime.Remoting;
32 using System.Runtime.InteropServices;
33 using System.Runtime.CompilerServices;
34 using System.Threading;
35 using System.Collections;
36 using System.Diagnostics;
38 namespace System.Windows.Forms
40 public class NativeWindow : MarshalByRefObject
45 internal IntPtr window_handle;
46 static internal Hashtable window_collection = new Hashtable();
49 static NativeWindow WindowCreating;
51 #region Public Constructors
54 window_handle=IntPtr.Zero;
56 #endregion // Public Constructors
58 #region Public Instance Properties
59 public IntPtr Handle {
64 #endregion // Public Instance Properties
66 #region Public Static Methods
67 public static NativeWindow FromHandle(IntPtr handle)
69 NativeWindow window=new NativeWindow();
71 window.AssignHandle(handle);
74 #endregion // Public Static Methods
76 #region Private and Internal Methods
77 internal static NativeWindow FindWindow(IntPtr handle)
80 lock (window_collection) {
81 rv = (NativeWindow)window_collection[handle];
86 internal void InvalidateHandle()
88 lock (window_collection) {
89 window_collection.Remove(window_handle);
91 window_handle = IntPtr.Zero;
95 #region Public Instance Methods
96 public void AssignHandle(IntPtr handle)
98 lock (window_collection) {
99 if (window_handle != IntPtr.Zero)
100 window_collection.Remove(window_handle);
101 window_handle=handle;
102 window_collection.Add(window_handle, this);
107 public virtual void CreateHandle(CreateParams cp)
110 WindowCreating = this;
112 window_handle=XplatUI.CreateWindow(cp);
114 WindowCreating = null;
116 if (window_handle != IntPtr.Zero) {
117 lock (window_collection) {
118 window_collection[window_handle] = this;
125 public void DefWndProc(ref Message m)
127 m.Result=XplatUI.DefWndProc(ref m);
130 public virtual void DestroyHandle()
132 if (window_handle != IntPtr.Zero) {
133 XplatUI.DestroyWindow(window_handle);
137 public virtual void ReleaseHandle()
139 lock (window_collection) {
140 window_collection.Remove(window_handle);
142 window_handle=IntPtr.Zero;
146 #endregion // Public Instance Methods
148 #region Protected Instance Methods
153 protected virtual void OnHandleChange()
157 protected virtual void OnThreadException(Exception e)
159 Application.OnThreadException(e);
162 protected virtual void WndProc(ref Message m)
167 internal static IntPtr WndProc(IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
169 Message m = new Message();
170 NativeWindow window = null;
173 Console.WriteLine("NativeWindow.cs ({0}, {1}, {2}, {3}): result {4}", hWnd, msg, wParam, lParam, m.Result);
178 lock (window_collection) {
179 window = (NativeWindow)window_collection[hWnd];
185 m.Result=IntPtr.Zero;
188 window.WndProc(ref m);
189 else if (WindowCreating != null) {
190 // we need to do this AssignHandle here instead of relying on
191 // Control.WndProc to do it, because subclasses can override
192 // WndProc, install their own WM_CREATE block, and look at
193 // this.Handle, and it needs to be set. Otherwise, we end up
194 // recursively creating windows and emitting WM_CREATE.
195 window = WindowCreating;
196 WindowCreating = null;
197 if (window.window_handle == IntPtr.Zero)
198 window.AssignHandle (hWnd);
200 window.WndProc (ref m);
203 m.Result=XplatUI.DefWndProc(ref m);
205 // catch (Exception ex) {
206 //#if !ExternalExceptionHandler
207 // if (window != null)
208 // window.OnThreadException(ex);
215 Console.WriteLine("NativeWindow.cs: Message {0}, result {1}", msg, m.Result);
220 #endregion // Protected Instance Methods