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