**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Windows.Forms / WINELib / NativeWindow.cs
blobd089e15b511ca9be6c17d5be8296999ee1716148
1 //
2 // System.Windows.Forms.NativeWindow.cs
3 //
4 // Author:
5 // stubbed out by Paul Osman (paul.osman@sympatico.ca)
6 // Dennis Hayes (dennish@Raytek.com)
7 // WINELib implementation started by John Sohn (jsohn@columbus.rr.com)
8 //
9 // (C) 2002 Ximian, Inc
12 using System.Runtime.Remoting;
13 using System.Runtime.InteropServices;
14 using System.Runtime.CompilerServices;
15 using System.Collections;
17 namespace System.Windows.Forms {
19 // <summary>
20 // Implementation started.
22 // </summary>
24 public class NativeWindow : MarshalByRefObject {
26 // the window's HWND
27 private IntPtr windowHandle;
28 static private Hashtable windowCollection = new Hashtable ();
29 static bool registeredClass = false;
32 // --- Constructor
34 public NativeWindow ()
36 windowHandle = (IntPtr) 0;
40 // --- Public Properties
42 public IntPtr Handle
44 get {
45 return windowHandle;
50 // --- Public Methods
52 public void AssignHandle (IntPtr handle)
54 if (windowHandle != (IntPtr) 0)
55 windowCollection.Remove (windowHandle);
57 windowHandle = handle;
58 windowCollection.Add (windowHandle, this);
59 OnHandleChange ();
62 public virtual void CreateHandle (CreateParams cp)
64 IntPtr createdHWnd = (IntPtr) 0;
65 Object lpParam = new Object();
67 if (!registeredClass) {
68 Win32.WndProc wp = new Win32.WndProc (WndProc);
69 if (Win32.MonoRegisterClass(
70 (int) (Win32.CS_OWNDC |
71 Win32.CS_VREDRAW |
72 Win32.CS_HREDRAW),
73 wp, 0, 0, (IntPtr) 0, (IntPtr) 0,
74 (IntPtr) 0, (IntPtr) 6, "",
75 "mono_native_window") != 0) {
76 registeredClass = true;
77 } else {
78 windowHandle = (IntPtr) 0;
79 return;
83 windowHandle = Win32.CreateWindowExA (
84 (uint) cp.ExStyle, cp.ClassName,
85 cp.Caption,(uint) cp.Style,
86 cp.X, cp.Y, cp.Width, cp.Height,
87 (IntPtr) cp.Parent, (IntPtr) 0,
88 (IntPtr) 0, ref lpParam);
90 if (windowHandle != (IntPtr) 0)
91 windowCollection.Add (windowHandle, this);
94 [MonoTODO]
95 public override ObjRef CreateObjRef (Type requestedType)
97 throw new NotImplementedException ();
100 public void DefWndProc (ref Message m)
102 m.Result = Win32.DefWindowProcA (m.HWnd, m.Msg,
103 m.WParam, m.LParam);
106 public virtual void DestroyHandle ()
108 windowCollection.Remove (windowHandle);
109 Win32.DestroyWindow (windowHandle);
112 [MonoTODO]
113 public override bool Equals (object o)
115 throw new NotImplementedException ();
118 //inherited
119 //public static bool Equals(object o1, object o2)
121 // throw new NotImplementedException ();
123 [MonoTODO]
124 public override int GetHashCode ()
126 //FIXME add our proprities
127 return base.GetHashCode ();
130 public static NativeWindow FromHandle (IntPtr handle)
132 NativeWindow window = new NativeWindow ();
133 window.AssignHandle (handle);
134 return window;
137 //inherited
138 //public object GetLifetimeService() {
139 // throw new NotImplementedException ();
142 //public Type GetType() {
143 // throw new NotImplementedException ();
146 //public virtual object InitializeLifetimeService(){
147 // throw new NotImplementedException ();
150 public virtual void ReleaseHandle ()
152 windowHandle = (IntPtr) 0;
153 OnHandleChange ();
156 [MonoTODO]
157 public override string ToString ()
159 throw new NotImplementedException ();
163 // --- Protected Methods
165 //inherited
166 //protected object MemberwiseClone() {
167 // throw new NotImplementedException ();
170 [MonoTODO]
171 protected virtual void OnHandleChange ()
173 // to be overridden
176 [MonoTODO]
177 protected virtual void OnThreadException (Exception e)
179 throw new NotImplementedException ();
182 protected virtual void WndProc (ref Message m)
184 if (m.Msg == Win32.WM_CREATE)
185 Console.WriteLine ("NW WndProc WM_CREATE");
186 DefWndProc (ref m);
190 // --- Destructor
192 [MonoTODO]
193 ~NativeWindow ()
197 static private IntPtr WndProc (
198 IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
200 // windowCollection is a collection of all the
201 // NativeWindow(s) that have been created.
202 // Dispatch the current message to the approriate
203 // window.
204 NativeWindow window =
205 (NativeWindow) windowCollection[hWnd];
206 Message message = new Message ();
207 message.HWnd = hWnd;
208 message.Msg = msg;
209 message.WParam = wParam;
210 message.LParam = lParam;
211 message.Result = (IntPtr) 0;
213 if (msg == Win32.WM_CREATE)
214 Console.WriteLine ("WM_CREATE (static)");
216 if (window != null) {
217 if (msg == Win32.WM_CREATE)
218 Console.WriteLine ("WM_CREATE (static != null)");
219 window.WndProc(ref message);
220 } else {
221 Console.WriteLine ("no window, defwndproc");
222 // even though we are not managing the
223 // window let the window get the message
224 message.Result = Win32.DefWindowProcA (
225 hWnd, msg, wParam, lParam);
228 return message.Result;