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