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