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