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