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