]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/cocoa/window.mm
pending events detected didn't work properly, go the safe route now, fixes #11797
[wxWidgets.git] / src / osx / cocoa / window.mm
... / ...
CommitLineData
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 #include "wx/textctrl.h"
19#endif
20
21#ifdef __WXMAC__
22 #include "wx/osx/private.h"
23#endif
24
25#include "wx/evtloop.h"
26
27#if wxUSE_CARET
28 #include "wx/caret.h"
29#endif
30
31#if wxUSE_DRAG_AND_DROP
32 #include "wx/dnd.h"
33#endif
34
35#if wxUSE_TOOLTIPS
36 #include "wx/tooltip.h"
37#endif
38
39#include <objc/objc-runtime.h>
40
41// Get the window with the focus
42
43NSView* GetViewFromResponder( NSResponder* responder )
44{
45 NSView* view = nil;
46 if ( [responder isKindOfClass:[NSTextView class]] )
47 {
48 NSView* delegate = (NSView*) [(NSTextView*)responder delegate];
49 if ( [delegate isKindOfClass:[NSTextField class] ] )
50 view = delegate;
51 else
52 view = (NSView*) responder;
53 }
54 else
55 {
56 if ( [responder isKindOfClass:[NSView class]] )
57 view = (NSView*) responder;
58 }
59 return view;
60}
61
62NSView* GetFocusedViewInWindow( NSWindow* keyWindow )
63{
64 NSView* focusedView = nil;
65 if ( keyWindow != nil )
66 focusedView = GetViewFromResponder([keyWindow firstResponder]);
67
68 return focusedView;
69}
70
71WXWidget wxWidgetImpl::FindFocus()
72{
73 return GetFocusedViewInWindow( [[NSApplication sharedApplication] keyWindow] );
74}
75
76NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
77{
78 int x, y, w, h ;
79
80 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
81 wxRect bounds(x,y,w,h);
82 NSView* sv = (window->GetParent()->GetHandle() );
83
84 return wxToNSRect( sv, bounds );
85}
86
87@interface wxNSView : NSView
88{
89 NSTrackingRectTag rectTag;
90#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
91 NSTrackingArea* _trackingArea;
92#endif
93}
94
95// the tracking tag is needed to track mouse enter / exit events
96- (void) setTrackingTag: (NSTrackingRectTag)tag;
97- (NSTrackingRectTag) trackingTag;
98#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
99// under 10.5 we can also track mouse moved events on non-focused windows if
100// we use the new NSTrackingArea APIs.
101- (void) updateTrackingArea;
102- (NSTrackingArea*) trackingArea;
103#endif
104@end // wxNSView
105
106@interface NSView(PossibleMethods)
107- (void)setTitle:(NSString *)aString;
108- (void)setStringValue:(NSString *)aString;
109- (void)setIntValue:(int)anInt;
110- (void)setFloatValue:(float)aFloat;
111- (void)setDoubleValue:(double)aDouble;
112
113- (double)minValue;
114- (double)maxValue;
115- (void)setMinValue:(double)aDouble;
116- (void)setMaxValue:(double)aDouble;
117
118- (void)sizeToFit;
119
120- (BOOL)isEnabled;
121- (void)setEnabled:(BOOL)flag;
122
123- (void)setImage:(NSImage *)image;
124- (void)setControlSize:(NSControlSize)size;
125
126- (void)setFont:(NSFont *)fontObject;
127
128- (id)contentView;
129
130- (void)setTarget:(id)anObject;
131- (void)setAction:(SEL)aSelector;
132- (void)setDoubleAction:(SEL)aSelector;
133- (void)setBackgroundColor:(NSColor*)aColor;
134- (void)setImagePosition:(NSCellImagePosition)aPosition;
135@end
136
137long wxOSXTranslateCocoaKey( NSEvent* event )
138{
139 long retval = 0;
140
141 if ([event type] != NSFlagsChanged)
142 {
143 NSString* s = [event charactersIgnoringModifiers];
144 // backspace char reports as delete w/modifiers for some reason
145 if ([s length] == 1)
146 {
147 switch ( [s characterAtIndex:0] )
148 {
149 // backspace key
150 case 0x7F :
151 case 8 :
152 retval = WXK_BACK;
153 break;
154 case NSUpArrowFunctionKey :
155 retval = WXK_UP;
156 break;
157 case NSDownArrowFunctionKey :
158 retval = WXK_DOWN;
159 break;
160 case NSLeftArrowFunctionKey :
161 retval = WXK_LEFT;
162 break;
163 case NSRightArrowFunctionKey :
164 retval = WXK_RIGHT;
165 break;
166 case NSInsertFunctionKey :
167 retval = WXK_INSERT;
168 break;
169 case NSDeleteFunctionKey :
170 retval = WXK_DELETE;
171 break;
172 case NSHomeFunctionKey :
173 retval = WXK_HOME;
174 break;
175 // case NSBeginFunctionKey :
176 // retval = WXK_BEGIN;
177 // break;
178 case NSEndFunctionKey :
179 retval = WXK_END;
180 break;
181 case NSPageUpFunctionKey :
182 retval = WXK_PAGEUP;
183 break;
184 case NSPageDownFunctionKey :
185 retval = WXK_PAGEDOWN;
186 break;
187 case NSHelpFunctionKey :
188 retval = WXK_HELP;
189 break;
190 default:
191 int intchar = [s characterAtIndex: 0];
192 if ( intchar >= NSF1FunctionKey && intchar <= NSF24FunctionKey )
193 retval = WXK_F1 + (intchar - NSF1FunctionKey );
194 break;
195 }
196 }
197 }
198
199 // Some keys don't seem to have constants. The code mimics the approach
200 // taken by WebKit. See:
201 // http://trac.webkit.org/browser/trunk/WebCore/platform/mac/KeyEventMac.mm
202 switch( [event keyCode] )
203 {
204 // command key
205 case 54:
206 case 55:
207 retval = WXK_COMMAND;
208 break;
209 // caps locks key
210 case 57: // Capslock
211 retval = WXK_CAPITAL;
212 break;
213 // shift key
214 case 56: // Left Shift
215 case 60: // Right Shift
216 retval = WXK_SHIFT;
217 break;
218 // alt key
219 case 58: // Left Alt
220 case 61: // Right Alt
221 retval = WXK_ALT;
222 break;
223 // ctrl key
224 case 59: // Left Ctrl
225 case 62: // Right Ctrl
226 retval = WXK_CONTROL;
227 break;
228 // clear key
229 case 71:
230 retval = WXK_CLEAR;
231 break;
232 // tab key
233 case 48:
234 retval = WXK_TAB;
235 break;
236
237 case 75: // /
238 retval = WXK_NUMPAD_DIVIDE;
239 break;
240 case 67: // *
241 retval = WXK_NUMPAD_MULTIPLY;
242 break;
243 case 78: // -
244 retval = WXK_NUMPAD_SUBTRACT;
245 break;
246 case 69: // +
247 retval = WXK_NUMPAD_ADD;
248 break;
249 case 76: // Enter
250 retval = WXK_NUMPAD_ENTER;
251 break;
252 case 65: // .
253 retval = WXK_NUMPAD_DECIMAL;
254 break;
255 case 82: // 0
256 retval = WXK_NUMPAD0;
257 break;
258 case 83: // 1
259 retval = WXK_NUMPAD1;
260 break;
261 case 84: // 2
262 retval = WXK_NUMPAD2;
263 break;
264 case 85: // 3
265 retval = WXK_NUMPAD3;
266 break;
267 case 86: // 4
268 retval = WXK_NUMPAD4;
269 break;
270 case 87: // 5
271 retval = WXK_NUMPAD5;
272 break;
273 case 88: // 6
274 retval = WXK_NUMPAD6;
275 break;
276 case 89: // 7
277 retval = WXK_NUMPAD7;
278 break;
279 case 91: // 8
280 retval = WXK_NUMPAD8;
281 break;
282 case 92: // 9
283 retval = WXK_NUMPAD9;
284 break;
285 default:
286 //retval = [event keyCode];
287 break;
288 }
289 return retval;
290}
291
292void wxWidgetCocoaImpl::SetupKeyEvent(wxKeyEvent &wxevent , NSEvent * nsEvent, NSString* charString)
293{
294 UInt32 modifiers = [nsEvent modifierFlags] ;
295 int eventType = [nsEvent type];
296
297 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
298 wxevent.m_controlDown = modifiers & NSControlKeyMask;
299 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
300 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
301
302 wxevent.m_rawCode = [nsEvent keyCode];
303 wxevent.m_rawFlags = modifiers;
304
305 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
306
307 wxString chars;
308 if ( eventType != NSFlagsChanged )
309 {
310 NSString* nschars = [nsEvent charactersIgnoringModifiers];
311 if ( charString )
312 {
313 // if charString is set, it did not come from key up / key down
314 wxevent.SetEventType( wxEVT_CHAR );
315 chars = wxCFStringRef::AsString(charString);
316 }
317 else if ( nschars )
318 {
319 chars = wxCFStringRef::AsString(nschars);
320 }
321 }
322
323 int aunichar = chars.Length() > 0 ? chars[0] : 0;
324 long keyval = 0;
325
326 if (wxevent.GetEventType() != wxEVT_CHAR)
327 {
328 keyval = wxOSXTranslateCocoaKey(nsEvent) ;
329 switch (eventType)
330 {
331 case NSKeyDown :
332 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
333 break;
334 case NSKeyUp :
335 wxevent.SetEventType( wxEVT_KEY_UP ) ;
336 break;
337 case NSFlagsChanged :
338 switch (keyval)
339 {
340 case WXK_CONTROL:
341 wxevent.SetEventType( wxevent.m_controlDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
342 break;
343 case WXK_SHIFT:
344 wxevent.SetEventType( wxevent.m_shiftDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
345 break;
346 case WXK_ALT:
347 wxevent.SetEventType( wxevent.m_altDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
348 break;
349 case WXK_COMMAND:
350 wxevent.SetEventType( wxevent.m_metaDown ? wxEVT_KEY_DOWN : wxEVT_KEY_UP);
351 break;
352 }
353 break;
354 default :
355 break ;
356 }
357 }
358
359 if ( !keyval )
360 {
361 if ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN )
362 keyval = wxToupper( aunichar ) ;
363 else
364 keyval = aunichar;
365 }
366
367#if wxUSE_UNICODE
368 wxevent.m_uniChar = aunichar;
369#endif
370 wxevent.m_keyCode = keyval;
371
372 wxWindowMac* peer = GetWXPeer();
373 if ( peer )
374 {
375 wxevent.SetEventObject(peer);
376 wxevent.SetId(peer->GetId()) ;
377 }
378}
379
380UInt32 g_lastButton = 0 ;
381bool g_lastButtonWasFakeRight = false ;
382
383// better scroll wheel support
384// see http://lists.apple.com/archives/cocoa-dev/2007/Feb/msg00050.html
385
386@interface NSEvent (DeviceDelta)
387- (float)deviceDeltaX;
388- (float)deviceDeltaY;
389@end
390
391void wxWidgetCocoaImpl::SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
392{
393 int eventType = [nsEvent type];
394 UInt32 modifiers = [nsEvent modifierFlags] ;
395 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
396
397 // these parameters are not given for all events
398 UInt32 button = [nsEvent buttonNumber];
399 UInt32 clickCount = 0;
400
401 wxevent.m_x = screenMouseLocation.x;
402 wxevent.m_y = screenMouseLocation.y;
403 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
404 wxevent.m_controlDown = modifiers & NSControlKeyMask;
405 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
406 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
407 wxevent.SetTimestamp( (int)([nsEvent timestamp] * 1000) ) ;
408
409 UInt32 mouseChord = 0;
410
411 switch (eventType)
412 {
413 case NSLeftMouseDown :
414 case NSLeftMouseDragged :
415 mouseChord = 1U;
416 break;
417 case NSRightMouseDown :
418 case NSRightMouseDragged :
419 mouseChord = 2U;
420 break;
421 case NSOtherMouseDown :
422 case NSOtherMouseDragged :
423 mouseChord = 4U;
424 break;
425 }
426
427 // a control click is interpreted as a right click
428 bool thisButtonIsFakeRight = false ;
429 if ( button == 0 && (modifiers & NSControlKeyMask) )
430 {
431 button = 1 ;
432 thisButtonIsFakeRight = true ;
433 }
434
435 // otherwise we report double clicks by connecting a left click with a ctrl-left click
436 if ( clickCount > 1 && button != g_lastButton )
437 clickCount = 1 ;
438
439 // we must make sure that our synthetic 'right' button corresponds in
440 // mouse down, moved and mouse up, and does not deliver a right down and left up
441 switch (eventType)
442 {
443 case NSLeftMouseDown :
444 case NSRightMouseDown :
445 case NSOtherMouseDown :
446 g_lastButton = button ;
447 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
448 break;
449 }
450
451 if ( button == 0 )
452 {
453 g_lastButton = 0 ;
454 g_lastButtonWasFakeRight = false ;
455 }
456 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
457 button = g_lastButton ;
458
459 // Adjust the chord mask to remove the primary button and add the
460 // secondary button. It is possible that the secondary button is
461 // already pressed, e.g. on a mouse connected to a laptop, but this
462 // possibility is ignored here:
463 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
464 mouseChord = ((mouseChord & ~1U) | 2U);
465
466 if(mouseChord & 1U)
467 wxevent.m_leftDown = true ;
468 if(mouseChord & 2U)
469 wxevent.m_rightDown = true ;
470 if(mouseChord & 4U)
471 wxevent.m_middleDown = true ;
472
473 // translate into wx types
474 switch (eventType)
475 {
476 case NSLeftMouseDown :
477 case NSRightMouseDown :
478 case NSOtherMouseDown :
479 clickCount = [nsEvent clickCount];
480 switch ( button )
481 {
482 case 0 :
483 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
484 break ;
485
486 case 1 :
487 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
488 break ;
489
490 case 2 :
491 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
492 break ;
493
494 default:
495 break ;
496 }
497 break ;
498
499 case NSLeftMouseUp :
500 case NSRightMouseUp :
501 case NSOtherMouseUp :
502 clickCount = [nsEvent clickCount];
503 switch ( button )
504 {
505 case 0 :
506 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
507 break ;
508
509 case 1 :
510 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
511 break ;
512
513 case 2 :
514 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
515 break ;
516
517 default:
518 break ;
519 }
520 break ;
521
522 case NSScrollWheel :
523 {
524 float deltaX = 0.0;
525 float deltaY = 0.0;
526
527 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
528
529 // see http://developer.apple.com/qa/qa2005/qa1453.html
530 // for more details on why we have to look for the exact type
531
532 const EventRef cEvent = (EventRef) [nsEvent eventRef];
533 bool isMouseScrollEvent = false;
534 if ( cEvent )
535 isMouseScrollEvent = ::GetEventKind(cEvent) == kEventMouseScroll;
536
537 if ( isMouseScrollEvent )
538 {
539 deltaX = [nsEvent deviceDeltaX];
540 deltaY = [nsEvent deviceDeltaY];
541 }
542 else
543 {
544 deltaX = ([nsEvent deltaX] * 10);
545 deltaY = ([nsEvent deltaY] * 10);
546 }
547
548 wxevent.m_wheelDelta = 10;
549 wxevent.m_linesPerAction = 1;
550
551 if ( fabs(deltaX) > fabs(deltaY) )
552 {
553 wxevent.m_wheelAxis = 1;
554 wxevent.m_wheelRotation = (int)deltaX;
555 }
556 else
557 {
558 wxevent.m_wheelRotation = (int)deltaY;
559 }
560
561 }
562 break ;
563
564 case NSMouseEntered :
565 wxevent.SetEventType( wxEVT_ENTER_WINDOW ) ;
566 break;
567 case NSMouseExited :
568 wxevent.SetEventType( wxEVT_LEAVE_WINDOW ) ;
569 break;
570 case NSLeftMouseDragged :
571 case NSRightMouseDragged :
572 case NSOtherMouseDragged :
573 case NSMouseMoved :
574 wxevent.SetEventType( wxEVT_MOTION ) ;
575 break;
576 default :
577 break ;
578 }
579
580 wxevent.m_clickCount = clickCount;
581 wxWindowMac* peer = GetWXPeer();
582 if ( peer )
583 {
584 wxevent.SetEventObject(peer);
585 wxevent.SetId(peer->GetId()) ;
586 }
587}
588
589@implementation wxNSView
590
591+ (void)initialize
592{
593 static BOOL initialized = NO;
594 if (!initialized)
595 {
596 initialized = YES;
597 wxOSXCocoaClassAddWXMethods( self );
598 }
599}
600
601- (void) setTrackingTag: (NSTrackingRectTag)tag
602{
603 rectTag = tag;
604}
605
606- (NSTrackingRectTag) trackingTag
607{
608 return rectTag;
609}
610
611#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
612- (void) updateTrackingArea
613{
614 if (_trackingArea)
615 {
616 [self removeTrackingArea: _trackingArea];
617 [_trackingArea release];
618 }
619
620 NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited|NSTrackingMouseMoved|NSTrackingActiveAlways;
621
622 NSTrackingArea* area = [[NSTrackingArea alloc] initWithRect: [self bounds] options: options owner: self userInfo: nil];
623 [self addTrackingArea: area];
624
625 _trackingArea = area;
626}
627
628- (NSTrackingArea*) trackingArea
629{
630 return _trackingArea;
631}
632#endif
633@end // wxNSView
634
635//
636// event handlers
637//
638
639#if wxUSE_DRAG_AND_DROP
640
641// see http://lists.apple.com/archives/Cocoa-dev/2005/Jul/msg01244.html
642// for details on the NSPasteboard -> PasteboardRef conversion
643
644NSDragOperation wxOSX_draggingEntered( id self, SEL _cmd, id <NSDraggingInfo>sender )
645{
646 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
647 if (impl == NULL)
648 return NSDragOperationNone;
649
650 return impl->draggingEntered(sender, self, _cmd);
651}
652
653void wxOSX_draggingExited( id self, SEL _cmd, id <NSDraggingInfo> sender )
654{
655 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
656 if (impl == NULL)
657 return ;
658
659 return impl->draggingExited(sender, self, _cmd);
660}
661
662NSDragOperation wxOSX_draggingUpdated( id self, SEL _cmd, id <NSDraggingInfo>sender )
663{
664 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
665 if (impl == NULL)
666 return NSDragOperationNone;
667
668 return impl->draggingUpdated(sender, self, _cmd);
669}
670
671BOOL wxOSX_performDragOperation( id self, SEL _cmd, id <NSDraggingInfo> sender )
672{
673 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
674 if (impl == NULL)
675 return NSDragOperationNone;
676
677 return impl->performDragOperation(sender, self, _cmd) ? YES:NO ;
678}
679
680#endif
681
682void wxOSX_mouseEvent(NSView* self, SEL _cmd, NSEvent *event)
683{
684 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
685 if (impl == NULL)
686 return;
687
688 impl->mouseEvent(event, self, _cmd);
689}
690
691BOOL wxOSX_acceptsFirstMouse(NSView* self, SEL _cmd, NSEvent *event)
692{
693 // This is needed to support click through, otherwise the first click on a window
694 // will not do anything unless it is the active window already.
695 return YES;
696}
697
698void wxOSX_keyEvent(NSView* self, SEL _cmd, NSEvent *event)
699{
700 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
701 if (impl == NULL)
702 return;
703
704 impl->keyEvent(event, self, _cmd);
705}
706
707void wxOSX_insertText(NSView* self, SEL _cmd, NSString* text)
708{
709 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
710 if (impl == NULL)
711 return;
712
713 impl->insertText(text, self, _cmd);
714}
715
716BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event)
717{
718 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
719 if (impl == NULL)
720 return NO;
721
722 return impl->performKeyEquivalent(event, self, _cmd);
723}
724
725BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd)
726{
727 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
728 if (impl == NULL)
729 return NO;
730
731 return impl->acceptsFirstResponder(self, _cmd);
732}
733
734BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd)
735{
736 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
737 if (impl == NULL)
738 return NO;
739
740 return impl->becomeFirstResponder(self, _cmd);
741}
742
743BOOL wxOSX_resignFirstResponder(NSView* self, SEL _cmd)
744{
745 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
746 if (impl == NULL)
747 return NO;
748
749 return impl->resignFirstResponder(self, _cmd);
750}
751
752void wxOSX_resetCursorRects(NSView* self, SEL _cmd)
753{
754 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
755 if (impl == NULL)
756 return;
757
758 impl->resetCursorRects(self, _cmd);
759}
760
761BOOL wxOSX_isFlipped(NSView* self, SEL _cmd)
762{
763 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
764 if (impl == NULL)
765 return NO;
766
767 return impl->isFlipped(self, _cmd) ? YES:NO;
768}
769
770void wxOSX_drawRect(NSView* self, SEL _cmd, NSRect rect)
771{
772 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
773 if (impl == NULL)
774 return;
775
776 return impl->drawRect(&rect, self, _cmd);
777}
778
779void wxOSX_controlAction(NSView* self, SEL _cmd, id sender)
780{
781 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
782 if (impl == NULL)
783 return;
784
785 impl->controlAction(self, _cmd, sender);
786}
787
788void wxOSX_controlDoubleAction(NSView* self, SEL _cmd, id sender)
789{
790 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
791 if (impl == NULL)
792 return;
793
794 impl->controlDoubleAction(self, _cmd, sender);
795}
796
797unsigned int wxWidgetCocoaImpl::draggingEntered(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
798{
799 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
800 NSPasteboard *pboard = [sender draggingPasteboard];
801 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
802
803 wxWindow* wxpeer = GetWXPeer();
804 if ( wxpeer == NULL )
805 return NSDragOperationNone;
806
807 wxDropTarget* target = wxpeer->GetDropTarget();
808 if ( target == NULL )
809 return NSDragOperationNone;
810
811 wxDragResult result = wxDragNone;
812 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
813 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
814
815 if ( sourceDragMask & NSDragOperationLink )
816 result = wxDragLink;
817 else if ( sourceDragMask & NSDragOperationCopy )
818 result = wxDragCopy;
819 else if ( sourceDragMask & NSDragOperationMove )
820 result = wxDragMove;
821
822 PasteboardRef pboardRef;
823 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
824 target->SetCurrentDragPasteboard(pboardRef);
825 result = target->OnEnter(pt.x, pt.y, result);
826 CFRelease(pboardRef);
827
828 NSDragOperation nsresult = NSDragOperationNone;
829 switch (result )
830 {
831 case wxDragLink:
832 nsresult = NSDragOperationLink;
833 case wxDragMove:
834 nsresult = NSDragOperationMove;
835 case wxDragCopy:
836 nsresult = NSDragOperationCopy;
837 default :
838 break;
839 }
840 return nsresult;
841}
842
843void wxWidgetCocoaImpl::draggingExited(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
844{
845 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
846 NSPasteboard *pboard = [sender draggingPasteboard];
847
848 wxWindow* wxpeer = GetWXPeer();
849 if ( wxpeer == NULL )
850 return;
851
852 wxDropTarget* target = wxpeer->GetDropTarget();
853 if ( target == NULL )
854 return;
855
856 PasteboardRef pboardRef;
857 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
858 target->SetCurrentDragPasteboard(pboardRef);
859 target->OnLeave();
860 CFRelease(pboardRef);
861 }
862
863unsigned int wxWidgetCocoaImpl::draggingUpdated(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
864{
865 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
866 NSPasteboard *pboard = [sender draggingPasteboard];
867 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
868
869 wxWindow* wxpeer = GetWXPeer();
870 if ( wxpeer == NULL )
871 return NSDragOperationNone;
872
873 wxDropTarget* target = wxpeer->GetDropTarget();
874 if ( target == NULL )
875 return NSDragOperationNone;
876
877 wxDragResult result = wxDragNone;
878 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
879 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
880
881 if ( sourceDragMask & NSDragOperationLink )
882 result = wxDragLink;
883 else if ( sourceDragMask & NSDragOperationCopy )
884 result = wxDragCopy;
885 else if ( sourceDragMask & NSDragOperationMove )
886 result = wxDragMove;
887
888 PasteboardRef pboardRef;
889 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
890 target->SetCurrentDragPasteboard(pboardRef);
891 result = target->OnDragOver(pt.x, pt.y, result);
892 CFRelease(pboardRef);
893
894 NSDragOperation nsresult = NSDragOperationNone;
895 switch (result )
896 {
897 case wxDragLink:
898 nsresult = NSDragOperationLink;
899 case wxDragMove:
900 nsresult = NSDragOperationMove;
901 case wxDragCopy:
902 nsresult = NSDragOperationCopy;
903 default :
904 break;
905 }
906 return nsresult;
907}
908
909bool wxWidgetCocoaImpl::performDragOperation(void* s, WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
910{
911 id <NSDraggingInfo>sender = (id <NSDraggingInfo>) s;
912
913 NSPasteboard *pboard = [sender draggingPasteboard];
914 NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
915
916 wxWindow* wxpeer = GetWXPeer();
917 wxDropTarget* target = wxpeer->GetDropTarget();
918 wxDragResult result = wxDragNone;
919 NSPoint nspoint = [m_osxView convertPoint:[sender draggingLocation] fromView:nil];
920 wxPoint pt = wxFromNSPoint( m_osxView, nspoint );
921
922 if ( sourceDragMask & NSDragOperationLink )
923 result = wxDragLink;
924 else if ( sourceDragMask & NSDragOperationCopy )
925 result = wxDragCopy;
926 else if ( sourceDragMask & NSDragOperationMove )
927 result = wxDragMove;
928
929 PasteboardRef pboardRef;
930 PasteboardCreate((CFStringRef)[pboard name], &pboardRef);
931 target->SetCurrentDragPasteboard(pboardRef);
932
933 if (target->OnDrop(pt.x, pt.y))
934 result = target->OnData(pt.x, pt.y, result);
935
936 CFRelease(pboardRef);
937
938 return result != wxDragNone;
939}
940
941typedef void (*wxOSX_TextEventHandlerPtr)(NSView* self, SEL _cmd, NSString *event);
942typedef void (*wxOSX_EventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
943typedef BOOL (*wxOSX_PerformKeyEventHandlerPtr)(NSView* self, SEL _cmd, NSEvent *event);
944typedef BOOL (*wxOSX_FocusHandlerPtr)(NSView* self, SEL _cmd);
945typedef BOOL (*wxOSX_ResetCursorRectsHandlerPtr)(NSView* self, SEL _cmd);
946typedef void (*wxOSX_DrawRectHandlerPtr)(NSView* self, SEL _cmd, NSRect rect);
947
948void wxWidgetCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
949{
950 if ( !DoHandleMouseEvent(event) )
951 {
952 // for plain NSView mouse events would propagate to parents otherwise
953 if (!m_wxPeer->MacIsUserPane())
954 {
955 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
956 superimpl(slf, (SEL)_cmd, event);
957 }
958 }
959}
960
961void wxWidgetCocoaImpl::keyEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
962{
963 if ( [event type] == NSKeyDown )
964 m_lastKeyDownEvent = event;
965 if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
966 {
967 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
968 superimpl(slf, (SEL)_cmd, event);
969 }
970 m_lastKeyDownEvent = NULL;
971}
972
973void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
974{
975 if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
976 {
977 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
978 superimpl(slf, (SEL)_cmd, text);
979 }
980}
981
982
983bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
984{
985 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
986 return superimpl(slf, (SEL)_cmd, event);
987}
988
989bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
990{
991 if ( m_wxPeer->MacIsUserPane() )
992 return m_wxPeer->AcceptsFocus();
993 else
994 {
995 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
996 return superimpl(slf, (SEL)_cmd);
997 }
998}
999
1000bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
1001{
1002 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1003 // get the current focus before running becomeFirstResponder
1004 NSView* otherView = FindFocus();
1005
1006 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
1007 BOOL r = superimpl(slf, (SEL)_cmd);
1008 if ( r )
1009 {
1010 DoNotifyFocusEvent( true, otherWindow );
1011 }
1012
1013 return r;
1014}
1015
1016bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
1017{
1018 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1019 BOOL r = superimpl(slf, (SEL)_cmd);
1020 // get the current focus after running resignFirstResponder
1021 // note that this value isn't reliable, it might return the same view that
1022 // is resigning
1023 NSView* otherView = FindFocus();
1024 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
1025 // NSTextViews have an editor as true responder, therefore the might get the
1026 // resign notification if their editor takes over, don't trigger any event then
1027 if ( r && !m_hasEditor)
1028 {
1029 DoNotifyFocusEvent( false, otherWindow );
1030 }
1031 return r;
1032}
1033
1034void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
1035{
1036 wxWindow* wxpeer = GetWXPeer();
1037 if ( wxpeer )
1038 {
1039 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
1040 if (cursor == NULL)
1041 {
1042 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
1043 superimpl(slf, (SEL)_cmd);
1044 }
1045 else
1046 {
1047 [slf addCursorRect: [slf bounds]
1048 cursor: cursor];
1049 }
1050 }
1051}
1052
1053bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
1054{
1055 return m_isFlipped;
1056}
1057
1058
1059#define OSX_DEBUG_DRAWING 0
1060
1061void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
1062{
1063 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
1064 CGContextSaveGState( context );
1065
1066#if OSX_DEBUG_DRAWING
1067 CGContextBeginPath( context );
1068 CGContextMoveToPoint(context, 0, 0);
1069 NSRect bounds = [self bounds];
1070 CGContextAddLineToPoint(context, 10, 0);
1071 CGContextMoveToPoint(context, 0, 0);
1072 CGContextAddLineToPoint(context, 0, 10);
1073 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1074 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
1075 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
1076 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
1077 CGContextClosePath( context );
1078 CGContextStrokePath(context);
1079#endif
1080
1081 if ( !m_isFlipped )
1082 {
1083 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
1084 CGContextScaleCTM( context, 1, -1 );
1085 }
1086
1087 wxRegion updateRgn;
1088 const NSRect *rects;
1089 NSInteger count;
1090
1091 [slf getRectsBeingDrawn:&rects count:&count];
1092 for ( int i = 0 ; i < count ; ++i )
1093 {
1094 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
1095 }
1096
1097 wxWindow* wxpeer = GetWXPeer();
1098 wxpeer->GetUpdateRegion() = updateRgn;
1099 wxpeer->MacSetCGContextRef( context );
1100
1101 bool handled = wxpeer->MacDoRedraw( 0 );
1102
1103 CGContextRestoreGState( context );
1104
1105 CGContextSaveGState( context );
1106 if ( !handled )
1107 {
1108 // call super
1109 SEL _cmd = @selector(drawRect:);
1110 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1111 superimpl(slf, _cmd, *(NSRect*)rect);
1112 CGContextRestoreGState( context );
1113 CGContextSaveGState( context );
1114 }
1115 wxpeer->MacPaintChildrenBorders();
1116 wxpeer->MacSetCGContextRef( NULL );
1117 CGContextRestoreGState( context );
1118}
1119
1120void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1121{
1122 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1123 if ( wxpeer )
1124 wxpeer->OSXHandleClicked(0);
1125}
1126
1127void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1128{
1129}
1130
1131void wxWidgetCocoaImpl::controlTextDidChange()
1132{
1133 wxWindow* wxpeer = (wxWindow*)GetWXPeer();
1134 if ( wxpeer )
1135 {
1136 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
1137 event.SetEventObject( wxpeer );
1138 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
1139 wxpeer->HandleWindowEvent( event );
1140 }
1141}
1142
1143//
1144
1145#if OBJC_API_VERSION >= 2
1146
1147#define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1148 class_addMethod(c, s, i, t );
1149
1150#else
1151
1152#define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1153 { s, t, i },
1154
1155#endif
1156
1157void wxOSXCocoaClassAddWXMethods(Class c)
1158{
1159
1160#if OBJC_API_VERSION < 2
1161 static objc_method wxmethods[] =
1162 {
1163#endif
1164
1165 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1166 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1167 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1168
1169 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1170 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1171 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1172
1173 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1174
1175 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1176 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1177 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1178
1179 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstMouse:), (IMP) wxOSX_acceptsFirstMouse, "v@:@" )
1180
1181 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1182 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1183 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1184
1185 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1186 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1187 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1188
1189 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1190
1191 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1192
1193 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1194 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1195 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1196 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
1197
1198 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1199 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1200
1201 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1202 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1203
1204#if wxUSE_DRAG_AND_DROP
1205 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1206 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1207 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1208 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1209#endif
1210
1211#if OBJC_API_VERSION < 2
1212 } ;
1213 static int method_count = WXSIZEOF( wxmethods );
1214 static objc_method_list *wxmethodlist = NULL;
1215 if ( wxmethodlist == NULL )
1216 {
1217 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1218 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1219 wxmethodlist->method_count = method_count;
1220 wxmethodlist->obsolete = 0;
1221 }
1222 class_addMethods( c, wxmethodlist );
1223#endif
1224}
1225
1226//
1227// C++ implementation class
1228//
1229
1230IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1231
1232wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1233 wxWidgetImpl( peer, isRootControl )
1234{
1235 Init();
1236 m_osxView = w;
1237
1238 // check if the user wants to create the control initially hidden
1239 if ( !peer->IsShown() )
1240 SetVisibility(false);
1241
1242 // gc aware handling
1243 if ( m_osxView )
1244 CFRetain(m_osxView);
1245 [m_osxView release];
1246}
1247
1248wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1249{
1250 Init();
1251}
1252
1253void wxWidgetCocoaImpl::Init()
1254{
1255 m_osxView = NULL;
1256 m_isFlipped = true;
1257 m_lastKeyDownEvent = NULL;
1258 m_hasEditor = false;
1259}
1260
1261wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1262{
1263 RemoveAssociations( this );
1264
1265 if ( !IsRootControl() )
1266 {
1267 NSView *sv = [m_osxView superview];
1268 if ( sv != nil )
1269 [m_osxView removeFromSuperview];
1270 }
1271 // gc aware handling
1272 if ( m_osxView )
1273 CFRelease(m_osxView);
1274}
1275
1276bool wxWidgetCocoaImpl::IsVisible() const
1277{
1278 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1279}
1280
1281void wxWidgetCocoaImpl::SetVisibility( bool visible )
1282{
1283 [m_osxView setHidden:(visible ? NO:YES)];
1284}
1285
1286// ----------------------------------------------------------------------------
1287// window animation stuff
1288// ----------------------------------------------------------------------------
1289
1290// define a delegate used to refresh the window during animation
1291@interface wxNSAnimationDelegate : NSObject wxOSX_10_6_AND_LATER(<NSAnimationDelegate>)
1292{
1293 wxWindow *m_win;
1294 bool m_isDone;
1295}
1296
1297- (id)init:(wxWindow *)win;
1298
1299- (bool)isDone;
1300
1301// NSAnimationDelegate methods
1302- (void)animationDidEnd:(NSAnimation*)animation;
1303- (void)animation:(NSAnimation*)animation
1304 didReachProgressMark:(NSAnimationProgress)progress;
1305@end
1306
1307@implementation wxNSAnimationDelegate
1308
1309- (id)init:(wxWindow *)win
1310{
1311 [super init];
1312
1313 m_win = win;
1314 m_isDone = false;
1315
1316 return self;
1317}
1318
1319- (bool)isDone
1320{
1321 return m_isDone;
1322}
1323
1324- (void)animation:(NSAnimation*)animation
1325 didReachProgressMark:(NSAnimationProgress)progress
1326{
1327 wxUnusedVar(animation);
1328 wxUnusedVar(progress);
1329
1330 m_win->SendSizeEvent();
1331}
1332
1333- (void)animationDidEnd:(NSAnimation*)animation
1334{
1335 m_isDone = true;
1336}
1337
1338@end
1339
1340/* static */
1341bool
1342wxWidgetCocoaImpl::ShowViewOrWindowWithEffect(wxWindow *win,
1343 bool show,
1344 wxShowEffect effect,
1345 unsigned timeout)
1346{
1347 // create the dictionary describing the animation to perform on this view
1348 NSObject * const
1349 viewOrWin = static_cast<NSObject *>(win->OSXGetViewOrWindow());
1350 NSMutableDictionary * const
1351 dict = [NSMutableDictionary dictionaryWithCapacity:4];
1352 [dict setObject:viewOrWin forKey:NSViewAnimationTargetKey];
1353
1354 // determine the start and end rectangles assuming we're hiding the window
1355 const wxRect rectOrig = win->GetRect();
1356 wxRect rectStart,
1357 rectEnd;
1358 rectStart =
1359 rectEnd = rectOrig;
1360
1361 if ( show )
1362 {
1363 if ( effect == wxSHOW_EFFECT_ROLL_TO_LEFT ||
1364 effect == wxSHOW_EFFECT_SLIDE_TO_LEFT )
1365 effect = wxSHOW_EFFECT_ROLL_TO_RIGHT;
1366 else if ( effect == wxSHOW_EFFECT_ROLL_TO_RIGHT ||
1367 effect == wxSHOW_EFFECT_SLIDE_TO_RIGHT )
1368 effect = wxSHOW_EFFECT_ROLL_TO_LEFT;
1369 else if ( effect == wxSHOW_EFFECT_ROLL_TO_TOP ||
1370 effect == wxSHOW_EFFECT_SLIDE_TO_TOP )
1371 effect = wxSHOW_EFFECT_ROLL_TO_BOTTOM;
1372 else if ( effect == wxSHOW_EFFECT_ROLL_TO_BOTTOM ||
1373 effect == wxSHOW_EFFECT_SLIDE_TO_BOTTOM )
1374 effect = wxSHOW_EFFECT_ROLL_TO_TOP;
1375 }
1376
1377 switch ( effect )
1378 {
1379 case wxSHOW_EFFECT_ROLL_TO_LEFT:
1380 case wxSHOW_EFFECT_SLIDE_TO_LEFT:
1381 rectEnd.width = 0;
1382 break;
1383
1384 case wxSHOW_EFFECT_ROLL_TO_RIGHT:
1385 case wxSHOW_EFFECT_SLIDE_TO_RIGHT:
1386 rectEnd.x = rectStart.GetRight();
1387 rectEnd.width = 0;
1388 break;
1389
1390 case wxSHOW_EFFECT_ROLL_TO_TOP:
1391 case wxSHOW_EFFECT_SLIDE_TO_TOP:
1392 rectEnd.height = 0;
1393 break;
1394
1395 case wxSHOW_EFFECT_ROLL_TO_BOTTOM:
1396 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM:
1397 rectEnd.y = rectStart.GetBottom();
1398 rectEnd.height = 0;
1399 break;
1400
1401 case wxSHOW_EFFECT_EXPAND:
1402 rectEnd.x = rectStart.x + rectStart.width / 2;
1403 rectEnd.y = rectStart.y + rectStart.height / 2;
1404 rectEnd.width =
1405 rectEnd.height = 0;
1406 break;
1407
1408 case wxSHOW_EFFECT_BLEND:
1409 [dict setObject:(show ? NSViewAnimationFadeInEffect
1410 : NSViewAnimationFadeOutEffect)
1411 forKey:NSViewAnimationEffectKey];
1412 break;
1413
1414 case wxSHOW_EFFECT_NONE:
1415 case wxSHOW_EFFECT_MAX:
1416 wxFAIL_MSG( "unexpected animation effect" );
1417 return false;
1418
1419 default:
1420 wxFAIL_MSG( "unknown animation effect" );
1421 return false;
1422 };
1423
1424 if ( show )
1425 {
1426 // we need to restore it to the original rectangle instead of making it
1427 // disappear
1428 wxSwap(rectStart, rectEnd);
1429
1430 // and as the window is currently hidden, we need to show it for the
1431 // animation to be visible at all (but don't restore it at its full
1432 // rectangle as it shouldn't appear immediately)
1433 win->SetSize(rectStart);
1434 win->Show();
1435 }
1436
1437 NSView * const parentView = [viewOrWin isKindOfClass:[NSView class]]
1438 ? [(NSView *)viewOrWin superview]
1439 : nil;
1440 const NSRect rStart = wxToNSRect(parentView, rectStart);
1441 const NSRect rEnd = wxToNSRect(parentView, rectEnd);
1442
1443 [dict setObject:[NSValue valueWithRect:rStart]
1444 forKey:NSViewAnimationStartFrameKey];
1445 [dict setObject:[NSValue valueWithRect:rEnd]
1446 forKey:NSViewAnimationEndFrameKey];
1447
1448 // create an animation using the values in the above dictionary
1449 NSViewAnimation * const
1450 anim = [[NSViewAnimation alloc]
1451 initWithViewAnimations:[NSArray arrayWithObject:dict]];
1452
1453 if ( !timeout )
1454 {
1455 // what is a good default duration? Windows uses 200ms, Web frameworks
1456 // use anything from 250ms to 1s... choose something in the middle
1457 timeout = 500;
1458 }
1459
1460 [anim setDuration:timeout/1000.]; // duration is in seconds here
1461
1462 // if the window being animated changes its layout depending on its size
1463 // (which is almost always the case) we need to redo it during animation
1464 //
1465 // the number of layouts here is arbitrary, but 10 seems like too few (e.g.
1466 // controls in wxInfoBar visibly jump around)
1467 const int NUM_LAYOUTS = 20;
1468 for ( float f = 1./NUM_LAYOUTS; f < 1.; f += 1./NUM_LAYOUTS )
1469 [anim addProgressMark:f];
1470
1471 wxNSAnimationDelegate * const
1472 animDelegate = [[wxNSAnimationDelegate alloc] init:win];
1473 [anim setDelegate:animDelegate];
1474 [anim startAnimation];
1475
1476 // Cocoa is capable of doing animation asynchronously or even from separate
1477 // thread but wx API doesn't provide any way to be notified about the
1478 // animation end and without this we really must ensure that the window has
1479 // the expected (i.e. the same as if a simple Show() had been used) size
1480 // when we return, so block here until the animation finishes
1481 //
1482 // notice that because the default animation mode is NSAnimationBlocking,
1483 // no user input events ought to be processed from here
1484 {
1485 wxEventLoopGuarantor ensureEventLoopExistence;
1486 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
1487 while ( ![animDelegate isDone] )
1488 loop->Dispatch();
1489 }
1490
1491 if ( !show )
1492 {
1493 // NSViewAnimation is smart enough to hide the NSView being animated at
1494 // the end but we also must ensure that it's hidden for wx too
1495 win->Hide();
1496
1497 // and we must also restore its size because it isn't expected to
1498 // change just because the window was hidden
1499 win->SetSize(rectOrig);
1500 }
1501 else
1502 {
1503 // refresh it once again after the end to ensure that everything is in
1504 // place
1505 win->SendSizeEvent();
1506 }
1507
1508 [anim setDelegate:nil];
1509 [animDelegate release];
1510 [anim release];
1511
1512 return true;
1513}
1514
1515bool wxWidgetCocoaImpl::ShowWithEffect(bool show,
1516 wxShowEffect effect,
1517 unsigned timeout)
1518{
1519 return ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
1520}
1521
1522void wxWidgetCocoaImpl::Raise()
1523{
1524 // Not implemented
1525}
1526
1527void wxWidgetCocoaImpl::Lower()
1528{
1529 // Not implemented
1530}
1531
1532void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1533{
1534#if 1
1535 SetNeedsDisplay() ;
1536#else
1537 // We should do something like this, but it wasn't working in 10.4.
1538 if (GetNeedsDisplay() )
1539 {
1540 SetNeedsDisplay() ;
1541 }
1542 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1543 NSSize offset = NSMakeSize((float)dx, (float)dy);
1544 [m_osxView scrollRect:r by:offset];
1545#endif
1546}
1547
1548void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1549{
1550 wxWindowMac* parent = GetWXPeer()->GetParent();
1551 // under Cocoa we might have a contentView in the wxParent to which we have to
1552 // adjust the coordinates
1553 if (parent && [m_osxView superview] != parent->GetHandle() )
1554 {
1555 int cx = 0,cy = 0,cw = 0,ch = 0;
1556 if ( parent->GetPeer() )
1557 {
1558 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1559 x -= cx;
1560 y -= cy;
1561 }
1562 }
1563 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1564 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1565 [m_osxView setFrame:r];
1566 [[m_osxView superview] setNeedsDisplayInRect:r];
1567
1568 wxNSView* wxview = (wxNSView*)m_osxView;
1569#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
1570 if ([wxview respondsToSelector:@selector(updateTrackingArea)] )
1571 [wxview updateTrackingArea];
1572#else
1573 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1574 {
1575 if ( [wxview trackingTag] )
1576 [wxview removeTrackingRect: [wxview trackingTag]];
1577
1578 [wxview setTrackingTag: [wxview addTrackingRect: [m_osxView bounds] owner: wxview userData: nil assumeInside: NO]];
1579 }
1580#endif
1581}
1582
1583void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1584{
1585 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1586 x = r.GetLeft();
1587 y = r.GetTop();
1588}
1589
1590void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1591{
1592 NSRect rect = [m_osxView frame];
1593 width = (int)rect.size.width;
1594 height = (int)rect.size.height;
1595}
1596
1597void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1598{
1599 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1600 {
1601 NSView* cv = [m_osxView contentView];
1602
1603 NSRect bounds = [m_osxView bounds];
1604 NSRect rect = [cv frame];
1605
1606 int y = (int)rect.origin.y;
1607 int x = (int)rect.origin.x;
1608 if ( ![ m_osxView isFlipped ] )
1609 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
1610 left = x;
1611 top = y;
1612 width = (int)rect.size.width;
1613 height = (int)rect.size.height;
1614 }
1615 else
1616 {
1617 left = top = 0;
1618 GetSize( width, height );
1619 }
1620}
1621
1622void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1623{
1624 if ( where )
1625 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1626 else
1627 [m_osxView setNeedsDisplay:YES];
1628}
1629
1630bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1631{
1632 return [m_osxView needsDisplay];
1633}
1634
1635bool wxWidgetCocoaImpl::CanFocus() const
1636{
1637 return [m_osxView canBecomeKeyView] == YES;
1638}
1639
1640bool wxWidgetCocoaImpl::HasFocus() const
1641{
1642 return ( FindFocus() == m_osxView );
1643}
1644
1645bool wxWidgetCocoaImpl::SetFocus()
1646{
1647 if ( !CanFocus() )
1648 return false;
1649
1650 [[m_osxView window] makeFirstResponder: m_osxView] ;
1651 [[m_osxView window] makeKeyAndOrderFront:nil] ;
1652 return true;
1653}
1654
1655
1656void wxWidgetCocoaImpl::RemoveFromParent()
1657{
1658 [m_osxView removeFromSuperview];
1659}
1660
1661void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1662{
1663 NSView* container = parent->GetWXWidget() ;
1664 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1665 [container addSubview:m_osxView];
1666}
1667
1668void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &col )
1669{
1670 NSView* targetView = m_osxView;
1671 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1672 targetView = [(NSScrollView*) m_osxView documentView];
1673
1674 if ( [targetView respondsToSelector:@selector(setBackgroundColor:) ] )
1675 {
1676 [targetView setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
1677 green:(CGFloat) (col.Green() / 255.0)
1678 blue:(CGFloat) (col.Blue() / 255.0)
1679 alpha:(CGFloat) (col.Alpha() / 255.0)]];
1680 }
1681}
1682
1683void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1684{
1685 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1686 {
1687 wxCFStringRef cf( title , encoding );
1688 [m_osxView setTitle:cf.AsNSString()];
1689 }
1690 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1691 {
1692 wxCFStringRef cf( title , encoding );
1693 [m_osxView setStringValue:cf.AsNSString()];
1694 }
1695}
1696
1697
1698void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1699{
1700 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1701 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1702 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1703}
1704
1705wxInt32 wxWidgetCocoaImpl::GetValue() const
1706{
1707 return [(NSControl*)m_osxView intValue];
1708}
1709
1710void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1711{
1712 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1713 {
1714 [m_osxView setIntValue:v];
1715 }
1716 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1717 {
1718 [m_osxView setFloatValue:(double)v];
1719 }
1720 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1721 {
1722 [m_osxView setDoubleValue:(double)v];
1723 }
1724}
1725
1726void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1727{
1728 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1729 {
1730 [m_osxView setMinValue:(double)v];
1731 }
1732}
1733
1734void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1735{
1736 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1737 {
1738 [m_osxView setMaxValue:(double)v];
1739 }
1740}
1741
1742wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1743{
1744 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1745 {
1746 return (int)[m_osxView minValue];
1747 }
1748 return 0;
1749}
1750
1751wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1752{
1753 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1754 {
1755 return (int)[m_osxView maxValue];
1756 }
1757 return 0;
1758}
1759
1760wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1761{
1762 wxBitmap bmp;
1763
1764 // TODO: how to create a wxBitmap from NSImage?
1765#if 0
1766 if ( [m_osxView respondsToSelector:@selector(image:)] )
1767 bmp = [m_osxView image];
1768#endif
1769
1770 return bmp;
1771}
1772
1773void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1774{
1775 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1776 {
1777 [m_osxView setImage:bitmap.GetNSImage()];
1778 [m_osxView setNeedsDisplay:YES];
1779 }
1780}
1781
1782void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1783{
1784 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1785 {
1786 NSCellImagePosition pos;
1787 switch ( dir )
1788 {
1789 case wxLEFT:
1790 pos = NSImageLeft;
1791 break;
1792
1793 case wxRIGHT:
1794 pos = NSImageRight;
1795 break;
1796
1797 case wxTOP:
1798 pos = NSImageAbove;
1799 break;
1800
1801 case wxBOTTOM:
1802 pos = NSImageBelow;
1803 break;
1804
1805 default:
1806 wxFAIL_MSG( "invalid image position" );
1807 pos = NSNoImage;
1808 }
1809
1810 [m_osxView setImagePosition:pos];
1811 }
1812}
1813
1814void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1815{
1816 // implementation in subclass
1817}
1818
1819void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1820{
1821 r->x = r->y = r->width = r->height = 0;
1822
1823 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1824 {
1825 NSRect former = [m_osxView frame];
1826 [m_osxView sizeToFit];
1827 NSRect best = [m_osxView frame];
1828 [m_osxView setFrame:former];
1829 r->width = (int)best.size.width;
1830 r->height = (int)best.size.height;
1831 }
1832}
1833
1834bool wxWidgetCocoaImpl::IsEnabled() const
1835{
1836 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1837 return [m_osxView isEnabled];
1838 return true;
1839}
1840
1841void wxWidgetCocoaImpl::Enable( bool enable )
1842{
1843 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1844 [m_osxView setEnabled:enable];
1845}
1846
1847void wxWidgetCocoaImpl::PulseGauge()
1848{
1849}
1850
1851void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1852{
1853}
1854
1855void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1856{
1857 NSControlSize size = NSRegularControlSize;
1858
1859 switch ( variant )
1860 {
1861 case wxWINDOW_VARIANT_NORMAL :
1862 size = NSRegularControlSize;
1863 break ;
1864
1865 case wxWINDOW_VARIANT_SMALL :
1866 size = NSSmallControlSize;
1867 break ;
1868
1869 case wxWINDOW_VARIANT_MINI :
1870 size = NSMiniControlSize;
1871 break ;
1872
1873 case wxWINDOW_VARIANT_LARGE :
1874 size = NSRegularControlSize;
1875 break ;
1876
1877 default:
1878 wxFAIL_MSG(wxT("unexpected window variant"));
1879 break ;
1880 }
1881 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1882 [m_osxView setControlSize:size];
1883 else if ([m_osxView respondsToSelector:@selector(cell)])
1884 {
1885 id cell = [(id)m_osxView cell];
1886 if ([cell respondsToSelector:@selector(setControlSize:)])
1887 [cell setControlSize:size];
1888 }
1889}
1890
1891void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1892{
1893 if ([m_osxView respondsToSelector:@selector(setFont:)])
1894 [m_osxView setFont: font.OSXGetNSFont()];
1895}
1896
1897void wxWidgetCocoaImpl::SetToolTip(wxToolTip* tooltip)
1898{
1899 if (tooltip)
1900 {
1901 wxCFStringRef cf( tooltip->GetTip() , m_wxPeer->GetFont().GetEncoding() );
1902 [m_osxView setToolTip: cf.AsNSString()];
1903 }
1904 else
1905 [m_osxView setToolTip: nil];
1906
1907}
1908
1909void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1910{
1911 WXWidget c = control ? control : (WXWidget) m_osxView;
1912 wxWidgetImpl::Associate( c, this ) ;
1913 if ([c respondsToSelector:@selector(setAction:)])
1914 {
1915 [c setTarget: c];
1916 [c setAction: @selector(controlAction:)];
1917 if ([c respondsToSelector:@selector(setDoubleAction:)])
1918 {
1919 [c setDoubleAction: @selector(controlDoubleAction:)];
1920 }
1921
1922 }
1923}
1924
1925bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1926{
1927 wxKeyEvent wxevent(wxEVT_CHAR);
1928 SetupKeyEvent( wxevent, event, text );
1929
1930 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1931}
1932
1933bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1934{
1935 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1936 SetupKeyEvent( wxevent, event );
1937 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1938
1939 // this will fire higher level events, like insertText, to help
1940 // us handle EVT_CHAR, etc.
1941
1942 if ( m_wxPeer->MacIsUserPane() && [event type] == NSKeyDown)
1943 {
1944 if ( !result )
1945 {
1946 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1947 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1948 else
1949 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1950 result = true;
1951 }
1952 }
1953
1954 return result;
1955}
1956
1957bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1958{
1959 NSPoint clickLocation;
1960 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1961 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1962 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1963 SetupMouseEvent(wxevent , event) ;
1964 wxevent.m_x = pt.x;
1965 wxevent.m_y = pt.y;
1966
1967 return GetWXPeer()->HandleWindowEvent(wxevent);
1968}
1969
1970void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1971{
1972 wxWindow* thisWindow = GetWXPeer();
1973 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1974 {
1975 thisWindow->MacInvalidateBorders();
1976 }
1977
1978 if ( receivedFocus )
1979 {
1980 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
1981 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1982 thisWindow->HandleWindowEvent(eventFocus);
1983
1984#if wxUSE_CARET
1985 if ( thisWindow->GetCaret() )
1986 thisWindow->GetCaret()->OnSetFocus();
1987#endif
1988
1989 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1990 event.SetEventObject(thisWindow);
1991 if (otherWindow)
1992 event.SetWindow(otherWindow->GetWXPeer());
1993 thisWindow->HandleWindowEvent(event) ;
1994 }
1995 else // !receivedFocuss
1996 {
1997#if wxUSE_CARET
1998 if ( thisWindow->GetCaret() )
1999 thisWindow->GetCaret()->OnKillFocus();
2000#endif
2001
2002 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
2003
2004 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
2005 event.SetEventObject(thisWindow);
2006 if (otherWindow)
2007 event.SetWindow(otherWindow->GetWXPeer());
2008 thisWindow->HandleWindowEvent(event) ;
2009 }
2010}
2011
2012void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
2013{
2014 NSPoint location = [NSEvent mouseLocation];
2015 location = [[m_osxView window] convertScreenToBase:location];
2016 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
2017
2018 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
2019 {
2020 [(NSCursor*)cursor.GetHCURSOR() set];
2021 }
2022 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
2023}
2024
2025void wxWidgetCocoaImpl::CaptureMouse()
2026{
2027 [[m_osxView window] disableCursorRects];
2028}
2029
2030void wxWidgetCocoaImpl::ReleaseMouse()
2031{
2032 [[m_osxView window] enableCursorRects];
2033}
2034
2035void wxWidgetCocoaImpl::SetFlipped(bool flipped)
2036{
2037 m_isFlipped = flipped;
2038}
2039
2040//
2041// Factory methods
2042//
2043
2044wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
2045 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
2046 long WXUNUSED(style), long WXUNUSED(extraStyle))
2047{
2048 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
2049 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
2050
2051 // temporary hook for dnd
2052 [v registerForDraggedTypes:[NSArray arrayWithObjects:
2053 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
2054
2055 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
2056 return c;
2057}
2058
2059wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
2060{
2061 NSWindow* tlw = now->GetWXWindow();
2062 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
2063 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
2064 c->InstallEventHandler();
2065 [tlw setContentView:v];
2066 return c;
2067}