- Implement 2.0 image key feature.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XplatUIOSX.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Geoff Norton  <gnorton@customerdna.com>
24 //
25 //
26
27 // This really doesn't work at all; please dont file bugs on it yet.
28
29 // MAJOR TODO:
30 //  Fix clipping of children
31 //  Wire up keyboard
32
33 using System;
34 using System.Threading;
35 using System.Drawing;
36 using System.ComponentModel;
37 using System.Collections;
38 using System.Diagnostics;
39 using System.Runtime.InteropServices;
40
41 /// OSX Version
42 namespace System.Windows.Forms {
43
44         // The Carbon Event callback delegate
45         delegate int CarbonEventDelegate (IntPtr inCallRef, IntPtr inEvent, IntPtr userData);
46
47         internal class XplatUIOSX : XplatUIDriver {
48                 
49                 #region Local Variables
50                 
51                 // General driver variables
52                 private static XplatUIOSX Instance;
53                 private static int RefCount;
54                 private static bool themes_enabled;
55                 private static IntPtr FocusWindow;
56
57                 // Mouse 
58                 private static MouseButtons MouseState;
59                 Point mouse_position;
60                 private static Hwnd MouseWindow;
61                 
62                 // OSX Specific
63                 private static GrabStruct Grab;
64                 private static OSXCaret Caret;
65                 private static OSXHover Hover;
66                 private CarbonEventDelegate CarbonEventHandler;
67                 private static Hashtable WindowMapping;
68                 private static Hashtable WindowBackgrounds;
69                 private static Hwnd GrabWindowHwnd;
70                 private static IntPtr FosterParent;
71                 private static int TitleBarHeight;
72                 private static int MenuBarHeight;
73                 private static EventTypeSpec [] viewEvents = new EventTypeSpec [] {
74                                                                         new EventTypeSpec (OSXConstants.kEventClassControl, OSXConstants.kEventControlSetFocusPart), 
75                                                                         new EventTypeSpec (OSXConstants.kEventClassControl, OSXConstants.kEventControlClick), 
76                                                                         new EventTypeSpec (OSXConstants.kEventClassControl, OSXConstants.kEventControlContextualMenuClick), 
77                                                                         new EventTypeSpec (OSXConstants.kEventClassControl, OSXConstants.kEventControlTrack), 
78                                                                         new EventTypeSpec (OSXConstants.kEventClassControl, OSXConstants.kEventControlSimulateHit), 
79                                                                         new EventTypeSpec (OSXConstants.kEventClassControl, OSXConstants.kEventControlBoundsChanged), 
80                                                                         new EventTypeSpec (OSXConstants.kEventClassControl, OSXConstants.kEventControlDraw) 
81                                                                         };
82                 private static EventTypeSpec [] windowEvents = new EventTypeSpec[] {
83                                                                         //new EventTypeSpec (OSXConstants.kEventClassMouse, OSXConstants.kEventMouseEntered),
84                                                                         //new EventTypeSpec (OSXConstants.kEventClassMouse, OSXConstants.kEventMouseExited),
85                                                                         new EventTypeSpec (OSXConstants.kEventClassMouse, OSXConstants.kEventMouseMoved),
86                                                                         //new EventTypeSpec (OSXConstants.kEventClassMouse, OSXConstants.kEventMouseDragged),
87                                                                         //new EventTypeSpec (OSXConstants.kEventClassMouse, OSXConstants.kEventMouseWheelMoved),
88                                                                         new EventTypeSpec (OSXConstants.kEventClassWindow, OSXConstants.kEventWindowBoundsChanged),
89                                                                         new EventTypeSpec (OSXConstants.kEventClassWindow, OSXConstants.kEventWindowClose),
90                                                                         new EventTypeSpec (OSXConstants.kEventClassKeyboard, OSXConstants.kEventRawKeyDown),
91                                                                         new EventTypeSpec (OSXConstants.kEventClassKeyboard, OSXConstants.kEventRawKeyRepeat),
92                                                                         new EventTypeSpec (OSXConstants.kEventClassKeyboard, OSXConstants.kEventRawKeyUp)
93                                                                         };
94                                                                         
95                 
96                 // Message loop
97                 private static Queue MessageQueue;
98                 private static bool GetMessageResult;
99
100                 // Timers
101                 private ArrayList TimerList;
102                 
103                 static readonly object lockobj = new object ();
104                 
105                 // Event Handlers
106                 internal override event EventHandler Idle;
107
108                 #endregion
109                 
110                 #region Constructors
111                 private XplatUIOSX() {
112
113                         RefCount = 0;
114                         TimerList = new ArrayList ();
115                         MessageQueue = new Queue ();
116                         
117                         Initialize ();
118                 }
119
120                 ~XplatUIOSX() {
121                         // FIXME: Clean up the FosterParent here.
122                 }
123
124                 #endregion
125
126                 #region Singleton specific code
127                 
128                 public static XplatUIOSX GetInstance() {
129                         lock (lockobj) {
130                                 if (Instance == null) {
131                                         Instance = new XplatUIOSX ();
132                                 }
133                                 RefCount++;
134                         }
135                         return Instance;
136                 }
137
138                 public int Reference {
139                         get {
140                                 return RefCount;
141                         }
142                 }
143                 
144                 #endregion
145                 
146                 #region Internal methods
147                 
148                 internal void Initialize () {
149
150                         // Initialize the Event Handler delegate
151                         CarbonEventHandler = new CarbonEventDelegate (EventCallback);
152                         
153                         // Initilize the mouse controls
154                         Hover.Interval = 500;
155                         Hover.Timer = new Timer ();
156                         Hover.Timer.Enabled = false;
157                         Hover.Timer.Interval = Hover.Interval;
158                         Hover.Timer.Tick += new EventHandler (HoverCallback);
159                         Hover.X = -1;
160                         Hover.Y = -1;
161                         MouseState = MouseButtons.None;
162                         mouse_position = Point.Empty;
163                                 
164                         // Initialize the Caret
165                         Caret.Timer = new Timer ();
166                         Caret.Timer.Interval = 500;
167                         Caret.Timer.Tick += new EventHandler (CaretCallback);
168                         
169                         // Initialize the OSX Specific stuff
170                         WindowMapping = new Hashtable ();
171                         WindowBackgrounds = new Hashtable ();
172                         
173                         // Initialize the FosterParent
174                         IntPtr rect = IntPtr.Zero;
175                         SetRect (ref rect, (short)0, (short)0, (short)0, (short)0);
176                         CheckError (CreateNewWindow (WindowClass.kDocumentWindowClass, WindowAttributes.kWindowStandardHandlerAttribute | WindowAttributes.kWindowCloseBoxAttribute | WindowAttributes.kWindowFullZoomAttribute | WindowAttributes.kWindowCollapseBoxAttribute | WindowAttributes.kWindowResizableAttribute | WindowAttributes.kWindowCompositingAttribute, ref rect, ref FosterParent), "CreateFosterParent ()");
177                         
178                         // Get some values about bar heights
179                         Rect structRect = new Rect ();
180                         Rect contentRect = new Rect ();
181                         CheckError (GetWindowBounds (FosterParent, 32, ref structRect), "GetWindowBounds ()");
182                         CheckError (GetWindowBounds (FosterParent, 33, ref contentRect), "GetWindowBounds ()");
183                         
184                         TitleBarHeight = Math.Abs(structRect.top - contentRect.top);
185                         MenuBarHeight = GetMBarHeight ();
186                         
187                         // Focus
188                         FocusWindow = IntPtr.Zero;
189                         
190                         // Message loop
191                         GetMessageResult = true;
192                 }
193                 
194                 #endregion
195                 
196                 #region Private methods
197                 #endregion
198                 
199                 #region Callbacks
200                 
201                 private void CaretCallback (object sender, EventArgs e) {
202                         if (Caret.Paused) {
203                                 return;
204                         }
205
206                         if (!Caret.On) {
207                                 ShowCaret ();
208                         } else {
209                                 HideCaret ();
210                         }
211                 }
212                 
213                 private void HoverCallback (object sender, EventArgs e) {
214                         if ((Hover.X == mouse_position.X) && (Hover.Y == mouse_position.Y)) {
215                                 MSG msg = new MSG ();
216                                 msg.hwnd = Hover.Hwnd;
217                                 msg.message = Msg.WM_MOUSEHOVER;
218                                 msg.wParam = GetMousewParam (0);
219                                 msg.lParam = (IntPtr)((ushort)Hover.X << 16 | (ushort)Hover.X);
220                                 MessageQueue.Enqueue (msg);
221                         }
222                 }
223                 
224                 internal int EventCallback (IntPtr inCallRef, IntPtr inEvent, IntPtr handle) {
225                         uint eventClass = GetEventClass (inEvent);
226                         uint eventKind = GetEventKind (inEvent);
227                         int retVal = 0;
228                         lock (MessageQueue) {
229                                 switch (eventClass) {
230                                         // keyboard
231                                         case OSXConstants.kEventClassKeyboard: {
232                                                 retVal = ProcessKeyboardEvent (inEvent, eventKind, handle);
233                                                 break;
234                                         }
235                                         //window
236                                         case OSXConstants.kEventClassWindow: {
237                                                 retVal = ProcessWindowEvent (inEvent, eventKind, handle);
238                                                 break;
239                                         }
240                                         // mouse
241                                         case OSXConstants.kEventClassMouse: {
242                                                 retVal = ProcessMouseEvent (inEvent, eventKind, handle);
243                                                 break;
244                                         }
245                                         // control
246                                         case OSXConstants.kEventClassControl: {
247                                                 retVal = ProcessControlEvent (inEvent, eventKind, handle);
248                                                 break;
249                                         }
250                                         default: {
251                                                 Console.WriteLine ("WARNING: Unhandled eventClass {0}", eventClass);
252                                                 break;
253                                         }
254                                 }
255                         }
256                         
257                         return retVal;
258                 }
259
260                 #endregion
261                 
262                 #region Private Methods
263                 
264                 // This sucks write a real driver
265                 private int ProcessKeyboardEvent (IntPtr inEvent, uint eventKind, IntPtr handle) {
266                         MSG msg = new MSG ();
267                         byte charCode = 0x00;
268                         GetEventParameter (inEvent, OSXConstants.EventParamName.kEventParamKeyMacCharCodes, OSXConstants.EventParamType.typeChar, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (byte)), IntPtr.Zero, ref charCode);
269                         IntPtr cntrl = IntPtr.Zero;
270                         CheckError (GetKeyboardFocus (handle, ref cntrl), "GetKeyboardFocus()");
271                         msg.hwnd = cntrl;
272                         msg.lParam = IntPtr.Zero;
273                         switch (charCode) {
274                                 case 28:
275                                         charCode = 0x25;
276                                         break;
277                                 case 29:
278                                         charCode = 0x27;
279                                         break;
280                                 case 30:
281                                         charCode = 0x26;
282                                         break;
283                                 case 31:
284                                         charCode = 0x28;
285                                         break;
286                         }
287                         msg.wParam = (IntPtr)charCode;
288                         switch (eventKind) {
289                                 // keydown
290                                 case OSXConstants.kEventRawKeyDown: {
291                                         msg.message = Msg.WM_KEYDOWN;
292                                         break;
293                                 }
294                                 // repeat
295                                 case OSXConstants.kEventRawKeyRepeat: {
296                                         msg.message = Msg.WM_KEYDOWN;
297                                         break;
298                                 }
299                                 // keyup
300                                 case OSXConstants.kEventRawKeyUp: {
301                                         msg.message = Msg.WM_KEYUP;
302                                         break;
303                                 }
304                         }
305                         MessageQueue.Enqueue (msg);
306                         return -9874;
307                 }
308
309                 private int ProcessWindowEvent (IntPtr inEvent, uint eventKind, IntPtr handle) {
310                         MSG msg = new MSG ();
311                         switch (eventKind) {
312                                 // Someone closed a window
313                                 case OSXConstants.kEventWindowClose: {
314                                         // This is our real window; so we have to post to the corresponding view
315                                         // FIXME: Should we doublehash the table to get the real window handle without this loop?
316                                         IDictionaryEnumerator e = WindowMapping.GetEnumerator ();
317                                         while (e.MoveNext ()) {
318                                                 if ((IntPtr)e.Value == handle) {
319                                                         NativeWindow.WndProc((IntPtr)e.Key, Msg.WM_DESTROY, IntPtr.Zero, IntPtr.Zero);
320                                                 }
321                                         }
322                                         return 0;
323                                 }
324                                 case OSXConstants.kEventWindowBoundsChanged: {
325                                         // This is our real window; so we have to resize the corresponding view as well
326                                         // FIXME: Should we doublehash the table to get the real window handle without this loop?
327                                         
328                                         IDictionaryEnumerator e = WindowMapping.GetEnumerator ();
329                                         while (e.MoveNext ()) {
330                                                 if ((IntPtr)e.Value == handle) {
331                                                         Hwnd hwnd = Hwnd.ObjectFromHandle ((IntPtr) e.Key);
332                                                         // Get the bounds of the window
333                                                         Rect bounds = new Rect ();
334                                                         CheckError (GetWindowBounds (handle, 33, ref bounds), "GetWindowBounds ()");
335                                                         HIRect r = new HIRect ();
336                                                         
337                                                         // Get our frame for the Handle
338                                                         CheckError (HIViewGetFrame (hwnd.Handle, ref r), "HIViewGetFrame ()");
339                                                         r.size.width = bounds.right-bounds.left;
340                                                         r.size.height = bounds.bottom-bounds.top;
341                                                         // Set the view to the new size
342                                                 CheckError (HIViewSetFrame (hwnd.WholeWindow, ref r), "HIViewSetFrame ()");
343                                                 
344                                                 // Update the hwnd internal size representation
345                                                         hwnd.x = (int)r.origin.x;
346                                                         hwnd.y = (int)r.origin.y;
347                                                         hwnd.width = (int)r.size.width;
348                                                         hwnd.height = (int)r.size.height;
349                                                         Rectangle client_rect = hwnd.ClientRect;
350                                                         
351                                                         r.size.width = client_rect.Width;
352                                                         r.size.height = client_rect.Height;
353                                                         r.origin.x = client_rect.X;
354                                                         r.origin.y = client_rect.Y;
355                                                         
356                                                         // Update the client area too
357                                                         CheckError (HIViewSetFrame (hwnd.ClientWindow, ref r));
358                                                         
359                                                         // Add the message to the queue
360                                                         msg.message = Msg.WM_WINDOWPOSCHANGED;
361                                                         msg.hwnd = hwnd.Handle;
362                                                         msg.wParam = IntPtr.Zero;
363                                                         msg.lParam = IntPtr.Zero;
364                                                         MessageQueue.Enqueue (msg);
365                                                         
366                                                         return 0;
367                                                 }
368                                         }
369                                         break;
370                                 }
371                         }
372                         return -9874;
373                 }
374                                 
375                 private int ProcessMouseEvent (IntPtr inEvent, uint eventKind, IntPtr handle) {
376                         MSG msg = new MSG ();           
377                         
378                         switch (eventKind) {
379                                 case OSXConstants.kEventMouseMoved: {
380                                         // Where is the mouse in global coordinates
381                                         QDPoint pt = new QDPoint ();
382                                         GetEventParameter (inEvent, OSXConstants.EventParamName.kEventParamMouseLocation, OSXConstants.EventParamType.typeQDPoint, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (QDPoint)), IntPtr.Zero, ref pt);
383                                         
384                                         // Where is the mouse in the window
385                                         Rect window_bounds = new Rect ();
386                                         GetWindowBounds (handle, 33, ref window_bounds);
387                                         CGPoint window_pt = new CGPoint ((short) (pt.x - window_bounds.left), (short) (pt.y - window_bounds.top));
388                                         
389                                         IntPtr window_handle = IntPtr.Zero;
390                                         HIViewFindByID (HIViewGetRoot (handle), new HIViewID (OSXConstants.kEventClassWindow, 1), ref window_handle);
391                                         
392                                         // Determine which control was hit
393                                         IntPtr view_handle = IntPtr.Zero;
394                                         HIViewGetSubviewHit (window_handle, ref window_pt, true, ref view_handle);
395                                         
396                                         // Convert the point to view local coordinates
397                                         HIViewConvertPoint (ref window_pt, window_handle, view_handle);
398                                         
399                                         Hwnd hwnd = Hwnd.ObjectFromHandle (view_handle);
400                                         
401                                         if (hwnd == null)
402                                                 return -9874;
403                                                 
404                                         // Generate the message
405                                         msg.hwnd = hwnd.Handle;
406                                         msg.message = Msg.WM_MOUSEMOVE;
407                                         msg.lParam = (IntPtr) ((ushort)window_pt.y << 16 | (ushort)window_pt.x);
408                                         msg.wParam = GetMousewParam (0);
409                                         mouse_position.X = (int)window_pt.x;
410                                         mouse_position.Y = (int)window_pt.y;
411                                         
412                                         Hover.Hwnd = msg.hwnd;
413                                         Hover.Timer.Enabled = true;
414                                         MessageQueue.Enqueue (msg);
415                                         return -9874;
416                                 }
417                         }
418                         return -9874;
419                 }
420                                         
421                 private int ProcessControlEvent (IntPtr inEvent, uint eventKind, IntPtr handle) {
422                         GetEventParameter (inEvent, OSXConstants.EventParamName.kEventParamDirectObject, OSXConstants.EventParamType.typeControlRef, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref handle);
423                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
424                         MSG msg = new MSG ();
425                                         
426                         switch (eventKind) {
427                                 case OSXConstants.kEventControlDraw: {
428                                         
429                                         if(!hwnd.visible || !HIViewIsVisible (handle))
430                                                 return 0;
431
432                                         /*
433                                         IntPtr rgnhandle = IntPtr.Zero;
434                                         GetEventParameter (inEvent, OSXConstants.EventParamName.kEventParamRgnHandle, OSXConstants.EventParamType.typeQDRgnHandle, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref rgnhandle);
435                                         IntPtr duprgn = NewRgn ();
436                                         CopyRgn (rgnhandle, duprgn);
437                                         ClipRegions [hwnd.Handle] = duprgn;             
438                                         */
439                                         
440                                         // Get the dirty area
441                                         HIRect bounds = new HIRect ();
442                                         HIViewGetBounds (handle, ref bounds); 
443                                         
444                                         bool client = (hwnd.ClientWindow == handle ? true : false);
445                                         
446                                         if (!client && bounds.origin.x >= hwnd.ClientRect.X && bounds.origin.y >= hwnd.ClientRect.Y) {
447                                                 // This is a paint on WholeWindow inside the clientRect; we can safely discard this
448                                                 return 0;
449                                         }
450                                         
451                                         hwnd.AddInvalidArea ((int)bounds.origin.x, (int)bounds.origin.y, (int)bounds.size.width, (int)bounds.size.height);
452                                         if (WindowBackgrounds [hwnd] != null) {
453                                                 Color c = (Color)WindowBackgrounds [hwnd];
454                                                 IntPtr contextref = IntPtr.Zero;
455                                                 GetEventParameter (inEvent, OSXConstants.EventParamName.kEventParamCGContextRef, OSXConstants.EventParamType.typeCGContextRef, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref contextref);
456                                                 CGContextSetRGBFillColor (contextref, (float)c.R/255, (float)c.G/255, (float)c.B/255, (float)c.A/255);
457                                                 CGContextFillRect (contextref, bounds);
458                                         }
459                                         
460                                         // Add a paint to the queue
461                                         msg.hwnd = hwnd.Handle;
462                                         msg.message = Msg.WM_PAINT;
463                                         msg.wParam = IntPtr.Zero;
464                                         msg.lParam = IntPtr.Zero;
465                                         MessageQueue.Enqueue (msg);
466                         
467                                         return 0;
468                                 }
469                                 case OSXConstants.kEventControlBoundsChanged: {
470                                         // This can happen before our HWND is created so we need to check to make sure its not null
471                                         if (hwnd != null) {
472                                                 // Get the bounds
473                                                 HIRect bounds = new HIRect ();
474                                                 HIViewGetFrame (handle, ref bounds); 
475                                                 // Update the hwnd size
476                                                 hwnd.x = (int)bounds.origin.x;
477                                                 hwnd.y = (int)bounds.origin.y;
478                                                 hwnd.width = (int)bounds.size.width;
479                                                 hwnd.height = (int)bounds.size.height;
480                                                 
481                                                 // TODO: Do we need to send a paint here or does BoundsChanged make a ControlDraw for the exposed area?
482                                         }                                                       
483                                         return 0;
484                                 }
485                                 case OSXConstants.kEventControlTrack: {
486                                         // get the point that was hit
487                                         QDPoint point = new QDPoint ();
488                                         CheckError (GetEventParameter (inEvent, OSXConstants.EventParamName.kEventParamMouseLocation, OSXConstants.EventParamType.typeQDPoint, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (QDPoint)), IntPtr.Zero, ref point), "GetEventParameter() MouseLocation");
489                                         MouseTrackingResult mousestatus = MouseTrackingResult.kMouseTrackingMouseDown;
490                                         IntPtr modifiers = IntPtr.Zero;
491                                         
492                                         while (mousestatus != MouseTrackingResult.kMouseTrackingMouseUp) {
493                                                 CheckTimers (DateTime.Now);
494                                                 if (mousestatus == MouseTrackingResult.kMouseTrackingMouseDragged) {
495                                                         QDPoint realpoint = point;
496                                                         int x = point.x;
497                                                         int y = point.y;
498                                                         ScreenToClient (hwnd.Handle, ref x, ref y);
499                                                         realpoint.x = (short)x;
500                                                         realpoint.y = (short)y;
501                                                         NativeWindow.WndProc (hwnd.Handle, Msg.WM_MOUSEMOVE, GetMousewParam (0), (IntPtr) ((ushort)realpoint.y << 16 | (ushort)realpoint.x));
502                                                 }
503                                                 // Process the rest of the event queue
504                                                 while (MessageQueue.Count > 0) {
505                                                         msg = (MSG)MessageQueue.Dequeue ();
506                                                         NativeWindow.WndProc (msg.hwnd, msg.message, msg.wParam, msg.lParam);
507                                                 }
508                                                 TrackMouseLocationWithOptions ((IntPtr)(-1), 0, 0.01, ref point, ref modifiers, ref mousestatus);
509                                         }
510                                         
511                                         msg.hwnd = hwnd.Handle;
512                                         
513                                         bool client = (hwnd.ClientWindow == handle ? true : false);
514                                         
515                                         int wparam = (int)GetMousewParam (0);
516                                         switch (MouseState) {
517                                                 case MouseButtons.Left:
518                                                         MouseState &= ~MouseButtons.Left;
519                                                         msg.message = (client ? Msg.WM_LBUTTONUP : Msg.WM_NCLBUTTONUP);
520                                                         wparam &= (int)MsgButtons.MK_LBUTTON;
521                                                         break;
522                                                 case MouseButtons.Middle:
523                                                         MouseState &= ~MouseButtons.Middle;
524                                                         msg.message = (client ? Msg.WM_MBUTTONUP : Msg.WM_NCMBUTTONUP);
525                                                         wparam &= (int)MsgButtons.MK_MBUTTON;
526                                                         break;
527                                                 case MouseButtons.Right:
528                                                         MouseState &= ~MouseButtons.Right;
529                                                         msg.message = (client ? Msg.WM_RBUTTONUP : Msg.WM_NCRBUTTONUP);
530                                                         wparam &= (int)MsgButtons.MK_RBUTTON;
531                                                         break;
532                                         }
533                                         int x2 = point.x;
534                                         int y2 = point.y;
535                                         ScreenToClient (hwnd.Handle, ref x2, ref y2);
536                                         point.x = (short)x2;
537                                         point.y = (short)y2;
538
539                                         msg.wParam = (IntPtr)wparam;
540                                                 
541                                         msg.lParam = (IntPtr) ((ushort)point.y << 16 | (ushort)point.x);
542                                         mouse_position.X = (int)point.x;
543                                         mouse_position.Y = (int)point.y;
544                                         //NativeWindow.WndProc (msg.hwnd, msg.message, msg.lParam, msg.wParam);
545                                         MessageQueue.Enqueue (msg);
546                                         
547                                         IntPtr window = GetControlOwner (hwnd.Handle);
548                                         SetKeyboardFocus (window, hwnd.Handle, 1);
549                                         
550                                         return 0;
551                                 }
552                                 case OSXConstants.kEventControlContextualMenuClick:
553                                 case OSXConstants.kEventControlClick: {
554                                         // get the point that was hit
555                                         QDPoint point = new QDPoint ();
556                                         CheckError (GetEventParameter (inEvent, OSXConstants.EventParamName.kEventParamMouseLocation, OSXConstants.EventParamType.typeQDPoint, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (QDPoint)), IntPtr.Zero, ref point), "GetEventParameter() MouseLocation");
557                                         QDPoint trackpoint = point;
558                                         int x = point.x;
559                                         int y = point.y;
560                                         ScreenToClient (hwnd.Handle, ref x, ref y);
561                                         point.x = (short)x;
562                                         point.y = (short)y;
563                                         
564                                         // which button was pressed?
565                                         ushort button = 0;
566                                         GetEventParameter (inEvent, OSXConstants.EventParamName.kEventParamMouseButton, OSXConstants.EventParamType.typeMouseButton, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (ushort)), IntPtr.Zero, ref button);
567                                         if (button == 2) {
568                                                 point.x = (short)mouse_position.X;
569                                                 point.y = (short)mouse_position.Y;
570                                         }
571                                         
572                                         msg.hwnd = hwnd.Handle;
573                                         
574                                         bool client = (hwnd.ClientWindow == handle ? true : false);
575                                         
576                                         int wparam = (int)GetMousewParam (0);
577                                         switch (button) {
578                                                 case 1:
579                                                         MouseState |= MouseButtons.Left;
580                                                         msg.message = (client ? Msg.WM_LBUTTONDOWN : Msg.WM_NCLBUTTONDOWN);
581                                                         wparam |= (int)MsgButtons.MK_LBUTTON;
582                                                         break;
583                                                 case 2:
584                                                         MouseState |= MouseButtons.Right;
585                                                         msg.message = (client ? Msg.WM_RBUTTONDOWN : Msg.WM_NCRBUTTONDOWN);
586                                                         wparam |= (int)MsgButtons.MK_RBUTTON;
587                                                         break;
588                                                 case 3:
589                                                         MouseState |= MouseButtons.Middle;
590                                                         msg.message = (client ? Msg.WM_MBUTTONDOWN : Msg.WM_NCMBUTTONDOWN);
591                                                         wparam |= (int)MsgButtons.MK_MBUTTON;
592                                                         break;
593                                         }
594                                         msg.wParam = (IntPtr)wparam;
595                                                 
596                                         msg.lParam = (IntPtr) ((ushort)point.y << 16 | (ushort)point.x);
597                                         mouse_position.X = (int)point.x;
598                                         mouse_position.Y = (int)point.y;
599                                         NativeWindow.WndProc (msg.hwnd, msg.message, msg.wParam, msg.lParam);
600                                         
601                                         TrackControl (handle, trackpoint, IntPtr.Zero);
602                                         return 0;
603                                 }
604                                 case OSXConstants.kEventControlSetFocusPart: {
605                                         // This handles setting focus
606                                         short pcode = 1;
607                                         GetEventParameter (inEvent, OSXConstants.EventParamName.kEventParamControlPart, OSXConstants.EventParamType.typeControlPartCode, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (short)), IntPtr.Zero, ref pcode);
608                                         switch (pcode) {
609                                                 case 0:
610                                                 case -1:
611                                                 case -2:
612                                                         pcode = 0;
613                                                         break;
614                                         }
615                                         SetEventParameter (inEvent, OSXConstants.EventParamName.kEventParamControlPart, OSXConstants.EventParamType.typeControlPartCode, (uint)Marshal.SizeOf (typeof (short)), ref pcode);
616                                         return 0;
617                                 }
618                         }
619                         return -9874;
620                 }
621                 private IntPtr GetMousewParam(int Delta) {
622                         int     result = 0;
623
624                         if ((MouseState & MouseButtons.Left) != 0) {
625                                 result |= (int)MsgButtons.MK_LBUTTON;
626                         }
627
628                         if ((MouseState & MouseButtons.Middle) != 0) {
629                                 result |= (int)MsgButtons.MK_MBUTTON;
630                         }
631
632                         if ((MouseState & MouseButtons.Right) != 0) {
633                                 result |= (int)MsgButtons.MK_RBUTTON;
634                         }
635
636                         return (IntPtr)result;
637                 }
638
639                 private double NextTimeout ()
640                 {
641                         DateTime now = DateTime.Now;
642                         int timeout = 0x7FFFFFF;
643                         lock (TimerList) {
644                                 foreach (Timer timer in TimerList) {
645                                         int next = (int) (timer.Expires - now).TotalMilliseconds;
646                                         if (next < 0)
647                                                 return 0;
648                                         if (next < timeout)
649                                                 timeout = next;
650                                 }
651                         }
652                         if (timeout < Timer.Minimum)
653                                 timeout = Timer.Minimum;
654
655                         return (double)((double)timeout/1000);
656                 }
657                 
658                 private void CheckTimers (DateTime now)
659                 {
660                         lock (TimerList) {
661                                 int count = TimerList.Count;
662                                 if (count == 0)
663                                         return;
664                                 for (int i = 0; i < TimerList.Count; i++) {
665                                         Timer timer = (Timer) TimerList [i];
666                                         if (timer.Enabled && timer.Expires <= now) {
667                                                 timer.FireTick ();
668                                                 timer.Update (now);
669                                         }
670                                 }
671                         }
672                 }
673
674                 internal void InvertCaret () {
675                         IntPtr window = GetControlOwner (Caret.Hwnd);
676                         SetPortWindowPort (window);
677                         Rect r = new Rect ();
678                         GetWindowPortBounds (window, ref r);
679                         r.top += (short)Caret.Y;
680                         r.left += (short)Caret.X;
681                         r.bottom = (short)(r.top + Caret.Height);
682                         r.right = (short)(r.left + Caret.Width);
683                         InvertRect (ref r);
684                 }
685                 
686                 private void SetHwndStyles(Hwnd hwnd, CreateParams cp) {
687                         throw new NotImplementedException();
688                 }
689                 
690                 internal void ShowCaret () {
691                         if (Caret.On)
692                                 return;
693                         Caret.On = true;
694                         InvertCaret ();
695                 }
696
697                 internal void HideCaret () {
698                         if (!Caret.On)
699                                 return;
700                         Caret.On = false;
701                         InvertCaret ();
702                 }
703                 
704                 internal void InstallTracking (Hwnd hwnd) {
705                         // This is currently not used
706                         
707                         /*
708                         if (hwnd.client_region_ptr != IntPtr.Zero) {
709                                 ReleaseMouseTrackingRegion (hwnd.client_region_ptr);
710                                 hwnd.client_region_ptr = IntPtr.Zero;
711                         }
712                         if (hwnd.whole_region_ptr != IntPtr.Zero) {
713                                 ReleaseMouseTrackingRegion (hwnd.whole_region_ptr);
714                                 hwnd.whole_region_ptr = IntPtr.Zero;
715                         }
716                         // Setup the new track region
717                         if (hwnd.visible) {
718                                 HIRect client_bounds = new HIRect ();   
719                                 HIViewGetBounds (hwnd.client_window, ref client_bounds);
720                                 HIViewConvertRect (ref client_bounds, hwnd.client_window, IntPtr.Zero);
721                         
722                                 IntPtr rgn = NewRgn ();
723                                 SetRectRgn (rgn, (short)client_bounds.origin.x, (short)client_bounds.origin.y, (short)(client_bounds.origin.x+hwnd.ClientRect.Width), (short)(client_bounds.origin.y+hwnd.ClientRect.Height));
724                                 CreateMouseTrackingRegion (GetControlOwner (hwnd.client_window), rgn, IntPtr.Zero, 0, hwnd.client_region_id, hwnd.client_window, IntPtr.Zero, ref hwnd.client_region_ptr);
725                                 Console.WriteLine (hwnd.ClientRect);
726                                 Console.WriteLine ("Created a mouse trcaking region on the client window @ {0}x{1} {2}x{3}", (short)client_bounds.origin.x, (short)client_bounds.origin.y, (short)(client_bounds.origin.x+hwnd.ClientRect.Width), (short)(client_bounds.origin.y+hwnd.ClientRect.Height));
727                                 if (hwnd.ClientRect.X > 0 && hwnd.ClientRect.Y > 0) {
728                                         HIRect window_bounds = new HIRect ();
729                                         HIViewGetBounds (hwnd.whole_window, ref window_bounds);
730                                         HIViewConvertRect (ref window_bounds, hwnd.whole_window, IntPtr.Zero);
731                                         rgn = NewRgn ();
732                                         SetRectRgn (rgn, (short)window_bounds.origin.x, (short)window_bounds.origin.y, (short)(window_bounds.origin.x+hwnd.ClientRect.X), (short)(window_bounds.origin.y+hwnd.ClientRect.Y));
733                                         CreateMouseTrackingRegion (GetControlOwner (hwnd.whole_window), rgn, IntPtr.Zero, 0, hwnd.whole_region_id, hwnd.whole_window, IntPtr.Zero, ref hwnd.whole_region_ptr);
734                                         Console.WriteLine ("Created a mouse trcaking region on the whole window @ {0}x{1} {2}x{3}", (short)window_bounds.origin.x, (short)window_bounds.origin.y, (short)(window_bounds.origin.x+hwnd.ClientRect.X), (short)(window_bounds.origin.y+hwnd.ClientRect.Y));
735                                 }
736                         }
737                         */
738                 }
739
740                 internal void CheckError (int result, string error) {
741                         if (result != 0)
742                                 throw new Exception ("XplatUIOSX.cs::" + error + "() Carbon subsystem threw an error: " + result);
743                 }
744
745                 internal void CheckError (int result) {
746                         if (result != 0)
747                                 throw new Exception ("XplatUIOSX.cs::Carbon subsystem threw an error: " + result);
748                 }
749
750                 #endregion 
751                 
752                 #region Public Methods
753
754                 internal override IntPtr InitializeDriver() {
755                         return IntPtr.Zero;
756                 }
757
758                 internal override void ShutdownDriver(IntPtr token) {
759                 }
760
761                 internal override void EnableThemes() {
762                         themes_enabled = true;
763                 }
764
765                 internal override void Activate(IntPtr handle) {
766                         ActivateWindow (GetControlOwner (handle), true);
767                 }
768
769                 internal override void AudibleAlert() {
770                         throw new NotImplementedException();
771                 }
772
773                 internal override void CaretVisible (IntPtr hwnd, bool visible) {
774                         if (Caret.Hwnd == hwnd) {
775                                 if (visible) {
776                                         if (Caret.Visible < 1) {
777                                                 Caret.Visible++;
778                                                 Caret.On = false;
779                                                 if (Caret.Visible == 1) {
780                                                         ShowCaret ();
781                                                         Caret.Timer.Start ();
782                                                 }
783                                         }
784                                 } else {
785                                         Caret.Visible--;
786                                         if (Caret.Visible == 0) {
787                                                 Caret.Timer.Stop ();
788                                                 HideCaret ();
789                                         }
790                                 }
791                         }
792                 }
793                 
794                 internal override bool CalculateWindowRect(ref Rectangle ClientRect, int Style, int ExStyle, Menu menu, out Rectangle WindowRect) {
795                         FormBorderStyle border_style;
796                         TitleStyle      title_style;
797                         bool border_static = false;
798
799                         title_style = TitleStyle.None;
800                         if ((Style & (int)WindowStyles.WS_CAPTION) != 0) {
801                                 if ((ExStyle & (int)WindowExStyles.WS_EX_TOOLWINDOW) != 0) {
802                                         title_style = TitleStyle.Tool;
803                                 } else {
804                                         title_style = TitleStyle.Normal;
805                                 }
806                         }
807
808                         border_style = FormBorderStyle.None;
809                         if ((ExStyle & (int)WindowExStyles.WS_EX_WINDOWEDGE) != 0) {
810                                 if ((ExStyle & (int)WindowExStyles.WS_EX_TOOLWINDOW) != 0) {
811                                         if ((Style & (int)WindowStyles.WS_THICKFRAME) != 0) {
812                                                 border_style = FormBorderStyle.SizableToolWindow;
813                                         } else {
814                                                 border_style = FormBorderStyle.FixedToolWindow;
815                                         }
816                                 } else if ((ExStyle & (int)WindowExStyles.WS_EX_DLGMODALFRAME) != 0) {
817                                         border_style = FormBorderStyle.FixedDialog;
818                                 } else if ((ExStyle & (int)WindowStyles.WS_THICKFRAME) != 0) {
819                                         border_style = FormBorderStyle.Sizable;
820                                 } else {
821                                         border_style = FormBorderStyle.FixedSingle;
822                                 }
823                         } else {
824                                 border_style = FormBorderStyle.Fixed3D;
825                         }
826
827                         WindowRect = Hwnd.GetWindowRectangle(border_style, border_static, menu, title_style,
828                                         SystemInformation.CaptionHeight,
829                                         SystemInformation.ToolWindowCaptionHeight, ClientRect);
830
831                         return true;
832                 }
833                 
834                 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {
835                         CGPoint pt = new CGPoint ();
836                         Rect wBounds = new Rect ();
837                         Hwnd    hwnd;
838
839                         hwnd = Hwnd.ObjectFromHandle(handle);
840
841                         pt.x = x;
842                         pt.y = y;
843
844                         GetWindowBounds (GetControlOwner (hwnd.client_window), 32, ref wBounds);
845                         HIViewConvertPoint (ref pt, handle, IntPtr.Zero);
846
847                         x = (int)(pt.x+wBounds.left);
848                         y = (int)(pt.y+wBounds.top);
849                 }
850
851                 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
852                         return null;
853                 }
854
855                 internal override void ClipboardClose(IntPtr handle) {
856                         throw new NotImplementedException();
857                 }
858
859                 internal override int ClipboardGetID(IntPtr handle, string format) {
860                         return 0;
861                 }
862
863                 internal override IntPtr ClipboardOpen(bool primary_selection) {
864                         throw new NotImplementedException();
865                 }
866
867                 internal override object ClipboardRetrieve(IntPtr handle, int id, XplatUI.ClipboardToObject converter) {
868                         throw new NotImplementedException();
869                 }
870
871                 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
872                         throw new NotImplementedException();
873                 }
874                 
875                 internal override void CreateCaret (IntPtr hwnd, int width, int height) {
876                         if (Caret.Hwnd != IntPtr.Zero)
877                                 DestroyCaret (Caret.Hwnd);
878
879                         Caret.Hwnd = hwnd;
880                         Caret.Width = width;
881                         Caret.Height = height;
882                         Caret.Visible = 0;
883                         Caret.On = false;
884                 }
885                 
886                 internal override IntPtr CreateWindow(CreateParams cp) {
887                         IntPtr windowHnd = IntPtr.Zero;
888                         IntPtr parentHnd = cp.Parent;
889                         bool realWindow = false;
890                         Rectangle clientRect;
891                         Hwnd hwnd = new Hwnd ();
892                         
893                         SetHwndStyles (hwnd, cp);
894                         
895                         if (parentHnd == IntPtr.Zero) {
896                                 if ((cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
897                                         // This is a child view that is going to be parentless;
898                                         realWindow = false;
899                                         CheckError (HIViewFindByID (HIViewGetRoot (FosterParent), new HIViewID (OSXConstants.kEventClassWindow, 1), ref parentHnd), "HIViewFindByID ()");
900                                 } else if ((cp.Style & (int)(WindowStyles.WS_POPUP))!=0) {
901                                         // This is a popup window that will be real.
902                                         if (cp.X < 1) cp.X = 0;
903                                         if (cp.Y < 1) cp.Y = 0;
904                                         realWindow = true;
905                                 } else {
906                                         // This is a real root window too
907                                         if (cp.X < 1) cp.X = 0;
908                                         if (cp.Y < 1) cp.Y = 0;
909                                         realWindow = true;
910                                 }
911                         } else {
912                                 realWindow = false;
913                         }
914
915                         if (realWindow) {
916                                 WindowClass windowklass = WindowClass.kOverlayWindowClass;
917                                 WindowAttributes attributes = WindowAttributes.kWindowCompositingAttribute | WindowAttributes.kWindowStandardHandlerAttribute;
918                                 if ((cp.Style & ((int)WindowStyles.WS_MINIMIZEBOX)) != 0) { 
919                                         attributes |= WindowAttributes.kWindowCollapseBoxAttribute;
920                                 }
921                                 if ((cp.Style & ((int)WindowStyles.WS_MAXIMIZEBOX)) != 0) {
922                                         attributes |= WindowAttributes.kWindowResizableAttribute | WindowAttributes.kWindowHorizontalZoomAttribute | WindowAttributes.kWindowVerticalZoomAttribute;
923                                 }
924                                 if ((cp.Style & ((int)WindowStyles.WS_SYSMENU)) != 0) {
925                                         attributes |= WindowAttributes.kWindowCloseBoxAttribute;
926                                 }
927                                 if ((cp.ExStyle & ((int)WindowExStyles.WS_EX_TOOLWINDOW)) != 0) {
928                                         attributes = WindowAttributes.kWindowStandardHandlerAttribute | WindowAttributes.kWindowCompositingAttribute;
929                                 }
930                                 if ((cp.Style & ((int)WindowStyles.WS_CAPTION)) != 0) {
931                                         windowklass = WindowClass.kDocumentWindowClass;
932                                 }
933                                         
934                                 IntPtr rect = IntPtr.Zero;
935                                 IntPtr viewHnd = IntPtr.Zero;
936                                 SetRect (ref rect, (short)cp.X, (short)(cp.Y + MenuBarHeight + TitleBarHeight), (short)(cp.Width+cp.X), (short)(cp.Height+cp.Y+MenuBarHeight+TitleBarHeight));
937                                 CheckError (CreateNewWindow (windowklass, attributes, ref rect, ref windowHnd), "CreateNewWindow ()");
938
939                                 CheckError (InstallEventHandler (GetWindowEventTarget (windowHnd), CarbonEventHandler, (uint)windowEvents.Length, windowEvents, windowHnd, IntPtr.Zero), "InstallEventHandler ()");
940                                 CheckError (HIViewFindByID (HIViewGetRoot (windowHnd), new HIViewID (OSXConstants.kEventClassWindow, 1), ref viewHnd), "HIViewFindByID ()");
941                                 parentHnd = viewHnd;
942                         }
943                         hwnd.X = cp.X;
944                         hwnd.Y = cp.Y;
945                         hwnd.Width = cp.Width;
946                         hwnd.Height = cp.Height;
947                         hwnd.Parent = Hwnd.ObjectFromHandle (cp.Parent);
948                         hwnd.visible = false;
949                         clientRect = hwnd.ClientRect;
950                         
951                         HIRect r = new HIRect (0, 0, cp.Width, cp.Height);
952                         CheckError (HIObjectCreate (__CFStringMakeConstantString ("com.apple.hiview"), 0, ref hwnd.whole_window), "HIObjectCreate ()");
953                         CheckError (InstallEventHandler (GetControlEventTarget (hwnd.whole_window), CarbonEventHandler, (uint)viewEvents.Length, viewEvents, hwnd.whole_window, IntPtr.Zero), "InstallEventHandler ()");
954                         CheckError (HIViewChangeFeatures (hwnd.whole_window, 1 << 1, 0), "HIViewChangeFeatures ()");
955                         CheckError (HIViewSetFrame (hwnd.whole_window, ref r), "HIViewSetFrame ()");
956                         hwnd.WholeWindow = hwnd.whole_window;
957                         
958                         r = new HIRect (0, 0, clientRect.Width, clientRect.Height);
959                         CheckError (HIObjectCreate (__CFStringMakeConstantString ("com.apple.hiview"), 0, ref hwnd.client_window), "HIObjectCreate ()");
960                         CheckError (InstallEventHandler (GetControlEventTarget (hwnd.client_window), CarbonEventHandler, (uint)viewEvents.Length, viewEvents, hwnd.client_window, IntPtr.Zero), "InstallEventHandler ()");
961                         CheckError (HIViewChangeFeatures (hwnd.client_window, 1 << 1, 0), "HIViewChangeFeatures ()");
962                         CheckError (HIViewSetFrame (hwnd.client_window, ref r), "HIViewSetFrame ()");
963                         hwnd.ClientWindow = hwnd.client_window;
964                         
965                         CheckError (HIViewAddSubview (hwnd.whole_window, hwnd.client_window));
966                         CheckError (HIViewPlaceInSuperviewAt (hwnd.client_window, clientRect.X, clientRect.Y));
967                         
968                         if (parentHnd != IntPtr.Zero && parentHnd != hwnd.WholeWindow) {
969                                 CheckError (HIViewAddSubview (parentHnd, hwnd.whole_window), "HIViewAddSubview ()");
970                                 CheckError (HIViewPlaceInSuperviewAt (hwnd.whole_window, cp.X, cp.Y), "HIPlaceInSuperviewAt ()");
971                                 if ((cp.Style & (int)(WindowStyles.WS_VISIBLE))!=0) {
972                                         CheckError (HIViewSetVisible (hwnd.whole_window, true), "HIViewSetVisible ()");
973                                         CheckError (HIViewSetVisible (hwnd.client_window, true), "HIViewSetVisible ()");
974                                         hwnd.visible = true;
975                                 } else {
976                                         CheckError (HIViewSetVisible (hwnd.whole_window, false), "HIViewSetVisible ()");
977                                         CheckError (HIViewSetVisible (hwnd.client_window, false), "HIViewSetVisible ()");
978                                         hwnd.visible = false;
979                                 }
980                         }
981                         if (realWindow) {
982                                 WindowMapping [hwnd.Handle] = windowHnd;
983                                 if ((cp.Style & (int)(WindowStyles.WS_VISIBLE))!=0) {
984                                         CheckError (ShowWindow (windowHnd));
985                                         CheckError (HIViewSetVisible (hwnd.whole_window, true), "HIViewSetVisible ()");
986                                         CheckError (HIViewSetVisible (hwnd.client_window, true), "HIViewSetVisible ()");
987                                         hwnd.visible = true;
988                                 }
989                                 if ((cp.Style & (int)(WindowStyles.WS_POPUP))!=0) {
990                                         CheckError (HIViewSetVisible (hwnd.whole_window, true), "HIViewSetVisible ()");
991                                         CheckError (HIViewSetVisible (hwnd.client_window, true), "HIViewSetVisible ()");
992                                         hwnd.visible = true;
993                                 }
994                         }       
995                         
996                         return hwnd.Handle;
997                 }
998
999                 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
1000                         CreateParams create_params = new CreateParams();
1001
1002                         create_params.Caption = "";
1003                         create_params.X = X;
1004                         create_params.Y = Y;
1005                         create_params.Width = Width;
1006                         create_params.Height = Height;
1007
1008                         create_params.ClassName=XplatUI.DefaultClassName;
1009                         create_params.ClassStyle = 0;
1010                         create_params.ExStyle=0;
1011                         create_params.Parent=IntPtr.Zero;
1012                         create_params.Param=0;
1013
1014                         return CreateWindow(create_params);
1015                 }
1016
1017                 [MonoTODO]
1018                 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
1019                         throw new NotImplementedException ();
1020                 }
1021                 
1022                 [MonoTODO]
1023                 internal override IntPtr DefineStdCursor(StdCursor id) {
1024                         switch (id) {
1025                                 case StdCursor.AppStarting:
1026                                         return (IntPtr)ThemeCursor.kThemeSpinningCursor;
1027                                 case StdCursor.Arrow:
1028                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1029                                 case StdCursor.Cross:
1030                                         return (IntPtr)ThemeCursor.kThemeCrossCursor;
1031                                 case StdCursor.Default:
1032                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1033                                 case StdCursor.Hand:
1034                                         return (IntPtr)ThemeCursor.kThemeOpenHandCursor;
1035                                 case StdCursor.Help:
1036                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1037                                 case StdCursor.HSplit:
1038                                         return (IntPtr)ThemeCursor.kThemeResizeLeftRightCursor;
1039                                 case StdCursor.IBeam:
1040                                         return (IntPtr)ThemeCursor.kThemeIBeamCursor;
1041                                 case StdCursor.No:
1042                                         return (IntPtr)ThemeCursor.kThemeNotAllowedCursor;
1043                                 case StdCursor.NoMove2D:
1044                                         return (IntPtr)ThemeCursor.kThemeNotAllowedCursor;
1045                                 case StdCursor.NoMoveHoriz:
1046                                         return (IntPtr)ThemeCursor.kThemeNotAllowedCursor;
1047                                 case StdCursor.NoMoveVert:
1048                                         return (IntPtr)ThemeCursor.kThemeNotAllowedCursor;
1049                                 case StdCursor.PanEast:
1050                                         return (IntPtr)ThemeCursor.kThemeResizeRightCursor;
1051                                 case StdCursor.PanNE:
1052                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1053                                 case StdCursor.PanNorth:
1054                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1055                                 case StdCursor.PanNW:
1056                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1057                                 case StdCursor.PanSE:
1058                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1059                                 case StdCursor.PanSouth:
1060                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1061                                 case StdCursor.PanSW:
1062                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1063                                 case StdCursor.PanWest:
1064                                         return (IntPtr)ThemeCursor.kThemeResizeLeftCursor;
1065                                 case StdCursor.SizeAll:
1066                                         return (IntPtr)ThemeCursor.kThemeResizeLeftRightCursor;
1067                                 case StdCursor.SizeNESW:
1068                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1069                                 case StdCursor.SizeNS:
1070                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1071                                 case StdCursor.SizeNWSE:
1072                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1073                                 case StdCursor.SizeWE:
1074                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1075                                 case StdCursor.UpArrow:
1076                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1077                                 case StdCursor.VSplit:
1078                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1079                                 case StdCursor.WaitCursor:
1080                                         return (IntPtr)ThemeCursor.kThemeSpinningCursor;
1081                                 default:
1082                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1083                         }
1084                 }
1085                 
1086                 internal override IntPtr DefWndProc(ref Message msg) {
1087                         Hwnd hwnd = Hwnd.ObjectFromHandle (msg.HWnd);
1088                         switch ((Msg)msg.Msg) {
1089                                 case Msg.WM_DESTROY: {
1090                                         if (WindowMapping [hwnd.Handle] != null)
1091
1092                                                 Exit ();
1093                                         break;
1094                                 }
1095                         }
1096                         return IntPtr.Zero;
1097                 }
1098
1099                 internal override void DestroyCaret (IntPtr hwnd) {
1100                         if (Caret.Hwnd == hwnd) {
1101                                 if (Caret.Visible == 1) {
1102                                         Caret.Timer.Stop ();
1103                                         HideCaret ();
1104                                 }
1105                                 Caret.Hwnd = IntPtr.Zero;
1106                                 Caret.Visible = 0;
1107                                 Caret.On = false;
1108                         }
1109                 }
1110                 
1111                 [MonoTODO]
1112                 internal override void DestroyCursor(IntPtr cursor) {
1113                         throw new NotImplementedException ();
1114                 }
1115         
1116                 internal override void DestroyWindow(IntPtr handle) {
1117                         Hwnd    hwnd;
1118
1119                         hwnd = Hwnd.ObjectFromHandle(handle);
1120                         
1121                         if ((hwnd.whole_window != IntPtr.Zero) && HIViewGetSuperview (hwnd.whole_window) != IntPtr.Zero)
1122                                 CheckError (HIViewRemoveFromSuperview (handle), "HIViewRemoveFromSuperview ()");
1123
1124                         if (WindowMapping [hwnd.Handle] != null) {
1125                                 DisposeWindow ((IntPtr)(WindowMapping [hwnd.Handle]));
1126                         }
1127                         CFRelease (hwnd.ClientWindow);
1128                         CFRelease (hwnd.WholeWindow);
1129                 }
1130
1131                 internal override IntPtr DispatchMessage(ref MSG msg) {
1132                         return NativeWindow.WndProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
1133                 }
1134                 
1135                 internal override void DoEvents() {
1136                 }
1137
1138                 internal override void EnableWindow(IntPtr handle, bool Enable) {
1139                         //Like X11 we need not do anything here
1140                 }
1141
1142                 internal override void EndLoop(Thread thread) {
1143                         throw new NotImplementedException();
1144                 }
1145
1146                 internal void Exit() {
1147                         GetMessageResult = false;
1148                         ExitToShell ();
1149                 }
1150                 
1151                 internal override IntPtr GetActive() {
1152                         foreach (DictionaryEntry entry in WindowMapping)
1153                                 if (IsWindowActive ((IntPtr)(entry.Value)))
1154                                         return (IntPtr)(entry.Key);
1155
1156                         return IntPtr.Zero;
1157                 }
1158
1159                 internal override Region GetClipRegion(IntPtr hwnd) {
1160                         return null;
1161                 }
1162
1163                 [MonoTODO]
1164                 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
1165                         throw new NotImplementedException ();
1166                 }
1167                 
1168                 internal override void GetDisplaySize(out Size size) {
1169                         HIRect bounds = CGDisplayBounds (CGMainDisplayID ());
1170                         size = new Size ((int)bounds.size.width, (int)bounds.size.height);
1171                 }
1172
1173                 internal override IntPtr GetParent(IntPtr handle) {
1174                         Hwnd    hwnd;
1175
1176                         hwnd = Hwnd.ObjectFromHandle(handle);
1177                         if (hwnd != null && hwnd.parent != null) {
1178                                 return hwnd.parent.Handle;
1179                         }
1180                         return IntPtr.Zero;
1181                 }
1182                 
1183                 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
1184                         QDPoint pt = new QDPoint ();
1185                         GetGlobalMouse (ref pt);
1186                         x = pt.x;
1187                         y = pt.y;
1188                 }
1189
1190                 internal override IntPtr GetFocus() {
1191                         return FocusWindow;
1192                 }
1193
1194                 
1195                 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
1196                         return GetFontMetrics(g.GetHdc(), font.ToHfont(), out ascent, out descent);
1197                 }
1198                 
1199                 [MonoTODO]
1200                 internal override Point GetMenuOrigin(IntPtr hwnd) {
1201                         throw new NotImplementedException();
1202                 }
1203
1204                 internal override bool GetMessage(object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1205                         IntPtr evtRef = IntPtr.Zero;
1206                         IntPtr target = GetEventDispatcherTarget();
1207                         CheckTimers (DateTime.Now);
1208                         ReceiveNextEvent (0, IntPtr.Zero, 0, true, ref evtRef);
1209                         if (evtRef != IntPtr.Zero && target != IntPtr.Zero) {
1210                                 SendEventToEventTarget (evtRef, target);
1211                                 ReleaseEvent (evtRef);
1212                         }
1213                         
1214                         lock (MessageQueue) {
1215                                 if (MessageQueue.Count <= 0) {
1216                                         if (Idle != null) 
1217                                                 Idle (this, EventArgs.Empty);
1218                                         else if (TimerList.Count == 0) {
1219                                                 ReceiveNextEvent (0, IntPtr.Zero, Convert.ToDouble ("0." + Timer.Minimum), true, ref evtRef);
1220                                                 if (evtRef != IntPtr.Zero && target != IntPtr.Zero) {
1221                                                         SendEventToEventTarget (evtRef, target);
1222                                                         ReleaseEvent (evtRef);
1223                                                 }
1224                                         } else {
1225                                                 ReceiveNextEvent (0, IntPtr.Zero, NextTimeout (), true, ref evtRef);
1226                                                 if (evtRef != IntPtr.Zero && target != IntPtr.Zero) {
1227                                                         SendEventToEventTarget (evtRef, target);
1228                                                         ReleaseEvent (evtRef);
1229                                                 }
1230                                         }
1231                                         msg.hwnd = IntPtr.Zero;
1232                                         msg.message = Msg.WM_ENTERIDLE;
1233                                         return GetMessageResult;
1234                                 }
1235                                 msg = (MSG) MessageQueue.Dequeue ();
1236                         }
1237                         return GetMessageResult;
1238                 }
1239                 
1240                 [MonoTODO]
1241                 internal override bool GetText(IntPtr handle, out string text) {
1242                         throw new NotImplementedException ();
1243                 }
1244                 
1245                 internal override void GetWindowPos(IntPtr handle, bool is_toplevel, out int x, out int y, out int width, out int height, out int client_width, out int client_height) {
1246                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1247                         Rectangle rect = hwnd.ClientRect;
1248                         
1249                         x = hwnd.x;
1250                         y = hwnd.y;
1251                         width = hwnd.width;
1252                         height = hwnd.height;
1253
1254                         client_width = rect.Width;
1255                         client_height = rect.Height;
1256                 }
1257                 
1258                 internal override FormWindowState GetWindowState(IntPtr hwnd) {
1259                         IntPtr window = GetControlOwner (hwnd);
1260
1261                         if (IsWindowCollapsed (window))
1262                                 return FormWindowState.Minimized;
1263                         if (IsWindowInStandardState (window, IntPtr.Zero, IntPtr.Zero))
1264                                 return FormWindowState.Maximized;
1265
1266                         return FormWindowState.Normal;
1267                 }
1268                 
1269                 internal override void GrabInfo(out IntPtr handle, out bool GrabConfined, out Rectangle GrabArea) {
1270                         handle = Grab.Hwnd;
1271                         GrabConfined = Grab.Confined;
1272                         GrabArea = Grab.Area;
1273                 }
1274                 
1275                 internal override void GrabWindow(IntPtr handle, IntPtr confine_to_handle) {
1276                         GrabWindowHwnd = Hwnd.ObjectFromHandle (handle);
1277                 }
1278                 
1279                 internal override void UngrabWindow(IntPtr hwnd) {
1280                         GrabWindowHwnd = null;
1281                         Grab.Hwnd = IntPtr.Zero;
1282                         Grab.Confined = false;
1283                 }
1284                 
1285                 internal override void HandleException(Exception e) {
1286                         StackTrace st = new StackTrace(e);
1287                         Console.WriteLine("Exception '{0}'", e.Message+st.ToString());
1288                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
1289                 }
1290                 
1291                 internal override void Invalidate (IntPtr handle, Rectangle rc, bool clear) {
1292                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1293                         
1294                         if (hwnd.visible && HIViewIsVisible (handle)) {
1295                                 MSG msg = new MSG ();
1296                                 msg.hwnd = hwnd.Handle;
1297                                 msg.wParam = IntPtr.Zero;
1298                                 msg.lParam = IntPtr.Zero;
1299                                 msg.message = Msg.WM_PAINT;
1300                                 MessageQueue.Enqueue (msg);
1301                                 // This is currently causing some graphics corruption
1302                                 //hwnd.AddInvalidArea (rc.X, rc.Y, rc.Width, rc.Height);
1303                                 hwnd.AddInvalidArea (0, 0, hwnd.ClientRect.Width, hwnd.ClientRect.Height);
1304                                 hwnd.expose_pending = true;
1305                         }
1306                 }
1307
1308                 internal override void InvalidateNC (IntPtr handle)
1309                 {
1310                         // XXX FIXME
1311                 }
1312                 
1313                 internal override bool IsEnabled(IntPtr handle) {
1314                         return Hwnd.ObjectFromHandle(handle).Enabled;
1315                 }
1316                 
1317                 internal override bool IsVisible(IntPtr handle) {
1318                         return Hwnd.ObjectFromHandle(handle).visible;
1319                 }
1320                 
1321                 internal override void KillTimer(Timer timer) {
1322                         lock (TimerList) {
1323                                 TimerList.Remove(timer);
1324                         }
1325                 }
1326                 
1327                 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {
1328                         CGPoint pt = new CGPoint ();
1329                         Rect wBounds = new Rect ();
1330                         Hwnd    hwnd;
1331
1332                         hwnd = Hwnd.ObjectFromHandle(handle);
1333
1334                         pt.x = x;
1335                         pt.y = y;
1336
1337                         GetWindowBounds (GetControlOwner (hwnd.whole_window), 32, ref wBounds);
1338                         HIViewConvertPoint (ref pt, handle, IntPtr.Zero);
1339
1340                         x = (int)(pt.x+wBounds.left);
1341                         y = (int)(pt.y+wBounds.top);
1342                 }
1343
1344                 [MonoTODO]
1345                 internal override void OverrideCursor(IntPtr cursor) {
1346                         throw new NotImplementedException ();
1347                 }
1348
1349                 internal override PaintEventArgs PaintEventStart(IntPtr handle, bool client) {
1350                         PaintEventArgs  paint_event;
1351                         Hwnd            hwnd;
1352
1353                         hwnd = Hwnd.ObjectFromHandle(handle);
1354
1355                         if (Caret.Visible == 1) {
1356                                 Caret.Paused = true;
1357                                 HideCaret();
1358                         }
1359
1360                         Graphics dc = Graphics.FromHwnd (hwnd.client_window);
1361                         paint_event = new PaintEventArgs(dc, hwnd.Invalid);
1362
1363                         hwnd.expose_pending = false;
1364                         hwnd.ClearInvalidArea();
1365
1366                         hwnd.drawing_stack.Push (dc);
1367
1368                         return paint_event;
1369                 }
1370                 
1371                 internal override void PaintEventEnd(IntPtr handle, bool client) {
1372                         Hwnd    hwnd;
1373
1374                         hwnd = Hwnd.ObjectFromHandle(handle);
1375
1376                         Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1377                         dc.Flush ();
1378                         dc.Dispose ();
1379                         
1380                         if (Caret.Visible == 1) {
1381                                 ShowCaret();
1382                                 Caret.Paused = false;
1383                         }
1384                 }
1385                 
1386                 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1387                         Console.WriteLine("XplatUIOSX.PeekMessage");
1388                         return true;
1389                 }
1390
1391                 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
1392                         MSG msg = new MSG();
1393                         msg.hwnd = hwnd;
1394                         msg.message = message;
1395                         msg.wParam = wParam;
1396                         msg.lParam = lParam;
1397                         MessageQueue.Enqueue (msg);
1398                         return true;
1399                 }
1400
1401                 [MonoTODO]
1402                 internal override void PostQuitMessage(int exitCode) {
1403                         throw new NotImplementedException();
1404                 }
1405
1406                 [MonoTODO]
1407                 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave) {
1408                         throw new NotImplementedException();
1409                 }
1410
1411                 [MonoTODO]              
1412                 internal override void RequestNCRecalc(IntPtr handle) {
1413                         throw new NotImplementedException();
1414                 }
1415
1416                 [MonoTODO]              
1417                 internal override void ResetMouseHover(IntPtr handle) {
1418                         throw new NotImplementedException();
1419                 }
1420
1421                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y) {
1422                         CGPoint pt = new CGPoint ();
1423                         Rect wBounds = new Rect ();
1424
1425                         GetWindowBounds (GetControlOwner (handle), 32, ref wBounds);
1426                         pt.x = (x-wBounds.left);
1427                         pt.y = (y-wBounds.top);
1428                         HIViewConvertPoint (ref pt, IntPtr.Zero, handle);
1429
1430                         x = (int)pt.x;
1431                         y = (int)pt.y;
1432                 }
1433
1434                 [MonoTODO]
1435                 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
1436                         CGPoint pt = new CGPoint ();
1437                         Rect wBounds = new Rect ();
1438
1439                         GetWindowBounds (GetControlOwner (handle), 32, ref wBounds);
1440                         pt.x = (x-wBounds.left);
1441                         pt.y = (y-wBounds.top);
1442                         HIViewConvertPoint (ref pt, IntPtr.Zero, handle);
1443
1444                         x = (int)pt.x;
1445                         y = (int)pt.y;
1446                 }
1447
1448                 internal override void ScrollWindow(IntPtr handle, Rectangle area, int XAmount, int YAmount, bool clear) {
1449                         //IntPtr rect = IntPtr.Zero;
1450                         //HIRect vBounds = new HIRect ();
1451                    
1452             Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
1453           
1454                         /*
1455                         if (hwnd.invalid != Rectangle.Empty) {
1456                                 // BIG FAT WARNING. This only works with how we use this function right now
1457                                 // where we basically still scroll the whole window, but work around areas
1458                                 // that are covered by our children
1459
1460                                 hwnd.invalid.X += XAmount;
1461                                 hwnd.invalid.Y += YAmount;
1462
1463                                 if (hwnd.invalid.X < 0) {
1464                                         hwnd.invalid.Width += hwnd.invalid.X;
1465                                         hwnd.invalid.X =0;
1466                                 }
1467
1468                                 if (hwnd.invalid.Y < 0) {
1469                                         hwnd.invalid.Height += hwnd.invalid.Y;
1470                                         hwnd.invalid.Y =0;
1471                                 }
1472                         }*/
1473                         
1474                         HIRect scrollrect = new HIRect ();
1475                         scrollrect.origin.x = area.X;
1476                         scrollrect.origin.y = area.Y;
1477                         scrollrect.size.width = area.Width;
1478                         scrollrect.size.height = area.Height;
1479                         HIViewScrollRect (hwnd.Handle, ref scrollrect, (float)XAmount, (float)-YAmount);
1480                         /*
1481             HIViewGetBounds (hwnd.client_window, ref vBounds);
1482                         HIViewConvertRect (ref vBounds, hwnd.client_window, IntPtr.Zero);
1483                         SetRect (ref rect, (short)(vBounds.origin.x+area.X), (short)(vBounds.origin.y-TitleBarHeight+area.Y), (short)(vBounds.origin.x+area.Width), (short)(vBounds.origin.y+area.Height-TitleBarHeight));
1484                         ScrollRect (ref rect, (short)XAmount, (short)-YAmount, IntPtr.Zero);
1485                         */
1486                         // Generate an expose for the area exposed by the horizontal scroll
1487                         /*
1488                         if (XAmount > 0) {
1489                                 hwnd.AddInvalidArea (area.X, area.Y, XAmount, area.Height);
1490                         } else if (XAmount < 0) {
1491                                 hwnd.AddInvalidArea (XAmount + area.X + area.Width, area.Y, -XAmount, area.Height);
1492                         }
1493
1494                         // Generate an expose for the area exposed by the vertical scroll
1495                         if (YAmount > 0) {
1496                                 hwnd.AddInvalidArea (area.X, area.Y, area.Width, YAmount);
1497                         } else if (YAmount < 0) {
1498                                 hwnd.AddInvalidArea (area.X, YAmount + area.Y + area.Height, area.Width, -YAmount);
1499                         }
1500                         
1501                         UpdateWindow (handle);
1502                         */
1503                 }
1504                 
1505                 
1506                 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool clear) {
1507                         throw new NotImplementedException("");
1508                 }
1509                 
1510                 [MonoTODO]
1511                 internal override void SendAsyncMethod (AsyncMethodData method) {
1512                         throw new NotImplementedException ();
1513                 }
1514
1515                 [MonoTODO]
1516                 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
1517                         throw new NotImplementedException ();
1518                 }
1519                 
1520                 internal override int SendInput(IntPtr hwnd, Queue keys) {
1521                         return 0;
1522                 }
1523
1524
1525                 internal override void SetCaretPos (IntPtr hwnd, int x, int y) {
1526                         if (Caret.Hwnd == hwnd) {
1527                                 CGPoint cpt = new CGPoint ();
1528                                 cpt.x = x;
1529                                 cpt.y = y;
1530                                 HIViewConvertPoint (ref cpt, hwnd, IntPtr.Zero);
1531                                 Caret.Timer.Stop ();
1532                                 HideCaret ();
1533                                 Caret.X = (int)cpt.x;
1534                                 Caret.Y = (int)cpt.y-23;
1535                                 if (Caret.Visible == 1) {
1536                                         ShowCaret ();
1537                                         Caret.Timer.Start ();
1538                                 }
1539                         }
1540                 }
1541
1542                 internal override void SetClipRegion(IntPtr hwnd, Region region) {
1543                         throw new NotImplementedException();
1544                 }
1545                 
1546                 internal override void SetCursor(IntPtr window, IntPtr cursor) {
1547                         SetThemeCursor ((uint) cursor);
1548                 }
1549                 
1550                 internal override void SetCursorPos(IntPtr handle, int x, int y) {
1551                         CGDisplayMoveCursorToPoint (CGMainDisplayID (), new CGPoint (x, y));
1552                 }
1553                 
1554                 internal override void SetFocus(IntPtr handle) {
1555                         if (FocusWindow != IntPtr.Zero) {
1556                                 PostMessage(FocusWindow, Msg.WM_KILLFOCUS, handle, IntPtr.Zero);
1557                         }
1558                         PostMessage(handle, Msg.WM_SETFOCUS, FocusWindow, IntPtr.Zero);
1559                         FocusWindow = handle;
1560                 }
1561
1562                 [MonoTODO]
1563                 internal override void SetIcon(IntPtr handle, Icon icon) {
1564                         throw new NotImplementedException();
1565                 }
1566
1567                 
1568                 internal override void SetModal(IntPtr handle, bool Modal) {
1569                         IntPtr hWnd = GetControlOwner (Hwnd.ObjectFromHandle (handle).WholeWindow);
1570                         if (Modal)
1571                                 BeginAppModalStateForWindow (hWnd);
1572                         else
1573                                 EndAppModalStateForWindow (hWnd);
1574                         return;
1575                 }
1576
1577                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
1578                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1579                         
1580                         hwnd.parent = Hwnd.ObjectFromHandle (parent);
1581                         if (HIViewGetSuperview (hwnd.whole_window) != IntPtr.Zero) {
1582                                 CheckError (HIViewRemoveFromSuperview (hwnd.whole_window), "HIViewRemoveFromSuperview ()");
1583                         }
1584                         CheckError (HIViewAddSubview (hwnd.parent.client_window, hwnd.whole_window));
1585                         CheckError (HIViewAddSubview (hwnd.whole_window, hwnd.client_window));
1586                         HIViewPlaceInSuperviewAt (hwnd.client_window, hwnd.ClientRect.X, hwnd.ClientRect.Y);
1587                         
1588                         return IntPtr.Zero;
1589                 }
1590                 
1591                 internal override void SetTimer (Timer timer) {
1592                         lock (TimerList) {
1593                                 TimerList.Add (timer);
1594                         }
1595                 }
1596                 
1597                 internal override bool SetTopmost(IntPtr hWnd, IntPtr hWndOwner, bool Enabled) {
1598                         HIViewSetZOrder (hWnd, 1, IntPtr.Zero);
1599                         return true;
1600                 }
1601                 
1602                 
1603                 internal override bool SetVisible(IntPtr handle, bool visible, bool activate) {
1604                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1605                         object window = WindowMapping [hwnd.Handle];
1606                         if (window != null)
1607                                 if (visible)
1608                                         ShowWindow ((IntPtr)window);
1609                                 else
1610                                         HideWindow ((IntPtr)window);
1611                                         
1612                         HIViewSetVisible (hwnd.whole_window, visible);
1613                         HIViewSetVisible (hwnd.client_window, visible);
1614                         hwnd.visible = visible;
1615                         return true;
1616                 }
1617                 
1618                 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
1619                         Hwnd    hwnd;
1620
1621                         hwnd = Hwnd.ObjectFromHandle(handle);
1622                         hwnd.border_style = border_style;
1623
1624                         // FIXME - do we need to trigger some resize?
1625                 }
1626
1627                 internal override void SetMenu(IntPtr handle, Menu menu) {
1628                         Hwnd    hwnd;
1629
1630                         hwnd = Hwnd.ObjectFromHandle(handle);
1631                         hwnd.menu = menu;
1632
1633                         // FIXME - do we need to trigger some resize?
1634                 }
1635                 
1636                 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1637                         throw new NotImplementedException();
1638                 }
1639
1640                 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1641                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1642                         Rectangle client_rect = Hwnd.GetClientRectangle(hwnd.border_style, hwnd.border_static, hwnd.menu,
1643                                         hwnd.title_style, SystemInformation.CaptionHeight,
1644                                         SystemInformation.ToolWindowCaptionHeight, width, height);
1645
1646                         // Save a server roundtrip (and prevent a feedback loop)
1647                         if ((hwnd.x == x) && (hwnd.y == y) && (hwnd.width == width) && (hwnd.height == height)) {
1648                                 return;
1649                         }
1650
1651
1652                         if (WindowMapping [hwnd.Handle] != null) {
1653                                 if (y <= MenuBarHeight+TitleBarHeight) {
1654                                         y+=MenuBarHeight+TitleBarHeight;
1655                                 }
1656                                 IntPtr rect = IntPtr.Zero;
1657                                 SetRect (ref rect, (short)x, (short)y, (short)(x+width), (short)(y+height));
1658                                 CheckError (SetWindowBounds ((IntPtr) WindowMapping [hwnd.Handle], 33, ref rect), "SetWindowBounds ()");
1659                                 HIRect r = new HIRect (0, 0, width, height);
1660                                 CheckError (HIViewSetFrame (hwnd.whole_window, ref r), "HIViewSetFrame ()");
1661                                 r = new HIRect (client_rect.X, client_rect.Y, client_rect.X+client_rect.Width, client_rect.Y+client_rect.Height);
1662                                 CheckError (HIViewSetFrame (hwnd.client_window, ref r), "HIViewSetFrame ()");
1663                         } else {
1664                                 HIRect r = new HIRect (x, y, width, height);
1665                                 CheckError (HIViewSetFrame (hwnd.whole_window, ref r), "HIViewSetFrame ()");
1666                                 r = new HIRect (client_rect.X, client_rect.Y, client_rect.X+client_rect.Width, client_rect.Y+client_rect.Height);
1667                                 CheckError (HIViewSetFrame (hwnd.client_window, ref r), "HIViewSetFrame ()");
1668                         }                       
1669                 }
1670                 
1671                 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1672                         IntPtr window = GetControlOwner (hwnd);
1673
1674                         switch (state) {
1675                                 case FormWindowState.Minimized: {
1676                                         CollapseWindow (window, true);
1677                                         break;
1678                                 }
1679                                 case FormWindowState.Normal: {
1680                                         ZoomWindow (window, 7, false);
1681                                         break;
1682                                 }
1683                                 case FormWindowState.Maximized: {
1684                                         ZoomWindow (window, 8, false);
1685                                         break;
1686                                 }
1687                         }
1688                 }
1689                 
1690                 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1691                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1692                         SetHwndStyles(hwnd, cp);
1693                         
1694                         if (WindowMapping [hwnd.Handle] != null) {
1695                                 WindowAttributes attributes = WindowAttributes.kWindowCompositingAttribute | WindowAttributes.kWindowStandardHandlerAttribute;
1696                                 if ((cp.Style & ((int)WindowStyles.WS_MINIMIZEBOX)) != 0) { 
1697                                         attributes |= WindowAttributes.kWindowCollapseBoxAttribute;
1698                                 }
1699                                 if ((cp.Style & ((int)WindowStyles.WS_MAXIMIZEBOX)) != 0) {
1700                                         attributes |= WindowAttributes.kWindowResizableAttribute | WindowAttributes.kWindowHorizontalZoomAttribute | WindowAttributes.kWindowVerticalZoomAttribute;
1701                                 }
1702                                 if ((cp.Style & ((int)WindowStyles.WS_SYSMENU)) != 0) {
1703                                         attributes |= WindowAttributes.kWindowCloseBoxAttribute;
1704                                 }
1705                                 if ((cp.ExStyle & ((int)WindowExStyles.WS_EX_TOOLWINDOW)) != 0) {
1706                                         attributes = WindowAttributes.kWindowStandardHandlerAttribute | WindowAttributes.kWindowCompositingAttribute;
1707                                 }
1708
1709                                 WindowAttributes outAttributes = WindowAttributes.kWindowNoAttributes;
1710                                 GetWindowAttributes ((IntPtr)WindowMapping [hwnd.Handle], ref outAttributes);
1711                                 ChangeWindowAttributes ((IntPtr)WindowMapping [hwnd.Handle], attributes, outAttributes);
1712                         }
1713                 }
1714
1715                 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1716                 }
1717
1718                 internal override double GetWindowTransparency(IntPtr handle)
1719                 {
1720                         return 1.0;
1721                 }
1722
1723                 internal override TransparencySupport SupportsTransparency() {
1724                         return TransparencySupport.None;
1725                 }
1726                 
1727                 internal override bool SetZOrder(IntPtr handle, IntPtr after_handle, bool Top, bool Bottom) {
1728                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1729                         
1730                         if (Top) {
1731                                 HIViewSetZOrder (hwnd.whole_window, 2, IntPtr.Zero);
1732                                 return true;
1733                         } else if (!Bottom) {
1734                                 Hwnd after_hwnd = Hwnd.ObjectFromHandle (after_handle);
1735                                 HIViewSetZOrder (hwnd.whole_window, 2, after_hwnd.whole_window);
1736                         } else {
1737                                 HIViewSetZOrder (hwnd.whole_window, 1, IntPtr.Zero);
1738                                 return true;
1739                         }
1740                         return false;
1741                 }
1742
1743                 internal override void ShowCursor(bool show) {
1744                         if (show)
1745                                 CGDisplayShowCursor (CGMainDisplayID ());
1746                         else
1747                                 CGDisplayHideCursor (CGMainDisplayID ());
1748                 }
1749
1750                 internal override object StartLoop(Thread thread) {
1751                         throw new NotImplementedException();
1752                 }
1753                 
1754                 [MonoTODO]
1755                 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
1756                         throw new NotImplementedException();
1757                 }
1758
1759                 [MonoTODO]
1760                 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
1761                         throw new NotImplementedException();
1762                 }
1763
1764                 [MonoTODO]
1765                 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
1766                         throw new NotImplementedException();
1767                 }
1768                 
1769                 internal override bool Text(IntPtr handle, string text) {
1770                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1771                         if (WindowMapping [hwnd.Handle] != null) {
1772                                 CheckError (SetWindowTitleWithCFString ((IntPtr)(WindowMapping [hwnd.Handle]), __CFStringMakeConstantString (text)));
1773                         }
1774                         CheckError (SetControlTitleWithCFString (hwnd.whole_window, __CFStringMakeConstantString (text)));
1775                         CheckError (SetControlTitleWithCFString (hwnd.client_window, __CFStringMakeConstantString (text)));
1776                         return true;
1777                 }
1778                 
1779                 internal override void UpdateWindow(IntPtr handle) {
1780                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1781                         
1782                         if (hwnd.visible && HIViewIsVisible (handle) && !hwnd.expose_pending) {
1783                                 MSG msg = new MSG ();
1784                                 msg.message = Msg.WM_PAINT;
1785                                 msg.hwnd = hwnd.Handle;
1786                                 msg.lParam = IntPtr.Zero;
1787                                 msg.wParam = IntPtr.Zero;
1788                                 MessageQueue.Enqueue (msg);
1789                         }
1790                 }
1791                 
1792                 internal override bool TranslateMessage(ref MSG msg) {
1793                         bool res = false;
1794                         Hwnd hwnd = Hwnd.ObjectFromHandle (msg.hwnd);
1795                                         
1796                         switch (msg.message) {
1797                                 case Msg.WM_MOUSEMOVE: {
1798                                         // We're grabbed
1799                                         if (GrabWindowHwnd != null) {
1800                                                 if (GrabWindowHwnd.Handle != hwnd.Handle) {
1801                                                         return false;
1802                                                 }
1803                                         } else {
1804                                                 if (MouseWindow != null) {
1805                                                         if (MouseWindow.Handle != hwnd.Handle) {
1806                                                                 PostMessage (MouseWindow.Handle, Msg.WM_MOUSE_LEAVE, IntPtr.Zero, IntPtr.Zero);
1807                                                                 PostMessage (hwnd.Handle, Msg.WM_MOUSE_ENTER, IntPtr.Zero, IntPtr.Zero);
1808                                                                 MouseWindow = hwnd;
1809                                                         }
1810                                                 } else {
1811                                                         MouseWindow = hwnd;
1812                                                 }
1813                                         }
1814                                         break;
1815                                 }
1816                                 case Msg.WM_SETFOCUS: {
1817                                         break;   
1818                                 }                                       
1819                                 
1820                         }
1821                         
1822                         // This is a hideous temporary keyboard hack to bind some keys  
1823                         if (msg.message >= Msg.WM_KEYFIRST && msg.message <= Msg.WM_KEYLAST)
1824                                 res = true;
1825
1826                         if (msg.message != Msg.WM_KEYDOWN && msg.message != Msg.WM_SYSKEYDOWN)
1827                                 return res;
1828
1829                         if ((int)msg.wParam >= (int)'0' && (int)msg.wParam <= (int)'z') {
1830                                 Msg message;
1831                                 message = Msg.WM_CHAR;
1832                                 PostMessage (msg.hwnd, message, msg.wParam, msg.lParam);
1833                         }
1834                         return true;
1835                 }
1836                 
1837                 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
1838                         throw new NotImplementedException();
1839                 }
1840
1841                 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor) {
1842                         throw new NotImplementedException();
1843                 }
1844
1845                 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
1846                         throw new NotImplementedException();
1847                 }
1848
1849                 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
1850                         throw new NotImplementedException();
1851                 }
1852
1853                 [MonoTODO]
1854                 internal override SizeF GetAutoScaleSize(Font font) {
1855                         throw new NotImplementedException();
1856                 }
1857
1858                 internal override Point MousePosition {
1859                         get {
1860                                 return mouse_position;
1861                         }
1862                 }
1863                 #endregion
1864                 
1865                 #region System information
1866                 internal override int KeyboardSpeed { get{ throw new NotImplementedException(); } } 
1867                 internal override int KeyboardDelay { get{ throw new NotImplementedException(); } } 
1868
1869                 internal override  int Caption { get{ throw new NotImplementedException(); } }
1870                 internal override  Size CursorSize { get{ throw new NotImplementedException(); } }
1871                 internal override  bool DragFullWindows { get{ throw new NotImplementedException(); } }
1872                 internal override  Size DragSize { get{ throw new NotImplementedException(); } }
1873                 internal override  Size FrameBorderSize { get{ throw new NotImplementedException(); } }
1874                 internal override  Size IconSize { get{ throw new NotImplementedException(); } }
1875                 internal override  Size MaxWindowTrackSize { get{ throw new NotImplementedException(); } }
1876                 internal override  Size MinimizedWindowSize { get{ throw new NotImplementedException(); } }
1877                 internal override  Size MinimizedWindowSpacingSize { get{ throw new NotImplementedException(); } }
1878                 internal override  Size MinimumWindowSize { get{ throw new NotImplementedException(); } }
1879                 internal override  Size MinWindowTrackSize { get{ throw new NotImplementedException(); } }
1880                 internal override  Size SmallIconSize { get{ throw new NotImplementedException(); } }
1881                 internal override  int MouseButtonCount { get{ throw new NotImplementedException(); } }
1882                 internal override  bool MouseButtonsSwapped { get{ throw new NotImplementedException(); } }
1883                 internal override  bool MouseWheelPresent { get{ throw new NotImplementedException(); } }
1884                 internal override  Rectangle VirtualScreen { get{ throw new NotImplementedException(); } }
1885                 internal override  Rectangle WorkingArea { 
1886                         get { 
1887                                 HIRect bounds = CGDisplayBounds (CGMainDisplayID ());
1888                                 return new Rectangle ((int)bounds.origin.x, (int)bounds.origin.y, (int)bounds.size.width, (int)bounds.size.height);
1889                         }
1890                 }
1891                 internal override bool ThemesEnabled {
1892                         get {
1893                                 return XplatUIOSX.themes_enabled;
1894                         }
1895                 }
1896  
1897
1898                 #endregion
1899                 
1900                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1901                 internal static extern int HIViewSetNeedsDisplayInRegion (IntPtr view, IntPtr rgn, bool needsDisplay);
1902                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1903                 internal static extern int HIViewGetSubviewHit (IntPtr contentView, ref CGPoint point, bool tval, ref IntPtr outPtr);
1904                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1905                 internal static extern int HIViewGetViewForMouseEvent (IntPtr inView, IntPtr inEvent, ref IntPtr outView);
1906                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1907                 internal static extern int HIViewConvertPoint (ref CGPoint point, IntPtr pView, IntPtr cView);
1908                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1909                 internal static extern int HIViewChangeFeatures (IntPtr aView, ulong bitsin, ulong bitsout);
1910                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1911                 internal static extern int HIViewFindByID (IntPtr rootWnd, HIViewID id, ref IntPtr outPtr);
1912                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1913                 internal static extern IntPtr HIViewGetRoot (IntPtr hWnd);
1914                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1915                 internal static extern int HIObjectCreate (IntPtr cfStr, uint what, ref IntPtr hwnd);
1916                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1917                 internal static extern int HIViewSetNeedsDisplay (IntPtr viewHnd, bool update);
1918                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1919                 internal static extern int HIViewGetFrame (IntPtr viewHnd, ref HIRect rect);
1920                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1921                 internal static extern int HIViewSetFrame (IntPtr viewHnd, ref HIRect rect);
1922                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1923                 internal static extern int HIViewPlaceInSuperviewAt (IntPtr view, float x, float y);
1924                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1925                 internal static extern int HIViewAddSubview (IntPtr parentHnd, IntPtr childHnd);
1926                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1927                 internal static extern IntPtr HIViewGetNextView (IntPtr aView);
1928                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1929                 internal static extern IntPtr HIViewGetPreviousView (IntPtr aView);
1930                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1931                 internal static extern IntPtr HIViewGetFirstSubview (IntPtr aView);
1932                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1933                 internal static extern IntPtr HIViewGetSuperview (IntPtr aView);
1934                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1935                 internal static extern int HIViewRemoveFromSuperview (IntPtr aView);
1936                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1937                 internal static extern int HIViewSetVisible (IntPtr vHnd, bool visible);
1938                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1939                 internal static extern bool HIViewIsVisible (IntPtr vHnd);
1940                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1941                 internal static extern int HIViewGetBounds (IntPtr vHnd, ref HIRect r);
1942                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1943                 internal static extern int HIViewScrollRect (IntPtr vHnd, ref HIRect rect, float x, float y);
1944                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1945                 internal static extern int HIViewScrollRect (IntPtr vHnd, IntPtr rect, float x, float y);
1946                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1947                 internal static extern int HIViewSetZOrder (IntPtr hWnd, int cmd, IntPtr oHnd);
1948                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1949                 internal static extern int HIViewSetBoundsOrigin (IntPtr vHnd, float x, float y);
1950                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1951                 internal static extern int HIViewConvertRect (ref HIRect r, IntPtr a, IntPtr b);
1952                 
1953                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1954                 internal static extern void ScrollRect (ref IntPtr r, short dh, short dv, IntPtr rgnHandle);
1955                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1956                 internal static extern void SetRect (ref IntPtr r, short left, short top, short right, short bottom);
1957
1958                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1959                 //static extern int CreateEvent (IntPtr allocator, uint classid, uint kind, double when, uint attributes, ref IntPtr outEvent);
1960                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1961                 static extern int InstallEventHandler (IntPtr window, CarbonEventDelegate handlerProc, uint numtypes, EventTypeSpec [] typeList, IntPtr userData, IntPtr handlerRef);
1962                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1963                 internal static extern IntPtr GetControlOwner (IntPtr aView);
1964                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1965                 static extern int ActivateWindow (IntPtr windowHnd, bool inActivate);
1966                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1967                 static extern bool IsWindowActive (IntPtr windowHnd);
1968                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1969                 static extern int SetKeyboardFocus (IntPtr windowHdn, IntPtr cntrlHnd, short partcode);
1970                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1971                 static extern int GetKeyboardFocus (IntPtr handle, ref IntPtr cntrl);
1972
1973                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1974                 internal static extern IntPtr GetWindowEventTarget (IntPtr window);
1975                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1976                 internal static extern IntPtr GetControlEventTarget (IntPtr aControl);
1977                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1978                 internal static extern IntPtr GetEventDispatcherTarget ();
1979                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1980                 internal static extern int SendEventToEventTarget (IntPtr evt, IntPtr target);
1981                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1982                 internal static extern int ReleaseEvent (IntPtr evt);
1983                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1984                 internal static extern int ReceiveNextEvent (uint evtCount, IntPtr evtTypes, double timeout, bool processEvt, ref IntPtr evt);
1985                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1986                 static extern uint GetEventClass (IntPtr eventRef);
1987                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1988                 static extern uint GetEventKind (IntPtr eventRef);
1989                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1990                 static extern int GetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, IntPtr outActualType, uint bufSize, IntPtr outActualSize, ref byte outData);
1991                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1992                 static extern int GetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, IntPtr outActualType, uint bufSize, IntPtr outActualSize, ref IntPtr outData);
1993                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1994                 static extern int GetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, IntPtr outActualType, uint bufSize, IntPtr outActualSize, ref ushort outData);
1995                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1996                 static extern int GetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, IntPtr outActualType, uint bufSize, IntPtr outActualSize, ref short outData);
1997                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1998                 static extern int GetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, IntPtr outActualType, uint bufSize, IntPtr outActualSize, ref QDPoint outData);
1999                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2000                 static extern int SetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, uint bufSize, ref short outData);
2001                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2002                 //static extern int SetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, uint bufSize, ref IntPtr outData);
2003
2004                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2005                 internal static extern void CGContextFlush (IntPtr cgc);
2006                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2007                 internal static extern int CGContextFillRect (IntPtr cgc, HIRect r);
2008                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2009                 internal static extern CGAffineTransform CGContextGetTextMatrix (IntPtr cgContext);
2010                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2011                 internal static extern int CGContextSetTextMatrix (IntPtr cgContext, CGAffineTransform ctm);
2012                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2013                 internal static extern int CGContextSetRGBFillColor (IntPtr cgContext, float r, float g, float b, float alpha);
2014                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2015                 internal static extern int CGContextSetRGBStrokeColor (IntPtr cgContext, float r, float g, float b, float alpha);
2016                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2017                 internal static extern int CGContextSetTextDrawingMode (IntPtr cgContext, int drawingMode);
2018                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2019                 internal static extern int CGContextSelectFont (IntPtr cgContext, string fontName, float size, int textEncoding);
2020                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2021                 internal static extern int CGContextShowTextAtPoint (IntPtr cgContext, float x, float y, string text, int length);
2022                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2023                 internal static extern int CGContextClipToRect (IntPtr cgContext, HIRect clip);
2024                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2025                 internal static extern void CreateCGContextForPort (IntPtr port, ref IntPtr cgc);
2026                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2027                 internal static extern bool IsWindowCollapsed (IntPtr hWnd);
2028                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2029                 internal static extern bool IsWindowInStandardState (IntPtr hWnd, IntPtr a, IntPtr b);
2030                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2031                 internal static extern void CollapseWindow (IntPtr hWnd, bool collapse);
2032                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2033                 internal static extern void ZoomWindow (IntPtr hWnd, short partCode, bool front);
2034                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2035                 internal static extern int GetWindowAttributes (IntPtr hWnd, ref WindowAttributes outAttributes);
2036                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2037                 internal static extern int ChangeWindowAttributes (IntPtr hWnd, WindowAttributes inAttributes, WindowAttributes outAttributes);
2038                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2039                 internal static extern IntPtr GetWindowPort (IntPtr hWnd);
2040                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2041                 static extern int SetPortWindowPort (IntPtr hWnd);
2042                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2043                 static extern int GetGlobalMouse (ref QDPoint outData);
2044                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2045                 //static extern int GlobalToLocal (ref QDPoint outData);
2046                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2047                 //static extern int LocalToGlobal (ref QDPoint outData);
2048                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2049                 static extern int TrackControl (IntPtr handle, QDPoint point, IntPtr data);
2050                 
2051                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2052                 internal static extern int BeginAppModalStateForWindow (IntPtr window);
2053                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2054                 internal static extern int EndAppModalStateForWindow (IntPtr window);
2055                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2056                 internal static extern int CreateNewWindow (WindowClass klass, WindowAttributes attributes, ref IntPtr r, ref IntPtr window);
2057                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2058                 internal static extern int DisposeWindow (IntPtr wHnd);
2059                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2060                 internal static extern int ShowWindow (IntPtr wHnd);
2061                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2062                 internal static extern int HideWindow (IntPtr wHnd);
2063                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2064                 internal static extern int SetWindowBounds (IntPtr wHnd, uint reg, ref IntPtr rect);
2065                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2066                 internal static extern int GetWindowPortBounds (IntPtr wHnd, ref Rect rect);
2067                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2068                 internal static extern int GetWindowBounds (IntPtr wHnd, uint reg, ref Rect rect);
2069                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2070                 internal static extern int InvertRect (ref Rect r);
2071
2072                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2073                 internal static extern int SetControlTitleWithCFString (IntPtr hWnd, IntPtr titleCFStr);
2074                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2075                 internal static extern int SetWindowTitleWithCFString (IntPtr hWnd, IntPtr titleCFStr);
2076                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2077                 internal static extern IntPtr __CFStringMakeConstantString (string cString);
2078                 
2079                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2080                 internal static extern void CGContextRestoreGState (IntPtr ctx);
2081                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2082                 internal static extern void CGContextSaveGState (IntPtr ctx);
2083                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2084                 internal static extern void CGContextTranslateCTM (IntPtr ctx, double tx, double ty);
2085                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2086                 internal static extern void CGContextScaleCTM (IntPtr ctx, double tx, double ty);
2087
2088                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2089                 //static extern int SetWindowContentColor (IntPtr hWnd, ref RGBColor backColor);
2090                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2091                 static extern int TrackMouseLocationWithOptions (IntPtr port, int options, double eventtimeout, ref QDPoint point, ref IntPtr modifier, ref MouseTrackingResult status);
2092                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2093                 //static extern int CreateMouseTrackingRegion (IntPtr windowref, IntPtr rgn, IntPtr clip, int options, MouseTrackingRegionID rid, IntPtr refcon, IntPtr evttargetref, ref IntPtr mousetrackref);
2094                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2095                 //static extern int ReleaseMouseTrackingRegion (IntPtr region_handle);
2096                 
2097                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2098                 internal static extern int CFRelease (IntPtr wHnd);
2099                 
2100                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2101                 internal extern static IntPtr NewRgn ();
2102                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2103                 internal extern static void CopyRgn (IntPtr srcrgn, IntPtr destrgn);
2104                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2105                 internal extern static void SetRectRgn (IntPtr rgn, short left, short top, short right, short bottom);
2106                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2107                 internal extern static void DisposeRgn (IntPtr rgn);
2108                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2109                 internal extern static void ExitToShell ();
2110                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2111                 internal extern static short GetMBarHeight ();
2112                 
2113                 #region Cursor imports
2114                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2115                 internal extern static HIRect CGDisplayBounds (IntPtr displayID);
2116                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2117                 internal extern static IntPtr CGMainDisplayID ();
2118                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2119                 internal extern static void CGDisplayShowCursor (IntPtr display);
2120                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2121                 internal extern static void CGDisplayHideCursor (IntPtr display);
2122                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2123                 internal extern static void CGDisplayMoveCursorToPoint (IntPtr display, CGPoint point);
2124                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2125                 internal extern static void SetThemeCursor (uint inCursor);
2126                 #endregion
2127
2128                 [DllImport ("gdiplus", EntryPoint="GetFontMetrics")]
2129                 internal extern static bool GetFontMetrics(IntPtr graphicsObject, IntPtr nativeObject, out int ascent, out int descent);
2130         }
2131
2132 }