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