]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/window.mm
adapting to name change in r58318
[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/nonownedwnd.h"
16 #include "wx/log.h"
17 #endif
18
19 #ifdef __WXMAC__
20 #include "wx/osx/private.h"
21 #endif
22
23 #if wxUSE_CARET
24 #include "wx/caret.h"
25 #endif
26
27 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
28 {
29 int x, y, w, h ;
30
31 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
32 wxRect bounds(x,y,w,h);
33 NSView* sv = (window->GetParent()->GetHandle() );
34
35 return wxToNSRect( sv, bounds );
36 }
37
38 @interface wxNSView : NSView
39 {
40 WXCOCOAIMPL_COMMON_MEMBERS
41 }
42
43 - (void)drawRect: (NSRect) rect;
44
45 WXCOCOAIMPL_COMMON_INTERFACE
46
47 - (BOOL) canBecomeKeyView;
48
49 @end // wxNSView
50
51 @interface NSView(PossibleMethods)
52 - (void)setImplementation:(wxWidgetCocoaImpl *)theImplementation;
53 - (void)setTitle:(NSString *)aString;
54 - (void)setStringValue:(NSString *)aString;
55 - (void)setIntValue:(int)anInt;
56 - (void)setFloatValue:(float)aFloat;
57 - (void)setDoubleValue:(double)aDouble;
58
59 - (double)minValue;
60 - (double)maxValue;
61 - (void)setMinValue:(double)aDouble;
62 - (void)setMaxValue:(double)aDouble;
63
64 - (void)sizeToFit;
65
66 - (BOOL)isEnabled;
67 - (void)setEnabled:(BOOL)flag;
68
69 - (void)setImage:(NSImage *)image;
70 - (void)setControlSize:(NSControlSize)size;
71
72 - (id)contentView;
73 @end
74
75 long wxOSXTranslateCocoaKey( int unichar )
76 {
77 long retval = unichar;
78 switch( unichar )
79 {
80 case NSUpArrowFunctionKey :
81 retval = WXK_UP;
82 break;
83 case NSDownArrowFunctionKey :
84 retval = WXK_DOWN;
85 break;
86 case NSLeftArrowFunctionKey :
87 retval = WXK_LEFT;
88 break;
89 case NSRightArrowFunctionKey :
90 retval = WXK_RIGHT;
91 break;
92 case NSInsertFunctionKey :
93 retval = WXK_INSERT;
94 break;
95 case NSDeleteFunctionKey :
96 retval = WXK_DELETE;
97 break;
98 case NSHomeFunctionKey :
99 retval = WXK_HOME;
100 break;
101 // case NSBeginFunctionKey :
102 // retval = WXK_BEGIN;
103 // break;
104 case NSEndFunctionKey :
105 retval = WXK_END;
106 break;
107 case NSPageUpFunctionKey :
108 retval = WXK_PAGEUP;
109 break;
110 case NSPageDownFunctionKey :
111 retval = WXK_PAGEDOWN;
112 break;
113 case NSHelpFunctionKey :
114 retval = WXK_HELP;
115 break;
116
117 default :
118 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
119 retval = WXK_F1 + (unichar - NSF1FunctionKey );
120 break;
121 }
122 return retval;
123 }
124
125 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
126 {
127 UInt32 modifiers = [nsEvent modifierFlags] ;
128 int eventType = [nsEvent type];
129
130 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
131 wxevent.m_controlDown = modifiers & NSControlKeyMask;
132 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
133 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
134
135 wxevent.m_rawCode = [nsEvent keyCode];
136 wxevent.m_rawFlags = modifiers;
137
138 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
139 switch (eventType)
140 {
141 case NSKeyDown :
142 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
143 break;
144 case NSKeyUp :
145 wxevent.SetEventType( wxEVT_KEY_UP ) ;
146 break;
147 case NSFlagsChanged :
148 // setup common code here
149 break;
150 default :
151 break ;
152 }
153
154 wxString chars;
155 if ( eventType != NSFlagsChanged )
156 {
157 NSString* nschars = [nsEvent characters];
158 if ( nschars )
159 {
160 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
161 chars = cfchars.AsString();
162 }
163 }
164
165 int unichar = chars.Length() > 0 ? chars[0] : 0;
166 long keyval = wxOSXTranslateCocoaKey(unichar) ;
167 if ( keyval == unichar && ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) )
168 keyval = wxToupper( keyval ) ;
169
170 #if wxUSE_UNICODE
171 wxevent.m_uniChar = unichar;
172 #endif
173 wxevent.m_keyCode = keyval;
174 }
175
176 UInt32 g_lastButton = 0 ;
177 bool g_lastButtonWasFakeRight = false ;
178
179 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
180 {
181 UInt32 modifiers = [nsEvent modifierFlags] ;
182 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
183
184 // these parameters are not given for all events
185 UInt32 button = [nsEvent buttonNumber];
186 UInt32 clickCount = [nsEvent clickCount];
187
188 wxevent.m_x = screenMouseLocation.x;
189 wxevent.m_y = screenMouseLocation.y;
190 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
191 wxevent.m_controlDown = modifiers & NSControlKeyMask;
192 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
193 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
194 wxevent.m_clickCount = clickCount;
195 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
196
197 UInt32 mouseChord = 0;
198 int eventType = [nsEvent type];
199
200 switch (eventType)
201 {
202 case NSLeftMouseDown :
203 case NSLeftMouseDragged :
204 mouseChord = 1U;
205 break;
206 case NSRightMouseDown :
207 case NSRightMouseDragged :
208 mouseChord = 2U;
209 break;
210 case NSOtherMouseDown :
211 case NSOtherMouseDragged :
212 mouseChord = 4U;
213 break;
214 }
215
216 // a control click is interpreted as a right click
217 bool thisButtonIsFakeRight = false ;
218 if ( button == 0 && (modifiers & NSControlKeyMask) )
219 {
220 button = 1 ;
221 thisButtonIsFakeRight = true ;
222 }
223
224 // otherwise we report double clicks by connecting a left click with a ctrl-left click
225 if ( clickCount > 1 && button != g_lastButton )
226 clickCount = 1 ;
227
228 // we must make sure that our synthetic 'right' button corresponds in
229 // mouse down, moved and mouse up, and does not deliver a right down and left up
230 switch (eventType)
231 {
232 case NSLeftMouseDown :
233 case NSRightMouseDown :
234 case NSOtherMouseDown :
235 g_lastButton = button ;
236 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
237 break;
238 }
239
240 if ( button == 0 )
241 {
242 g_lastButton = 0 ;
243 g_lastButtonWasFakeRight = false ;
244 }
245 else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
246 button = g_lastButton ;
247
248 // Adjust the chord mask to remove the primary button and add the
249 // secondary button. It is possible that the secondary button is
250 // already pressed, e.g. on a mouse connected to a laptop, but this
251 // possibility is ignored here:
252 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
253 mouseChord = ((mouseChord & ~1U) | 2U);
254
255 if(mouseChord & 1U)
256 wxevent.m_leftDown = true ;
257 if(mouseChord & 2U)
258 wxevent.m_rightDown = true ;
259 if(mouseChord & 4U)
260 wxevent.m_middleDown = true ;
261
262 // translate into wx types
263 switch (eventType)
264 {
265 case NSLeftMouseDown :
266 case NSRightMouseDown :
267 case NSOtherMouseDown :
268 switch ( button )
269 {
270 case 0 :
271 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
272 break ;
273
274 case 1 :
275 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
276 break ;
277
278 case 2 :
279 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
280 break ;
281
282 default:
283 break ;
284 }
285 break ;
286
287 case NSLeftMouseUp :
288 case NSRightMouseUp :
289 case NSOtherMouseUp :
290 switch ( button )
291 {
292 case 0 :
293 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
294 break ;
295
296 case 1 :
297 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
298 break ;
299
300 case 2 :
301 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
302 break ;
303
304 default:
305 break ;
306 }
307 break ;
308
309 case NSScrollWheel :
310 {
311 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
312 /*
313 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
314 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
315 */
316 wxevent.m_wheelRotation = 10; // delta;
317 wxevent.m_wheelDelta = 1;
318 wxevent.m_linesPerAction = 1;
319 if ( 0 /* axis == kEventMouseWheelAxisX*/ )
320 wxevent.m_wheelAxis = 1;
321 }
322 break ;
323
324 case NSMouseEntered :
325 case NSMouseExited :
326 case NSLeftMouseDragged :
327 case NSRightMouseDragged :
328 case NSOtherMouseDragged :
329 case NSMouseMoved :
330 wxevent.SetEventType( wxEVT_MOTION ) ;
331 break;
332 default :
333 break ;
334 }
335 }
336
337 @implementation wxNSView
338
339 #define OSX_DEBUG_DRAWING 0
340
341 - (void)drawRect: (NSRect) rect
342 {
343 if ( impl )
344 {
345 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
346 CGContextSaveGState( context );
347 #if OSX_DEBUG_DRAWING
348 CGContextBeginPath( context );
349 CGContextMoveToPoint(context, 0, 0);
350 NSRect bounds = [self bounds];
351 CGContextAddLineToPoint(context, 10, 0);
352 CGContextMoveToPoint(context, 0, 0);
353 CGContextAddLineToPoint(context, 0, 10);
354 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
355 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
356 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
357 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
358 CGContextClosePath( context );
359 CGContextStrokePath(context);
360 #endif
361
362 if ( [ self isFlipped ] == NO )
363 {
364 CGContextTranslateCTM( context, 0, [self bounds].size.height );
365 CGContextScaleCTM( context, 1, -1 );
366 }
367
368 wxRegion updateRgn;
369 const NSRect *rects;
370 NSInteger count;
371
372 [self getRectsBeingDrawn:&rects count:&count];
373 for ( int i = 0 ; i < count ; ++i )
374 {
375 updateRgn.Union(wxFromNSRect(self, rects[i]) );
376 }
377
378 wxWindow* wxpeer = impl->GetWXPeer();
379 wxpeer->GetUpdateRegion() = updateRgn;
380 wxpeer->MacSetCGContextRef( context );
381
382 wxPaintEvent event;
383 event.SetTimestamp(0); // todo
384 event.SetEventObject(wxpeer);
385 wxpeer->HandleWindowEvent(event);
386
387 CGContextRestoreGState( context );
388 }
389 }
390
391 WXCOCOAIMPL_COMMON_IMPLEMENTATION
392
393 - (BOOL) canBecomeKeyView
394 {
395 return YES;
396 }
397
398 @end // wxNSView
399
400 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
401
402 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
403 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
404 {
405 }
406
407 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
408 {
409 }
410
411 void wxWidgetCocoaImpl::Init()
412 {
413 m_osxView = NULL;
414 }
415
416 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
417 {
418 if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] )
419 [m_osxView setImplementation:NULL];
420 if ( !IsRootControl() )
421 {
422 NSView *sv = [m_osxView superview];
423 if ( sv != nil )
424 [m_osxView removeFromSuperview];
425 }
426 [m_osxView release];
427 }
428
429 bool wxWidgetCocoaImpl::IsVisible() const
430 {
431 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
432 }
433
434 void wxWidgetCocoaImpl::SetVisibility( bool visible )
435 {
436 [m_osxView setHidden:(visible ? NO:YES)];
437 }
438
439 void wxWidgetCocoaImpl::Raise()
440 {
441 }
442
443 void wxWidgetCocoaImpl::Lower()
444 {
445 }
446
447 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
448 {
449 #if 1
450 SetNeedsDisplay() ;
451 #else
452 // We should do something like this, but it wasn't working in 10.4.
453 if (GetNeedsDisplay() )
454 {
455 SetNeedsDisplay() ;
456 }
457 NSRect r = wxToNSRect( [m_osxView superview], *rect );
458 NSSize offset = NSMakeSize((float)dx, (float)dy);
459 [m_osxView scrollRect:r by:offset];
460 #endif
461 }
462
463 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
464 {
465 wxWindowMac* parent = GetWXPeer()->GetParent();
466 // under Cocoa we might have a contentView in the wxParent to which we have to
467 // adjust the coordinates
468 if (parent)
469 {
470 wxPoint pt(parent->GetClientAreaOrigin());
471 x -= pt.x;
472 y -= pt.y;
473 }
474 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
475 [m_osxView setFrame:r];
476 }
477
478 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
479 {
480 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
481 x = r.GetLeft();
482 y = r.GetTop();
483 }
484
485 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
486 {
487 NSRect rect = [m_osxView frame];
488 width = rect.size.width;
489 height = rect.size.height;
490 }
491
492 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
493 {
494 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
495 {
496 NSView* cv = [m_osxView contentView];
497
498 NSRect bounds = [m_osxView bounds];
499 NSRect rect = [cv frame];
500
501 int y = rect.origin.y;
502 int x = rect.origin.x;
503 if ( ![ m_osxView isFlipped ] )
504 y = bounds.size.height - (rect.origin.y + rect.size.height);
505 left = x;
506 top = y;
507 width = rect.size.width;
508 height = rect.size.height;
509 }
510 else
511 {
512 left = top = 0;
513 GetSize( width, height );
514 }
515 }
516
517 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
518 {
519 if ( where )
520 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
521 else
522 [m_osxView setNeedsDisplay:YES];
523 }
524
525 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
526 {
527 return [m_osxView needsDisplay];
528 }
529
530 bool wxWidgetCocoaImpl::CanFocus() const
531 {
532 return [m_osxView canBecomeKeyView] == YES;
533 }
534
535 bool wxWidgetCocoaImpl::HasFocus() const
536 {
537 return ( [[m_osxView window] firstResponder] == m_osxView );
538 }
539
540 bool wxWidgetCocoaImpl::SetFocus()
541 {
542 if ( [m_osxView canBecomeKeyView] == NO )
543 return false;
544
545 [[m_osxView window] makeFirstResponder: m_osxView] ;
546 return true;
547 }
548
549
550 void wxWidgetCocoaImpl::RemoveFromParent()
551 {
552 [m_osxView removeFromSuperview];
553 }
554
555 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
556 {
557 NSView* container = parent->GetWXWidget() ;
558 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
559 [container addSubview:m_osxView];
560 }
561
562 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
563 {
564 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
565 }
566
567 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
568 {
569 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
570 {
571 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
572 [m_osxView setTitle:cf.AsNSString()];
573 }
574 }
575
576
577 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
578 {
579 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
580 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
581 *pt = wxFromNSPoint( to->GetWXWidget(), p );
582 }
583
584 wxInt32 wxWidgetCocoaImpl::GetValue() const
585 {
586 return [(NSControl*)m_osxView intValue];
587 }
588
589 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
590 {
591 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
592 {
593 [m_osxView setIntValue:v];
594 }
595 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
596 {
597 [m_osxView setFloatValue:(double)v];
598 }
599 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
600 {
601 [m_osxView setDoubleValue:(double)v];
602 }
603 }
604
605 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
606 {
607 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
608 {
609 [m_osxView setMinValue:(double)v];
610 }
611 }
612
613 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
614 {
615 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
616 {
617 [m_osxView setMaxValue:(double)v];
618 }
619 }
620
621 wxInt32 wxWidgetCocoaImpl::GetMinimum() const
622 {
623 if ( [m_osxView respondsToSelector:@selector(getMinValue:)] )
624 {
625 return [m_osxView minValue];
626 }
627 return 0;
628 }
629
630 wxInt32 wxWidgetCocoaImpl::GetMaximum() const
631 {
632 if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] )
633 {
634 return [m_osxView maxValue];
635 }
636 return 0;
637 }
638
639 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
640 {
641 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
642 {
643 [m_osxView setImage:bitmap.GetNSImage()];
644 }
645 }
646
647 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
648 {
649 // implementation in subclass
650 }
651
652 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
653 {
654 r->x = r->y = r->width = r->height = 0;
655 // if ( [m_osxView isKindOfClass:[NSControl class]] )
656 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
657 {
658 NSRect former = [m_osxView frame];
659 [m_osxView sizeToFit];
660 NSRect best = [m_osxView frame];
661 [m_osxView setFrame:former];
662 r->width = best.size.width;
663 r->height = best.size.height;
664 }
665 }
666
667 bool wxWidgetCocoaImpl::IsEnabled() const
668 {
669 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
670 return [m_osxView isEnabled];
671 return true;
672 }
673
674 void wxWidgetCocoaImpl::Enable( bool enable )
675 {
676 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
677 [m_osxView setEnabled:enable];
678 }
679
680 void wxWidgetCocoaImpl::PulseGauge()
681 {
682 }
683
684 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
685 {
686 }
687
688 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
689 {
690 NSControlSize size = NSRegularControlSize;
691
692 switch ( variant )
693 {
694 case wxWINDOW_VARIANT_NORMAL :
695 size = NSRegularControlSize;
696 break ;
697
698 case wxWINDOW_VARIANT_SMALL :
699 size = NSSmallControlSize;
700 break ;
701
702 case wxWINDOW_VARIANT_MINI :
703 size = NSMiniControlSize;
704 break ;
705
706 case wxWINDOW_VARIANT_LARGE :
707 size = NSRegularControlSize;
708 break ;
709
710 default:
711 wxFAIL_MSG(_T("unexpected window variant"));
712 break ;
713 }
714 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
715 [m_osxView setControlSize:size];
716 }
717
718 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
719 {
720 // TODO
721 }
722
723 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
724 {
725 }
726
727 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
728 {
729 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
730 SetupKeyEvent( wxevent, event );
731
732 return GetWXPeer()->OSXHandleKeyEvent(wxevent);
733 }
734
735 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
736 {
737 NSPoint clickLocation;
738 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
739 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
740 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
741 SetupMouseEvent( wxevent , event ) ;
742 wxevent.m_x = pt.x;
743 wxevent.m_y = pt.y;
744
745 return GetWXPeer()->HandleWindowEvent(wxevent);
746 }
747
748 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
749 {
750 wxWindow* thisWindow = GetWXPeer();
751 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
752 {
753 thisWindow->MacInvalidateBorders();
754 }
755
756 if ( receivedFocus )
757 {
758 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
759 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
760 thisWindow->HandleWindowEvent(eventFocus);
761
762 #if wxUSE_CARET
763 if ( thisWindow->GetCaret() )
764 thisWindow->GetCaret()->OnSetFocus();
765 #endif
766
767 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
768 event.SetEventObject(thisWindow);
769 // TODO how to find out the targetFocusWindow ?
770 // event.SetWindow(targetFocusWindow);
771 thisWindow->HandleWindowEvent(event) ;
772 }
773 else // !receivedFocuss
774 {
775 #if wxUSE_CARET
776 if ( thisWindow->GetCaret() )
777 thisWindow->GetCaret()->OnKillFocus();
778 #endif
779
780 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
781
782 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
783 event.SetEventObject(thisWindow);
784 // TODO how to find out the targetFocusWindow ?
785 // event.SetWindow(targetFocusWindow);
786 thisWindow->HandleWindowEvent(event) ;
787 }
788 }
789
790 void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor)
791 {
792 NSPoint location = [NSEvent mouseLocation];
793 location = [[m_osxView window] convertScreenToBase:location];
794 NSPoint locationInView = [m_osxView convertPoint:location fromView:nil];
795
796 if( NSMouseInRect(locationInView, [m_osxView bounds], YES) )
797 {
798 [(NSCursor*)cursor.GetHCURSOR() set];
799 }
800 [[m_osxView window] invalidateCursorRectsForView:m_osxView];
801 }
802
803 void wxWidgetCocoaImpl::CaptureMouse()
804 {
805 [[m_osxView window] disableCursorRects];
806 }
807
808 void wxWidgetCocoaImpl::ReleaseMouse()
809 {
810 [[m_osxView window] enableCursorRects];
811 }
812
813 //
814 // Factory methods
815 //
816
817 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
818 long style, long extraStyle)
819 {
820 NSView* sv = (wxpeer->GetParent()->GetHandle() );
821
822 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
823 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
824 [sv addSubview:v];
825 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
826 [v setImplementation:c];
827 return c;
828 }
829
830 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
831 {
832 NSWindow* tlw = now->GetWXWindow();
833 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
834 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
835 [v setImplementation:c];
836 [tlw setContentView:v];
837 return c;
838 }