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 {
41 internal IntPtr window_handle;
42 static internal Hashtable window_collection = new Hashtable();
45 static NativeWindow WindowCreating;
47 #region Public Constructors
50 window_handle=IntPtr.Zero;
52 #endregion // Public Constructors
54 #region Public Instance Properties
55 public IntPtr Handle {
60 #endregion // Public Instance Properties
62 #region Public Static Methods
63 public static NativeWindow FromHandle(IntPtr handle)
65 NativeWindow window=new NativeWindow();
67 window.AssignHandle(handle);
70 #endregion // Public Static Methods
72 #region Private and Internal Methods
73 internal static NativeWindow FindWindow(IntPtr handle)
76 lock (window_collection) {
77 rv = (NativeWindow)window_collection[handle];
82 internal void InvalidateHandle()
84 lock (window_collection) {
85 window_collection.Remove(window_handle);
87 window_handle = IntPtr.Zero;
91 #region Public Instance Methods
92 public void AssignHandle(IntPtr handle)
94 lock (window_collection) {
95 if (window_handle != IntPtr.Zero)
96 window_collection.Remove(window_handle);
98 window_collection.Add(window_handle, this);
103 public virtual void CreateHandle(CreateParams create_params)
105 if (create_params != null) {
106 WindowCreating = this;
108 window_handle=XplatUI.CreateWindow(create_params);
110 WindowCreating = null;
112 if (window_handle != IntPtr.Zero) {
113 lock (window_collection) {
114 window_collection[window_handle] = this;
121 public void DefWndProc(ref Message m)
123 m.Result=XplatUI.DefWndProc(ref m);
126 public virtual void DestroyHandle()
128 if (window_handle != IntPtr.Zero) {
129 XplatUI.DestroyWindow(window_handle);
133 public virtual void ReleaseHandle()
135 lock (window_collection) {
136 window_collection.Remove(window_handle);
138 window_handle=IntPtr.Zero;
142 #endregion // Public Instance Methods
144 #region Protected Instance Methods
149 protected virtual void OnHandleChange()
153 protected virtual void OnThreadException(Exception e)
155 Application.OnThreadException(e);
158 protected virtual void WndProc(ref Message m)
163 internal static IntPtr WndProc(IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam)
165 Message m = new Message();
166 NativeWindow window = null;
169 lock (window_collection) {
170 window = (NativeWindow)window_collection[hWnd];
176 m.Result=IntPtr.Zero;
179 window.WndProc(ref m);
180 else if (WindowCreating != null) {
181 // we need to do this AssignHandle here instead of relying on
182 // Control.WndProc to do it, because subclasses can override
183 // WndProc, install their own WM_CREATE block, and look at
184 // this.Handle, and it needs to be set. Otherwise, we end up
185 // recursively creating windows and emitting WM_CREATE.
186 if (WindowCreating.window_handle == IntPtr.Zero)
187 WindowCreating.AssignHandle (hWnd);
189 WindowCreating.WndProc (ref m);
192 m.Result=XplatUI.DefWndProc(ref m);
194 catch (Exception ex) {
195 #if !ExternalExceptionHandler
197 window.OnThreadException(ex);
204 Console.WriteLine("NativeWindow.cs: Message {0}, result {1}", msg, m.Result);
209 #endregion // Protected Instance Methods