clickCount is not available for mouse entered/exited events also, just use respondsTo...
[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 #if wxUSE_CARET
25     #include "wx/caret.h"
26 #endif
27
28 #if wxUSE_DRAG_AND_DROP
29     #include "wx/dnd.h"
30 #endif
31
32 #include <objc/objc-runtime.h>
33
34 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
35 {
36     int x, y, w, h ;
37
38     window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
39     wxRect bounds(x,y,w,h);
40     NSView* sv = (window->GetParent()->GetHandle() );
41
42     return wxToNSRect( sv, bounds );
43 }
44
45 @interface wxNSView : NSView
46 {
47 }
48
49 - (BOOL) canBecomeKeyView;
50
51 @end // wxNSView
52
53 @interface NSView(PossibleMethods) 
54 - (void)setTitle:(NSString *)aString;
55 - (void)setStringValue:(NSString *)aString;
56 - (void)setIntValue:(int)anInt;
57 - (void)setFloatValue:(float)aFloat;
58 - (void)setDoubleValue:(double)aDouble;
59
60 - (double)minValue;
61 - (double)maxValue;
62 - (void)setMinValue:(double)aDouble;
63 - (void)setMaxValue:(double)aDouble;
64
65 - (void)sizeToFit;
66
67 - (BOOL)isEnabled;
68 - (void)setEnabled:(BOOL)flag;
69
70 - (void)setImage:(NSImage *)image;
71 - (void)setControlSize:(NSControlSize)size;
72
73 - (id)contentView;
74
75 - (void)setTarget:(id)anObject;
76 - (void)setAction:(SEL)aSelector;
77 - (void)setDoubleAction:(SEL)aSelector;
78 @end 
79
80 long wxOSXTranslateCocoaKey( int unichar )
81 {
82     long retval = unichar;
83     switch( unichar )
84     {
85         case NSUpArrowFunctionKey :
86             retval = WXK_UP;
87             break;
88         case NSDownArrowFunctionKey :
89             retval = WXK_DOWN;
90             break;
91         case NSLeftArrowFunctionKey :
92             retval = WXK_LEFT;
93             break;
94         case NSRightArrowFunctionKey :
95             retval = WXK_RIGHT;
96             break;
97         case NSInsertFunctionKey  :
98             retval = WXK_INSERT;
99             break;
100         case NSDeleteFunctionKey  :
101             retval = WXK_DELETE;
102             break;
103         case NSHomeFunctionKey  :
104             retval = WXK_HOME;
105             break;
106 //        case NSBeginFunctionKey  :
107 //            retval = WXK_BEGIN;
108 //            break;
109         case NSEndFunctionKey  :
110             retval = WXK_END;
111             break;
112         case NSPageUpFunctionKey  :
113             retval = WXK_PAGEUP;
114             break;
115        case NSPageDownFunctionKey  :
116             retval = WXK_PAGEDOWN;
117             break;
118        case NSHelpFunctionKey  :
119             retval = WXK_HELP;
120             break;
121
122         default :
123             if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
124                 retval = WXK_F1 + (unichar - NSF1FunctionKey );
125             break;
126     }
127     return retval;
128 }
129
130 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
131 {
132     UInt32 modifiers = [nsEvent modifierFlags] ;
133     int eventType = [nsEvent type];
134
135     wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
136     wxevent.m_controlDown = modifiers & NSControlKeyMask;
137     wxevent.m_altDown = modifiers & NSAlternateKeyMask;
138     wxevent.m_metaDown = modifiers & NSCommandKeyMask;
139     
140     wxevent.m_rawCode = [nsEvent keyCode];
141     wxevent.m_rawFlags = modifiers;
142     
143     wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
144     switch (eventType)
145     {
146         case NSKeyDown :
147             wxevent.SetEventType( wxEVT_KEY_DOWN )  ;
148             break;
149         case NSKeyUp :
150             wxevent.SetEventType( wxEVT_KEY_UP )  ;
151             break;
152         case NSFlagsChanged :
153             // setup common code here
154             break;
155         default :
156             break ;
157     }
158
159     wxString chars;
160     if ( eventType != NSFlagsChanged )
161     {
162         NSString* nschars = [nsEvent characters];
163         if ( nschars )
164         {
165             wxCFStringRef cfchars((CFStringRef)[nschars retain]);
166             chars = cfchars.AsString();
167         }
168     }
169     
170     int unichar = chars.Length() > 0 ? chars[0] : 0;
171     long keyval = wxOSXTranslateCocoaKey(unichar) ;
172     if ( keyval == unichar && ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) )
173         keyval = wxToupper( keyval ) ;
174
175 #if wxUSE_UNICODE
176     wxevent.m_uniChar = unichar;
177 #endif
178     wxevent.m_keyCode = keyval;
179 }
180
181 UInt32 g_lastButton = 0 ;
182 bool g_lastButtonWasFakeRight = false ;
183
184 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
185 {
186     int eventType = [nsEvent type];
187     UInt32 modifiers = [nsEvent modifierFlags] ;
188     wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
189
190     // these parameters are not given for all events
191     UInt32 button = [nsEvent buttonNumber];
192     UInt32 clickCount = 0;
193     if ( [nsEvent respondsToSelector:@selector(clickCount:)] ) 
194         [nsEvent clickCount];
195
196     wxevent.m_x = screenMouseLocation.x;
197     wxevent.m_y = screenMouseLocation.y;
198     wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
199     wxevent.m_controlDown = modifiers & NSControlKeyMask;
200     wxevent.m_altDown = modifiers & NSAlternateKeyMask;
201     wxevent.m_metaDown = modifiers & NSCommandKeyMask;
202     wxevent.m_clickCount = clickCount;
203     wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
204
205     UInt32 mouseChord = 0; 
206
207     switch (eventType)
208     {
209         case NSLeftMouseDown :
210         case NSLeftMouseDragged :
211             mouseChord = 1U;
212             break;
213         case NSRightMouseDown :
214         case NSRightMouseDragged :
215             mouseChord = 2U;
216             break;
217         case NSOtherMouseDown :
218         case NSOtherMouseDragged :
219             mouseChord = 4U;
220             break;
221     }
222
223     // a control click is interpreted as a right click
224     bool thisButtonIsFakeRight = false ;
225     if ( button == 0 && (modifiers & NSControlKeyMask) )
226     {
227         button = 1 ;
228         thisButtonIsFakeRight = true ;
229     }
230
231     // otherwise we report double clicks by connecting a left click with a ctrl-left click
232     if ( clickCount > 1 && button != g_lastButton )
233         clickCount = 1 ;
234
235     // we must make sure that our synthetic 'right' button corresponds in
236     // mouse down, moved and mouse up, and does not deliver a right down and left up
237     switch (eventType)
238     {
239         case NSLeftMouseDown :
240         case NSRightMouseDown :
241         case NSOtherMouseDown :
242             g_lastButton = button ;
243             g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
244             break;
245      }
246
247     if ( button == 0 )
248     {
249         g_lastButton = 0 ;
250         g_lastButtonWasFakeRight = false ;
251     }
252     else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
253         button = g_lastButton ;
254
255     // Adjust the chord mask to remove the primary button and add the
256     // secondary button.  It is possible that the secondary button is
257     // already pressed, e.g. on a mouse connected to a laptop, but this
258     // possibility is ignored here:
259     if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
260         mouseChord = ((mouseChord & ~1U) | 2U);
261
262     if(mouseChord & 1U)
263                 wxevent.m_leftDown = true ;
264     if(mouseChord & 2U)
265                 wxevent.m_rightDown = true ;
266     if(mouseChord & 4U)
267                 wxevent.m_middleDown = true ;
268
269     // translate into wx types
270     switch (eventType)
271     {
272         case NSLeftMouseDown :
273         case NSRightMouseDown :
274         case NSOtherMouseDown :
275             switch ( button )
276             {
277                 case 0 :
278                     wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN )  ;
279                     break ;
280
281                 case 1 :
282                     wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
283                     break ;
284
285                 case 2 :
286                     wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
287                     break ;
288
289                 default:
290                     break ;
291             }
292             break ;
293
294         case NSLeftMouseUp :
295         case NSRightMouseUp :
296         case NSOtherMouseUp :
297             switch ( button )
298             {
299                 case 0 :
300                     wxevent.SetEventType( wxEVT_LEFT_UP )  ;
301                     break ;
302
303                 case 1 :
304                     wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
305                     break ;
306
307                 case 2 :
308                     wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
309                     break ;
310
311                 default:
312                     break ;
313             }
314             break ;
315
316      case NSScrollWheel :
317         {
318             wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
319             wxevent.m_wheelDelta = 10;
320             wxevent.m_linesPerAction = 1;
321             NSLog(@"deltaX %f, deltaY %f",[nsEvent deltaX], [nsEvent deltaY]);
322             if ( abs([nsEvent deltaX]) > abs([nsEvent deltaY]) )
323             {
324                 wxevent.m_wheelAxis = 1;
325                 wxevent.m_wheelRotation = [nsEvent deltaX] * 10.0;
326             }
327             else
328             {
329                 wxevent.m_wheelRotation = [nsEvent deltaY] * 10.0;
330             }
331         }
332         break ;
333
334         case NSMouseEntered :
335             wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
336             break;
337         case NSMouseExited :
338             wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
339             break;
340         case NSLeftMouseDragged :
341         case NSRightMouseDragged :
342         case NSOtherMouseDragged :
343         case NSMouseMoved :
344             wxevent.SetEventType( wxEVT_MOTION ) ;
345             break;
346         default :
347             break ;
348     }
349 }
350
351 @implementation wxNSView
352
353 + (void)initialize
354 {
355     static BOOL initialized = NO;
356     if (!initialized) 
357     {
358         initialized = YES;
359         wxOSXCocoaClassAddWXMethods( self );
360     }
361 }
362
363 - (BOOL) canBecomeKeyView
364 {
365     return YES;
366 }
367
368 @end // wxNSView
369
370 //
371 // event handlers
372 //
373
374 #if wxUSE_DRAG_AND_DROP
375
376 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
377 // for details on the NSPasteboard -> PasteboardRef conversion
378
379 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
380 {
381     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
382     if (impl == NULL)
383         return NSDragOperationNone;
384         
385     return impl->draggingEntered(sender, self, _cmd);
386 }
387
388 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
389 {
390     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
391     if (impl == NULL)
392         return ;
393         
394     return impl->draggingExited(sender, self, _cmd);
395 }
396
397 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
398 {
399     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
400     if (impl == NULL)
401         return NSDragOperationNone;
402         
403     return impl->draggingUpdated(sender, self, _cmd);
404 }
405
406 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
407 {
408     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
409     if (impl == NULL)
410         return NSDragOperationNone;
411         
412     return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
413 }
414
415 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event) 
416 {
417     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
418     if (impl == NULL)
419         return;
420         
421     impl->mouseEvent(event, self, _cmd);
422 }
423
424 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event) 
425 {
426     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
427     if (impl == NULL)
428         return;
429         
430     impl->keyEvent(event, self, _cmd);
431 }
432
433 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event) 
434 {
435     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
436     if (impl == NULL)
437         return NO;
438         
439     return impl->performKeyEquivalent(event, self, _cmd);
440 }
441
442 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
443 {
444     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
445     if (impl == NULL)
446         return NO;
447         
448     return impl->becomeFirstResponder(self, _cmd);
449 }
450
451 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
452 {
453     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
454     if (impl == NULL)
455         return NO;
456         
457     return impl->resignFirstResponder(self, _cmd);
458 }
459
460 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
461 {
462     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
463     if (impl == NULL)
464         return;
465         
466     impl->resetCursorRects(self, _cmd);
467 }
468
469 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
470 {
471     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
472     if (impl == NULL)
473         return NO;
474         
475     return impl->isFlipped(self, _cmd) ? YES:NO;
476 }
477
478 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
479 {
480     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
481     if (impl == NULL)
482         return;
483         
484     return impl->drawRect(&rect, self, _cmd);
485 }
486
487 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
488 {
489     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
490     if (impl == NULL)
491         return;
492         
493     impl->controlAction(self, _cmd, sender);
494 }
495
496 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
497 {
498     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
499     if (impl == NULL)
500         return;
501         
502     impl->controlDoubleAction(self, _cmd, sender);
503 }
504
505 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
506 {
507     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
508     NSPasteboard *pboard = [sender draggingPasteboard];
509     NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
510     
511     wxWindow* wxpeer = GetWXPeer();
512     if ( wxpeer == NULL )
513         return NSDragOperationNone;
514         
515     wxDropTarget* target = wxpeer->GetDropTarget();
516     if ( target == NULL )
517         return NSDragOperationNone;
518
519     wxDragResult result = wxDragNone;
520     wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
521
522     if ( sourceDragMask & NSDragOperationLink )
523         result = wxDragLink;
524     else if ( sourceDragMask & NSDragOperationCopy )
525         result = wxDragCopy;
526     else if ( sourceDragMask & NSDragOperationMove )
527         result = wxDragMove;
528
529     PasteboardRef pboardRef;    
530     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
531     target->SetCurrentDragPasteboard(pboardRef);
532     result = target->OnEnter(pt.x, pt.y, result);
533     CFRelease(pboardRef);
534      
535     NSDragOperation nsresult = NSDragOperationNone;
536     switch (result )
537     {
538         case wxDragLink:
539             nsresult = NSDragOperationLink;
540         case wxDragMove:
541             nsresult = NSDragOperationMove;
542         case wxDragCopy:
543             nsresult = NSDragOperationCopy;
544         default :
545             break;
546     }
547     return nsresult;
548 }
549
550 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
551 {
552     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
553     NSPasteboard *pboard = [sender draggingPasteboard];
554     
555     wxWindow* wxpeer = GetWXPeer();
556     if ( wxpeer == NULL )
557         return;
558         
559     wxDropTarget* target = wxpeer->GetDropTarget();
560     if ( target == NULL )
561         return;
562         
563     PasteboardRef pboardRef;    
564     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
565     target->SetCurrentDragPasteboard(pboardRef);
566     target->OnLeave();
567     CFRelease(pboardRef);
568  }
569
570 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
571 {
572     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
573     NSPasteboard *pboard = [sender draggingPasteboard];
574     NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
575         
576     wxWindow* wxpeer = GetWXPeer();
577     if ( wxpeer == NULL )
578         return NSDragOperationNone;
579         
580     wxDropTarget* target = wxpeer->GetDropTarget();
581     if ( target == NULL )
582         return NSDragOperationNone;
583
584     wxDragResult result = wxDragNone;
585     wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
586
587     if ( sourceDragMask & NSDragOperationLink )
588         result = wxDragLink;
589     else if ( sourceDragMask & NSDragOperationCopy )
590         result = wxDragCopy;
591     else if ( sourceDragMask & NSDragOperationMove )
592         result = wxDragMove;
593
594     PasteboardRef pboardRef;    
595     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
596     target->SetCurrentDragPasteboard(pboardRef);
597     result = target->OnDragOver(pt.x, pt.y, result);
598     CFRelease(pboardRef);
599      
600     NSDragOperation nsresult = NSDragOperationNone;
601     switch (result )
602     {
603         case wxDragLink:
604             nsresult = NSDragOperationLink;
605         case wxDragMove:
606             nsresult = NSDragOperationMove;
607         case wxDragCopy:
608             nsresult = NSDragOperationCopy;
609         default :
610             break;
611     }
612     return nsresult;
613 }
614
615 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
616 {
617     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
618
619     NSPasteboard *pboard = [sender draggingPasteboard];
620     NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
621     
622     wxWindow* wxpeer = GetWXPeer();
623     wxDropTarget* target = wxpeer->GetDropTarget();
624     wxDragResult result = wxDragNone;
625     wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
626
627     if ( sourceDragMask & NSDragOperationLink )
628         result = wxDragLink;
629     else if ( sourceDragMask & NSDragOperationCopy )
630         result = wxDragCopy;
631     else if ( sourceDragMask & NSDragOperationMove )
632         result = wxDragMove;
633
634     PasteboardRef pboardRef;    
635     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
636     target->SetCurrentDragPasteboard(pboardRef);
637     result = target->OnData(pt.x, pt.y, result);
638     CFRelease(pboardRef);
639      
640     return result != wxDragNone;
641 }
642
643 #endif
644
645 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
646 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
647 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
648 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
649 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
650
651 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
652 {
653     if ( !DoHandleMouseEvent(event) )
654     {
655         wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
656         superimpl(slf, (SEL)_cmd, event);
657     }
658 }
659
660 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
661 {
662     if ( !DoHandleKeyEvent(event) )
663     {
664         wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
665         superimpl(slf, (SEL)_cmd, event);
666     }
667 }
668
669 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
670 {
671     if ( !DoHandleKeyEvent(event) )
672     {
673         wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
674         return superimpl(slf, (SEL)_cmd, event);
675     }
676
677     return YES;
678 }
679
680 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
681 {
682     wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
683     BOOL r = superimpl(slf, (SEL)_cmd);
684     if ( r )
685         DoNotifyFocusEvent( true );
686     return r;
687 }
688
689 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
690 {
691     wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
692     BOOL r = superimpl(slf, (SEL)_cmd);
693     if ( r )
694         DoNotifyFocusEvent( false );
695     return r;
696 }
697
698 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
699 {
700     wxWindow* wxpeer = GetWXPeer();
701     if ( wxpeer )
702     {
703         NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
704         if (cursor == NULL)
705         {
706             wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
707             superimpl(slf, (SEL)_cmd);
708         }
709         else
710         {
711             [slf addCursorRect: [slf bounds]
712                 cursor: cursor];
713         }
714     }
715 }
716   
717 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
718 {
719     return m_isFlipped;
720 }
721
722
723 #define OSX_DEBUG_DRAWING 0
724
725 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
726 {
727     CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
728     CGContextSaveGState( context );
729     
730 #if OSX_DEBUG_DRAWING
731     CGContextBeginPath( context );
732     CGContextMoveToPoint(context, 0, 0);
733     NSRect bounds = [self bounds];
734     CGContextAddLineToPoint(context, 10, 0);
735     CGContextMoveToPoint(context, 0, 0);
736     CGContextAddLineToPoint(context, 0, 10);
737     CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
738     CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
739     CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
740     CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
741     CGContextClosePath( context );
742     CGContextStrokePath(context);
743 #endif
744
745     if ( !m_isFlipped )
746     {
747         CGContextTranslateCTM( context, 0,  [m_osxView bounds].size.height );
748         CGContextScaleCTM( context, 1, -1 );
749     }
750     
751     wxRegion updateRgn;
752     const NSRect *rects;
753     NSInteger count;
754
755     [slf getRectsBeingDrawn:&rects count:&count];
756     for ( int i = 0 ; i < count ; ++i )
757     {
758         updateRgn.Union(wxFromNSRect(slf, rects[i]) );
759     }
760
761     wxWindow* wxpeer = GetWXPeer();
762     wxpeer->GetUpdateRegion() = updateRgn;
763     wxpeer->MacSetCGContextRef( context );
764     
765     bool handled = wxpeer->MacDoRedraw( 0 );
766             
767     CGContextRestoreGState( context );
768
769     CGContextSaveGState( context );
770     if ( !handled )
771     {
772         // call super
773         SEL _cmd = @selector(drawRect:);
774         wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
775         superimpl(slf, _cmd, *(NSRect*)rect);
776         CGContextRestoreGState( context );
777         CGContextSaveGState( context );
778     }
779     wxpeer->MacPaintChildrenBorders();
780     wxpeer->MacSetCGContextRef( NULL );
781     CGContextRestoreGState( context );
782 }
783
784 void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender)
785 {
786     wxWindow* wxpeer = (wxWindow*) GetWXPeer();
787     if ( wxpeer )
788         wxpeer->OSXHandleClicked(0);
789 }
790
791 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender)
792 {
793 }
794
795 // 
796
797 #if OBJC_API_VERSION >= 2
798
799 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
800     class_addMethod(c, s, i, t );
801
802 #else
803
804 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
805     { s, t, i },
806
807 #endif
808
809 void wxOSXCocoaClassAddWXMethods(Class c)
810 {
811
812 #if OBJC_API_VERSION < 2
813     static objc_method wxmethods[] =
814     {
815 #endif
816
817     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
818     wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
819     wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
820
821     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
822     wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
823     wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
824
825     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
826
827     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
828     wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
829     wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
830
831     wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
832     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
833     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
834     
835     wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
836     wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
837     wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
838
839     wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
840
841
842     wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
843     wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
844     wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
845
846     wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
847     wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
848
849     wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
850     wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
851
852 #if wxUSE_DRAG_AND_DROP
853     wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
854     wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
855     wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
856     wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
857 #endif     
858         
859 #if OBJC_API_VERSION < 2
860     } ;
861     static int method_count = WXSIZEOF( wxmethods );
862     static objc_method_list *wxmethodlist = NULL;
863     if ( wxmethodlist == NULL )
864     {
865         wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
866         memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
867         wxmethodlist->method_count = method_count;
868         wxmethodlist->obsolete = 0;
869     }
870     class_addMethods( c, wxmethodlist );
871 #endif
872 }
873
874 //
875 // C++ implementation class
876 //
877
878 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
879
880 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
881     wxWidgetImpl( peer, isRootControl )
882 {
883     Init();
884     m_osxView = w;
885 }
886
887 wxWidgetCocoaImpl::wxWidgetCocoaImpl() 
888 {
889     Init();
890 }
891
892 void wxWidgetCocoaImpl::Init()
893 {
894     m_osxView = NULL;
895     m_isFlipped = true;
896 }
897
898 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
899 {
900     RemoveAssociations( this );
901
902     if ( !IsRootControl() )
903     {
904         NSView *sv = [m_osxView superview];
905         if ( sv != nil )
906             [m_osxView removeFromSuperview];
907     }
908     [m_osxView release];
909 }
910     
911 bool wxWidgetCocoaImpl::IsVisible() const 
912 {
913     return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
914 }
915
916 void wxWidgetCocoaImpl::SetVisibility( bool visible )
917 {
918     [m_osxView setHidden:(visible ? NO:YES)];
919 }
920
921 void wxWidgetCocoaImpl::Raise()
922 {
923 }
924     
925 void wxWidgetCocoaImpl::Lower()
926 {
927 }
928
929 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
930 {
931 #if 1
932     SetNeedsDisplay() ;
933 #else
934     // We should do something like this, but it wasn't working in 10.4.
935     if (GetNeedsDisplay() )
936     {
937         SetNeedsDisplay() ;
938     }
939     NSRect r = wxToNSRect( [m_osxView superview], *rect );
940     NSSize offset = NSMakeSize((float)dx, (float)dy);
941     [m_osxView scrollRect:r by:offset];
942 #endif
943 }
944
945 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
946 {
947     wxWindowMac* parent = GetWXPeer()->GetParent();
948     // under Cocoa we might have a contentView in the wxParent to which we have to 
949     // adjust the coordinates
950     if (parent)
951     {
952         int cx = 0,cy = 0,cw = 0,ch = 0;
953         if ( parent->GetPeer() )
954         {
955             parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
956             x -= cx;
957             y -= cy;
958         }
959     }
960     NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
961     [m_osxView setFrame:r];
962 }
963
964 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
965 {
966     wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
967     x = r.GetLeft();
968     y = r.GetTop();
969 }
970
971 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
972 {
973     NSRect rect = [m_osxView frame];
974     width = rect.size.width;
975     height = rect.size.height;
976 }
977
978 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
979 {
980     if ( [m_osxView respondsToSelector:@selector(contentView) ] )
981     {
982         NSView* cv = [m_osxView contentView];
983      
984         NSRect bounds = [m_osxView bounds];
985         NSRect rect = [cv frame];
986         
987         int y = rect.origin.y;
988         int x = rect.origin.x;
989         if ( ![ m_osxView isFlipped ] )
990             y = bounds.size.height - (rect.origin.y + rect.size.height);
991         left = x;
992         top = y;
993         width = rect.size.width;
994         height = rect.size.height;
995     }
996     else
997     {
998         left = top = 0;
999         GetSize( width, height );
1000     }
1001 }
1002
1003 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1004 {
1005     if ( where )
1006         [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1007     else
1008         [m_osxView setNeedsDisplay:YES];
1009 }
1010
1011 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1012 {
1013     return [m_osxView needsDisplay];
1014 }
1015
1016 bool wxWidgetCocoaImpl::CanFocus() const
1017 {
1018     return [m_osxView canBecomeKeyView] == YES;
1019 }
1020
1021 bool wxWidgetCocoaImpl::HasFocus() const
1022 {
1023     return ( [[m_osxView window] firstResponder] == m_osxView );
1024 }
1025
1026 bool wxWidgetCocoaImpl::SetFocus() 
1027 {
1028     if ( [m_osxView canBecomeKeyView] == NO )
1029         return false;
1030         
1031     [[m_osxView window] makeFirstResponder: m_osxView] ;
1032     return true;
1033 }
1034
1035
1036 void wxWidgetCocoaImpl::RemoveFromParent()
1037 {
1038     [m_osxView removeFromSuperview];
1039 }
1040
1041 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1042 {
1043     NSView* container = parent->GetWXWidget() ;
1044     wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1045     [container addSubview:m_osxView];
1046 }
1047
1048 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1049 {
1050     // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1051 }
1052
1053 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1054 {
1055     if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1056     {
1057         wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1058         [m_osxView setTitle:cf.AsNSString()];
1059     }
1060     else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1061     {
1062         wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1063         [m_osxView setStringValue:cf.AsNSString()];
1064     }
1065 }
1066     
1067
1068 void  wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1069 {
1070     NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1071     p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ]; 
1072     *pt = wxFromNSPoint( to->GetWXWidget(), p );
1073 }
1074
1075 wxInt32 wxWidgetCocoaImpl::GetValue() const 
1076 {
1077     return [(NSControl*)m_osxView intValue];
1078 }
1079
1080 void wxWidgetCocoaImpl::SetValue( wxInt32 v ) 
1081 {
1082     if (  [m_osxView respondsToSelector:@selector(setIntValue:)] )
1083     {
1084         [m_osxView setIntValue:v];
1085     }
1086     else if (  [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1087     {
1088         [m_osxView setFloatValue:(double)v];
1089     }
1090     else if (  [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1091     {
1092         [m_osxView setDoubleValue:(double)v];
1093     }
1094 }
1095
1096 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v ) 
1097 {
1098     if (  [m_osxView respondsToSelector:@selector(setMinValue:)] )
1099     {
1100         [m_osxView setMinValue:(double)v];
1101     }
1102 }
1103
1104 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v ) 
1105 {
1106     if (  [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1107     {
1108         [m_osxView setMaxValue:(double)v];
1109     }
1110 }
1111
1112 wxInt32 wxWidgetCocoaImpl::GetMinimum() const 
1113 {
1114     if (  [m_osxView respondsToSelector:@selector(getMinValue:)] )
1115     {
1116         return [m_osxView minValue];
1117     }
1118     return 0;
1119 }
1120
1121 wxInt32 wxWidgetCocoaImpl::GetMaximum() const 
1122 {
1123     if (  [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1124     {
1125         return [m_osxView maxValue];
1126     }
1127     return 0;
1128 }
1129
1130 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1131 {
1132     if (  [m_osxView respondsToSelector:@selector(setImage:)] )
1133     {
1134         [m_osxView setImage:bitmap.GetNSImage()];
1135     }
1136 }
1137
1138 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1139 {
1140     // implementation in subclass
1141 }
1142
1143 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1144 {
1145     r->x = r->y = r->width = r->height = 0;
1146 //    if (  [m_osxView isKindOfClass:[NSControl class]] )
1147     if (  [m_osxView respondsToSelector:@selector(sizeToFit)] )
1148     {
1149         NSRect former = [m_osxView frame];
1150         [m_osxView sizeToFit];
1151         NSRect best = [m_osxView frame];
1152         [m_osxView setFrame:former];
1153         r->width = best.size.width;
1154         r->height = best.size.height;
1155     }
1156 }
1157
1158 bool wxWidgetCocoaImpl::IsEnabled() const
1159 {
1160     if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1161         return [m_osxView isEnabled];
1162     return true;
1163 }
1164
1165 void wxWidgetCocoaImpl::Enable( bool enable )
1166 {
1167     if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1168         [m_osxView setEnabled:enable];
1169 }
1170
1171 void wxWidgetCocoaImpl::PulseGauge()
1172 {
1173 }
1174
1175 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1176 {
1177 }
1178
1179 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant ) 
1180 {
1181     NSControlSize size = NSRegularControlSize;
1182     
1183     switch ( variant )
1184     {
1185         case wxWINDOW_VARIANT_NORMAL :
1186             size = NSRegularControlSize;
1187             break ;
1188
1189         case wxWINDOW_VARIANT_SMALL :
1190             size = NSSmallControlSize;
1191             break ;
1192
1193         case wxWINDOW_VARIANT_MINI :
1194             size = NSMiniControlSize;
1195             break ;
1196
1197         case wxWINDOW_VARIANT_LARGE :
1198             size = NSRegularControlSize;
1199             break ;
1200
1201         default:
1202             wxFAIL_MSG(_T("unexpected window variant"));
1203             break ;
1204     }
1205     if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1206         [m_osxView setControlSize:size];
1207 }
1208
1209 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1210 {
1211     // TODO
1212 }
1213
1214 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1215 {
1216     WXWidget c =  control ? control : (WXWidget) m_osxView;
1217     wxWidgetImpl::Associate( c, this ) ;
1218     if ([c respondsToSelector:@selector(setAction:)])
1219     {
1220         [c setTarget: c];
1221         [c setAction: @selector(controlAction:)];
1222         if ([c respondsToSelector:@selector(setDoubleAction:)])
1223         {
1224             [c setDoubleAction: @selector(controlDoubleAction:)];
1225         }
1226         
1227     }
1228 }
1229
1230 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1231 {
1232     wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1233     SetupKeyEvent( wxevent, event );
1234
1235     return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1236 }
1237
1238 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1239 {
1240     NSPoint clickLocation; 
1241     clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil]; 
1242     wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1243     wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1244     SetupMouseEvent( wxevent , event ) ;
1245     wxevent.m_x = pt.x;
1246     wxevent.m_y = pt.y;
1247
1248     return GetWXPeer()->HandleWindowEvent(wxevent);
1249 }
1250
1251 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1252 {
1253     wxWindow* thisWindow = GetWXPeer();
1254     if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1255     {
1256         thisWindow->MacInvalidateBorders();
1257     }
1258
1259     if ( receivedFocus )
1260     {
1261         wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1262         wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1263         thisWindow->HandleWindowEvent(eventFocus);
1264
1265 #if wxUSE_CARET
1266         if ( thisWindow->GetCaret() )
1267             thisWindow->GetCaret()->OnSetFocus();
1268 #endif
1269
1270         wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1271         event.SetEventObject(thisWindow);
1272         // TODO how to find out the targetFocusWindow ?
1273         // event.SetWindow(targetFocusWindow);
1274         thisWindow->HandleWindowEvent(event) ;
1275     }
1276     else // !receivedFocuss
1277     {
1278 #if wxUSE_CARET
1279         if ( thisWindow->GetCaret() )
1280             thisWindow->GetCaret()->OnKillFocus();
1281 #endif
1282
1283         wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1284                     
1285         wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1286         event.SetEventObject(thisWindow);
1287         // TODO how to find out the targetFocusWindow ?
1288         // event.SetWindow(targetFocusWindow);
1289         thisWindow->HandleWindowEvent(event) ;
1290     }
1291 }
1292
1293 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1294 {
1295     NSPoint location = [NSEvent mouseLocation];
1296     location = [[m_osxView window] convertScreenToBase:location];
1297     NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1298
1299     if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1300     {
1301         [(NSCursor*)cursor.GetHCURSOR() set];
1302     }
1303     [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1304 }
1305
1306 void wxWidgetCocoaImpl::CaptureMouse()
1307 {
1308     [[m_osxView window] disableCursorRects];
1309 }
1310
1311 void wxWidgetCocoaImpl::ReleaseMouse()
1312 {
1313     [[m_osxView window] enableCursorRects];
1314 }
1315
1316 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1317 {
1318     m_isFlipped = flipped;
1319 }
1320
1321 //
1322 // Factory methods
1323 //
1324
1325 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1326                             long style, long extraStyle)
1327 {
1328     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1329     wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1330
1331     // temporary hook for dnd
1332     [v registerForDraggedTypes:[NSArray arrayWithObjects:
1333         NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1334         
1335     wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1336     return c;
1337 }
1338
1339 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now ) 
1340 {
1341     NSWindow* tlw = now->GetWXWindow();
1342     wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1343     wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1344     c->InstallEventHandler();
1345     [tlw setContentView:v];
1346     return c;
1347 }