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