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