2007-08-28 Jonathan Pobst <monkey@jpobst.com>
[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                 internal override void RaiseIdle (EventArgs e)
754                 {
755                         if (Idle != null)
756                                 Idle (this, e);
757                 }
758
759                 internal override IntPtr InitializeDriver() {
760                         return IntPtr.Zero;
761                 }
762
763                 internal override void ShutdownDriver(IntPtr token) {
764                 }
765
766                 internal override void EnableThemes() {
767                         themes_enabled = true;
768                 }
769
770                 internal override void Activate(IntPtr handle) {
771                         ActivateWindow (GetControlOwner (handle), true);
772                 }
773
774                 internal override void AudibleAlert() {
775                         throw new NotImplementedException();
776                 }
777
778                 internal override void CaretVisible (IntPtr hwnd, bool visible) {
779                         if (Caret.Hwnd == hwnd) {
780                                 if (visible) {
781                                         if (Caret.Visible < 1) {
782                                                 Caret.Visible++;
783                                                 Caret.On = false;
784                                                 if (Caret.Visible == 1) {
785                                                         ShowCaret ();
786                                                         Caret.Timer.Start ();
787                                                 }
788                                         }
789                                 } else {
790                                         Caret.Visible--;
791                                         if (Caret.Visible == 0) {
792                                                 Caret.Timer.Stop ();
793                                                 HideCaret ();
794                                         }
795                                 }
796                         }
797                 }
798                 
799                 internal override bool CalculateWindowRect(ref Rectangle ClientRect, CreateParams cp, Menu menu, out Rectangle WindowRect) {
800                         WindowRect = Hwnd.GetWindowRectangle (cp, menu, ClientRect);
801                         return true;
802                 }
803                 
804                 internal override void ClientToScreen(IntPtr handle, ref int x, ref int y) {
805                         CGPoint pt = new CGPoint ();
806                         Rect wBounds = new Rect ();
807                         Hwnd    hwnd;
808
809                         hwnd = Hwnd.ObjectFromHandle(handle);
810
811                         pt.x = x;
812                         pt.y = y;
813
814                         GetWindowBounds (GetControlOwner (hwnd.client_window), 32, ref wBounds);
815                         HIViewConvertPoint (ref pt, handle, IntPtr.Zero);
816
817                         x = (int)(pt.x+wBounds.left);
818                         y = (int)(pt.y+wBounds.top);
819                 }
820
821                 internal override int[] ClipboardAvailableFormats(IntPtr handle) {
822                         return null;
823                 }
824
825                 internal override void ClipboardClose(IntPtr handle) {
826                         throw new NotImplementedException();
827                 }
828
829                 internal override int ClipboardGetID(IntPtr handle, string format) {
830                         return 0;
831                 }
832
833                 internal override IntPtr ClipboardOpen(bool primary_selection) {
834                         throw new NotImplementedException();
835                 }
836
837                 internal override object ClipboardRetrieve(IntPtr handle, int id, XplatUI.ClipboardToObject converter) {
838                         throw new NotImplementedException();
839                 }
840
841                 internal override void ClipboardStore(IntPtr handle, object obj, int type, XplatUI.ObjectToClipboard converter) {
842                         throw new NotImplementedException();
843                 }
844                 
845                 internal override void CreateCaret (IntPtr hwnd, int width, int height) {
846                         if (Caret.Hwnd != IntPtr.Zero)
847                                 DestroyCaret (Caret.Hwnd);
848
849                         Caret.Hwnd = hwnd;
850                         Caret.Width = width;
851                         Caret.Height = height;
852                         Caret.Visible = 0;
853                         Caret.On = false;
854                 }
855                 
856                 internal override IntPtr CreateWindow(CreateParams cp) {
857                         IntPtr windowHnd = IntPtr.Zero;
858                         IntPtr parentHnd = cp.Parent;
859                         bool realWindow = false;
860                         Rectangle clientRect;
861                         Hwnd hwnd = new Hwnd ();
862                         
863                         SetHwndStyles (hwnd, cp);
864                         
865                         if (parentHnd == IntPtr.Zero) {
866                                 if ((cp.Style & (int)(WindowStyles.WS_CHILD))!=0) {
867                                         // This is a child view that is going to be parentless;
868                                         realWindow = false;
869                                         CheckError (HIViewFindByID (HIViewGetRoot (FosterParent), new HIViewID (OSXConstants.kEventClassWindow, 1), ref parentHnd), "HIViewFindByID ()");
870                                 } else if ((cp.Style & (int)(WindowStyles.WS_POPUP))!=0) {
871                                         // This is a popup window that will be real.
872                                         if (cp.X < 1) cp.X = 0;
873                                         if (cp.Y < 1) cp.Y = 0;
874                                         realWindow = true;
875                                 } else {
876                                         // This is a real root window too
877                                         if (cp.X < 1) cp.X = 0;
878                                         if (cp.Y < 1) cp.Y = 0;
879                                         realWindow = true;
880                                 }
881                         } else {
882                                 realWindow = false;
883                         }
884
885                         if (realWindow) {
886                                 WindowClass windowklass = WindowClass.kOverlayWindowClass;
887                                 WindowAttributes attributes = WindowAttributes.kWindowCompositingAttribute | WindowAttributes.kWindowStandardHandlerAttribute;
888                                 if ((cp.Style & ((int)WindowStyles.WS_MINIMIZEBOX)) != 0) { 
889                                         attributes |= WindowAttributes.kWindowCollapseBoxAttribute;
890                                 }
891                                 if ((cp.Style & ((int)WindowStyles.WS_MAXIMIZEBOX)) != 0) {
892                                         attributes |= WindowAttributes.kWindowResizableAttribute | WindowAttributes.kWindowHorizontalZoomAttribute | WindowAttributes.kWindowVerticalZoomAttribute;
893                                 }
894                                 if ((cp.Style & ((int)WindowStyles.WS_SYSMENU)) != 0) {
895                                         attributes |= WindowAttributes.kWindowCloseBoxAttribute;
896                                 }
897                                 if ((cp.ExStyle & ((int)WindowExStyles.WS_EX_TOOLWINDOW)) != 0) {
898                                         attributes = WindowAttributes.kWindowStandardHandlerAttribute | WindowAttributes.kWindowCompositingAttribute;
899                                 }
900                                 if ((cp.Style & ((int)WindowStyles.WS_CAPTION)) != 0) {
901                                         windowklass = WindowClass.kDocumentWindowClass;
902                                 }
903                                         
904                                 IntPtr rect = IntPtr.Zero;
905                                 IntPtr viewHnd = IntPtr.Zero;
906                                 SetRect (ref rect, (short)cp.X, (short)(cp.Y + MenuBarHeight + TitleBarHeight), (short)(cp.Width+cp.X), (short)(cp.Height+cp.Y+MenuBarHeight+TitleBarHeight));
907                                 CheckError (CreateNewWindow (windowklass, attributes, ref rect, ref windowHnd), "CreateNewWindow ()");
908
909                                 CheckError (InstallEventHandler (GetWindowEventTarget (windowHnd), CarbonEventHandler, (uint)windowEvents.Length, windowEvents, windowHnd, IntPtr.Zero), "InstallEventHandler ()");
910                                 CheckError (HIViewFindByID (HIViewGetRoot (windowHnd), new HIViewID (OSXConstants.kEventClassWindow, 1), ref viewHnd), "HIViewFindByID ()");
911                                 parentHnd = viewHnd;
912                         }
913                         hwnd.X = cp.X;
914                         hwnd.Y = cp.Y;
915                         hwnd.Width = cp.Width;
916                         hwnd.Height = cp.Height;
917                         hwnd.Parent = Hwnd.ObjectFromHandle (cp.Parent);
918                         hwnd.visible = false;
919                         clientRect = hwnd.ClientRect;
920                         
921                         HIRect r = new HIRect (0, 0, cp.Width, cp.Height);
922                         CheckError (HIObjectCreate (__CFStringMakeConstantString ("com.apple.hiview"), 0, ref hwnd.whole_window), "HIObjectCreate ()");
923                         CheckError (InstallEventHandler (GetControlEventTarget (hwnd.whole_window), CarbonEventHandler, (uint)viewEvents.Length, viewEvents, hwnd.whole_window, IntPtr.Zero), "InstallEventHandler ()");
924                         CheckError (HIViewChangeFeatures (hwnd.whole_window, 1 << 1, 0), "HIViewChangeFeatures ()");
925                         CheckError (HIViewSetFrame (hwnd.whole_window, ref r), "HIViewSetFrame ()");
926                         hwnd.WholeWindow = hwnd.whole_window;
927                         
928                         r = new HIRect (0, 0, clientRect.Width, clientRect.Height);
929                         CheckError (HIObjectCreate (__CFStringMakeConstantString ("com.apple.hiview"), 0, ref hwnd.client_window), "HIObjectCreate ()");
930                         CheckError (InstallEventHandler (GetControlEventTarget (hwnd.client_window), CarbonEventHandler, (uint)viewEvents.Length, viewEvents, hwnd.client_window, IntPtr.Zero), "InstallEventHandler ()");
931                         CheckError (HIViewChangeFeatures (hwnd.client_window, 1 << 1, 0), "HIViewChangeFeatures ()");
932                         CheckError (HIViewSetFrame (hwnd.client_window, ref r), "HIViewSetFrame ()");
933                         hwnd.ClientWindow = hwnd.client_window;
934                         
935                         CheckError (HIViewAddSubview (hwnd.whole_window, hwnd.client_window));
936                         CheckError (HIViewPlaceInSuperviewAt (hwnd.client_window, clientRect.X, clientRect.Y));
937                         
938                         if (parentHnd != IntPtr.Zero && parentHnd != hwnd.WholeWindow) {
939                                 CheckError (HIViewAddSubview (parentHnd, hwnd.whole_window), "HIViewAddSubview ()");
940                                 CheckError (HIViewPlaceInSuperviewAt (hwnd.whole_window, cp.X, cp.Y), "HIPlaceInSuperviewAt ()");
941                                 if ((cp.Style & (int)(WindowStyles.WS_VISIBLE))!=0) {
942                                         CheckError (HIViewSetVisible (hwnd.whole_window, true), "HIViewSetVisible ()");
943                                         CheckError (HIViewSetVisible (hwnd.client_window, true), "HIViewSetVisible ()");
944                                         hwnd.visible = true;
945                                 } else {
946                                         CheckError (HIViewSetVisible (hwnd.whole_window, false), "HIViewSetVisible ()");
947                                         CheckError (HIViewSetVisible (hwnd.client_window, false), "HIViewSetVisible ()");
948                                         hwnd.visible = false;
949                                 }
950                         }
951                         if (realWindow) {
952                                 WindowMapping [hwnd.Handle] = windowHnd;
953                                 if ((cp.Style & (int)(WindowStyles.WS_VISIBLE))!=0) {
954                                         CheckError (ShowWindow (windowHnd));
955                                         CheckError (HIViewSetVisible (hwnd.whole_window, true), "HIViewSetVisible ()");
956                                         CheckError (HIViewSetVisible (hwnd.client_window, true), "HIViewSetVisible ()");
957                                         hwnd.visible = true;
958                                 }
959                                 if ((cp.Style & (int)(WindowStyles.WS_POPUP))!=0) {
960                                         CheckError (HIViewSetVisible (hwnd.whole_window, true), "HIViewSetVisible ()");
961                                         CheckError (HIViewSetVisible (hwnd.client_window, true), "HIViewSetVisible ()");
962                                         hwnd.visible = true;
963                                 }
964                         }       
965                         
966                         return hwnd.Handle;
967                 }
968
969                 internal override IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height) {
970                         CreateParams create_params = new CreateParams();
971
972                         create_params.Caption = "";
973                         create_params.X = X;
974                         create_params.Y = Y;
975                         create_params.Width = Width;
976                         create_params.Height = Height;
977
978                         create_params.ClassName=XplatUI.DefaultClassName;
979                         create_params.ClassStyle = 0;
980                         create_params.ExStyle=0;
981                         create_params.Parent=IntPtr.Zero;
982                         create_params.Param=0;
983
984                         return CreateWindow(create_params);
985                 }
986
987                 [MonoTODO]
988                 internal override Bitmap DefineStdCursorBitmap (StdCursor id)
989                 {
990                         throw new NotImplementedException ();
991                 }
992                 [MonoTODO]
993                 internal override IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot) {
994                         throw new NotImplementedException ();
995                 }
996                 
997                 [MonoTODO]
998                 internal override IntPtr DefineStdCursor(StdCursor id) {
999                         switch (id) {
1000                                 case StdCursor.AppStarting:
1001                                         return (IntPtr)ThemeCursor.kThemeSpinningCursor;
1002                                 case StdCursor.Arrow:
1003                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1004                                 case StdCursor.Cross:
1005                                         return (IntPtr)ThemeCursor.kThemeCrossCursor;
1006                                 case StdCursor.Default:
1007                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1008                                 case StdCursor.Hand:
1009                                         return (IntPtr)ThemeCursor.kThemeOpenHandCursor;
1010                                 case StdCursor.Help:
1011                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1012                                 case StdCursor.HSplit:
1013                                         return (IntPtr)ThemeCursor.kThemeResizeLeftRightCursor;
1014                                 case StdCursor.IBeam:
1015                                         return (IntPtr)ThemeCursor.kThemeIBeamCursor;
1016                                 case StdCursor.No:
1017                                         return (IntPtr)ThemeCursor.kThemeNotAllowedCursor;
1018                                 case StdCursor.NoMove2D:
1019                                         return (IntPtr)ThemeCursor.kThemeNotAllowedCursor;
1020                                 case StdCursor.NoMoveHoriz:
1021                                         return (IntPtr)ThemeCursor.kThemeNotAllowedCursor;
1022                                 case StdCursor.NoMoveVert:
1023                                         return (IntPtr)ThemeCursor.kThemeNotAllowedCursor;
1024                                 case StdCursor.PanEast:
1025                                         return (IntPtr)ThemeCursor.kThemeResizeRightCursor;
1026                                 case StdCursor.PanNE:
1027                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1028                                 case StdCursor.PanNorth:
1029                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1030                                 case StdCursor.PanNW:
1031                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1032                                 case StdCursor.PanSE:
1033                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1034                                 case StdCursor.PanSouth:
1035                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1036                                 case StdCursor.PanSW:
1037                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1038                                 case StdCursor.PanWest:
1039                                         return (IntPtr)ThemeCursor.kThemeResizeLeftCursor;
1040                                 case StdCursor.SizeAll:
1041                                         return (IntPtr)ThemeCursor.kThemeResizeLeftRightCursor;
1042                                 case StdCursor.SizeNESW:
1043                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1044                                 case StdCursor.SizeNS:
1045                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1046                                 case StdCursor.SizeNWSE:
1047                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1048                                 case StdCursor.SizeWE:
1049                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1050                                 case StdCursor.UpArrow:
1051                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1052                                 case StdCursor.VSplit:
1053                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1054                                 case StdCursor.WaitCursor:
1055                                         return (IntPtr)ThemeCursor.kThemeSpinningCursor;
1056                                 default:
1057                                         return (IntPtr)ThemeCursor.kThemeArrowCursor;
1058                         }
1059                 }
1060                 
1061                 internal override IntPtr DefWndProc(ref Message msg) {
1062                         Hwnd hwnd = Hwnd.ObjectFromHandle (msg.HWnd);
1063                         switch ((Msg)msg.Msg) {
1064                                 case Msg.WM_DESTROY: {
1065                                         if (WindowMapping [hwnd.Handle] != null)
1066
1067                                                 Exit ();
1068                                         break;
1069                                 }
1070                         }
1071                         return IntPtr.Zero;
1072                 }
1073
1074                 internal override void DestroyCaret (IntPtr hwnd) {
1075                         if (Caret.Hwnd == hwnd) {
1076                                 if (Caret.Visible == 1) {
1077                                         Caret.Timer.Stop ();
1078                                         HideCaret ();
1079                                 }
1080                                 Caret.Hwnd = IntPtr.Zero;
1081                                 Caret.Visible = 0;
1082                                 Caret.On = false;
1083                         }
1084                 }
1085                 
1086                 [MonoTODO]
1087                 internal override void DestroyCursor(IntPtr cursor) {
1088                         throw new NotImplementedException ();
1089                 }
1090         
1091                 internal override void DestroyWindow(IntPtr handle) {
1092                         Hwnd    hwnd;
1093
1094                         hwnd = Hwnd.ObjectFromHandle(handle);
1095                         
1096                         if ((hwnd.whole_window != IntPtr.Zero) && HIViewGetSuperview (hwnd.whole_window) != IntPtr.Zero)
1097                                 CheckError (HIViewRemoveFromSuperview (handle), "HIViewRemoveFromSuperview ()");
1098
1099                         if (WindowMapping [hwnd.Handle] != null) {
1100                                 DisposeWindow ((IntPtr)(WindowMapping [hwnd.Handle]));
1101                         }
1102                         CFRelease (hwnd.ClientWindow);
1103                         CFRelease (hwnd.WholeWindow);
1104                 }
1105
1106                 internal override IntPtr DispatchMessage(ref MSG msg) {
1107                         return NativeWindow.WndProc(msg.hwnd, msg.message, msg.wParam, msg.lParam);
1108                 }
1109                 
1110                 internal override void DoEvents() {
1111                 }
1112
1113                 internal override void EnableWindow(IntPtr handle, bool Enable) {
1114                         //Like X11 we need not do anything here
1115                 }
1116
1117                 internal override void EndLoop(Thread thread) {
1118                         throw new NotImplementedException();
1119                 }
1120
1121                 internal void Exit() {
1122                         GetMessageResult = false;
1123                         ExitToShell ();
1124                 }
1125                 
1126                 internal override IntPtr GetActive() {
1127                         foreach (DictionaryEntry entry in WindowMapping)
1128                                 if (IsWindowActive ((IntPtr)(entry.Value)))
1129                                         return (IntPtr)(entry.Key);
1130
1131                         return IntPtr.Zero;
1132                 }
1133
1134                 internal override Region GetClipRegion(IntPtr hwnd) {
1135                         return null;
1136                 }
1137
1138                 [MonoTODO]
1139                 internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) {
1140                         throw new NotImplementedException ();
1141                 }
1142                 
1143                 internal override void GetDisplaySize(out Size size) {
1144                         HIRect bounds = CGDisplayBounds (CGMainDisplayID ());
1145                         size = new Size ((int)bounds.size.width, (int)bounds.size.height);
1146                 }
1147
1148                 internal override IntPtr GetParent(IntPtr handle) {
1149                         Hwnd    hwnd;
1150
1151                         hwnd = Hwnd.ObjectFromHandle(handle);
1152                         if (hwnd != null && hwnd.parent != null) {
1153                                 return hwnd.parent.Handle;
1154                         }
1155                         return IntPtr.Zero;
1156                 }
1157                 
1158                 internal override void GetCursorPos(IntPtr handle, out int x, out int y) {
1159                         QDPoint pt = new QDPoint ();
1160                         GetGlobalMouse (ref pt);
1161                         x = pt.x;
1162                         y = pt.y;
1163                 }
1164
1165                 internal override IntPtr GetFocus() {
1166                         return FocusWindow;
1167                 }
1168
1169                 
1170                 internal override bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent) {
1171                         FontFamily ff = font.FontFamily;
1172                         ascent = ff.GetCellAscent (font.Style);
1173                         descent = ff.GetCellDescent (font.Style);
1174                         return true;
1175                 }
1176                 
1177                 [MonoTODO]
1178                 internal override Point GetMenuOrigin(IntPtr hwnd) {
1179                         throw new NotImplementedException();
1180                 }
1181
1182                 internal override bool GetMessage(object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax) {
1183                         IntPtr evtRef = IntPtr.Zero;
1184                         IntPtr target = GetEventDispatcherTarget();
1185                         CheckTimers (DateTime.Now);
1186                         ReceiveNextEvent (0, IntPtr.Zero, 0, true, ref evtRef);
1187                         if (evtRef != IntPtr.Zero && target != IntPtr.Zero) {
1188                                 SendEventToEventTarget (evtRef, target);
1189                                 ReleaseEvent (evtRef);
1190                         }
1191                         
1192                         lock (MessageQueue) {
1193                                 if (MessageQueue.Count <= 0) {
1194                                         if (Idle != null) 
1195                                                 Idle (this, EventArgs.Empty);
1196                                         else if (TimerList.Count == 0) {
1197                                                 ReceiveNextEvent (0, IntPtr.Zero, Convert.ToDouble ("0." + Timer.Minimum), true, ref evtRef);
1198                                                 if (evtRef != IntPtr.Zero && target != IntPtr.Zero) {
1199                                                         SendEventToEventTarget (evtRef, target);
1200                                                         ReleaseEvent (evtRef);
1201                                                 }
1202                                         } else {
1203                                                 ReceiveNextEvent (0, IntPtr.Zero, NextTimeout (), true, ref evtRef);
1204                                                 if (evtRef != IntPtr.Zero && target != IntPtr.Zero) {
1205                                                         SendEventToEventTarget (evtRef, target);
1206                                                         ReleaseEvent (evtRef);
1207                                                 }
1208                                         }
1209                                         msg.hwnd = IntPtr.Zero;
1210                                         msg.message = Msg.WM_ENTERIDLE;
1211                                         return GetMessageResult;
1212                                 }
1213                                 msg = (MSG) MessageQueue.Dequeue ();
1214                         }
1215                         return GetMessageResult;
1216                 }
1217                 
1218                 [MonoTODO]
1219                 internal override bool GetText(IntPtr handle, out string text) {
1220                         throw new NotImplementedException ();
1221                 }
1222                 
1223                 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) {
1224                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1225                         Rectangle rect = hwnd.ClientRect;
1226                         
1227                         x = hwnd.x;
1228                         y = hwnd.y;
1229                         width = hwnd.width;
1230                         height = hwnd.height;
1231
1232                         client_width = rect.Width;
1233                         client_height = rect.Height;
1234                 }
1235                 
1236                 internal override FormWindowState GetWindowState(IntPtr hwnd) {
1237                         IntPtr window = GetControlOwner (hwnd);
1238
1239                         if (IsWindowCollapsed (window))
1240                                 return FormWindowState.Minimized;
1241                         if (IsWindowInStandardState (window, IntPtr.Zero, IntPtr.Zero))
1242                                 return FormWindowState.Maximized;
1243
1244                         return FormWindowState.Normal;
1245                 }
1246                 
1247                 internal override void GrabInfo(out IntPtr handle, out bool GrabConfined, out Rectangle GrabArea) {
1248                         handle = Grab.Hwnd;
1249                         GrabConfined = Grab.Confined;
1250                         GrabArea = Grab.Area;
1251                 }
1252                 
1253                 internal override void GrabWindow(IntPtr handle, IntPtr confine_to_handle) {
1254                         GrabWindowHwnd = Hwnd.ObjectFromHandle (handle);
1255                 }
1256                 
1257                 internal override void UngrabWindow(IntPtr hwnd) {
1258                         GrabWindowHwnd = null;
1259                         Grab.Hwnd = IntPtr.Zero;
1260                         Grab.Confined = false;
1261                 }
1262                 
1263                 internal override void HandleException(Exception e) {
1264                         StackTrace st = new StackTrace(e);
1265                         Console.WriteLine("Exception '{0}'", e.Message+st.ToString());
1266                         Console.WriteLine("{0}{1}", e.Message, st.ToString());
1267                 }
1268                 
1269                 internal override void Invalidate (IntPtr handle, Rectangle rc, bool clear) {
1270                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1271                         
1272                         if (hwnd.visible && HIViewIsVisible (handle)) {
1273                                 MSG msg = new MSG ();
1274                                 msg.hwnd = hwnd.Handle;
1275                                 msg.wParam = IntPtr.Zero;
1276                                 msg.lParam = IntPtr.Zero;
1277                                 msg.message = Msg.WM_PAINT;
1278                                 MessageQueue.Enqueue (msg);
1279                                 // This is currently causing some graphics corruption
1280                                 //hwnd.AddInvalidArea (rc.X, rc.Y, rc.Width, rc.Height);
1281                                 hwnd.AddInvalidArea (0, 0, hwnd.ClientRect.Width, hwnd.ClientRect.Height);
1282                                 hwnd.expose_pending = true;
1283                         }
1284                 }
1285
1286                 internal override void InvalidateNC (IntPtr handle)
1287                 {
1288                         // XXX FIXME
1289                 }
1290                 
1291                 internal override bool IsEnabled(IntPtr handle) {
1292                         return Hwnd.ObjectFromHandle(handle).Enabled;
1293                 }
1294                 
1295                 internal override bool IsVisible(IntPtr handle) {
1296                         return Hwnd.ObjectFromHandle(handle).visible;
1297                 }
1298                 
1299                 internal override void KillTimer(Timer timer) {
1300                         lock (TimerList) {
1301                                 TimerList.Remove(timer);
1302                         }
1303                 }
1304                 
1305                 internal override void MenuToScreen(IntPtr handle, ref int x, ref int y) {
1306                         CGPoint pt = new CGPoint ();
1307                         Rect wBounds = new Rect ();
1308                         Hwnd    hwnd;
1309
1310                         hwnd = Hwnd.ObjectFromHandle(handle);
1311
1312                         pt.x = x;
1313                         pt.y = y;
1314
1315                         GetWindowBounds (GetControlOwner (hwnd.whole_window), 32, ref wBounds);
1316                         HIViewConvertPoint (ref pt, handle, IntPtr.Zero);
1317
1318                         x = (int)(pt.x+wBounds.left);
1319                         y = (int)(pt.y+wBounds.top);
1320                 }
1321
1322                 [MonoTODO]
1323                 internal override void OverrideCursor(IntPtr cursor) {
1324                         throw new NotImplementedException ();
1325                 }
1326
1327                 internal override PaintEventArgs PaintEventStart(ref Message msg, IntPtr handle, bool client) {
1328                         PaintEventArgs  paint_event;
1329                         Hwnd            hwnd;
1330                         Hwnd            paint_hwnd; 
1331                         
1332                         hwnd = Hwnd.ObjectFromHandle(msg.HWnd);
1333                         if (msg.HWnd == handle) {
1334                                 paint_hwnd = hwnd;
1335                         } else {
1336                                 paint_hwnd = Hwnd.ObjectFromHandle (handle);
1337                         }
1338                         
1339                         if (Caret.Visible == 1) {
1340                                 Caret.Paused = true;
1341                                 HideCaret();
1342                         }
1343
1344                         Graphics dc = Graphics.FromHwnd (paint_hwnd.client_window);
1345                         paint_event = new PaintEventArgs(dc, hwnd.Invalid);
1346
1347                         hwnd.expose_pending = false;
1348                         hwnd.ClearInvalidArea();
1349
1350                         hwnd.drawing_stack.Push (dc);
1351
1352                         return paint_event;
1353                 }
1354                 
1355                 internal override void PaintEventEnd(ref Message msg, IntPtr handle, bool client) {
1356                         Hwnd    hwnd;
1357
1358                         hwnd = Hwnd.ObjectFromHandle(handle);
1359
1360                         Graphics dc = (Graphics)hwnd.drawing_stack.Pop();
1361                         dc.Flush ();
1362                         dc.Dispose ();
1363                         
1364                         if (Caret.Visible == 1) {
1365                                 ShowCaret();
1366                                 Caret.Paused = false;
1367                         }
1368                 }
1369                 
1370                 internal override bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags) {
1371                         Console.WriteLine("XplatUIOSX.PeekMessage");
1372                         return true;
1373                 }
1374
1375                 internal override bool PostMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
1376                         MSG msg = new MSG();
1377                         msg.hwnd = hwnd;
1378                         msg.message = message;
1379                         msg.wParam = wParam;
1380                         msg.lParam = lParam;
1381                         MessageQueue.Enqueue (msg);
1382                         return true;
1383                 }
1384
1385                 [MonoTODO]
1386                 internal override void PostQuitMessage(int exitCode) {
1387                         throw new NotImplementedException();
1388                 }
1389
1390                 [MonoTODO]
1391                 internal override void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave) {
1392                         throw new NotImplementedException();
1393                 }
1394
1395                 [MonoTODO]              
1396                 internal override void RequestNCRecalc(IntPtr handle) {
1397                         throw new NotImplementedException();
1398                 }
1399
1400                 [MonoTODO]              
1401                 internal override void ResetMouseHover(IntPtr handle) {
1402                         throw new NotImplementedException();
1403                 }
1404
1405                 internal override void ScreenToClient(IntPtr handle, ref int x, ref int y) {
1406                         CGPoint pt = new CGPoint ();
1407                         Rect wBounds = new Rect ();
1408
1409                         GetWindowBounds (GetControlOwner (handle), 32, ref wBounds);
1410                         pt.x = (x-wBounds.left);
1411                         pt.y = (y-wBounds.top);
1412                         HIViewConvertPoint (ref pt, IntPtr.Zero, handle);
1413
1414                         x = (int)pt.x;
1415                         y = (int)pt.y;
1416                 }
1417
1418                 [MonoTODO]
1419                 internal override void ScreenToMenu(IntPtr handle, ref int x, ref int y) {
1420                         CGPoint pt = new CGPoint ();
1421                         Rect wBounds = new Rect ();
1422
1423                         GetWindowBounds (GetControlOwner (handle), 32, ref wBounds);
1424                         pt.x = (x-wBounds.left);
1425                         pt.y = (y-wBounds.top);
1426                         HIViewConvertPoint (ref pt, IntPtr.Zero, handle);
1427
1428                         x = (int)pt.x;
1429                         y = (int)pt.y;
1430                 }
1431
1432                 internal override void ScrollWindow(IntPtr handle, Rectangle area, int XAmount, int YAmount, bool clear) {
1433                         //IntPtr rect = IntPtr.Zero;
1434                         //HIRect vBounds = new HIRect ();
1435                    
1436             Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
1437           
1438                         /*
1439                         if (hwnd.invalid != Rectangle.Empty) {
1440                                 // BIG FAT WARNING. This only works with how we use this function right now
1441                                 // where we basically still scroll the whole window, but work around areas
1442                                 // that are covered by our children
1443
1444                                 hwnd.invalid.X += XAmount;
1445                                 hwnd.invalid.Y += YAmount;
1446
1447                                 if (hwnd.invalid.X < 0) {
1448                                         hwnd.invalid.Width += hwnd.invalid.X;
1449                                         hwnd.invalid.X =0;
1450                                 }
1451
1452                                 if (hwnd.invalid.Y < 0) {
1453                                         hwnd.invalid.Height += hwnd.invalid.Y;
1454                                         hwnd.invalid.Y =0;
1455                                 }
1456                         }*/
1457                         
1458                         HIRect scrollrect = new HIRect ();
1459                         scrollrect.origin.x = area.X;
1460                         scrollrect.origin.y = area.Y;
1461                         scrollrect.size.width = area.Width;
1462                         scrollrect.size.height = area.Height;
1463                         HIViewScrollRect (hwnd.Handle, ref scrollrect, (float)XAmount, (float)-YAmount);
1464                         /*
1465             HIViewGetBounds (hwnd.client_window, ref vBounds);
1466                         HIViewConvertRect (ref vBounds, hwnd.client_window, IntPtr.Zero);
1467                         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));
1468                         ScrollRect (ref rect, (short)XAmount, (short)-YAmount, IntPtr.Zero);
1469                         */
1470                         // Generate an expose for the area exposed by the horizontal scroll
1471                         /*
1472                         if (XAmount > 0) {
1473                                 hwnd.AddInvalidArea (area.X, area.Y, XAmount, area.Height);
1474                         } else if (XAmount < 0) {
1475                                 hwnd.AddInvalidArea (XAmount + area.X + area.Width, area.Y, -XAmount, area.Height);
1476                         }
1477
1478                         // Generate an expose for the area exposed by the vertical scroll
1479                         if (YAmount > 0) {
1480                                 hwnd.AddInvalidArea (area.X, area.Y, area.Width, YAmount);
1481                         } else if (YAmount < 0) {
1482                                 hwnd.AddInvalidArea (area.X, YAmount + area.Y + area.Height, area.Width, -YAmount);
1483                         }
1484                         
1485                         UpdateWindow (handle);
1486                         */
1487                 }
1488                 
1489                 
1490                 internal override void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool clear) {
1491                         throw new NotImplementedException("");
1492                 }
1493                 
1494                 [MonoTODO]
1495                 internal override void SendAsyncMethod (AsyncMethodData method) {
1496                         throw new NotImplementedException ();
1497                 }
1498
1499                 [MonoTODO]
1500                 internal override IntPtr SendMessage (IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam) {
1501                         throw new NotImplementedException ();
1502                 }
1503                 
1504                 internal override int SendInput(IntPtr hwnd, Queue keys) {
1505                         return 0;
1506                 }
1507
1508
1509                 internal override void SetCaretPos (IntPtr hwnd, int x, int y) {
1510                         if (Caret.Hwnd == hwnd) {
1511                                 CGPoint cpt = new CGPoint ();
1512                                 cpt.x = x;
1513                                 cpt.y = y;
1514                                 HIViewConvertPoint (ref cpt, hwnd, IntPtr.Zero);
1515                                 Caret.Timer.Stop ();
1516                                 HideCaret ();
1517                                 Caret.X = (int)cpt.x;
1518                                 Caret.Y = (int)cpt.y-23;
1519                                 if (Caret.Visible == 1) {
1520                                         ShowCaret ();
1521                                         Caret.Timer.Start ();
1522                                 }
1523                         }
1524                 }
1525
1526                 internal override void SetClipRegion(IntPtr hwnd, Region region) {
1527                         throw new NotImplementedException();
1528                 }
1529                 
1530                 internal override void SetCursor(IntPtr window, IntPtr cursor) {
1531                         SetThemeCursor ((uint) cursor);
1532                 }
1533                 
1534                 internal override void SetCursorPos(IntPtr handle, int x, int y) {
1535                         CGDisplayMoveCursorToPoint (CGMainDisplayID (), new CGPoint (x, y));
1536                 }
1537                 
1538                 internal override void SetFocus(IntPtr handle) {
1539                         if (FocusWindow != IntPtr.Zero) {
1540                                 PostMessage(FocusWindow, Msg.WM_KILLFOCUS, handle, IntPtr.Zero);
1541                         }
1542                         PostMessage(handle, Msg.WM_SETFOCUS, FocusWindow, IntPtr.Zero);
1543                         FocusWindow = handle;
1544                 }
1545
1546                 [MonoTODO]
1547                 internal override void SetIcon(IntPtr handle, Icon icon) {
1548                         throw new NotImplementedException();
1549                 }
1550
1551                 
1552                 internal override void SetModal(IntPtr handle, bool Modal) {
1553                         IntPtr hWnd = GetControlOwner (Hwnd.ObjectFromHandle (handle).WholeWindow);
1554                         if (Modal)
1555                                 BeginAppModalStateForWindow (hWnd);
1556                         else
1557                                 EndAppModalStateForWindow (hWnd);
1558                         return;
1559                 }
1560
1561                 internal override IntPtr SetParent(IntPtr handle, IntPtr parent) {
1562                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1563                         
1564                         hwnd.parent = Hwnd.ObjectFromHandle (parent);
1565                         if (HIViewGetSuperview (hwnd.whole_window) != IntPtr.Zero) {
1566                                 CheckError (HIViewRemoveFromSuperview (hwnd.whole_window), "HIViewRemoveFromSuperview ()");
1567                         }
1568                         CheckError (HIViewAddSubview (hwnd.parent.client_window, hwnd.whole_window));
1569                         CheckError (HIViewAddSubview (hwnd.whole_window, hwnd.client_window));
1570                         HIViewPlaceInSuperviewAt (hwnd.client_window, hwnd.ClientRect.X, hwnd.ClientRect.Y);
1571                         
1572                         return IntPtr.Zero;
1573                 }
1574                 
1575                 internal override void SetTimer (Timer timer) {
1576                         lock (TimerList) {
1577                                 TimerList.Add (timer);
1578                         }
1579                 }
1580                 
1581                 internal override bool SetTopmost(IntPtr hWnd, bool Enabled) {
1582                         HIViewSetZOrder (hWnd, 1, IntPtr.Zero);
1583                         return true;
1584                 }
1585                 
1586                 internal override bool SetOwner(IntPtr hWnd, IntPtr hWndOwner) {
1587                         // TODO: Set window owner. 
1588                         return true;
1589                 }
1590                 
1591                 internal override bool SetVisible(IntPtr handle, bool visible, bool activate) {
1592                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1593                         object window = WindowMapping [hwnd.Handle];
1594                         if (window != null)
1595                                 if (visible)
1596                                         ShowWindow ((IntPtr)window);
1597                                 else
1598                                         HideWindow ((IntPtr)window);
1599                                         
1600                         HIViewSetVisible (hwnd.whole_window, visible);
1601                         HIViewSetVisible (hwnd.client_window, visible);
1602                         hwnd.visible = visible;
1603                         return true;
1604                 }
1605                 
1606                 internal override void SetBorderStyle(IntPtr handle, FormBorderStyle border_style) {
1607                         Hwnd    hwnd;
1608
1609                         hwnd = Hwnd.ObjectFromHandle(handle);
1610                         hwnd.border_style = border_style;
1611
1612                         // FIXME - do we need to trigger some resize?
1613                 }
1614
1615                 internal override void SetMenu(IntPtr handle, Menu menu) {
1616                         Hwnd    hwnd;
1617
1618                         hwnd = Hwnd.ObjectFromHandle(handle);
1619                         hwnd.menu = menu;
1620
1621                         // FIXME - do we need to trigger some resize?
1622                 }
1623                 
1624                 internal override void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max) {
1625                         throw new NotImplementedException();
1626                 }
1627
1628                 internal override void SetWindowPos(IntPtr handle, int x, int y, int width, int height) {
1629                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1630                         Rectangle client_rect = hwnd.GetClientRectangle (width, height);
1631
1632                         // Save a server roundtrip (and prevent a feedback loop)
1633                         if ((hwnd.x == x) && (hwnd.y == y) && (hwnd.width == width) && (hwnd.height == height)) {
1634                                 return;
1635                         }
1636
1637
1638                         if (WindowMapping [hwnd.Handle] != null) {
1639                                 if (y <= MenuBarHeight+TitleBarHeight) {
1640                                         y+=MenuBarHeight+TitleBarHeight;
1641                                 }
1642                                 IntPtr rect = IntPtr.Zero;
1643                                 SetRect (ref rect, (short)x, (short)y, (short)(x+width), (short)(y+height));
1644                                 CheckError (SetWindowBounds ((IntPtr) WindowMapping [hwnd.Handle], 33, ref rect), "SetWindowBounds ()");
1645                                 HIRect r = new HIRect (0, 0, width, height);
1646                                 CheckError (HIViewSetFrame (hwnd.whole_window, ref r), "HIViewSetFrame ()");
1647                                 r = new HIRect (client_rect.X, client_rect.Y, client_rect.X+client_rect.Width, client_rect.Y+client_rect.Height);
1648                                 CheckError (HIViewSetFrame (hwnd.client_window, ref r), "HIViewSetFrame ()");
1649                         } else {
1650                                 HIRect r = new HIRect (x, y, width, height);
1651                                 CheckError (HIViewSetFrame (hwnd.whole_window, ref r), "HIViewSetFrame ()");
1652                                 r = new HIRect (client_rect.X, client_rect.Y, client_rect.X+client_rect.Width, client_rect.Y+client_rect.Height);
1653                                 CheckError (HIViewSetFrame (hwnd.client_window, ref r), "HIViewSetFrame ()");
1654                         }                       
1655                 }
1656                 
1657                 internal override void SetWindowState(IntPtr hwnd, FormWindowState state) {
1658                         IntPtr window = GetControlOwner (hwnd);
1659
1660                         switch (state) {
1661                                 case FormWindowState.Minimized: {
1662                                         CollapseWindow (window, true);
1663                                         break;
1664                                 }
1665                                 case FormWindowState.Normal: {
1666                                         ZoomWindow (window, 7, false);
1667                                         break;
1668                                 }
1669                                 case FormWindowState.Maximized: {
1670                                         ZoomWindow (window, 8, false);
1671                                         break;
1672                                 }
1673                         }
1674                 }
1675                 
1676                 internal override void SetWindowStyle(IntPtr handle, CreateParams cp) {
1677                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1678                         SetHwndStyles(hwnd, cp);
1679                         
1680                         if (WindowMapping [hwnd.Handle] != null) {
1681                                 WindowAttributes attributes = WindowAttributes.kWindowCompositingAttribute | WindowAttributes.kWindowStandardHandlerAttribute;
1682                                 if ((cp.Style & ((int)WindowStyles.WS_MINIMIZEBOX)) != 0) { 
1683                                         attributes |= WindowAttributes.kWindowCollapseBoxAttribute;
1684                                 }
1685                                 if ((cp.Style & ((int)WindowStyles.WS_MAXIMIZEBOX)) != 0) {
1686                                         attributes |= WindowAttributes.kWindowResizableAttribute | WindowAttributes.kWindowHorizontalZoomAttribute | WindowAttributes.kWindowVerticalZoomAttribute;
1687                                 }
1688                                 if ((cp.Style & ((int)WindowStyles.WS_SYSMENU)) != 0) {
1689                                         attributes |= WindowAttributes.kWindowCloseBoxAttribute;
1690                                 }
1691                                 if ((cp.ExStyle & ((int)WindowExStyles.WS_EX_TOOLWINDOW)) != 0) {
1692                                         attributes = WindowAttributes.kWindowStandardHandlerAttribute | WindowAttributes.kWindowCompositingAttribute;
1693                                 }
1694
1695                                 WindowAttributes outAttributes = WindowAttributes.kWindowNoAttributes;
1696                                 GetWindowAttributes ((IntPtr)WindowMapping [hwnd.Handle], ref outAttributes);
1697                                 ChangeWindowAttributes ((IntPtr)WindowMapping [hwnd.Handle], attributes, outAttributes);
1698                         }
1699                 }
1700
1701                 internal override void SetWindowTransparency(IntPtr handle, double transparency, Color key) {
1702                 }
1703
1704                 internal override double GetWindowTransparency(IntPtr handle)
1705                 {
1706                         return 1.0;
1707                 }
1708
1709                 internal override TransparencySupport SupportsTransparency() {
1710                         return TransparencySupport.None;
1711                 }
1712                 
1713                 internal override bool SetZOrder(IntPtr handle, IntPtr after_handle, bool Top, bool Bottom) {
1714                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1715                         
1716                         if (Top) {
1717                                 HIViewSetZOrder (hwnd.whole_window, 2, IntPtr.Zero);
1718                                 return true;
1719                         } else if (!Bottom) {
1720                                 Hwnd after_hwnd = Hwnd.ObjectFromHandle (after_handle);
1721                                 HIViewSetZOrder (hwnd.whole_window, 2, after_hwnd.whole_window);
1722                         } else {
1723                                 HIViewSetZOrder (hwnd.whole_window, 1, IntPtr.Zero);
1724                                 return true;
1725                         }
1726                         return false;
1727                 }
1728
1729                 internal override void ShowCursor(bool show) {
1730                         if (show)
1731                                 CGDisplayShowCursor (CGMainDisplayID ());
1732                         else
1733                                 CGDisplayHideCursor (CGMainDisplayID ());
1734                 }
1735
1736                 internal override object StartLoop(Thread thread) {
1737                         throw new NotImplementedException();
1738                 }
1739                 
1740                 [MonoTODO]
1741                 internal override bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt) {
1742                         throw new NotImplementedException();
1743                 }
1744
1745                 [MonoTODO]
1746                 internal override bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt) {
1747                         throw new NotImplementedException();
1748                 }
1749
1750                 [MonoTODO]
1751                 internal override void SystrayRemove(IntPtr hwnd, ref ToolTip tt) {
1752                         throw new NotImplementedException();
1753                 }
1754
1755 #if NET_2_0
1756                 [MonoTODO]
1757                 internal override void SystrayBalloon(IntPtr hwnd, int timeout, string title, string text, ToolTipIcon icon)
1758                 {
1759                         throw new NotImplementedException ();
1760                 }
1761 #endif
1762                 
1763                 internal override bool Text(IntPtr handle, string text) {
1764                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1765                         if (WindowMapping [hwnd.Handle] != null) {
1766                                 CheckError (SetWindowTitleWithCFString ((IntPtr)(WindowMapping [hwnd.Handle]), __CFStringMakeConstantString (text)));
1767                         }
1768                         CheckError (SetControlTitleWithCFString (hwnd.whole_window, __CFStringMakeConstantString (text)));
1769                         CheckError (SetControlTitleWithCFString (hwnd.client_window, __CFStringMakeConstantString (text)));
1770                         return true;
1771                 }
1772                 
1773                 internal override void UpdateWindow(IntPtr handle) {
1774                         Hwnd hwnd = Hwnd.ObjectFromHandle (handle);
1775                         
1776                         if (hwnd.visible && HIViewIsVisible (handle) && !hwnd.expose_pending) {
1777                                 MSG msg = new MSG ();
1778                                 msg.message = Msg.WM_PAINT;
1779                                 msg.hwnd = hwnd.Handle;
1780                                 msg.lParam = IntPtr.Zero;
1781                                 msg.wParam = IntPtr.Zero;
1782                                 MessageQueue.Enqueue (msg);
1783                         }
1784                 }
1785                 
1786                 internal override bool TranslateMessage(ref MSG msg) {
1787                         bool res = false;
1788                         Hwnd hwnd = Hwnd.ObjectFromHandle (msg.hwnd);
1789                                         
1790                         switch (msg.message) {
1791                                 case Msg.WM_MOUSEMOVE: {
1792                                         // We're grabbed
1793                                         if (GrabWindowHwnd != null) {
1794                                                 if (GrabWindowHwnd.Handle != hwnd.Handle) {
1795                                                         return false;
1796                                                 }
1797                                         } else {
1798                                                 if (MouseWindow != null) {
1799                                                         if (MouseWindow.Handle != hwnd.Handle) {
1800                                                                 PostMessage (MouseWindow.Handle, Msg.WM_MOUSELEAVE, IntPtr.Zero, IntPtr.Zero);
1801                                                                 PostMessage (hwnd.Handle, Msg.WM_MOUSE_ENTER, IntPtr.Zero, IntPtr.Zero);
1802                                                                 MouseWindow = hwnd;
1803                                                         }
1804                                                 } else {
1805                                                         MouseWindow = hwnd;
1806                                                 }
1807                                         }
1808                                         break;
1809                                 }
1810                                 case Msg.WM_SETFOCUS: {
1811                                         break;   
1812                                 }                                       
1813                                 
1814                         }
1815                         
1816                         // This is a hideous temporary keyboard hack to bind some keys  
1817                         if (msg.message >= Msg.WM_KEYFIRST && msg.message <= Msg.WM_KEYLAST)
1818                                 res = true;
1819
1820                         if (msg.message != Msg.WM_KEYDOWN && msg.message != Msg.WM_SYSKEYDOWN)
1821                                 return res;
1822
1823                         if ((int)msg.wParam >= (int)'0' && (int)msg.wParam <= (int)'z') {
1824                                 Msg message;
1825                                 message = Msg.WM_CHAR;
1826                                 PostMessage (msg.hwnd, message, msg.wParam, msg.lParam);
1827                         }
1828                         return true;
1829                 }
1830                 
1831                 internal override void DrawReversibleLine(Point start, Point end, Color backColor) {
1832                         throw new NotImplementedException();
1833                 }
1834
1835                 internal override void FillReversibleRectangle (Rectangle rectangle, Color backColor) {
1836                         throw new NotImplementedException();
1837                 }
1838
1839                 internal override void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style) {
1840                         throw new NotImplementedException();
1841                 }
1842
1843                 internal override void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width) {
1844                         throw new NotImplementedException();
1845                 }
1846
1847                 [MonoTODO]
1848                 internal override SizeF GetAutoScaleSize(Font font) {
1849                         throw new NotImplementedException();
1850                 }
1851
1852                 internal override Point MousePosition {
1853                         get {
1854                                 return mouse_position;
1855                         }
1856                 }
1857                 #endregion
1858                 
1859                 #region System information
1860                 internal override int KeyboardSpeed { get{ throw new NotImplementedException(); } } 
1861                 internal override int KeyboardDelay { get{ throw new NotImplementedException(); } } 
1862
1863                 internal override  int CaptionHeight { get{ throw new NotImplementedException(); } }
1864                 internal override  Size CursorSize { get{ throw new NotImplementedException(); } }
1865                 internal override  bool DragFullWindows { get{ throw new NotImplementedException(); } }
1866                 internal override  Size DragSize { get{ throw new NotImplementedException(); } }
1867                 internal override  Size FrameBorderSize { get{ throw new NotImplementedException(); } }
1868                 internal override  Size IconSize { get{ throw new NotImplementedException(); } }
1869                 internal override  Size MaxWindowTrackSize { get{ throw new NotImplementedException(); } }
1870                 internal override bool MenuAccessKeysUnderlined {
1871                         get {
1872                                 return false;
1873                         }
1874                 }
1875                 internal override  Size MinimizedWindowSize { get{ throw new NotImplementedException(); } }
1876                 internal override  Size MinimizedWindowSpacingSize { get{ throw new NotImplementedException(); } }
1877                 internal override  Size MinimumWindowSize { get{ throw new NotImplementedException(); } }
1878                 internal override  Size MinWindowTrackSize { get{ throw new NotImplementedException(); } }
1879                 internal override  Size SmallIconSize { get{ throw new NotImplementedException(); } }
1880                 internal override  int MouseButtonCount { get{ throw new NotImplementedException(); } }
1881                 internal override  bool MouseButtonsSwapped { get{ throw new NotImplementedException(); } }
1882                 internal override  bool MouseWheelPresent { get{ throw new NotImplementedException(); } }
1883                 internal override  Rectangle VirtualScreen { get{ throw new NotImplementedException(); } }
1884                 internal override  Rectangle WorkingArea { 
1885                         get { 
1886                                 HIRect bounds = CGDisplayBounds (CGMainDisplayID ());
1887                                 return new Rectangle ((int)bounds.origin.x, (int)bounds.origin.y, (int)bounds.size.width, (int)bounds.size.height);
1888                         }
1889                 }
1890                 internal override bool ThemesEnabled {
1891                         get {
1892                                 return XplatUIOSX.themes_enabled;
1893                         }
1894                 }
1895  
1896
1897                 #endregion
1898                 
1899                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1900                 internal static extern int HIViewSetNeedsDisplayInRegion (IntPtr view, IntPtr rgn, bool needsDisplay);
1901                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1902                 internal static extern int HIViewGetSubviewHit (IntPtr contentView, ref CGPoint point, bool tval, ref IntPtr outPtr);
1903                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1904                 internal static extern int HIViewGetViewForMouseEvent (IntPtr inView, IntPtr inEvent, ref IntPtr outView);
1905                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1906                 internal static extern int HIViewConvertPoint (ref CGPoint point, IntPtr pView, IntPtr cView);
1907                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1908                 internal static extern int HIViewChangeFeatures (IntPtr aView, ulong bitsin, ulong bitsout);
1909                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1910                 internal static extern int HIViewFindByID (IntPtr rootWnd, HIViewID id, ref IntPtr outPtr);
1911                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1912                 internal static extern IntPtr HIViewGetRoot (IntPtr hWnd);
1913                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1914                 internal static extern int HIObjectCreate (IntPtr cfStr, uint what, ref IntPtr hwnd);
1915                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1916                 internal static extern int HIViewSetNeedsDisplay (IntPtr viewHnd, bool update);
1917                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1918                 internal static extern int HIViewGetFrame (IntPtr viewHnd, ref HIRect rect);
1919                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1920                 internal static extern int HIViewSetFrame (IntPtr viewHnd, ref HIRect rect);
1921                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1922                 internal static extern int HIViewPlaceInSuperviewAt (IntPtr view, float x, float y);
1923                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1924                 internal static extern int HIViewAddSubview (IntPtr parentHnd, IntPtr childHnd);
1925                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1926                 internal static extern IntPtr HIViewGetNextView (IntPtr aView);
1927                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1928                 internal static extern IntPtr HIViewGetPreviousView (IntPtr aView);
1929                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1930                 internal static extern IntPtr HIViewGetFirstSubview (IntPtr aView);
1931                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1932                 internal static extern IntPtr HIViewGetSuperview (IntPtr aView);
1933                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1934                 internal static extern int HIViewRemoveFromSuperview (IntPtr aView);
1935                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1936                 internal static extern int HIViewSetVisible (IntPtr vHnd, bool visible);
1937                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1938                 internal static extern bool HIViewIsVisible (IntPtr vHnd);
1939                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1940                 internal static extern int HIViewGetBounds (IntPtr vHnd, ref HIRect r);
1941                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1942                 internal static extern int HIViewScrollRect (IntPtr vHnd, ref HIRect rect, float x, float y);
1943                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1944                 internal static extern int HIViewScrollRect (IntPtr vHnd, IntPtr rect, float x, float y);
1945                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1946                 internal static extern int HIViewSetZOrder (IntPtr hWnd, int cmd, IntPtr oHnd);
1947                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1948                 internal static extern int HIViewSetBoundsOrigin (IntPtr vHnd, float x, float y);
1949                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1950                 internal static extern int HIViewConvertRect (ref HIRect r, IntPtr a, IntPtr b);
1951                 
1952                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1953                 internal static extern void ScrollRect (ref IntPtr r, short dh, short dv, IntPtr rgnHandle);
1954                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1955                 internal static extern void SetRect (ref IntPtr r, short left, short top, short right, short bottom);
1956
1957                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1958                 //static extern int CreateEvent (IntPtr allocator, uint classid, uint kind, double when, uint attributes, ref IntPtr outEvent);
1959                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1960                 static extern int InstallEventHandler (IntPtr window, CarbonEventDelegate handlerProc, uint numtypes, EventTypeSpec [] typeList, IntPtr userData, IntPtr handlerRef);
1961                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1962                 internal static extern IntPtr GetControlOwner (IntPtr aView);
1963                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1964                 static extern int ActivateWindow (IntPtr windowHnd, bool inActivate);
1965                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1966                 static extern bool IsWindowActive (IntPtr windowHnd);
1967                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1968                 static extern int SetKeyboardFocus (IntPtr windowHdn, IntPtr cntrlHnd, short partcode);
1969                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1970                 static extern int GetKeyboardFocus (IntPtr handle, ref IntPtr cntrl);
1971
1972                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1973                 internal static extern IntPtr GetWindowEventTarget (IntPtr window);
1974                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1975                 internal static extern IntPtr GetControlEventTarget (IntPtr aControl);
1976                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1977                 internal static extern IntPtr GetEventDispatcherTarget ();
1978                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1979                 internal static extern int SendEventToEventTarget (IntPtr evt, IntPtr target);
1980                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1981                 internal static extern int ReleaseEvent (IntPtr evt);
1982                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1983                 internal static extern int ReceiveNextEvent (uint evtCount, IntPtr evtTypes, double timeout, bool processEvt, ref IntPtr evt);
1984                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1985                 static extern uint GetEventClass (IntPtr eventRef);
1986                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1987                 static extern uint GetEventKind (IntPtr eventRef);
1988                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1989                 static extern int GetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, IntPtr outActualType, uint bufSize, IntPtr outActualSize, ref byte outData);
1990                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1991                 static extern int GetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, IntPtr outActualType, uint bufSize, IntPtr outActualSize, ref IntPtr outData);
1992                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1993                 static extern int GetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, IntPtr outActualType, uint bufSize, IntPtr outActualSize, ref ushort outData);
1994                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1995                 static extern int GetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, IntPtr outActualType, uint bufSize, IntPtr outActualSize, ref short outData);
1996                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1997                 static extern int GetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, IntPtr outActualType, uint bufSize, IntPtr outActualSize, ref QDPoint outData);
1998                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
1999                 static extern int SetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, uint bufSize, ref short outData);
2000                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2001                 //static extern int SetEventParameter (IntPtr evt, OSXConstants.EventParamName inName, OSXConstants.EventParamType inType, uint bufSize, ref IntPtr outData);
2002
2003                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2004                 internal static extern void CGContextFlush (IntPtr cgc);
2005                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2006                 internal static extern int CGContextFillRect (IntPtr cgc, HIRect r);
2007                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2008                 internal static extern CGAffineTransform CGContextGetTextMatrix (IntPtr cgContext);
2009                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2010                 internal static extern int CGContextSetTextMatrix (IntPtr cgContext, CGAffineTransform ctm);
2011                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2012                 internal static extern int CGContextSetRGBFillColor (IntPtr cgContext, float r, float g, float b, float alpha);
2013                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2014                 internal static extern int CGContextSetRGBStrokeColor (IntPtr cgContext, float r, float g, float b, float alpha);
2015                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2016                 internal static extern int CGContextSetTextDrawingMode (IntPtr cgContext, int drawingMode);
2017                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2018                 internal static extern int CGContextSelectFont (IntPtr cgContext, string fontName, float size, int textEncoding);
2019                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2020                 internal static extern int CGContextShowTextAtPoint (IntPtr cgContext, float x, float y, string text, int length);
2021                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2022                 internal static extern int CGContextClipToRect (IntPtr cgContext, HIRect clip);
2023                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2024                 internal static extern void CreateCGContextForPort (IntPtr port, ref IntPtr cgc);
2025                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2026                 internal static extern bool IsWindowCollapsed (IntPtr hWnd);
2027                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2028                 internal static extern bool IsWindowInStandardState (IntPtr hWnd, IntPtr a, IntPtr b);
2029                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2030                 internal static extern void CollapseWindow (IntPtr hWnd, bool collapse);
2031                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2032                 internal static extern void ZoomWindow (IntPtr hWnd, short partCode, bool front);
2033                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2034                 internal static extern int GetWindowAttributes (IntPtr hWnd, ref WindowAttributes outAttributes);
2035                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2036                 internal static extern int ChangeWindowAttributes (IntPtr hWnd, WindowAttributes inAttributes, WindowAttributes outAttributes);
2037                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2038                 internal static extern IntPtr GetWindowPort (IntPtr hWnd);
2039                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2040                 static extern int SetPortWindowPort (IntPtr hWnd);
2041                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2042                 static extern int GetGlobalMouse (ref QDPoint outData);
2043                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2044                 //static extern int GlobalToLocal (ref QDPoint outData);
2045                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2046                 //static extern int LocalToGlobal (ref QDPoint outData);
2047                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2048                 static extern int TrackControl (IntPtr handle, QDPoint point, IntPtr data);
2049                 
2050                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2051                 internal static extern int BeginAppModalStateForWindow (IntPtr window);
2052                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2053                 internal static extern int EndAppModalStateForWindow (IntPtr window);
2054                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2055                 internal static extern int CreateNewWindow (WindowClass klass, WindowAttributes attributes, ref IntPtr r, ref IntPtr window);
2056                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2057                 internal static extern int DisposeWindow (IntPtr wHnd);
2058                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2059                 internal static extern int ShowWindow (IntPtr wHnd);
2060                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2061                 internal static extern int HideWindow (IntPtr wHnd);
2062                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2063                 internal static extern int SetWindowBounds (IntPtr wHnd, uint reg, ref IntPtr rect);
2064                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2065                 internal static extern int GetWindowPortBounds (IntPtr wHnd, ref Rect rect);
2066                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2067                 internal static extern int GetWindowBounds (IntPtr wHnd, uint reg, ref Rect rect);
2068                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2069                 internal static extern int InvertRect (ref Rect r);
2070
2071                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2072                 internal static extern int SetControlTitleWithCFString (IntPtr hWnd, IntPtr titleCFStr);
2073                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2074                 internal static extern int SetWindowTitleWithCFString (IntPtr hWnd, IntPtr titleCFStr);
2075                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2076                 internal static extern IntPtr __CFStringMakeConstantString (string cString);
2077                 
2078                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2079                 internal static extern void CGContextRestoreGState (IntPtr ctx);
2080                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2081                 internal static extern void CGContextSaveGState (IntPtr ctx);
2082                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2083                 internal static extern void CGContextTranslateCTM (IntPtr ctx, double tx, double ty);
2084                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2085                 internal static extern void CGContextScaleCTM (IntPtr ctx, double tx, double ty);
2086
2087                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2088                 //static extern int SetWindowContentColor (IntPtr hWnd, ref RGBColor backColor);
2089                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2090                 static extern int TrackMouseLocationWithOptions (IntPtr port, int options, double eventtimeout, ref QDPoint point, ref IntPtr modifier, ref MouseTrackingResult status);
2091                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2092                 //static extern int CreateMouseTrackingRegion (IntPtr windowref, IntPtr rgn, IntPtr clip, int options, MouseTrackingRegionID rid, IntPtr refcon, IntPtr evttargetref, ref IntPtr mousetrackref);
2093                 //[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2094                 //static extern int ReleaseMouseTrackingRegion (IntPtr region_handle);
2095                 
2096                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2097                 internal static extern int CFRelease (IntPtr wHnd);
2098                 
2099                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2100                 internal extern static IntPtr NewRgn ();
2101                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2102                 internal extern static void CopyRgn (IntPtr srcrgn, IntPtr destrgn);
2103                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2104                 internal extern static void SetRectRgn (IntPtr rgn, short left, short top, short right, short bottom);
2105                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2106                 internal extern static void DisposeRgn (IntPtr rgn);
2107                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2108                 internal extern static void ExitToShell ();
2109                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2110                 internal extern static short GetMBarHeight ();
2111                 
2112                 #region Cursor imports
2113                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2114                 internal extern static HIRect CGDisplayBounds (IntPtr displayID);
2115                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2116                 internal extern static IntPtr CGMainDisplayID ();
2117                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2118                 internal extern static void CGDisplayShowCursor (IntPtr display);
2119                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2120                 internal extern static void CGDisplayHideCursor (IntPtr display);
2121                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2122                 internal extern static void CGDisplayMoveCursorToPoint (IntPtr display, CGPoint point);
2123                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
2124                 internal extern static void SetThemeCursor (uint inCursor);
2125                 #endregion
2126         }
2127
2128 }