]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/window.mm
renaming clickedAction callbacks to more generic controlAction, textctrl updates
[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 [slf addCursorRect: [slf bounds]
707 cursor: cursor];
708 }
709 }
710
711 bool wxWidgetCocoaImpl::isFlipped(WXWidget slf, void *_cmd)
712 {
713 return m_isFlipped;
714 }
715
716
717 #define OSX_DEBUG_DRAWING 0
718
719 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *_cmd)
720 {
721 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
722 CGContextSaveGState( context );
723
724 #if OSX_DEBUG_DRAWING
725 CGContextBeginPath( context );
726 CGContextMoveToPoint(context, 0, 0);
727 NSRect bounds = [self bounds];
728 CGContextAddLineToPoint(context, 10, 0);
729 CGContextMoveToPoint(context, 0, 0);
730 CGContextAddLineToPoint(context, 0, 10);
731 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
732 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
733 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
734 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
735 CGContextClosePath( context );
736 CGContextStrokePath(context);
737 #endif
738
739 if ( !m_isFlipped )
740 {
741 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
742 CGContextScaleCTM( context, 1, -1 );
743 }
744
745 wxRegion updateRgn;
746 const NSRect *rects;
747 NSInteger count;
748
749 [slf getRectsBeingDrawn:&rects count:&count];
750 for ( int i = 0 ; i < count ; ++i )
751 {
752 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
753 }
754
755 wxWindow* wxpeer = GetWXPeer();
756 wxpeer->GetUpdateRegion() = updateRgn;
757 wxpeer->MacSetCGContextRef( context );
758
759 bool handled = wxpeer->MacDoRedraw( 0 );
760
761 CGContextRestoreGState( context );
762
763 CGContextSaveGState( context );
764 if ( !handled )
765 {
766 // call super
767 SEL _cmd = @selector(drawRect:);
768 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
769 superimpl(slf, _cmd, *(NSRect*)rect);
770 CGContextRestoreGState( context );
771 CGContextSaveGState( context );
772 }
773 wxpeer->MacPaintChildrenBorders();
774 wxpeer->MacSetCGContextRef( NULL );
775 CGContextRestoreGState( context );
776 }
777
778 void wxWidgetCocoaImpl::controlAction( WXWidget slf, void *_cmd, void *sender)
779 {
780 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
781 if ( wxpeer )
782 wxpeer->OSXHandleClicked(0);
783 }
784
785 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget slf, void *_cmd, void *sender)
786 {
787 }
788
789 //
790
791 #if OBJC_API_VERSION >= 2
792
793 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
794 class_addMethod(c, s, i, t );
795
796 #else
797
798 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
799 { s, t, i },
800
801 #endif
802
803 void wxOSXCocoaClassAddWXMethods(Class c)
804 {
805
806 #if OBJC_API_VERSION < 2
807 static objc_method wxmethods[] =
808 {
809 #endif
810
811 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
812 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
813 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
814
815 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
816 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
817 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
818
819 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
820
821 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
822 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
823 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
824
825 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
826 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
827 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
828
829 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
830 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
831 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
832
833 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" )
834
835
836 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
837 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
838 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
839
840 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
841 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
842
843 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
844 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
845
846 #if wxUSE_DRAG_AND_DROP
847 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
848 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
849 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
850 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
851 #endif
852
853 #if OBJC_API_VERSION < 2
854 } ;
855 static int method_count = WXSIZEOF( wxmethods );
856 static objc_method_list *wxmethodlist = NULL;
857 if ( wxmethodlist == NULL )
858 {
859 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
860 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
861 wxmethodlist->method_count = method_count;
862 wxmethodlist->obsolete = 0;
863 }
864 class_addMethods( c, wxmethodlist );
865 #endif
866 }
867
868 //
869 // C++ implementation class
870 //
871
872 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
873
874 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
875 wxWidgetImpl( peer, isRootControl )
876 {
877 Init();
878 m_osxView = w;
879 }
880
881 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
882 {
883 Init();
884 }
885
886 void wxWidgetCocoaImpl::Init()
887 {
888 m_osxView = NULL;
889 m_isFlipped = true;
890 }
891
892 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
893 {
894 RemoveAssociations( this );
895
896 if ( !IsRootControl() )
897 {
898 NSView *sv = [m_osxView superview];
899 if ( sv != nil )
900 [m_osxView removeFromSuperview];
901 }
902 [m_osxView release];
903 }
904
905 bool wxWidgetCocoaImpl::IsVisible() const
906 {
907 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
908 }
909
910 void wxWidgetCocoaImpl::SetVisibility( bool visible )
911 {
912 [m_osxView setHidden:(visible ? NO:YES)];
913 }
914
915 void wxWidgetCocoaImpl::Raise()
916 {
917 }
918
919 void wxWidgetCocoaImpl::Lower()
920 {
921 }
922
923 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
924 {
925 #if 1
926 SetNeedsDisplay() ;
927 #else
928 // We should do something like this, but it wasn't working in 10.4.
929 if (GetNeedsDisplay() )
930 {
931 SetNeedsDisplay() ;
932 }
933 NSRect r = wxToNSRect( [m_osxView superview], *rect );
934 NSSize offset = NSMakeSize((float)dx, (float)dy);
935 [m_osxView scrollRect:r by:offset];
936 #endif
937 }
938
939 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
940 {
941 wxWindowMac* parent = GetWXPeer()->GetParent();
942 // under Cocoa we might have a contentView in the wxParent to which we have to
943 // adjust the coordinates
944 if (parent)
945 {
946 wxPoint pt(parent->GetClientAreaOrigin());
947 x -= pt.x;
948 y -= pt.y;
949 }
950 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
951 [m_osxView setFrame:r];
952 }
953
954 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
955 {
956 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
957 x = r.GetLeft();
958 y = r.GetTop();
959 }
960
961 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
962 {
963 NSRect rect = [m_osxView frame];
964 width = rect.size.width;
965 height = rect.size.height;
966 }
967
968 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
969 {
970 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
971 {
972 NSView* cv = [m_osxView contentView];
973
974 NSRect bounds = [m_osxView bounds];
975 NSRect rect = [cv frame];
976
977 int y = rect.origin.y;
978 int x = rect.origin.x;
979 if ( ![ m_osxView isFlipped ] )
980 y = bounds.size.height - (rect.origin.y + rect.size.height);
981 left = x;
982 top = y;
983 width = rect.size.width;
984 height = rect.size.height;
985 }
986 else
987 {
988 left = top = 0;
989 GetSize( width, height );
990 }
991 }
992
993 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
994 {
995 if ( where )
996 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
997 else
998 [m_osxView setNeedsDisplay:YES];
999 }
1000
1001 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1002 {
1003 return [m_osxView needsDisplay];
1004 }
1005
1006 bool wxWidgetCocoaImpl::CanFocus() const
1007 {
1008 return [m_osxView canBecomeKeyView] == YES;
1009 }
1010
1011 bool wxWidgetCocoaImpl::HasFocus() const
1012 {
1013 return ( [[m_osxView window] firstResponder] == m_osxView );
1014 }
1015
1016 bool wxWidgetCocoaImpl::SetFocus()
1017 {
1018 if ( [m_osxView canBecomeKeyView] == NO )
1019 return false;
1020
1021 [[m_osxView window] makeFirstResponder: m_osxView] ;
1022 return true;
1023 }
1024
1025
1026 void wxWidgetCocoaImpl::RemoveFromParent()
1027 {
1028 [m_osxView removeFromSuperview];
1029 }
1030
1031 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1032 {
1033 NSView* container = parent->GetWXWidget() ;
1034 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1035 [container addSubview:m_osxView];
1036 }
1037
1038 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1039 {
1040 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1041 }
1042
1043 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1044 {
1045 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1046 {
1047 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1048 [m_osxView setTitle:cf.AsNSString()];
1049 }
1050 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1051 {
1052 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
1053 [m_osxView setStringValue:cf.AsNSString()];
1054 }
1055 }
1056
1057
1058 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1059 {
1060 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1061 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1062 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1063 }
1064
1065 wxInt32 wxWidgetCocoaImpl::GetValue() const
1066 {
1067 return [(NSControl*)m_osxView intValue];
1068 }
1069
1070 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1071 {
1072 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1073 {
1074 [m_osxView setIntValue:v];
1075 }
1076 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1077 {
1078 [m_osxView setFloatValue:(double)v];
1079 }
1080 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1081 {
1082 [m_osxView setDoubleValue:(double)v];
1083 }
1084 }
1085
1086 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1087 {
1088 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1089 {
1090 [m_osxView setMinValue:(double)v];
1091 }
1092 }
1093
1094 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1095 {
1096 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1097 {
1098 [m_osxView setMaxValue:(double)v];
1099 }
1100 }
1101
1102 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1103 {
1104 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1105 {
1106 return [m_osxView minValue];
1107 }
1108 return 0;
1109 }
1110
1111 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1112 {
1113 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1114 {
1115 return [m_osxView maxValue];
1116 }
1117 return 0;
1118 }
1119
1120 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1121 {
1122 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1123 {
1124 [m_osxView setImage:bitmap.GetNSImage()];
1125 }
1126 }
1127
1128 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
1129 {
1130 // implementation in subclass
1131 }
1132
1133 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1134 {
1135 r->x = r->y = r->width = r->height = 0;
1136 // if ( [m_osxView isKindOfClass:[NSControl class]] )
1137 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1138 {
1139 NSRect former = [m_osxView frame];
1140 [m_osxView sizeToFit];
1141 NSRect best = [m_osxView frame];
1142 [m_osxView setFrame:former];
1143 r->width = best.size.width;
1144 r->height = best.size.height;
1145 }
1146 }
1147
1148 bool wxWidgetCocoaImpl::IsEnabled() const
1149 {
1150 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1151 return [m_osxView isEnabled];
1152 return true;
1153 }
1154
1155 void wxWidgetCocoaImpl::Enable( bool enable )
1156 {
1157 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1158 [m_osxView setEnabled:enable];
1159 }
1160
1161 void wxWidgetCocoaImpl::PulseGauge()
1162 {
1163 }
1164
1165 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
1166 {
1167 }
1168
1169 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1170 {
1171 NSControlSize size = NSRegularControlSize;
1172
1173 switch ( variant )
1174 {
1175 case wxWINDOW_VARIANT_NORMAL :
1176 size = NSRegularControlSize;
1177 break ;
1178
1179 case wxWINDOW_VARIANT_SMALL :
1180 size = NSSmallControlSize;
1181 break ;
1182
1183 case wxWINDOW_VARIANT_MINI :
1184 size = NSMiniControlSize;
1185 break ;
1186
1187 case wxWINDOW_VARIANT_LARGE :
1188 size = NSRegularControlSize;
1189 break ;
1190
1191 default:
1192 wxFAIL_MSG(_T("unexpected window variant"));
1193 break ;
1194 }
1195 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1196 [m_osxView setControlSize:size];
1197 }
1198
1199 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
1200 {
1201 // TODO
1202 }
1203
1204 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1205 {
1206 WXWidget c = control ? control : (WXWidget) m_osxView;
1207 wxWidgetImpl::Associate( c, this ) ;
1208 if ([c respondsToSelector:@selector(setAction:)])
1209 {
1210 [c setTarget: c];
1211 [c setAction: @selector(controlAction:)];
1212 if ([c respondsToSelector:@selector(setDoubleAction:)])
1213 {
1214 [c setDoubleAction: @selector(controlDoubleAction:)];
1215 }
1216
1217 }
1218 }
1219
1220 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1221 {
1222 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1223 SetupKeyEvent( wxevent, event );
1224
1225 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1226 }
1227
1228 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1229 {
1230 NSPoint clickLocation;
1231 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1232 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1233 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1234 SetupMouseEvent( wxevent , event ) ;
1235 wxevent.m_x = pt.x;
1236 wxevent.m_y = pt.y;
1237
1238 return GetWXPeer()->HandleWindowEvent(wxevent);
1239 }
1240
1241 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
1242 {
1243 wxWindow* thisWindow = GetWXPeer();
1244 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1245 {
1246 thisWindow->MacInvalidateBorders();
1247 }
1248
1249 if ( receivedFocus )
1250 {
1251 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
1252 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1253 thisWindow->HandleWindowEvent(eventFocus);
1254
1255 #if wxUSE_CARET
1256 if ( thisWindow->GetCaret() )
1257 thisWindow->GetCaret()->OnSetFocus();
1258 #endif
1259
1260 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1261 event.SetEventObject(thisWindow);
1262 // TODO how to find out the targetFocusWindow ?
1263 // event.SetWindow(targetFocusWindow);
1264 thisWindow->HandleWindowEvent(event) ;
1265 }
1266 else // !receivedFocuss
1267 {
1268 #if wxUSE_CARET
1269 if ( thisWindow->GetCaret() )
1270 thisWindow->GetCaret()->OnKillFocus();
1271 #endif
1272
1273 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
1274
1275 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1276 event.SetEventObject(thisWindow);
1277 // TODO how to find out the targetFocusWindow ?
1278 // event.SetWindow(targetFocusWindow);
1279 thisWindow->HandleWindowEvent(event) ;
1280 }
1281 }
1282
1283 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1284 {
1285 NSPoint location = [NSEvent mouseLocation];
1286 location = [[m_osxView window] convertScreenToBase:location];
1287 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1288
1289 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1290 {
1291 [(NSCursor*)cursor.GetHCURSOR() set];
1292 }
1293 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1294 }
1295
1296 void wxWidgetCocoaImpl::CaptureMouse()
1297 {
1298 [[m_osxView window] disableCursorRects];
1299 }
1300
1301 void wxWidgetCocoaImpl::ReleaseMouse()
1302 {
1303 [[m_osxView window] enableCursorRects];
1304 }
1305
1306 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1307 {
1308 m_isFlipped = flipped;
1309 }
1310
1311 //
1312 // Factory methods
1313 //
1314
1315 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1316 long style, long extraStyle)
1317 {
1318 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1319 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1320
1321 // temporary hook for dnd
1322 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1323 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1324
1325 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1326 return c;
1327 }
1328
1329 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1330 {
1331 NSWindow* tlw = now->GetWXWindow();
1332 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1333 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1334 [tlw setContentView:v];
1335 return c;
1336 }