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