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