different contentview-offset code, otherwise 'wx' synthetic borders are also taken...
[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 ( eventType != NSScrollWheel ) 
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         case NSMouseExited :
336         case NSLeftMouseDragged :
337         case NSRightMouseDragged :
338         case NSOtherMouseDragged :
339         case NSMouseMoved :
340             wxevent.SetEventType( wxEVT_MOTION ) ;
341             break;
342         default :
343             break ;
344     }
345 }
346
347 @implementation wxNSView
348
349 + (void)initialize
350 {
351     static BOOL initialized = NO;
352     if (!initialized) 
353     {
354         initialized = YES;
355         wxOSXCocoaClassAddWXMethods( self );
356     }
357 }
358
359 - (BOOL) canBecomeKeyView
360 {
361     return YES;
362 }
363
364 @end // wxNSView
365
366 //
367 // event handlers
368 //
369
370 #if wxUSE_DRAG_AND_DROP
371
372 // see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
373 // for details on the NSPasteboard -> PasteboardRef conversion
374
375 NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
376 {
377     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
378     if (impl == NULL)
379         return NSDragOperationNone;
380         
381     return impl->draggingEntered(sender, self, _cmd);
382 }
383
384 void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
385 {
386     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
387     if (impl == NULL)
388         return ;
389         
390     return impl->draggingExited(sender, self, _cmd);
391 }
392
393 NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
394 {
395     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
396     if (impl == NULL)
397         return NSDragOperationNone;
398         
399     return impl->draggingUpdated(sender, self, _cmd);
400 }
401
402 BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
403 {
404     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
405     if (impl == NULL)
406         return NSDragOperationNone;
407         
408     return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
409 }
410
411 void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event) 
412 {
413     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
414     if (impl == NULL)
415         return;
416         
417     impl->mouseEvent(event, self, _cmd);
418 }
419
420 void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event) 
421 {
422     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
423     if (impl == NULL)
424         return;
425         
426     impl->keyEvent(event, self, _cmd);
427 }
428
429 BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event) 
430 {
431     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
432     if (impl == NULL)
433         return NO;
434         
435     return impl->performKeyEquivalent(event, self, _cmd);
436 }
437
438 BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
439 {
440     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
441     if (impl == NULL)
442         return NO;
443         
444     return impl->becomeFirstResponder(self, _cmd);
445 }
446
447 BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
448 {
449     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
450     if (impl == NULL)
451         return NO;
452         
453     return impl->resignFirstResponder(self, _cmd);
454 }
455
456 void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
457 {
458     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
459     if (impl == NULL)
460         return;
461         
462     impl->resetCursorRects(self, _cmd);
463 }
464
465 BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
466 {
467     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
468     if (impl == NULL)
469         return NO;
470         
471     return impl->isFlipped(self, _cmd) ? YES:NO;
472 }
473
474 void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
475 {
476     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
477     if (impl == NULL)
478         return;
479         
480     return impl->drawRect(&rect, self, _cmd);
481 }
482
483 void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
484 {
485     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
486     if (impl == NULL)
487         return;
488         
489     impl->controlAction(self, _cmd, sender);
490 }
491
492 void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
493 {
494     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
495     if (impl == NULL)
496         return;
497         
498     impl->controlDoubleAction(self, _cmd, sender);
499 }
500
501 unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget slf, void *_cmd)
502 {
503     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
504     NSPasteboard *pboard = [sender draggingPasteboard];
505     NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
506     
507     wxWindow* wxpeer = GetWXPeer();
508     if ( wxpeer == NULL )
509         return NSDragOperationNone;
510         
511     wxDropTarget* target = wxpeer->GetDropTarget();
512     if ( target == NULL )
513         return NSDragOperationNone;
514
515     wxDragResult result = wxDragNone;
516     wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
517
518     if ( sourceDragMask & NSDragOperationLink )
519         result = wxDragLink;
520     else if ( sourceDragMask & NSDragOperationCopy )
521         result = wxDragCopy;
522     else if ( sourceDragMask & NSDragOperationMove )
523         result = wxDragMove;
524
525     PasteboardRef pboardRef;    
526     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
527     target->SetCurrentDragPasteboard(pboardRef);
528     result = target->OnEnter(pt.x, pt.y, result);
529     CFRelease(pboardRef);
530      
531     NSDragOperation nsresult = NSDragOperationNone;
532     switch (result )
533     {
534         case wxDragLink:
535             nsresult = NSDragOperationLink;
536         case wxDragMove:
537             nsresult = NSDragOperationMove;
538         case wxDragCopy:
539             nsresult = NSDragOperationCopy;
540         default :
541             break;
542     }
543     return nsresult;
544 }
545
546 void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget slf, void *_cmd)
547 {
548     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
549     NSPasteboard *pboard = [sender draggingPasteboard];
550     
551     wxWindow* wxpeer = GetWXPeer();
552     if ( wxpeer == NULL )
553         return;
554         
555     wxDropTarget* target = wxpeer->GetDropTarget();
556     if ( target == NULL )
557         return;
558         
559     PasteboardRef pboardRef;    
560     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
561     target->SetCurrentDragPasteboard(pboardRef);
562     target->OnLeave();
563     CFRelease(pboardRef);
564  }
565
566 unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget slf, void *_cmd)
567 {
568     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
569     NSPasteboard *pboard = [sender draggingPasteboard];
570     NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
571         
572     wxWindow* wxpeer = GetWXPeer();
573     if ( wxpeer == NULL )
574         return NSDragOperationNone;
575         
576     wxDropTarget* target = wxpeer->GetDropTarget();
577     if ( target == NULL )
578         return NSDragOperationNone;
579
580     wxDragResult result = wxDragNone;
581     wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
582
583     if ( sourceDragMask & NSDragOperationLink )
584         result = wxDragLink;
585     else if ( sourceDragMask & NSDragOperationCopy )
586         result = wxDragCopy;
587     else if ( sourceDragMask & NSDragOperationMove )
588         result = wxDragMove;
589
590     PasteboardRef pboardRef;    
591     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
592     target->SetCurrentDragPasteboard(pboardRef);
593     result = target->OnDragOver(pt.x, pt.y, result);
594     CFRelease(pboardRef);
595      
596     NSDragOperation nsresult = NSDragOperationNone;
597     switch (result )
598     {
599         case wxDragLink:
600             nsresult = NSDragOperationLink;
601         case wxDragMove:
602             nsresult = NSDragOperationMove;
603         case wxDragCopy:
604             nsresult = NSDragOperationCopy;
605         default :
606             break;
607     }
608     return nsresult;
609 }
610
611 bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget slf, void *_cmd)
612 {
613     id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
614
615     NSPasteboard *pboard = [sender draggingPasteboard];
616     NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
617     
618     wxWindow* wxpeer = GetWXPeer();
619     wxDropTarget* target = wxpeer->GetDropTarget();
620     wxDragResult result = wxDragNone;
621     wxPoint pt = wxFromNSPoint( m_osxView, [sender draggingLocation] );
622
623     if ( sourceDragMask & NSDragOperationLink )
624         result = wxDragLink;
625     else if ( sourceDragMask & NSDragOperationCopy )
626         result = wxDragCopy;
627     else if ( sourceDragMask & NSDragOperationMove )
628         result = wxDragMove;
629
630     PasteboardRef pboardRef;    
631     PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
632     target->SetCurrentDragPasteboard(pboardRef);
633     result = target->OnData(pt.x, pt.y, result);
634     CFRelease(pboardRef);
635      
636     return result != wxDragNone;
637 }
638
639 #endif
640
641 typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
642 typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
643 typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
644 typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
645 typedef BOOL (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
646
647 void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
648 {
649     if ( !DoHandleMouseEvent(event) )
650     {
651         wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
652         superimpl(slf, (SEL)_cmd, event);
653     }
654 }
655
656 void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
657 {
658     if ( !DoHandleKeyEvent(event) )
659     {
660         wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
661         superimpl(slf, (SEL)_cmd, event);
662     }
663 }
664
665 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
666 {
667     if ( !DoHandleKeyEvent(event) )
668     {
669         wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
670         return superimpl(slf, (SEL)_cmd, event);
671     }
672
673     return YES;
674 }
675
676 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
677 {
678     wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
679     BOOL r = superimpl(slf, (SEL)_cmd);
680     if ( r )
681         DoNotifyFocusEvent( true );
682     return r;
683 }
684
685 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
686 {
687     wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
688     BOOL r = superimpl(slf, (SEL)_cmd);
689     if ( r )
690         DoNotifyFocusEvent( false );
691     return r;
692 }
693
694 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
695 {
696     wxWindow* wxpeer = GetWXPeer();
697     if ( wxpeer )
698     {
699         NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
700         if (cursor == NULL)
701         {
702             wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
703             superimpl(slf, (SEL)_cmd);
704         }
705         else
706         {
707             [slf addCursorRect: [slf bounds]
708                 cursor: cursor];
709         }
710     }
711 }
712   
713 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
714 {
715     return m_isFlipped;
716 }
717
718
719 #define OSX_DEBUG_DRAWING 0
720
721 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
722 {
723     CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
724     CGContextSaveGState( context );
725     
726 #if OSX_DEBUG_DRAWING
727     CGContextBeginPath( context );
728     CGContextMoveToPoint(context, 0, 0);
729     NSRect bounds = [self bounds];
730     CGContextAddLineToPoint(context, 10, 0);
731     CGContextMoveToPoint(context, 0, 0);
732     CGContextAddLineToPoint(context, 0, 10);
733     CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
734     CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
735     CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
736     CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
737     CGContextClosePath( context );
738     CGContextStrokePath(context);
739 #endif
740
741     if ( !m_isFlipped )
742     {
743         CGContextTranslateCTM( context, 0,  [m_osxView bounds].size.height );
744         CGContextScaleCTM( context, 1, -1 );
745     }
746     
747     wxRegion updateRgn;
748     const NSRect *rects;
749     NSInteger count;
750
751     [slf getRectsBeingDrawn:&rects count:&count];
752     for ( int i = 0 ; i < count ; ++i )
753     {
754         updateRgn.Union(wxFromNSRect(slf, rects[i]) );
755     }
756
757     wxWindow* wxpeer = GetWXPeer();
758     wxpeer->GetUpdateRegion() = updateRgn;
759     wxpeer->MacSetCGContextRef( context );
760     
761     bool handled = wxpeer->MacDoRedraw( 0 );
762             
763     CGContextRestoreGState( context );
764
765     CGContextSaveGState( context );
766     if ( !handled )
767     {
768         // call super
769         SEL _cmd = @selector(drawRect:);
770         wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
771         superimpl(slf, _cmd, *(NSRect*)rect);
772         CGContextRestoreGState( context );
773         CGContextSaveGState( context );
774     }
775     wxpeer->MacPaintChildrenBorders();
776     wxpeer->MacSetCGContextRef( NULL );
777     CGContextRestoreGState( context );
778 }
779
780 void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender)
781 {
782     wxWindow* wxpeer = (wxWindow*) GetWXPeer();
783     if ( wxpeer )
784         wxpeer->OSXHandleClicked(0);
785 }
786
787 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender)
788 {
789 }
790
791 // 
792
793 #if OBJC_API_VERSION >= 2
794
795 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
796     class_addMethod(c, s, i, t );
797
798 #else
799
800 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
801     { s, t, i },
802
803 #endif
804
805 void wxOSXCocoaClassAddWXMethods(Class c)
806 {
807
808 #if OBJC_API_VERSION < 2
809     static objc_method wxmethods[] =
810     {
811 #endif
812
813     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
814     wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
815     wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
816
817     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
818     wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
819     wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
820
821     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
822
823     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
824     wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
825     wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
826
827     wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
828     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
829     wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
830     
831     wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
832     wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
833     wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
834
835     wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
836
837
838     wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
839     wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
840     wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
841
842     wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
843     wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
844
845     wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
846     wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
847
848 #if wxUSE_DRAG_AND_DROP
849     wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
850     wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
851     wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
852     wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
853 #endif     
854         
855 #if OBJC_API_VERSION < 2
856     } ;
857     static int method_count = WXSIZEOF( wxmethods );
858     static objc_method_list *wxmethodlist = NULL;
859     if ( wxmethodlist == NULL )
860     {
861         wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
862         memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
863         wxmethodlist->method_count = method_count;
864         wxmethodlist->obsolete = 0;
865     }
866     class_addMethods( c, wxmethodlist );
867 #endif
868 }
869
870 //
871 // C++ implementation class
872 //
873
874 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
875
876 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
877     wxWidgetImpl( peer, isRootControl )
878 {
879     Init();
880     m_osxView = w;
881 }
882
883 wxWidgetCocoaImpl::wxWidgetCocoaImpl() 
884 {
885     Init();
886 }
887
888 void wxWidgetCocoaImpl::Init()
889 {
890     m_osxView = NULL;
891     m_isFlipped = true;
892 }
893
894 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
895 {
896     RemoveAssociations( this );
897
898     if ( !IsRootControl() )
899     {
900         NSView *sv = [m_osxView superview];
901         if ( sv != nil )
902             [m_osxView removeFromSuperview];
903     }
904     [m_osxView release];
905 }
906     
907 bool wxWidgetCocoaImpl::IsVisible() const 
908 {
909     return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
910 }
911
912 void wxWidgetCocoaImpl::SetVisibility( bool visible )
913 {
914     [m_osxView setHidden:(visible ? NO:YES)];
915 }
916
917 void wxWidgetCocoaImpl::Raise()
918 {
919 }
920     
921 void wxWidgetCocoaImpl::Lower()
922 {
923 }
924
925 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
926 {
927 #if 1
928     SetNeedsDisplay() ;
929 #else
930     // We should do something like this, but it wasn't working in 10.4.
931     if (GetNeedsDisplay() )
932     {
933         SetNeedsDisplay() ;
934     }
935     NSRect r = wxToNSRect( [m_osxView superview], *rect );
936     NSSize offset = NSMakeSize((float)dx, (float)dy);
937     [m_osxView scrollRect:r by:offset];
938 #endif
939 }
940
941 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
942 {
943     wxWindowMac* parent = GetWXPeer()->GetParent();
944     // under Cocoa we might have a contentView in the wxParent to which we have to 
945     // adjust the coordinates
946     if (parent)
947     {
948         int cx = 0,cy = 0,cw = 0,ch = 0;
949         if ( parent->GetPeer() )
950         {
951             parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
952             x -= cx;
953             y -= cy;
954         }
955     }
956     NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
957     [m_osxView setFrame:r];
958 }
959
960 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
961 {
962     wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
963     x = r.GetLeft();
964     y = r.GetTop();
965 }
966
967 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
968 {
969     NSRect rect = [m_osxView frame];
970     width = rect.size.width;
971     height = rect.size.height;
972 }
973
974 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
975 {
976     if ( [m_osxView respondsToSelector:@selector(contentView) ] )
977     {
978         NSView* cv = [m_osxView contentView];
979      
980         NSRect bounds = [m_osxView bounds];
981         NSRect rect = [cv frame];
982         
983         int y = rect.origin.y;
984         int x = rect.origin.x;
985         if ( ![ m_osxView isFlipped ] )
986             y = bounds.size.height - (rect.origin.y + rect.size.height);
987         left = x;
988         top = y;
989         width = rect.size.width;
990         height = rect.size.height;
991     }
992     else
993     {
994         left = top = 0;
995         GetSize( width, height );
996     }
997 }
998
999 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1000 {
1001     if ( where )
1002         [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1003     else
1004         [m_osxView setNeedsDisplay:YES];
1005 }
1006
1007 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1008 {
1009     return [m_osxView needsDisplay];
1010 }
1011
1012 bool wxWidgetCocoaImpl::CanFocus() const
1013 {
1014     return [m_osxView canBecomeKeyView] == YES;
1015 }
1016
1017 bool wxWidgetCocoaImpl::HasFocus() const
1018 {
1019     return ( [[m_osxView window] firstResponder] == m_osxView );
1020 }
1021
1022 bool wxWidgetCocoaImpl::SetFocus() 
1023 {
1024     if ( [m_osxView canBecomeKeyView] == NO )
1025         return false;
1026         
1027     [[m_osxView window] makeFirstResponder: m_osxView] ;
1028     return true;
1029 }
1030
1031
1032 void wxWidgetCocoaImpl::RemoveFromParent()
1033 {
1034     [m_osxView removeFromSuperview];
1035 }
1036
1037 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1038 {
1039     NSView* container = parent->GetWXWidget() ;
1040     wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1041     [container addSubview:m_osxView];
1042 }
1043
1044 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1045 {
1046     // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1047 }
1048
1049 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1050 {
1051     if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1052     {
1053         wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1054         [m_osxView setTitle:cf.AsNSString()];
1055     }
1056     else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1057     {
1058         wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1059         [m_osxView setStringValue:cf.AsNSString()];
1060     }
1061 }
1062     
1063
1064 void  wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1065 {
1066     NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1067     p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ]; 
1068     *pt = wxFromNSPoint( to->GetWXWidget(), p );
1069 }
1070
1071 wxInt32 wxWidgetCocoaImpl::GetValue() const 
1072 {
1073     return [(NSControl*)m_osxView intValue];
1074 }
1075
1076 void wxWidgetCocoaImpl::SetValue( wxInt32 v ) 
1077 {
1078     if (  [m_osxView respondsToSelector:@selector(setIntValue:)] )
1079     {
1080         [m_osxView setIntValue:v];
1081     }
1082     else if (  [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1083     {
1084         [m_osxView setFloatValue:(double)v];
1085     }
1086     else if (  [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1087     {
1088         [m_osxView setDoubleValue:(double)v];
1089     }
1090 }
1091
1092 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v ) 
1093 {
1094     if (  [m_osxView respondsToSelector:@selector(setMinValue:)] )
1095     {
1096         [m_osxView setMinValue:(double)v];
1097     }
1098 }
1099
1100 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v ) 
1101 {
1102     if (  [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1103     {
1104         [m_osxView setMaxValue:(double)v];
1105     }
1106 }
1107
1108 wxInt32 wxWidgetCocoaImpl::GetMinimum() const 
1109 {
1110     if (  [m_osxView respondsToSelector:@selector(getMinValue:)] )
1111     {
1112         return [m_osxView minValue];
1113     }
1114     return 0;
1115 }
1116
1117 wxInt32 wxWidgetCocoaImpl::GetMaximum() const 
1118 {
1119     if (  [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1120     {
1121         return [m_osxView maxValue];
1122     }
1123     return 0;
1124 }
1125
1126 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1127 {
1128     if (  [m_osxView respondsToSelector:@selector(setImage:)] )
1129     {
1130         [m_osxView setImage:bitmap.GetNSImage()];
1131     }
1132 }
1133
1134 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1135 {
1136     // implementation in subclass
1137 }
1138
1139 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1140 {
1141     r->x = r->y = r->width = r->height = 0;
1142 //    if (  [m_osxView isKindOfClass:[NSControl class]] )
1143     if (  [m_osxView respondsToSelector:@selector(sizeToFit)] )
1144     {
1145         NSRect former = [m_osxView frame];
1146         [m_osxView sizeToFit];
1147         NSRect best = [m_osxView frame];
1148         [m_osxView setFrame:former];
1149         r->width = best.size.width;
1150         r->height = best.size.height;
1151     }
1152 }
1153
1154 bool wxWidgetCocoaImpl::IsEnabled() const
1155 {
1156     if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1157         return [m_osxView isEnabled];
1158     return true;
1159 }
1160
1161 void wxWidgetCocoaImpl::Enable( bool enable )
1162 {
1163     if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1164         [m_osxView setEnabled:enable];
1165 }
1166
1167 void wxWidgetCocoaImpl::PulseGauge()
1168 {
1169 }
1170
1171 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1172 {
1173 }
1174
1175 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant ) 
1176 {
1177     NSControlSize size = NSRegularControlSize;
1178     
1179     switch ( variant )
1180     {
1181         case wxWINDOW_VARIANT_NORMAL :
1182             size = NSRegularControlSize;
1183             break ;
1184
1185         case wxWINDOW_VARIANT_SMALL :
1186             size = NSSmallControlSize;
1187             break ;
1188
1189         case wxWINDOW_VARIANT_MINI :
1190             size = NSMiniControlSize;
1191             break ;
1192
1193         case wxWINDOW_VARIANT_LARGE :
1194             size = NSRegularControlSize;
1195             break ;
1196
1197         default:
1198             wxFAIL_MSG(_T("unexpected window variant"));
1199             break ;
1200     }
1201     if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1202         [m_osxView setControlSize:size];
1203 }
1204
1205 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1206 {
1207     // TODO
1208 }
1209
1210 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1211 {
1212     WXWidget c =  control ? control : (WXWidget) m_osxView;
1213     wxWidgetImpl::Associate( c, this ) ;
1214     if ([c respondsToSelector:@selector(setAction:)])
1215     {
1216         [c setTarget: c];
1217         [c setAction: @selector(controlAction:)];
1218         if ([c respondsToSelector:@selector(setDoubleAction:)])
1219         {
1220             [c setDoubleAction: @selector(controlDoubleAction:)];
1221         }
1222         
1223     }
1224 }
1225
1226 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1227 {
1228     wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1229     SetupKeyEvent( wxevent, event );
1230
1231     return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1232 }
1233
1234 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1235 {
1236     NSPoint clickLocation; 
1237     clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil]; 
1238     wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1239     wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1240     SetupMouseEvent( wxevent , event ) ;
1241     wxevent.m_x = pt.x;
1242     wxevent.m_y = pt.y;
1243
1244     return GetWXPeer()->HandleWindowEvent(wxevent);
1245 }
1246
1247 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1248 {
1249     wxWindow* thisWindow = GetWXPeer();
1250     if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1251     {
1252         thisWindow->MacInvalidateBorders();
1253     }
1254
1255     if ( receivedFocus )
1256     {
1257         wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1258         wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1259         thisWindow->HandleWindowEvent(eventFocus);
1260
1261 #if wxUSE_CARET
1262         if ( thisWindow->GetCaret() )
1263             thisWindow->GetCaret()->OnSetFocus();
1264 #endif
1265
1266         wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1267         event.SetEventObject(thisWindow);
1268         // TODO how to find out the targetFocusWindow ?
1269         // event.SetWindow(targetFocusWindow);
1270         thisWindow->HandleWindowEvent(event) ;
1271     }
1272     else // !receivedFocuss
1273     {
1274 #if wxUSE_CARET
1275         if ( thisWindow->GetCaret() )
1276             thisWindow->GetCaret()->OnKillFocus();
1277 #endif
1278
1279         wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1280                     
1281         wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1282         event.SetEventObject(thisWindow);
1283         // TODO how to find out the targetFocusWindow ?
1284         // event.SetWindow(targetFocusWindow);
1285         thisWindow->HandleWindowEvent(event) ;
1286     }
1287 }
1288
1289 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1290 {
1291     NSPoint location = [NSEvent mouseLocation];
1292     location = [[m_osxView window] convertScreenToBase:location];
1293     NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1294
1295     if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1296     {
1297         [(NSCursor*)cursor.GetHCURSOR() set];
1298     }
1299     [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1300 }
1301
1302 void wxWidgetCocoaImpl::CaptureMouse()
1303 {
1304     [[m_osxView window] disableCursorRects];
1305 }
1306
1307 void wxWidgetCocoaImpl::ReleaseMouse()
1308 {
1309     [[m_osxView window] enableCursorRects];
1310 }
1311
1312 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1313 {
1314     m_isFlipped = flipped;
1315 }
1316
1317 //
1318 // Factory methods
1319 //
1320
1321 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1322                             long style, long extraStyle)
1323 {
1324     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1325     wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1326
1327     // temporary hook for dnd
1328     [v registerForDraggedTypes:[NSArray arrayWithObjects:
1329         NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1330         
1331     wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1332     return c;
1333 }
1334
1335 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now ) 
1336 {
1337     NSWindow* tlw = now->GetWXWindow();
1338     wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1339     wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1340     [tlw setContentView:v];
1341     return c;
1342 }