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