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