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