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