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