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