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