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