Make wxWindow::ShowWithEffect() synchronous under wxOSX/Cocoa.
[wxWidgets.git] / src / osx / cocoa / window.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/osx/cocoa/window.mm
3 // Purpose:     widgets (non tlw) for cocoa
4 // Author:      Stefan Csomor
5 // Modified by:
6 // Created:     2008-06-20
7 // RCS-ID:      $Id: window.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright:   (c) Stefan Csomor
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15     #include "wx/dcclient.h"
16     #include "wx/nonownedwnd.h"
17     #include "wx/log.h"
18 #endif
19
20 #ifdef __WXMAC__
21     #include "wx/osx/private.h"
22 #endif
23
24 #include "wx/evtloop.h"
25
26 #if wxUSE_CARET
27     #include "wx/caret.h"
28 #endif
29
30 #if wxUSE_DRAG_AND_DROP
31     #include "wx/dnd.h"
32 #endif
33
34 #include <objc/objc-runtime.h>
35
36 // Get the window with the focus
37
38 NSView* GetViewFromResponder( NSResponder* responder )
39 {
40     NSView* view = nil;
41     if ( [responder isKindOfClass:[NSTextView class]] )
42     {
43         NSView* delegate =  [(NSTextView*)responder delegate];
44         if ( [delegate isKindOfClass:[NSTextField class] ] )
45             view = delegate;
46         else
47             view =  (NSView*) responder;
48     }
49     else
50     {
51         if ( [responder isKindOfClass:[NSView class]] )
52             view = (NSView*) responder;
53     }
54     return view;
55 }
56
57 NSView* GetFocusedViewInWindow( NSWindow* keyWindow )
58 {
59     NSView* focusedView = nil;
60     if ( keyWindow != nil )
61         focusedView = GetViewFromResponder([keyWindow firstResponder]);
62
63     return focusedView;
64 }
65
66 WXWidget wxWidgetImpl::FindFocus()
67 {
68     return GetFocusedViewInWindow( [[NSApplication sharedApplication] keyWindow] );
69 }
70
71 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
72 {
73     int x, y, w, h ;
74
75     window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
76     wxRect bounds(x,y,w,h);
77     NSView* sv = (window->GetParent()->GetHandle() );
78
79     return wxToNSRect( sv, bounds );
80 }
81
82 @interface wxNSView : NSView
83 {
84     NSTrackingRectTag rectTag;
85 }
86
87 // the tracking tag is needed to track mouse enter / exit events
88 - (void) setTrackingTag: (NSTrackingRectTag)tag;
89 - (NSTrackingRectTag) trackingTag;
90 @end // wxNSView
91
92 @interface NSView(PossibleMethods)
93 - (void)setTitle:(NSString *)aString;
94 - (void)setStringValue:(NSString *)aString;
95 - (void)setIntValue:(int)anInt;
96 - (void)setFloatValue:(float)aFloat;
97 - (void)setDoubleValue:(double)aDouble;
98
99 - (double)minValue;
100 - (double)maxValue;
101 - (void)setMinValue:(double)aDouble;
102 - (void)setMaxValue:(double)aDouble;
103
104 - (void)sizeToFit;
105
106 - (BOOL)isEnabled;
107 - (void)setEnabled:(BOOL)flag;
108
109 - (void)setImage:(NSImage *)image;
110 - (void)setControlSize:(NSControlSize)size;
111
112 - (void)setFont:(NSFont *)fontObject;
113
114 - (id)contentView;
115
116 - (void)setTarget:(id)anObject;
117 - (void)setAction:(SEL)aSelector;
118 - (void)setDoubleAction:(SEL)aSelector;
119 - (void)setBackgroundColor:(NSColor*)aColor;
120 - (void)setImagePosition:(NSCellImagePosition)aPosition;
121 @end
122
123 long wxOSXTranslateCocoaKey( NSEvent* event )
124 {
125     long retval = 0;
126
127     if ([event type] != NSFlagsChanged)
128     {
129         NSString* s = [event charactersIgnoringModifiers];
130         // backspace char reports as delete w/modifiers for some reason
131         if ([s length] == 1)
132         {
133             switch ( [s characterAtIndex:0] )
134             {
135                 // backspace key
136                 case 0x7F :
137                 case 8 :
138                     retval = WXK_BACK;
139                     break;
140                 case NSUpArrowFunctionKey :
141                     retval = WXK_UP;
142                     break;
143                 case NSDownArrowFunctionKey :
144                     retval = WXK_DOWN;
145                     break;
146                 case NSLeftArrowFunctionKey :
147                     retval = WXK_LEFT;
148                     break;
149                 case NSRightArrowFunctionKey :
150                     retval = WXK_RIGHT;
151                     break;
152                 case NSInsertFunctionKey  :
153                     retval = WXK_INSERT;
154                     break;
155                 case NSDeleteFunctionKey  :
156                     retval = WXK_DELETE;
157                     break;
158                 case NSHomeFunctionKey  :
159                     retval = WXK_HOME;
160                     break;
161         //        case NSBeginFunctionKey  :
162         //            retval = WXK_BEGIN;
163         //            break;
164                 case NSEndFunctionKey  :
165                     retval = WXK_END;
166                     break;
167                 case NSPageUpFunctionKey  :
168                     retval = WXK_PAGEUP;
169                     break;
170                case NSPageDownFunctionKey  :
171                     retval = WXK_PAGEDOWN;
172                     break;
173                case NSHelpFunctionKey  :
174                     retval = WXK_HELP;
175                     break;
176                 default:
177                     int intchar = [s characterAtIndex: 0];
178                     if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
179                         retval = WXK_F1 + (intchar - NSF1FunctionKey );
180                     break;
181             }
182         }
183     }
184
185     // Some keys don't seem to have constants. The code mimics the approach
186     // taken by WebKit. See:
187     // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
188     switch( [event keyCode] )
189     {
190         // command key
191         case 54:
192         case 55:
193             retval = WXK_COMMAND;
194             break;
195         // caps locks key
196         case 57: // Capslock
197             retval = WXK_CAPITAL;
198             break;
199         // shift key
200         case 56: // Left Shift
201         case 60: // Right Shift
202             retval = WXK_SHIFT;
203             break;
204         // alt key
205         case 58: // Left Alt
206         case 61: // Right Alt
207             retval = WXK_ALT;
208             break;
209         // ctrl key
210         case 59: // Left Ctrl
211         case 62: // Right Ctrl
212             retval = WXK_CONTROL;
213             break;
214         // clear key
215         case 71:
216             retval = WXK_CLEAR;
217             break;
218         // tab key
219         case 48:
220             retval = WXK_TAB;
221             break;
222
223         case 75: // /
224             retval = WXK_NUMPAD_DIVIDE;
225             break;
226         case 67: // *
227             retval = WXK_NUMPAD_MULTIPLY;
228             break;
229         case 78: // -
230             retval = WXK_NUMPAD_SUBTRACT;
231             break;
232         case 69: // +
233             retval = WXK_NUMPAD_ADD;
234             break;
235         case 76: // Enter
236             retval = WXK_NUMPAD_ENTER;
237             break;
238         case 65: // .
239             retval = WXK_NUMPAD_DECIMAL;
240             break;
241         case 82: // 0
242             retval = WXK_NUMPAD0;
243             break;
244         case 83: // 1
245             retval = WXK_NUMPAD1;
246             break;
247         case 84: // 2
248             retval = WXK_NUMPAD2;
249             break;
250         case 85: // 3
251             retval = WXK_NUMPAD3;
252             break;
253         case 86: // 4
254             retval = WXK_NUMPAD4;
255             break;
256         case 87: // 5
257             retval = WXK_NUMPAD5;
258             break;
259         case 88: // 6
260             retval = WXK_NUMPAD6;
261             break;
262         case 89: // 7
263             retval = WXK_NUMPAD7;
264             break;
265         case 91: // 8
266             retval = WXK_NUMPAD8;
267             break;
268         case 92: // 9
269             retval = WXK_NUMPAD9;
270             break;
271         default:
272             //retval = [event keyCode];
273             break;
274     }
275     return retval;
276 }
277
278 void wxWidgetCocoaImpl::SetupKeyEvent(wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString)
279 {
280     UInt32 modifiers = [nsEvent modifierFlags] ;
281     int eventType = [nsEvent type];
282
283     wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
284     wxevent.m_controlDown = modifiers & NSControlKeyMask;
285     wxevent.m_altDown = modifiers & NSAlternateKeyMask;
286     wxevent.m_metaDown = modifiers & NSCommandKeyMask;
287
288     wxevent.m_rawCode = [nsEvent keyCode];
289     wxevent.m_rawFlags = modifiers;
290
291     wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
292
293     wxString chars;
294     if ( eventType != NSFlagsChanged )
295     {
296         NSString* nschars = (wxevent.GetEventType() != wxEVT_CHAR) ? [nsEvent charactersIgnoringModifiers] : [nsEvent characters];
297         if ( charString )
298         {
299             // if charString is set, it did not come from key up / key down
300             wxevent.SetEventType( wxEVT_CHAR );
301             chars = wxCFStringRef::AsString(charString);
302         }
303         else if ( nschars )
304         {
305             chars = wxCFStringRef::AsString(nschars);
306         }
307     }
308
309     int aunichar = chars.Length() > 0 ? chars[0] : 0;
310     long keyval = 0;
311
312     if (wxevent.GetEventType() != wxEVT_CHAR)
313     {
314         keyval = wxOSXTranslateCocoaKey(nsEvent) ;
315         switch (eventType)
316         {
317             case NSKeyDown :
318                 wxevent.SetEventType( wxEVT_KEY_DOWN )  ;
319                 break;
320             case NSKeyUp :
321                 wxevent.SetEventType( wxEVT_KEY_UP )  ;
322                 break;
323             case NSFlagsChanged :
324                 switch (keyval)
325                 {
326                     case WXK_CONTROL:
327                         wxevent.SetEventType( wxevent.m_controlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
328                         break;
329                     case WXK_SHIFT:
330                         wxevent.SetEventType( wxevent.m_shiftDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
331                         break;
332                     case WXK_ALT:
333                         wxevent.SetEventType( wxevent.m_altDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
334                         break;
335                     case WXK_COMMAND:
336                         wxevent.SetEventType( wxevent.m_metaDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
337                         break;
338                 }
339                 break;
340             default :
341                 break ;
342         }
343     }
344
345     if ( !keyval )
346     {
347         if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
348             keyval = wxToupper( aunichar ) ;
349         else
350             keyval = aunichar;
351     }
352
353 #if wxUSE_UNICODE
354     wxevent.m_uniChar = aunichar;
355 #endif
356     wxevent.m_keyCode = keyval;
357
358     wxWindowMac* peer = GetWXPeer();
359     if ( peer )
360     {
361         wxevent.SetEventObject(peer);
362         wxevent.SetId(peer->GetId()) ;
363     }
364 }
365
366 UInt32 g_lastButton = 0 ;
367 bool g_lastButtonWasFakeRight = false ;
368
369 void wxWidgetCocoaImpl::SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
370 {
371     int eventType = [nsEvent type];
372     UInt32 modifiers = [nsEvent modifierFlags] ;
373     wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
374
375     // these parameters are not given for all events
376     UInt32 button = [nsEvent buttonNumber];
377     UInt32 clickCount = 0;
378
379     wxevent.m_x = screenMouseLocation.x;
380     wxevent.m_y = screenMouseLocation.y;
381     wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
382     wxevent.m_controlDown = modifiers & NSControlKeyMask;
383     wxevent.m_altDown = modifiers & NSAlternateKeyMask;
384     wxevent.m_metaDown = modifiers & NSCommandKeyMask;
385     wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
386
387     UInt32 mouseChord = 0;
388
389     switch (eventType)
390     {
391         case NSLeftMouseDown :
392         case NSLeftMouseDragged :
393             mouseChord = 1U;
394             break;
395         case NSRightMouseDown :
396         case NSRightMouseDragged :
397             mouseChord = 2U;
398             break;
399         case NSOtherMouseDown :
400         case NSOtherMouseDragged :
401             mouseChord = 4U;
402             break;
403     }
404
405     // a control click is interpreted as a right click
406     bool thisButtonIsFakeRight = false ;
407     if ( button == 0 && (modifiers & NSControlKeyMask) )
408     {
409         button = 1 ;
410         thisButtonIsFakeRight = true ;
411     }
412
413     // otherwise we report double clicks by connecting a left click with a ctrl-left click
414     if ( clickCount > 1 && button != g_lastButton )
415         clickCount = 1 ;
416
417     // we must make sure that our synthetic 'right' button corresponds in
418     // mouse down, moved and mouse up, and does not deliver a right down and left up
419     switch (eventType)
420     {
421         case NSLeftMouseDown :
422         case NSRightMouseDown :
423         case NSOtherMouseDown :
424             g_lastButton = button ;
425             g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
426             break;
427      }
428
429     if ( button == 0 )
430     {
431         g_lastButton = 0 ;
432         g_lastButtonWasFakeRight = false ;
433     }
434     else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
435         button = g_lastButton ;
436
437     // Adjust the chord mask to remove the primary button and add the
438     // secondary button.  It is possible that the secondary button is
439     // already pressed, e.g. on a mouse connected to a laptop, but this
440     // possibility is ignored here:
441     if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
442         mouseChord = ((mouseChord & ~1U) | 2U);
443
444     if(mouseChord & 1U)
445                 wxevent.m_leftDown = true ;
446     if(mouseChord & 2U)
447                 wxevent.m_rightDown = true ;
448     if(mouseChord & 4U)
449                 wxevent.m_middleDown = true ;
450
451     // translate into wx types
452     switch (eventType)
453     {
454         case NSLeftMouseDown :
455         case NSRightMouseDown :
456         case NSOtherMouseDown :
457             clickCount = [nsEvent clickCount];
458             switch ( button )
459             {
460                 case 0 :
461                     wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN )  ;
462                     break ;
463
464                 case 1 :
465                     wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
466                     break ;
467
468                 case 2 :
469                     wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
470                     break ;
471
472                 default:
473                     break ;
474             }
475             break ;
476
477         case NSLeftMouseUp :
478         case NSRightMouseUp :
479         case NSOtherMouseUp :
480             clickCount = [nsEvent clickCount];
481             switch ( button )
482             {
483                 case 0 :
484                     wxevent.SetEventType( wxEVT_LEFT_UP )  ;
485                     break ;
486
487                 case 1 :
488                     wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
489                     break ;
490
491                 case 2 :
492                     wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
493                     break ;
494
495                 default:
496                     break ;
497             }
498             break ;
499
500      case NSScrollWheel :
501         {
502             wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
503             wxevent.m_wheelDelta = 10;
504             wxevent.m_linesPerAction = 1;
505
506             if ( fabs([nsEvent deltaX]) > fabs([nsEvent deltaY]) )
507             {
508                 wxevent.m_wheelAxis = 1;
509                 wxevent.m_wheelRotation = (int)([nsEvent deltaX] * 10);
510             }
511             else
512             {
513                 wxevent.m_wheelRotation = (int)([nsEvent deltaY] * 10);
514             }
515         }
516         break ;
517
518         case NSMouseEntered :
519             wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
520             break;
521         case NSMouseExited :
522             wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
523             break;
524         case NSLeftMouseDragged :
525         case NSRightMouseDragged :
526         case NSOtherMouseDragged :
527         case NSMouseMoved :
528             wxevent.SetEventType( wxEVT_MOTION ) ;
529             break;
530         default :
531             break ;
532     }
533
534     wxevent.m_clickCount = clickCount;
535     wxWindowMac* peer = GetWXPeer();
536     if ( peer )
537     {
538         wxevent.SetEventObject(peer);
539         wxevent.SetId(peer->GetId()) ;
540     }
541 }
542
543 @implementation wxNSView
544
545 + (void)initialize
546 {
547     static BOOL initialized = NO;
548     if (!initialized)
549     {
550         initialized = YES;
551         wxOSXCocoaClassAddWXMethods( self );
552     }
553 }
554
555 - (void) setTrackingTag: (NSTrackingRectTag)tag
556 {
557     rectTag = tag;
558 }
559
560 - (NSTrackingRectTag) trackingTag
561 {
562     return rectTag;
563 }
564
565 @end // wxNSView
566
567 //
568 // event handlers
569 //
570
571 #if wxUSE_DRAG_AND_DROP
572
573 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
574 // for details on the NSPasteboard -> PasteboardRef conversion
575
576 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
577 {
578     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
579     if (impl == NULL)
580         return NSDragOperationNone;
581
582     return impl->draggingEntered(sender, self, _cmd);
583 }
584
585 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
586 {
587     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
588     if (impl == NULL)
589         return ;
590
591     return impl->draggingExited(sender, self, _cmd);
592 }
593
594 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
595 {
596     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
597     if (impl == NULL)
598         return NSDragOperationNone;
599
600     return impl->draggingUpdated(sender, self, _cmd);
601 }
602
603 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
604 {
605     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
606     if (impl == NULL)
607         return NSDragOperationNone;
608
609     return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
610 }
611
612 #endif
613
614 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
615 {
616     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
617     if (impl == NULL)
618         return;
619
620     impl->mouseEvent(event, self, _cmd);
621 }
622
623 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
624 {
625     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
626     if (impl == NULL)
627         return;
628
629     impl->keyEvent(event, self, _cmd);
630 }
631
632 void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
633 {
634     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
635     if (impl == NULL)
636         return;
637
638     impl->insertText(text, self, _cmd);
639 }
640
641 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
642 {
643     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
644     if (impl == NULL)
645         return NO;
646
647     return impl->performKeyEquivalent(event, self, _cmd);
648 }
649
650 BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
651 {
652     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
653     if (impl == NULL)
654         return NO;
655
656     return impl->acceptsFirstResponder(self, _cmd);
657 }
658
659 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
660 {
661     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
662     if (impl == NULL)
663         return NO;
664
665     return impl->becomeFirstResponder(self, _cmd);
666 }
667
668 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
669 {
670     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
671     if (impl == NULL)
672         return NO;
673
674     return impl->resignFirstResponder(self, _cmd);
675 }
676
677 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
678 {
679     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
680     if (impl == NULL)
681         return;
682
683     impl->resetCursorRects(self, _cmd);
684 }
685
686 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
687 {
688     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
689     if (impl == NULL)
690         return NO;
691
692     return impl->isFlipped(self, _cmd) ? YES:NO;
693 }
694
695 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
696 {
697     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
698     if (impl == NULL)
699         return;
700
701     return impl->drawRect(&rect, self, _cmd);
702 }
703
704 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
705 {
706     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
707     if (impl == NULL)
708         return;
709
710     impl->controlAction(self, _cmd, sender);
711 }
712
713 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
714 {
715     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
716     if (impl == NULL)
717         return;
718
719     impl->controlDoubleAction(self, _cmd, sender);
720 }
721
722 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
723 {
724     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
725     NSPasteboard *pboard = [sender draggingPasteboard];
726     NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
727
728     wxWindow* wxpeer = GetWXPeer();
729     if ( wxpeer == NULL )
730         return NSDragOperationNone;
731
732     wxDropTarget* target = wxpeer->GetDropTarget();
733     if ( target == NULL )
734         return NSDragOperationNone;
735
736     wxDragResult result = wxDragNone;
737     NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
738     wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
739
740     if ( sourceDragMask & NSDragOperationLink )
741         result = wxDragLink;
742     else if ( sourceDragMask & NSDragOperationCopy )
743         result = wxDragCopy;
744     else if ( sourceDragMask & NSDragOperationMove )
745         result = wxDragMove;
746
747     PasteboardRef pboardRef;
748     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
749     target->SetCurrentDragPasteboard(pboardRef);
750     result = target->OnEnter(pt.x, pt.y, result);
751     CFRelease(pboardRef);
752
753     NSDragOperation nsresult = NSDragOperationNone;
754     switch (result )
755     {
756         case wxDragLink:
757             nsresult = NSDragOperationLink;
758         case wxDragMove:
759             nsresult = NSDragOperationMove;
760         case wxDragCopy:
761             nsresult = NSDragOperationCopy;
762         default :
763             break;
764     }
765     return nsresult;
766 }
767
768 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
769 {
770     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
771     NSPasteboard *pboard = [sender draggingPasteboard];
772
773     wxWindow* wxpeer = GetWXPeer();
774     if ( wxpeer == NULL )
775         return;
776
777     wxDropTarget* target = wxpeer->GetDropTarget();
778     if ( target == NULL )
779         return;
780
781     PasteboardRef pboardRef;
782     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
783     target->SetCurrentDragPasteboard(pboardRef);
784     target->OnLeave();
785     CFRelease(pboardRef);
786  }
787
788 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
789 {
790     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
791     NSPasteboard *pboard = [sender draggingPasteboard];
792     NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
793
794     wxWindow* wxpeer = GetWXPeer();
795     if ( wxpeer == NULL )
796         return NSDragOperationNone;
797
798     wxDropTarget* target = wxpeer->GetDropTarget();
799     if ( target == NULL )
800         return NSDragOperationNone;
801
802     wxDragResult result = wxDragNone;
803     NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
804     wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
805
806     if ( sourceDragMask & NSDragOperationLink )
807         result = wxDragLink;
808     else if ( sourceDragMask & NSDragOperationCopy )
809         result = wxDragCopy;
810     else if ( sourceDragMask & NSDragOperationMove )
811         result = wxDragMove;
812
813     PasteboardRef pboardRef;
814     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
815     target->SetCurrentDragPasteboard(pboardRef);
816     result = target->OnDragOver(pt.x, pt.y, result);
817     CFRelease(pboardRef);
818
819     NSDragOperation nsresult = NSDragOperationNone;
820     switch (result )
821     {
822         case wxDragLink:
823             nsresult = NSDragOperationLink;
824         case wxDragMove:
825             nsresult = NSDragOperationMove;
826         case wxDragCopy:
827             nsresult = NSDragOperationCopy;
828         default :
829             break;
830     }
831     return nsresult;
832 }
833
834 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
835 {
836     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
837
838     NSPasteboard *pboard = [sender draggingPasteboard];
839     NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
840
841     wxWindow* wxpeer = GetWXPeer();
842     wxDropTarget* target = wxpeer->GetDropTarget();
843     wxDragResult result = wxDragNone;
844     NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
845     wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
846
847     if ( sourceDragMask & NSDragOperationLink )
848         result = wxDragLink;
849     else if ( sourceDragMask & NSDragOperationCopy )
850         result = wxDragCopy;
851     else if ( sourceDragMask & NSDragOperationMove )
852         result = wxDragMove;
853
854     PasteboardRef pboardRef;
855     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
856     target->SetCurrentDragPasteboard(pboardRef);
857
858     if (target->OnDrop(pt.x, pt.y))
859         result = target->OnData(pt.x, pt.y, result);
860
861     CFRelease(pboardRef);
862
863     return result != wxDragNone;
864 }
865
866 typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
867 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
868 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
869 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
870 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
871 typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
872
873 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
874 {
875     if ( !DoHandleMouseEvent(event) )
876     {
877         // for plain NSView mouse events would propagate to parents otherwise
878         if (!m_wxPeer->MacIsUserPane())
879         {
880             wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
881             superimpl(slf, (SEL)_cmd, event);
882         }
883     }
884 }
885
886 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
887 {
888     if ( [event type] == NSKeyDown )
889         m_lastKeyDownEvent = event;
890     if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
891     {
892         wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
893         superimpl(slf, (SEL)_cmd, event);
894     }
895     m_lastKeyDownEvent = NULL;
896 }
897
898 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
899 {
900     if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
901     {
902         wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
903         superimpl(slf, (SEL)_cmd, text);
904     }
905 }
906
907
908 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
909 {
910     wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
911     return superimpl(slf, (SEL)_cmd, event);
912 }
913
914 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
915 {
916     if ( m_wxPeer->MacIsUserPane() )
917         return m_wxPeer->AcceptsFocus();
918     else
919     {
920         wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
921         return superimpl(slf, (SEL)_cmd);
922     }
923 }
924
925 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
926 {
927     wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
928     // get the current focus before running becomeFirstResponder
929     NSView* otherView = FindFocus();
930
931     wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
932     BOOL r = superimpl(slf, (SEL)_cmd);
933     if ( r )
934     {
935         DoNotifyFocusEvent( true, otherWindow );
936     }
937
938     return r;
939 }
940
941 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
942 {
943     wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
944     BOOL r = superimpl(slf, (SEL)_cmd);
945     // get the current focus after running resignFirstResponder
946     // note that this value isn't reliable, it might return the same view that
947     // is resigning
948     NSView* otherView = FindFocus();
949     wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
950     // NSTextViews have an editor as true responder, therefore the might get the
951     // resign notification if their editor takes over, don't trigger any event then
952     if ( r && !m_hasEditor)
953     {
954         DoNotifyFocusEvent( false, otherWindow );
955     }
956     return r;
957 }
958
959 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
960 {
961     wxWindow* wxpeer = GetWXPeer();
962     if ( wxpeer )
963     {
964         NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
965         if (cursor == NULL)
966         {
967             wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
968             superimpl(slf, (SEL)_cmd);
969         }
970         else
971         {
972             [slf addCursorRect: [slf bounds]
973                 cursor: cursor];
974         }
975     }
976 }
977
978 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
979 {
980     return m_isFlipped;
981 }
982
983
984 #define OSX_DEBUG_DRAWING 0
985
986 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
987 {
988     CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
989     CGContextSaveGState( context );
990
991 #if OSX_DEBUG_DRAWING
992     CGContextBeginPath( context );
993     CGContextMoveToPoint(context, 0, 0);
994     NSRect bounds = [self bounds];
995     CGContextAddLineToPoint(context, 10, 0);
996     CGContextMoveToPoint(context, 0, 0);
997     CGContextAddLineToPoint(context, 0, 10);
998     CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
999     CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
1000     CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1001     CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
1002     CGContextClosePath( context );
1003     CGContextStrokePath(context);
1004 #endif
1005
1006     if ( !m_isFlipped )
1007     {
1008         CGContextTranslateCTM( context, 0,  [m_osxView bounds].size.height );
1009         CGContextScaleCTM( context, 1, -1 );
1010     }
1011
1012     wxRegion updateRgn;
1013     const NSRect *rects;
1014     NSInteger count;
1015
1016     [slf getRectsBeingDrawn:&rects count:&count];
1017     for ( int i = 0 ; i < count ; ++i )
1018     {
1019         updateRgn.Union(wxFromNSRect(slf, rects[i]) );
1020     }
1021
1022     wxWindow* wxpeer = GetWXPeer();
1023     wxpeer->GetUpdateRegion() = updateRgn;
1024     wxpeer->MacSetCGContextRef( context );
1025
1026     bool handled = wxpeer->MacDoRedraw( 0 );
1027
1028     CGContextRestoreGState( context );
1029
1030     CGContextSaveGState( context );
1031     if ( !handled )
1032     {
1033         // call super
1034         SEL _cmd = @selector(drawRect:);
1035         wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1036         superimpl(slf, _cmd, *(NSRect*)rect);
1037         CGContextRestoreGState( context );
1038         CGContextSaveGState( context );
1039     }
1040     wxpeer->MacPaintChildrenBorders();
1041     wxpeer->MacSetCGContextRef( NULL );
1042     CGContextRestoreGState( context );
1043 }
1044
1045 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1046 {
1047     wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1048     if ( wxpeer )
1049         wxpeer->OSXHandleClicked(0);
1050 }
1051
1052 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1053 {
1054 }
1055
1056 //
1057
1058 #if OBJC_API_VERSION >= 2
1059
1060 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1061     class_addMethod(c, s, i, t );
1062
1063 #else
1064
1065 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1066     { s, t, i },
1067
1068 #endif
1069
1070 void wxOSXCocoaClassAddWXMethods(Class c)
1071 {
1072
1073 #if OBJC_API_VERSION < 2
1074     static objc_method wxmethods[] =
1075     {
1076 #endif
1077
1078     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1079     wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1080     wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1081
1082     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1083     wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1084     wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1085
1086     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1087
1088     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1089     wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1090     wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1091
1092     wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1093     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1094     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1095
1096     wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1097     wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1098     wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1099
1100     wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1101
1102     wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1103
1104     wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1105     wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1106     wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1107     wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
1108
1109     wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1110     wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1111
1112     wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1113     wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1114
1115 #if wxUSE_DRAG_AND_DROP
1116     wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1117     wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1118     wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1119     wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1120 #endif
1121
1122 #if OBJC_API_VERSION < 2
1123     } ;
1124     static int method_count = WXSIZEOF( wxmethods );
1125     static objc_method_list *wxmethodlist = NULL;
1126     if ( wxmethodlist == NULL )
1127     {
1128         wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1129         memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1130         wxmethodlist->method_count = method_count;
1131         wxmethodlist->obsolete = 0;
1132     }
1133     class_addMethods( c, wxmethodlist );
1134 #endif
1135 }
1136
1137 //
1138 // C++ implementation class
1139 //
1140
1141 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1142
1143 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1144     wxWidgetImpl( peer, isRootControl )
1145 {
1146     Init();
1147     m_osxView = w;
1148
1149     // check if the user wants to create the control initially hidden
1150     if ( !peer->IsShown() )
1151         SetVisibility(false);
1152
1153     // gc aware handling
1154     if ( m_osxView )
1155         CFRetain(m_osxView);
1156     [m_osxView release];
1157 }
1158
1159 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1160 {
1161     Init();
1162 }
1163
1164 void wxWidgetCocoaImpl::Init()
1165 {
1166     m_osxView = NULL;
1167     m_isFlipped = true;
1168     m_lastKeyDownEvent = NULL;
1169     m_hasEditor = false;
1170 }
1171
1172 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1173 {
1174     RemoveAssociations( this );
1175
1176     if ( !IsRootControl() )
1177     {
1178         NSView *sv = [m_osxView superview];
1179         if ( sv != nil )
1180             [m_osxView removeFromSuperview];
1181     }
1182     // gc aware handling
1183     if ( m_osxView )
1184         CFRelease(m_osxView);
1185 }
1186
1187 bool wxWidgetCocoaImpl::IsVisible() const
1188 {
1189     return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1190 }
1191
1192 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1193 {
1194     [m_osxView setHidden:(visible ? NO:YES)];
1195 }
1196
1197 // ----------------------------------------------------------------------------
1198 // window animation stuff
1199 // ----------------------------------------------------------------------------
1200
1201 // define a delegate used to refresh the window during animation
1202 @interface wxNSAnimationDelegate : NSObject
1203 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1204                                    <NSAnimationDelegate>
1205 #endif
1206 {
1207     wxWindow *m_win;
1208     bool m_isDone;
1209 }
1210
1211 - (id)init:(wxWindow *)win;
1212
1213 - (bool)isDone;
1214
1215 // NSAnimationDelegate methods
1216 - (void)animationDidEnd:(NSAnimation*)animation;
1217 - (void)animation:(NSAnimation*)animation
1218         didReachProgressMark:(NSAnimationProgress)progress;
1219 @end
1220
1221 @implementation wxNSAnimationDelegate
1222
1223 - (id)init:(wxWindow *)win
1224 {
1225     [super init];
1226
1227     m_win = win;
1228     m_isDone = false;
1229
1230     return self;
1231 }
1232
1233 - (bool)isDone
1234 {
1235     return m_isDone;
1236 }
1237
1238 - (void)animation:(NSAnimation*)animation
1239         didReachProgressMark:(NSAnimationProgress)progress
1240 {
1241     wxUnusedVar(animation);
1242     wxUnusedVar(progress);
1243
1244     m_win->SendSizeEvent();
1245 }
1246
1247 - (void)animationDidEnd:(NSAnimation*)animation
1248 {
1249     m_isDone = true;
1250 }
1251
1252 @end
1253
1254 /* static */
1255 bool
1256 wxWidgetCocoaImpl::ShowViewOrWindowWithEffect(wxWindow *win,
1257                                               bool show,
1258                                               wxShowEffect effect,
1259                                               unsigned timeout)
1260 {
1261     // create the dictionary describing the animation to perform on this view
1262     NSObject * const
1263         viewOrWin = static_cast<NSObject *>(win->OSXGetViewOrWindow());
1264     NSMutableDictionary * const
1265         dict = [NSMutableDictionary dictionaryWithCapacity:4];
1266     [dict setObject:viewOrWin forKey:NSViewAnimationTargetKey];
1267
1268     // determine the start and end rectangles assuming we're hiding the window
1269     const wxRect rectOrig = win->GetRect();
1270     wxRect rectStart,
1271            rectEnd;
1272     rectStart =
1273     rectEnd = rectOrig;
1274
1275     if ( show )
1276     {
1277         if ( effect == wxSHOW_EFFECT_ROLL_TO_LEFT ||
1278                 effect == wxSHOW_EFFECT_SLIDE_TO_LEFT )
1279             effect = wxSHOW_EFFECT_ROLL_TO_RIGHT;
1280         else if ( effect == wxSHOW_EFFECT_ROLL_TO_RIGHT ||
1281                     effect == wxSHOW_EFFECT_SLIDE_TO_RIGHT )
1282             effect = wxSHOW_EFFECT_ROLL_TO_LEFT;
1283         else if ( effect == wxSHOW_EFFECT_ROLL_TO_TOP ||
1284                     effect == wxSHOW_EFFECT_SLIDE_TO_TOP )
1285             effect = wxSHOW_EFFECT_ROLL_TO_BOTTOM;
1286         else if ( effect == wxSHOW_EFFECT_ROLL_TO_BOTTOM ||
1287                     effect == wxSHOW_EFFECT_SLIDE_TO_BOTTOM )
1288             effect = wxSHOW_EFFECT_ROLL_TO_TOP;
1289     }
1290
1291     switch ( effect )
1292     {
1293         case wxSHOW_EFFECT_ROLL_TO_LEFT:
1294         case wxSHOW_EFFECT_SLIDE_TO_LEFT:
1295             rectEnd.width = 0;
1296             break;
1297
1298         case wxSHOW_EFFECT_ROLL_TO_RIGHT:
1299         case wxSHOW_EFFECT_SLIDE_TO_RIGHT:
1300             rectEnd.x = rectStart.GetRight();
1301             rectEnd.width = 0;
1302             break;
1303
1304         case wxSHOW_EFFECT_ROLL_TO_TOP:
1305         case wxSHOW_EFFECT_SLIDE_TO_TOP:
1306             rectEnd.height = 0;
1307             break;
1308
1309         case wxSHOW_EFFECT_ROLL_TO_BOTTOM:
1310         case wxSHOW_EFFECT_SLIDE_TO_BOTTOM:
1311             rectEnd.y = rectStart.GetBottom();
1312             rectEnd.height = 0;
1313             break;
1314
1315         case wxSHOW_EFFECT_EXPAND:
1316             rectEnd.x = rectStart.x + rectStart.width / 2;
1317             rectEnd.y = rectStart.y + rectStart.height / 2;
1318             rectEnd.width =
1319             rectEnd.height = 0;
1320             break;
1321
1322         case wxSHOW_EFFECT_BLEND:
1323             [dict setObject:(show ? NSViewAnimationFadeInEffect
1324                                   : NSViewAnimationFadeOutEffect)
1325                   forKey:NSViewAnimationEffectKey];
1326             break;
1327
1328         case wxSHOW_EFFECT_NONE:
1329         case wxSHOW_EFFECT_MAX:
1330             wxFAIL_MSG( "unexpected animation effect" );
1331             return false;
1332
1333         default:
1334             wxFAIL_MSG( "unknown animation effect" );
1335             return false;
1336     };
1337
1338     if ( show )
1339     {
1340         // we need to restore it to the original rectangle instead of making it
1341         // disappear
1342         wxSwap(rectStart, rectEnd);
1343
1344         // and as the window is currently hidden, we need to show it for the
1345         // animation to be visible at all (but don't restore it at its full
1346         // rectangle as it shouldn't appear immediately)
1347         win->SetSize(rectStart);
1348         win->Show();
1349     }
1350
1351     NSView * const parentView = [viewOrWin isKindOfClass:[NSView class]]
1352                                     ? [(NSView *)viewOrWin superview]
1353                                     : nil;
1354     const NSRect rStart = wxToNSRect(parentView, rectStart);
1355     const NSRect rEnd = wxToNSRect(parentView, rectEnd);
1356
1357     [dict setObject:[NSValue valueWithRect:rStart]
1358           forKey:NSViewAnimationStartFrameKey];
1359     [dict setObject:[NSValue valueWithRect:rEnd]
1360           forKey:NSViewAnimationEndFrameKey];
1361
1362     // create an animation using the values in the above dictionary
1363     NSViewAnimation * const
1364         anim = [[NSViewAnimation alloc]
1365                 initWithViewAnimations:[NSArray arrayWithObject:dict]];
1366
1367     if ( !timeout )
1368     {
1369         // what is a good default duration? Windows uses 200ms, Web frameworks
1370         // use anything from 250ms to 1s... choose something in the middle
1371         timeout = 500;
1372     }
1373
1374     [anim setDuration:timeout/1000.];   // duration is in seconds here
1375
1376     // if the window being animated changes its layout depending on its size
1377     // (which is almost always the case) we need to redo it during animation
1378     //
1379     // the number of layouts here is arbitrary, but 10 seems like too few (e.g.
1380     // controls in wxInfoBar visibly jump around)
1381     const int NUM_LAYOUTS = 20;
1382     for ( float f = 1./NUM_LAYOUTS; f < 1.; f += 1./NUM_LAYOUTS )
1383         [anim addProgressMark:f];
1384
1385     wxNSAnimationDelegate * const
1386         animDelegate = [[wxNSAnimationDelegate alloc] init:win];
1387     [anim setDelegate:animDelegate];
1388     [anim startAnimation];
1389
1390     // Cocoa is capable of doing animation asynchronously or even from separate
1391     // thread but wx API doesn't provide any way to be notified about the
1392     // animation end and without this we really must ensure that the window has
1393     // the expected (i.e. the same as if a simple Show() had been used) size
1394     // when we return, so block here until the animation finishes
1395     //
1396     // notice that because the default animation mode is NSAnimationBlocking,
1397     // no user input events ought to be processed from here
1398     wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
1399     while ( ![animDelegate isDone] )
1400         loop->Dispatch();
1401
1402     if ( !show )
1403     {
1404         // NSViewAnimation is smart enough to hide the NSView being animated at
1405         // the end but we also must ensure that it's hidden for wx too
1406         win->Hide();
1407
1408         // and we must also restore its size because it isn't expected to
1409         // change just because the window was hidden
1410         win->SetSize(rectOrig);
1411     }
1412     else
1413     {
1414         // refresh it once again after the end to ensure that everything is in
1415         // place
1416         win->SendSizeEvent();
1417     }
1418
1419     [anim setDelegate:nil];
1420     [animDelegate release];
1421     [anim release];
1422
1423     return true;
1424 }
1425
1426 bool wxWidgetCocoaImpl::ShowWithEffect(bool show,
1427                                        wxShowEffect effect,
1428                                        unsigned timeout)
1429 {
1430     return ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
1431 }
1432
1433 void wxWidgetCocoaImpl::Raise()
1434 {
1435     // Not implemented
1436 }
1437
1438 void wxWidgetCocoaImpl::Lower()
1439 {
1440     // Not implemented
1441 }
1442
1443 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1444 {
1445 #if 1
1446     SetNeedsDisplay() ;
1447 #else
1448     // We should do something like this, but it wasn't working in 10.4.
1449     if (GetNeedsDisplay() )
1450     {
1451         SetNeedsDisplay() ;
1452     }
1453     NSRect r = wxToNSRect( [m_osxView superview], *rect );
1454     NSSize offset = NSMakeSize((float)dx, (float)dy);
1455     [m_osxView scrollRect:r by:offset];
1456 #endif
1457 }
1458
1459 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1460 {
1461     wxWindowMac* parent = GetWXPeer()->GetParent();
1462     // under Cocoa we might have a contentView in the wxParent to which we have to
1463     // adjust the coordinates
1464     if (parent && [m_osxView superview] != parent->GetHandle() )
1465     {
1466         int cx = 0,cy = 0,cw = 0,ch = 0;
1467         if ( parent->GetPeer() )
1468         {
1469             parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1470             x -= cx;
1471             y -= cy;
1472         }
1473     }
1474     [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1475     NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1476     [m_osxView setFrame:r];
1477     [[m_osxView superview] setNeedsDisplayInRect:r];
1478
1479     if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1480     {
1481         if ( [(wxNSView*)m_osxView trackingTag] )
1482             [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1483
1484         [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1485     }
1486 }
1487
1488 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1489 {
1490     wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1491     x = r.GetLeft();
1492     y = r.GetTop();
1493 }
1494
1495 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1496 {
1497     NSRect rect = [m_osxView frame];
1498     width = (int)rect.size.width;
1499     height = (int)rect.size.height;
1500 }
1501
1502 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1503 {
1504     if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1505     {
1506         NSView* cv = [m_osxView contentView];
1507
1508         NSRect bounds = [m_osxView bounds];
1509         NSRect rect = [cv frame];
1510
1511         int y = (int)rect.origin.y;
1512         int x = (int)rect.origin.x;
1513         if ( ![ m_osxView isFlipped ] )
1514             y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
1515         left = x;
1516         top = y;
1517         width = (int)rect.size.width;
1518         height = (int)rect.size.height;
1519     }
1520     else
1521     {
1522         left = top = 0;
1523         GetSize( width, height );
1524     }
1525 }
1526
1527 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1528 {
1529     if ( where )
1530         [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1531     else
1532         [m_osxView setNeedsDisplay:YES];
1533 }
1534
1535 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1536 {
1537     return [m_osxView needsDisplay];
1538 }
1539
1540 bool wxWidgetCocoaImpl::CanFocus() const
1541 {
1542     return [m_osxView canBecomeKeyView] == YES;
1543 }
1544
1545 bool wxWidgetCocoaImpl::HasFocus() const
1546 {
1547     return ( FindFocus() == m_osxView );
1548 }
1549
1550 bool wxWidgetCocoaImpl::SetFocus()
1551 {
1552     if ( [m_osxView canBecomeKeyView] == NO )
1553         return false;
1554
1555     [[m_osxView window] makeFirstResponder: m_osxView] ;
1556     [[m_osxView window] makeKeyAndOrderFront:nil] ;
1557     return true;
1558 }
1559
1560
1561 void wxWidgetCocoaImpl::RemoveFromParent()
1562 {
1563     [m_osxView removeFromSuperview];
1564 }
1565
1566 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1567 {
1568     NSView* container = parent->GetWXWidget() ;
1569     wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1570     [container addSubview:m_osxView];
1571 }
1572
1573 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &col )
1574 {
1575     NSView* targetView = m_osxView;
1576     if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1577         targetView = [(NSScrollView*) m_osxView documentView];
1578
1579     if ( [targetView respondsToSelector:@selector(setBackgroundColor:) ] )
1580     {
1581         [targetView setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
1582                                                                 green:(CGFloat) (col.Green() / 255.0)
1583                                                                  blue:(CGFloat) (col.Blue() / 255.0)
1584                                                                 alpha:(CGFloat) (col.Alpha() / 255.0)]];
1585     }
1586 }
1587
1588 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1589 {
1590     if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1591     {
1592         wxCFStringRef cf( title , encoding );
1593         [m_osxView setTitle:cf.AsNSString()];
1594     }
1595     else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1596     {
1597         wxCFStringRef cf( title , encoding );
1598         [m_osxView setStringValue:cf.AsNSString()];
1599     }
1600 }
1601
1602
1603 void  wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1604 {
1605     NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1606     p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1607     *pt = wxFromNSPoint( to->GetWXWidget(), p );
1608 }
1609
1610 wxInt32 wxWidgetCocoaImpl::GetValue() const
1611 {
1612     return [(NSControl*)m_osxView intValue];
1613 }
1614
1615 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1616 {
1617     if (  [m_osxView respondsToSelector:@selector(setIntValue:)] )
1618     {
1619         [m_osxView setIntValue:v];
1620     }
1621     else if (  [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1622     {
1623         [m_osxView setFloatValue:(double)v];
1624     }
1625     else if (  [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1626     {
1627         [m_osxView setDoubleValue:(double)v];
1628     }
1629 }
1630
1631 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1632 {
1633     if (  [m_osxView respondsToSelector:@selector(setMinValue:)] )
1634     {
1635         [m_osxView setMinValue:(double)v];
1636     }
1637 }
1638
1639 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1640 {
1641     if (  [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1642     {
1643         [m_osxView setMaxValue:(double)v];
1644     }
1645 }
1646
1647 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1648 {
1649     if (  [m_osxView respondsToSelector:@selector(getMinValue:)] )
1650     {
1651         return (int)[m_osxView minValue];
1652     }
1653     return 0;
1654 }
1655
1656 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1657 {
1658     if (  [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1659     {
1660         return (int)[m_osxView maxValue];
1661     }
1662     return 0;
1663 }
1664
1665 wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1666 {
1667     wxBitmap bmp;
1668
1669     // TODO: how to create a wxBitmap from NSImage?
1670 #if 0
1671     if ( [m_osxView respondsToSelector:@selector(image:)] )
1672         bmp = [m_osxView image];
1673 #endif
1674
1675     return bmp;
1676 }
1677
1678 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1679 {
1680     if (  [m_osxView respondsToSelector:@selector(setImage:)] )
1681     {
1682         [m_osxView setImage:bitmap.GetNSImage()];
1683     }
1684 }
1685
1686 void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1687 {
1688     if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1689     {
1690         NSCellImagePosition pos;
1691         switch ( dir )
1692         {
1693             case wxLEFT:
1694                 pos = NSImageLeft;
1695                 break;
1696
1697             case wxRIGHT:
1698                 pos = NSImageRight;
1699                 break;
1700
1701             case wxTOP:
1702                 pos = NSImageAbove;
1703                 break;
1704
1705             case wxBOTTOM:
1706                 pos = NSImageBelow;
1707                 break;
1708
1709             default:
1710                 wxFAIL_MSG( "invalid image position" );
1711                 pos = NSNoImage;
1712         }
1713
1714         [m_osxView setImagePosition:pos];
1715     }
1716 }
1717
1718 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1719 {
1720     // implementation in subclass
1721 }
1722
1723 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1724 {
1725     r->x = r->y = r->width = r->height = 0;
1726
1727     if (  [m_osxView respondsToSelector:@selector(sizeToFit)] )
1728     {
1729         NSRect former = [m_osxView frame];
1730         [m_osxView sizeToFit];
1731         NSRect best = [m_osxView frame];
1732         [m_osxView setFrame:former];
1733         r->width = (int)best.size.width;
1734         r->height = (int)best.size.height;
1735     }
1736 }
1737
1738 bool wxWidgetCocoaImpl::IsEnabled() const
1739 {
1740     if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1741         return [m_osxView isEnabled];
1742     return true;
1743 }
1744
1745 void wxWidgetCocoaImpl::Enable( bool enable )
1746 {
1747     if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1748         [m_osxView setEnabled:enable];
1749 }
1750
1751 void wxWidgetCocoaImpl::PulseGauge()
1752 {
1753 }
1754
1755 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1756 {
1757 }
1758
1759 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1760 {
1761     NSControlSize size = NSRegularControlSize;
1762
1763     switch ( variant )
1764     {
1765         case wxWINDOW_VARIANT_NORMAL :
1766             size = NSRegularControlSize;
1767             break ;
1768
1769         case wxWINDOW_VARIANT_SMALL :
1770             size = NSSmallControlSize;
1771             break ;
1772
1773         case wxWINDOW_VARIANT_MINI :
1774             size = NSMiniControlSize;
1775             break ;
1776
1777         case wxWINDOW_VARIANT_LARGE :
1778             size = NSRegularControlSize;
1779             break ;
1780
1781         default:
1782             wxFAIL_MSG(wxT("unexpected window variant"));
1783             break ;
1784     }
1785     if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1786         [m_osxView setControlSize:size];
1787     else if ([m_osxView respondsToSelector:@selector(cell)])
1788     {
1789         id cell = [(id)m_osxView cell];
1790         if ([cell respondsToSelector:@selector(setControlSize:)])
1791             [cell setControlSize:size];
1792     }
1793 }
1794
1795 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1796 {
1797     if ([m_osxView respondsToSelector:@selector(setFont:)])
1798         [m_osxView setFont: font.OSXGetNSFont()];
1799 }
1800
1801 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1802 {
1803     WXWidget c =  control ? control : (WXWidget) m_osxView;
1804     wxWidgetImpl::Associate( c, this ) ;
1805     if ([c respondsToSelector:@selector(setAction:)])
1806     {
1807         [c setTarget: c];
1808         [c setAction: @selector(controlAction:)];
1809         if ([c respondsToSelector:@selector(setDoubleAction:)])
1810         {
1811             [c setDoubleAction: @selector(controlDoubleAction:)];
1812         }
1813
1814     }
1815 }
1816
1817 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1818 {
1819     wxKeyEvent wxevent(wxEVT_CHAR);
1820     SetupKeyEvent( wxevent, event, text );
1821
1822     return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1823 }
1824
1825 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1826 {
1827     wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1828     SetupKeyEvent( wxevent, event );
1829     bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1830
1831     // this will fire higher level events, like insertText, to help
1832     // us handle EVT_CHAR, etc.
1833
1834     if ( m_wxPeer->MacIsUserPane() && [event type] == NSKeyDown)
1835     {
1836         if ( !result )
1837         {
1838             if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1839                 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1840             else
1841                 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1842             result = true;
1843         }
1844     }
1845
1846     return result;
1847 }
1848
1849 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1850 {
1851     NSPoint clickLocation;
1852     clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1853     wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1854     wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1855     SetupMouseEvent(wxevent , event) ;
1856     wxevent.m_x = pt.x;
1857     wxevent.m_y = pt.y;
1858
1859     return GetWXPeer()->HandleWindowEvent(wxevent);
1860 }
1861
1862 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1863 {
1864     wxWindow* thisWindow = GetWXPeer();
1865     if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1866     {
1867         thisWindow->MacInvalidateBorders();
1868     }
1869
1870     if ( receivedFocus )
1871     {
1872         wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
1873         wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1874         thisWindow->HandleWindowEvent(eventFocus);
1875
1876 #if wxUSE_CARET
1877         if ( thisWindow->GetCaret() )
1878             thisWindow->GetCaret()->OnSetFocus();
1879 #endif
1880
1881         wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1882         event.SetEventObject(thisWindow);
1883         if (otherWindow)
1884             event.SetWindow(otherWindow->GetWXPeer());
1885         thisWindow->HandleWindowEvent(event) ;
1886     }
1887     else // !receivedFocuss
1888     {
1889 #if wxUSE_CARET
1890         if ( thisWindow->GetCaret() )
1891             thisWindow->GetCaret()->OnKillFocus();
1892 #endif
1893
1894         wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
1895
1896         wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1897         event.SetEventObject(thisWindow);
1898         if (otherWindow)
1899             event.SetWindow(otherWindow->GetWXPeer());
1900         thisWindow->HandleWindowEvent(event) ;
1901     }
1902 }
1903
1904 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1905 {
1906     NSPoint location = [NSEvent mouseLocation];
1907     location = [[m_osxView window] convertScreenToBase:location];
1908     NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1909
1910     if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1911     {
1912         [(NSCursor*)cursor.GetHCURSOR() set];
1913     }
1914     [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1915 }
1916
1917 void wxWidgetCocoaImpl::CaptureMouse()
1918 {
1919     [[m_osxView window] disableCursorRects];
1920 }
1921
1922 void wxWidgetCocoaImpl::ReleaseMouse()
1923 {
1924     [[m_osxView window] enableCursorRects];
1925 }
1926
1927 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1928 {
1929     m_isFlipped = flipped;
1930 }
1931
1932 //
1933 // Factory methods
1934 //
1935
1936 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
1937     wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
1938     long WXUNUSED(style), long WXUNUSED(extraStyle))
1939 {
1940     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1941     wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1942
1943     // temporary hook for dnd
1944     [v registerForDraggedTypes:[NSArray arrayWithObjects:
1945         NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1946
1947     wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1948     return c;
1949 }
1950
1951 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1952 {
1953     NSWindow* tlw = now->GetWXWindow();
1954     wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1955     wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1956     c->InstallEventHandler();
1957     [tlw setContentView:v];
1958     return c;
1959 }