]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/window.mm
reducing key event handling complexity
[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 ( [event type] == NSKeyDown )
885 m_lastKeyDownEvent = event;
886 if ( GetFocusedViewInWindow([slf window]) != slf || m_hasEditor || !DoHandleKeyEvent(event) )
887 {
888 wxOSX_EventHandlerPtr superimpl = (wxOSX_EventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
889 superimpl(slf, (SEL)_cmd, event);
890 }
891 m_lastKeyDownEvent = NULL;
892 }
893
894 void wxWidgetCocoaImpl::insertText(NSString* text, WXWidget slf, void *_cmd)
895 {
896 if ( m_lastKeyDownEvent==NULL || m_hasEditor || !DoHandleCharEvent(m_lastKeyDownEvent, text) )
897 {
898 wxOSX_TextEventHandlerPtr superimpl = (wxOSX_TextEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
899 superimpl(slf, (SEL)_cmd, text);
900 }
901 }
902
903
904 bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, void *_cmd)
905 {
906 wxOSX_PerformKeyEventHandlerPtr superimpl = (wxOSX_PerformKeyEventHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
907 return superimpl(slf, (SEL)_cmd, event);
908 }
909
910 bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd)
911 {
912 if ( m_wxPeer->MacIsUserPane() )
913 return m_wxPeer->AcceptsFocus();
914 else
915 {
916 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
917 return superimpl(slf, (SEL)_cmd);
918 }
919 }
920
921 bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
922 {
923 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
924 // get the current focus before running becomeFirstResponder
925 NSView* otherView = FindFocus();
926
927 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
928 BOOL r = superimpl(slf, (SEL)_cmd);
929 if ( r )
930 {
931 DoNotifyFocusEvent( true, otherWindow );
932 }
933
934 return r;
935 }
936
937 bool wxWidgetCocoaImpl::resignFirstResponder(WXWidget slf, void *_cmd)
938 {
939 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
940 BOOL r = superimpl(slf, (SEL)_cmd);
941 // get the current focus after running resignFirstResponder
942 // note that this value isn't reliable, it might return the same view that
943 // is resigning
944 NSView* otherView = FindFocus();
945 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
946 // NSTextViews have an editor as true responder, therefore the might get the
947 // resign notification if their editor takes over, don't trigger any event then
948 if ( r && !m_hasEditor)
949 {
950 DoNotifyFocusEvent( false, otherWindow );
951 }
952 return r;
953 }
954
955 void wxWidgetCocoaImpl::resetCursorRects(WXWidget slf, void *_cmd)
956 {
957 wxWindow* wxpeer = GetWXPeer();
958 if ( wxpeer )
959 {
960 NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
961 if (cursor == NULL)
962 {
963 wxOSX_ResetCursorRectsHandlerPtr superimpl = (wxOSX_ResetCursorRectsHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
964 superimpl(slf, (SEL)_cmd);
965 }
966 else
967 {
968 [slf addCursorRect: [slf bounds]
969 cursor: cursor];
970 }
971 }
972 }
973
974 bool wxWidgetCocoaImpl::isFlipped(WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd))
975 {
976 return m_isFlipped;
977 }
978
979
980 #define OSX_DEBUG_DRAWING 0
981
982 void wxWidgetCocoaImpl::drawRect(void* rect, WXWidget slf, void *WXUNUSED(_cmd))
983 {
984 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
985 CGContextSaveGState( context );
986
987 #if OSX_DEBUG_DRAWING
988 CGContextBeginPath( context );
989 CGContextMoveToPoint(context, 0, 0);
990 NSRect bounds = [self bounds];
991 CGContextAddLineToPoint(context, 10, 0);
992 CGContextMoveToPoint(context, 0, 0);
993 CGContextAddLineToPoint(context, 0, 10);
994 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
995 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
996 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
997 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
998 CGContextClosePath( context );
999 CGContextStrokePath(context);
1000 #endif
1001
1002 if ( !m_isFlipped )
1003 {
1004 CGContextTranslateCTM( context, 0, [m_osxView bounds].size.height );
1005 CGContextScaleCTM( context, 1, -1 );
1006 }
1007
1008 wxRegion updateRgn;
1009 const NSRect *rects;
1010 NSInteger count;
1011
1012 [slf getRectsBeingDrawn:&rects count:&count];
1013 for ( int i = 0 ; i < count ; ++i )
1014 {
1015 updateRgn.Union(wxFromNSRect(slf, rects[i]) );
1016 }
1017
1018 wxWindow* wxpeer = GetWXPeer();
1019 wxpeer->GetUpdateRegion() = updateRgn;
1020 wxpeer->MacSetCGContextRef( context );
1021
1022 bool handled = wxpeer->MacDoRedraw( 0 );
1023
1024 CGContextRestoreGState( context );
1025
1026 CGContextSaveGState( context );
1027 if ( !handled )
1028 {
1029 // call super
1030 SEL _cmd = @selector(drawRect:);
1031 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
1032 superimpl(slf, _cmd, *(NSRect*)rect);
1033 CGContextRestoreGState( context );
1034 CGContextSaveGState( context );
1035 }
1036 wxpeer->MacPaintChildrenBorders();
1037 wxpeer->MacSetCGContextRef( NULL );
1038 CGContextRestoreGState( context );
1039 }
1040
1041 void wxWidgetCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1042 {
1043 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
1044 if ( wxpeer )
1045 wxpeer->OSXHandleClicked(0);
1046 }
1047
1048 void wxWidgetCocoaImpl::controlDoubleAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
1049 {
1050 }
1051
1052 //
1053
1054 #if OBJC_API_VERSION >= 2
1055
1056 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1057 class_addMethod(c, s, i, t );
1058
1059 #else
1060
1061 #define wxOSX_CLASS_ADD_METHOD( c, s, i, t ) \
1062 { s, t, i },
1063
1064 #endif
1065
1066 void wxOSXCocoaClassAddWXMethods(Class c)
1067 {
1068
1069 #if OBJC_API_VERSION < 2
1070 static objc_method wxmethods[] =
1071 {
1072 #endif
1073
1074 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1075 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1076 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDown:), (IMP) wxOSX_mouseEvent, "v@:@" )
1077
1078 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1079 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1080 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseUp:), (IMP) wxOSX_mouseEvent, "v@:@" )
1081
1082 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseMoved:), (IMP) wxOSX_mouseEvent, "v@:@" )
1083
1084 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1085 wxOSX_CLASS_ADD_METHOD(c, @selector(rightMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1086 wxOSX_CLASS_ADD_METHOD(c, @selector(otherMouseDragged:), (IMP) wxOSX_mouseEvent, "v@:@" )
1087
1088 wxOSX_CLASS_ADD_METHOD(c, @selector(scrollWheel:), (IMP) wxOSX_mouseEvent, "v@:@" )
1089 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseEntered:), (IMP) wxOSX_mouseEvent, "v@:@" )
1090 wxOSX_CLASS_ADD_METHOD(c, @selector(mouseExited:), (IMP) wxOSX_mouseEvent, "v@:@" )
1091
1092 wxOSX_CLASS_ADD_METHOD(c, @selector(keyDown:), (IMP) wxOSX_keyEvent, "v@:@" )
1093 wxOSX_CLASS_ADD_METHOD(c, @selector(keyUp:), (IMP) wxOSX_keyEvent, "v@:@" )
1094 wxOSX_CLASS_ADD_METHOD(c, @selector(flagsChanged:), (IMP) wxOSX_keyEvent, "v@:@" )
1095
1096 wxOSX_CLASS_ADD_METHOD(c, @selector(insertText:), (IMP) wxOSX_insertText, "v@:@" )
1097
1098 wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "c@:@" )
1099
1100 wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" )
1101 wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" )
1102 wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" )
1103 wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" )
1104
1105 wxOSX_CLASS_ADD_METHOD(c, @selector(isFlipped), (IMP) wxOSX_isFlipped, "c@:" )
1106 wxOSX_CLASS_ADD_METHOD(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_NSRect={_NSPoint=ff}{_NSSize=ff}}" )
1107
1108 wxOSX_CLASS_ADD_METHOD(c, @selector(controlAction:), (IMP) wxOSX_controlAction, "v@:@" )
1109 wxOSX_CLASS_ADD_METHOD(c, @selector(controlDoubleAction:), (IMP) wxOSX_controlDoubleAction, "v@:@" )
1110
1111 #if wxUSE_DRAG_AND_DROP
1112 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingEntered:), (IMP) wxOSX_draggingEntered, "I@:@" )
1113 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingUpdated:), (IMP) wxOSX_draggingUpdated, "I@:@" )
1114 wxOSX_CLASS_ADD_METHOD(c, @selector(draggingExited:), (IMP) wxOSX_draggingExited, "v@:@" )
1115 wxOSX_CLASS_ADD_METHOD(c, @selector(performDragOperation:), (IMP) wxOSX_performDragOperation, "c@:@" )
1116 #endif
1117
1118 #if OBJC_API_VERSION < 2
1119 } ;
1120 static int method_count = WXSIZEOF( wxmethods );
1121 static objc_method_list *wxmethodlist = NULL;
1122 if ( wxmethodlist == NULL )
1123 {
1124 wxmethodlist = (objc_method_list*) malloc(sizeof(objc_method_list) + sizeof(wxmethods) );
1125 memcpy( &wxmethodlist->method_list[0], &wxmethods[0], sizeof(wxmethods) );
1126 wxmethodlist->method_count = method_count;
1127 wxmethodlist->obsolete = 0;
1128 }
1129 class_addMethods( c, wxmethodlist );
1130 #endif
1131 }
1132
1133 //
1134 // C++ implementation class
1135 //
1136
1137 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
1138
1139 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
1140 wxWidgetImpl( peer, isRootControl )
1141 {
1142 Init();
1143 m_osxView = w;
1144
1145 // check if the user wants to create the control initially hidden
1146 if ( !peer->IsShown() )
1147 SetVisibility(false);
1148
1149 // gc aware handling
1150 if ( m_osxView )
1151 CFRetain(m_osxView);
1152 [m_osxView release];
1153 }
1154
1155 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
1156 {
1157 Init();
1158 }
1159
1160 void wxWidgetCocoaImpl::Init()
1161 {
1162 m_osxView = NULL;
1163 m_isFlipped = true;
1164 m_lastKeyDownEvent = NULL;
1165 m_hasEditor = false;
1166 }
1167
1168 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
1169 {
1170 RemoveAssociations( this );
1171
1172 if ( !IsRootControl() )
1173 {
1174 NSView *sv = [m_osxView superview];
1175 if ( sv != nil )
1176 [m_osxView removeFromSuperview];
1177 }
1178 // gc aware handling
1179 if ( m_osxView )
1180 CFRelease(m_osxView);
1181 }
1182
1183 bool wxWidgetCocoaImpl::IsVisible() const
1184 {
1185 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
1186 }
1187
1188 void wxWidgetCocoaImpl::SetVisibility( bool visible )
1189 {
1190 [m_osxView setHidden:(visible ? NO:YES)];
1191 }
1192
1193 void wxWidgetCocoaImpl::Raise()
1194 {
1195 // Not implemented
1196 }
1197
1198 void wxWidgetCocoaImpl::Lower()
1199 {
1200 // Not implemented
1201 }
1202
1203 void wxWidgetCocoaImpl::ScrollRect( const wxRect *WXUNUSED(rect), int WXUNUSED(dx), int WXUNUSED(dy) )
1204 {
1205 #if 1
1206 SetNeedsDisplay() ;
1207 #else
1208 // We should do something like this, but it wasn't working in 10.4.
1209 if (GetNeedsDisplay() )
1210 {
1211 SetNeedsDisplay() ;
1212 }
1213 NSRect r = wxToNSRect( [m_osxView superview], *rect );
1214 NSSize offset = NSMakeSize((float)dx, (float)dy);
1215 [m_osxView scrollRect:r by:offset];
1216 #endif
1217 }
1218
1219 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
1220 {
1221 wxWindowMac* parent = GetWXPeer()->GetParent();
1222 // under Cocoa we might have a contentView in the wxParent to which we have to
1223 // adjust the coordinates
1224 if (parent && [m_osxView superview] != parent->GetHandle() )
1225 {
1226 int cx = 0,cy = 0,cw = 0,ch = 0;
1227 if ( parent->GetPeer() )
1228 {
1229 parent->GetPeer()->GetContentArea(cx, cy, cw, ch);
1230 x -= cx;
1231 y -= cy;
1232 }
1233 }
1234 [[m_osxView superview] setNeedsDisplayInRect:[m_osxView frame]];
1235 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
1236 [m_osxView setFrame:r];
1237 [[m_osxView superview] setNeedsDisplayInRect:r];
1238
1239 if ([m_osxView respondsToSelector:@selector(trackingTag)] )
1240 {
1241 if ( [(wxNSView*)m_osxView trackingTag] )
1242 [m_osxView removeTrackingRect: [(wxNSView*)m_osxView trackingTag]];
1243
1244 [(wxNSView*)m_osxView setTrackingTag: [m_osxView addTrackingRect: [m_osxView bounds] owner: m_osxView userData: nil assumeInside: NO]];
1245 }
1246 }
1247
1248 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
1249 {
1250 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
1251 x = r.GetLeft();
1252 y = r.GetTop();
1253 }
1254
1255 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
1256 {
1257 NSRect rect = [m_osxView frame];
1258 width = (int)rect.size.width;
1259 height = (int)rect.size.height;
1260 }
1261
1262 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
1263 {
1264 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
1265 {
1266 NSView* cv = [m_osxView contentView];
1267
1268 NSRect bounds = [m_osxView bounds];
1269 NSRect rect = [cv frame];
1270
1271 int y = (int)rect.origin.y;
1272 int x = (int)rect.origin.x;
1273 if ( ![ m_osxView isFlipped ] )
1274 y = (int)(bounds.size.height - (rect.origin.y + rect.size.height));
1275 left = x;
1276 top = y;
1277 width = (int)rect.size.width;
1278 height = (int)rect.size.height;
1279 }
1280 else
1281 {
1282 left = top = 0;
1283 GetSize( width, height );
1284 }
1285 }
1286
1287 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
1288 {
1289 if ( where )
1290 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
1291 else
1292 [m_osxView setNeedsDisplay:YES];
1293 }
1294
1295 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
1296 {
1297 return [m_osxView needsDisplay];
1298 }
1299
1300 bool wxWidgetCocoaImpl::CanFocus() const
1301 {
1302 return [m_osxView canBecomeKeyView] == YES;
1303 }
1304
1305 bool wxWidgetCocoaImpl::HasFocus() const
1306 {
1307 return ( FindFocus() == m_osxView );
1308 }
1309
1310 bool wxWidgetCocoaImpl::SetFocus()
1311 {
1312 if ( [m_osxView canBecomeKeyView] == NO )
1313 return false;
1314
1315 [[m_osxView window] makeFirstResponder: m_osxView] ;
1316 [[m_osxView window] makeKeyAndOrderFront:nil] ;
1317 return true;
1318 }
1319
1320
1321 void wxWidgetCocoaImpl::RemoveFromParent()
1322 {
1323 [m_osxView removeFromSuperview];
1324 }
1325
1326 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
1327 {
1328 NSView* container = parent->GetWXWidget() ;
1329 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
1330 [container addSubview:m_osxView];
1331 }
1332
1333 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
1334 {
1335 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
1336 }
1337
1338 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
1339 {
1340 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
1341 {
1342 wxCFStringRef cf( title , encoding );
1343 [m_osxView setTitle:cf.AsNSString()];
1344 }
1345 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
1346 {
1347 wxCFStringRef cf( title , encoding );
1348 [m_osxView setStringValue:cf.AsNSString()];
1349 }
1350 }
1351
1352
1353 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
1354 {
1355 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
1356 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
1357 *pt = wxFromNSPoint( to->GetWXWidget(), p );
1358 }
1359
1360 wxInt32 wxWidgetCocoaImpl::GetValue() const
1361 {
1362 return [(NSControl*)m_osxView intValue];
1363 }
1364
1365 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
1366 {
1367 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
1368 {
1369 [m_osxView setIntValue:v];
1370 }
1371 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
1372 {
1373 [m_osxView setFloatValue:(double)v];
1374 }
1375 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
1376 {
1377 [m_osxView setDoubleValue:(double)v];
1378 }
1379 }
1380
1381 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
1382 {
1383 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
1384 {
1385 [m_osxView setMinValue:(double)v];
1386 }
1387 }
1388
1389 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
1390 {
1391 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
1392 {
1393 [m_osxView setMaxValue:(double)v];
1394 }
1395 }
1396
1397 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
1398 {
1399 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
1400 {
1401 return (int)[m_osxView minValue];
1402 }
1403 return 0;
1404 }
1405
1406 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
1407 {
1408 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
1409 {
1410 return (int)[m_osxView maxValue];
1411 }
1412 return 0;
1413 }
1414
1415 wxBitmap wxWidgetCocoaImpl::GetBitmap() const
1416 {
1417 wxBitmap bmp;
1418
1419 // TODO: how to create a wxBitmap from NSImage?
1420 #if 0
1421 if ( [m_osxView respondsToSelector:@selector(image:)] )
1422 bmp = [m_osxView image];
1423 #endif
1424
1425 return bmp;
1426 }
1427
1428 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
1429 {
1430 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
1431 {
1432 [m_osxView setImage:bitmap.GetNSImage()];
1433 }
1434 }
1435
1436 void wxWidgetCocoaImpl::SetBitmapPosition( wxDirection dir )
1437 {
1438 if ( [m_osxView respondsToSelector:@selector(setImagePosition:)] )
1439 {
1440 NSCellImagePosition pos;
1441 switch ( dir )
1442 {
1443 case wxLEFT:
1444 pos = NSImageLeft;
1445 break;
1446
1447 case wxRIGHT:
1448 pos = NSImageRight;
1449 break;
1450
1451 case wxTOP:
1452 pos = NSImageAbove;
1453 break;
1454
1455 case wxBOTTOM:
1456 pos = NSImageBelow;
1457 break;
1458
1459 default:
1460 wxFAIL_MSG( "invalid image position" );
1461 pos = NSNoImage;
1462 }
1463
1464 [m_osxView setImagePosition:pos];
1465 }
1466 }
1467
1468 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& WXUNUSED(notebook))
1469 {
1470 // implementation in subclass
1471 }
1472
1473 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
1474 {
1475 r->x = r->y = r->width = r->height = 0;
1476
1477 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
1478 {
1479 NSRect former = [m_osxView frame];
1480 [m_osxView sizeToFit];
1481 NSRect best = [m_osxView frame];
1482 [m_osxView setFrame:former];
1483 r->width = (int)best.size.width;
1484 r->height = (int)best.size.height;
1485 }
1486 }
1487
1488 bool wxWidgetCocoaImpl::IsEnabled() const
1489 {
1490 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
1491 return [m_osxView isEnabled];
1492 return true;
1493 }
1494
1495 void wxWidgetCocoaImpl::Enable( bool enable )
1496 {
1497 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
1498 [m_osxView setEnabled:enable];
1499 }
1500
1501 void wxWidgetCocoaImpl::PulseGauge()
1502 {
1503 }
1504
1505 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 WXUNUSED(val), wxInt32 WXUNUSED(view) )
1506 {
1507 }
1508
1509 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
1510 {
1511 NSControlSize size = NSRegularControlSize;
1512
1513 switch ( variant )
1514 {
1515 case wxWINDOW_VARIANT_NORMAL :
1516 size = NSRegularControlSize;
1517 break ;
1518
1519 case wxWINDOW_VARIANT_SMALL :
1520 size = NSSmallControlSize;
1521 break ;
1522
1523 case wxWINDOW_VARIANT_MINI :
1524 size = NSMiniControlSize;
1525 break ;
1526
1527 case wxWINDOW_VARIANT_LARGE :
1528 size = NSRegularControlSize;
1529 break ;
1530
1531 default:
1532 wxFAIL_MSG(wxT("unexpected window variant"));
1533 break ;
1534 }
1535 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
1536 [m_osxView setControlSize:size];
1537 else if ([m_osxView respondsToSelector:@selector(cell)])
1538 {
1539 id cell = [(id)m_osxView cell];
1540 if ([cell respondsToSelector:@selector(setControlSize:)])
1541 [cell setControlSize:size];
1542 }
1543 }
1544
1545 void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool)
1546 {
1547 if ([m_osxView respondsToSelector:@selector(setFont:)])
1548 [m_osxView setFont: font.OSXGetNSFont()];
1549 }
1550
1551 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
1552 {
1553 WXWidget c = control ? control : (WXWidget) m_osxView;
1554 wxWidgetImpl::Associate( c, this ) ;
1555 if ([c respondsToSelector:@selector(setAction:)])
1556 {
1557 [c setTarget: c];
1558 [c setAction: @selector(controlAction:)];
1559 if ([c respondsToSelector:@selector(setDoubleAction:)])
1560 {
1561 [c setDoubleAction: @selector(controlDoubleAction:)];
1562 }
1563
1564 }
1565 }
1566
1567 bool wxWidgetCocoaImpl::DoHandleCharEvent(NSEvent *event, NSString *text)
1568 {
1569 wxKeyEvent wxevent(wxEVT_CHAR);
1570 SetupKeyEvent( wxevent, event, text );
1571
1572 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
1573 }
1574
1575 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
1576 {
1577 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
1578 SetupKeyEvent( wxevent, event );
1579 bool result = GetWXPeer()->OSXHandleKeyEvent(wxevent);
1580
1581 // this will fire higher level events, like insertText, to help
1582 // us handle EVT_CHAR, etc.
1583
1584 if ( m_wxPeer->MacIsUserPane() && [event type] == NSKeyDown)
1585 {
1586 if ( !result )
1587 {
1588 if ( [m_osxView isKindOfClass:[NSScrollView class] ] )
1589 [[(NSScrollView*)m_osxView documentView] interpretKeyEvents:[NSArray arrayWithObject:event]];
1590 else
1591 [m_osxView interpretKeyEvents:[NSArray arrayWithObject:event]];
1592 result = true;
1593 }
1594 }
1595
1596 return result;
1597 }
1598
1599 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
1600 {
1601 NSPoint clickLocation;
1602 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
1603 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
1604 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
1605 SetupMouseEvent(wxevent , event) ;
1606 wxevent.m_x = pt.x;
1607 wxevent.m_y = pt.y;
1608
1609 return GetWXPeer()->HandleWindowEvent(wxevent);
1610 }
1611
1612 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
1613 {
1614 wxWindow* thisWindow = GetWXPeer();
1615 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
1616 {
1617 thisWindow->MacInvalidateBorders();
1618 }
1619
1620 if ( receivedFocus )
1621 {
1622 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
1623 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
1624 thisWindow->HandleWindowEvent(eventFocus);
1625
1626 #if wxUSE_CARET
1627 if ( thisWindow->GetCaret() )
1628 thisWindow->GetCaret()->OnSetFocus();
1629 #endif
1630
1631 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
1632 event.SetEventObject(thisWindow);
1633 if (otherWindow)
1634 event.SetWindow(otherWindow->GetWXPeer());
1635 thisWindow->HandleWindowEvent(event) ;
1636 }
1637 else // !receivedFocuss
1638 {
1639 #if wxUSE_CARET
1640 if ( thisWindow->GetCaret() )
1641 thisWindow->GetCaret()->OnKillFocus();
1642 #endif
1643
1644 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
1645
1646 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
1647 event.SetEventObject(thisWindow);
1648 if (otherWindow)
1649 event.SetWindow(otherWindow->GetWXPeer());
1650 thisWindow->HandleWindowEvent(event) ;
1651 }
1652 }
1653
1654 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
1655 {
1656 NSPoint location = [NSEvent mouseLocation];
1657 location = [[m_osxView window] convertScreenToBase:location];
1658 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
1659
1660 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
1661 {
1662 [(NSCursor*)cursor.GetHCURSOR() set];
1663 }
1664 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
1665 }
1666
1667 void wxWidgetCocoaImpl::CaptureMouse()
1668 {
1669 [[m_osxView window] disableCursorRects];
1670 }
1671
1672 void wxWidgetCocoaImpl::ReleaseMouse()
1673 {
1674 [[m_osxView window] enableCursorRects];
1675 }
1676
1677 void wxWidgetCocoaImpl::SetFlipped(bool flipped)
1678 {
1679 m_isFlipped = flipped;
1680 }
1681
1682 //
1683 // Factory methods
1684 //
1685
1686 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
1687 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
1688 long WXUNUSED(style), long WXUNUSED(extraStyle))
1689 {
1690 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1691 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
1692
1693 // temporary hook for dnd
1694 [v registerForDraggedTypes:[NSArray arrayWithObjects:
1695 NSStringPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPICTPboardType, NSPDFPboardType, nil]];
1696
1697 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
1698 return c;
1699 }
1700
1701 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
1702 {
1703 NSWindow* tlw = now->GetWXWindow();
1704 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
1705 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
1706 c->InstallEventHandler();
1707 [tlw setContentView:v];
1708 return c;
1709 }