1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/window.mm
3 // Purpose: widgets (non tlw) for cocoa
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: window.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include <Cocoa/Cocoa.h>
17 #include "wx/osx/private.h"
20 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
24 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
25 wxRect bounds(x,y,w,h);
26 NSView* sv = (window->GetParent()->GetHandle() );
28 return wxToNSRect( sv, bounds );
31 @interface wxNSView : NSView
36 - (void)drawRect: (NSRect) rect;
38 -(void)mouseDown:(NSEvent *)event ;
39 -(void)rightMouseDown:(NSEvent *)event ;
40 -(void)otherMouseDown:(NSEvent *)event ;
41 -(void)mouseUp:(NSEvent *)event ;
42 -(void)rightMouseUp:(NSEvent *)event ;
43 -(void)otherMouseUp:(NSEvent *)event ;
44 -(void)handleMouseEvent:(NSEvent *)event;
46 - (void)keyDown:(NSEvent *)event;
47 - (void)keyUp:(NSEvent *)event;
48 - (void)flagsChanged:(NSEvent *)event;
49 - (void)handleKeyEvent:(NSEvent *)event;
51 - (void)setImplementation: (wxWidgetImpl *) theImplementation;
52 - (wxWidgetImpl*) implementation;
54 - (BOOL) becomeFirstResponder;
55 - (BOOL) resignFirstResponder;
56 - (BOOL) canBecomeKeyView;
60 long wxOSXTranslateCocoaKey(unsigned short code, int unichar )
65 case NSUpArrowFunctionKey :
68 case NSDownArrowFunctionKey :
71 case NSLeftArrowFunctionKey :
74 case NSRightArrowFunctionKey :
77 case NSInsertFunctionKey :
80 case NSDeleteFunctionKey :
83 case NSHomeFunctionKey :
86 // case NSBeginFunctionKey :
87 // retval = WXK_BEGIN;
89 case NSEndFunctionKey :
92 case NSPageUpFunctionKey :
95 case NSPageDownFunctionKey :
96 retval = WXK_PAGEDOWN;
98 case NSHelpFunctionKey :
103 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
104 retval = WXK_F1 + (unichar - NSF1FunctionKey );
110 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
112 UInt32 modifiers = [nsEvent modifierFlags] ;
114 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
115 wxevent.m_controlDown = modifiers & NSControlKeyMask;
116 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
117 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
120 NSString* nschars = [nsEvent characters];
123 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
124 chars = cfchars.AsString();
127 int unichar = chars.Length() > 0 ? chars[0] : 0;
130 wxevent.m_uniChar = unichar;
132 wxevent.m_keyCode = wxOSXTranslateCocoaKey( [nsEvent keyCode], unichar ) ;
133 // wxevent.m_rawCode = keymessage;
134 wxevent.m_rawFlags = modifiers;
136 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
137 int eventType = [nsEvent type];
141 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
144 wxevent.SetEventType( wxEVT_KEY_UP ) ;
146 case NSFlagsChanged :
147 // setup common code here
154 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
156 UInt32 modifiers = [nsEvent modifierFlags] ;
157 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
159 // these parameters are not given for all events
160 UInt32 button = [nsEvent buttonNumber];
161 UInt32 clickCount = [nsEvent clickCount];
162 UInt32 mouseChord = 0; // TODO does this exist for cocoa
164 wxevent.m_x = screenMouseLocation.x;
165 wxevent.m_y = screenMouseLocation.y;
166 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
167 wxevent.m_controlDown = modifiers & NSControlKeyMask;
168 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
169 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
170 wxevent.m_clickCount = clickCount;
171 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
173 // a control click is interpreted as a right click
174 bool thisButtonIsFakeRight = false ;
175 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
177 button = kEventMouseButtonSecondary ;
178 thisButtonIsFakeRight = true ;
181 // otherwise we report double clicks by connecting a left click with a ctrl-left click
182 if ( clickCount > 1 && button != g_lastButton )
184 // we must make sure that our synthetic 'right' button corresponds in
185 // mouse down, moved and mouse up, and does not deliver a right down and left up
187 if ( cEvent.GetKind() == kEventMouseDown )
189 g_lastButton = button ;
190 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
196 g_lastButtonWasFakeRight = false ;
198 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
199 button = g_lastButton ;
201 // Adjust the chord mask to remove the primary button and add the
202 // secondary button. It is possible that the secondary button is
203 // already pressed, e.g. on a mouse connected to a laptop, but this
204 // possibility is ignored here:
205 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
206 mouseChord = ((mouseChord & ~1U) | 2U);
209 wxevent.m_leftDown = true ;
211 wxevent.m_rightDown = true ;
213 wxevent.m_middleDown = true ;
216 // translate into wx types
217 int eventType = [nsEvent type];
220 case NSLeftMouseDown :
221 case NSRightMouseDown :
222 case NSOtherMouseDown :
226 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
230 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
234 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
243 case NSRightMouseUp :
244 case NSOtherMouseUp :
248 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
252 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
256 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
266 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
268 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
269 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
271 wxevent.m_wheelRotation = 10; // delta;
272 wxevent.m_wheelDelta = 1;
273 wxevent.m_linesPerAction = 1;
274 if ( 0 /* axis == kEventMouseWheelAxisX*/ )
275 wxevent.m_wheelAxis = 1;
279 case NSMouseEntered :
281 case NSLeftMouseDragged :
282 case NSRightMouseDragged :
283 case NSOtherMouseDragged :
285 wxevent.SetEventType( wxEVT_MOTION ) ;
292 @implementation wxNSView
294 #define OSX_DEBUG_DRAWING 0
296 - (void)drawRect: (NSRect) rect
300 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
301 CGContextSaveGState( context );
302 #if OSX_DEBUG_DRAWING
303 CGContextBeginPath( context );
304 CGContextMoveToPoint(context, 0, 0);
305 NSRect bounds = [self bounds];
306 CGContextAddLineToPoint(context, 10, 0);
307 CGContextMoveToPoint(context, 0, 0);
308 CGContextAddLineToPoint(context, 0, 10);
309 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
310 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
311 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
312 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
313 CGContextClosePath( context );
314 CGContextStrokePath(context);
317 if ( [ self isFlipped ] == NO )
319 CGContextTranslateCTM( context, 0, [self bounds].size.height );
320 CGContextScaleCTM( context, 1, -1 );
327 [self getRectsBeingDrawn:&rects count:&count];
328 for ( int i = 0 ; i < count ; ++i )
330 updateRgn.Union(wxFromNSRect(self, rects[i]) );
333 wxWindow* wxpeer = impl->GetWXPeer();
334 wxpeer->GetUpdateRegion() = updateRgn;
335 wxpeer->MacSetCGContextRef( context );
338 event.SetTimestamp(0); // todo
339 event.SetEventObject(wxpeer);
340 wxpeer->HandleWindowEvent(event);
342 CGContextRestoreGState( context );
346 -(void)mouseDown:(NSEvent *)event
348 [self handleMouseEvent:event];
351 -(void)rightMouseDown:(NSEvent *)event
353 [self handleMouseEvent:event];
356 -(void)otherMouseDown:(NSEvent *)event
358 [self handleMouseEvent:event];
361 -(void)mouseUp:(NSEvent *)event
363 [self handleMouseEvent:event];
366 -(void)rightMouseUp:(NSEvent *)event
368 [self handleMouseEvent:event];
371 -(void)otherMouseUp:(NSEvent *)event
373 [self handleMouseEvent:event];
376 -(void)handleMouseEvent:(NSEvent *)event
378 NSPoint clickLocation;
379 clickLocation = [self convertPoint:[event locationInWindow] fromView:nil];
380 wxPoint pt = wxFromNSPoint( self, clickLocation );
381 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
382 SetupMouseEvent( wxevent , event ) ;
385 impl->GetWXPeer()->HandleWindowEvent(wxevent);
388 - (void)keyDown:(NSEvent *)event
390 [self handleKeyEvent:event];
393 - (void)keyUp:(NSEvent *)event
395 [self handleKeyEvent:event];
398 - (void)flagsChanged:(NSEvent *)event
400 [self handleKeyEvent:event];
403 - (void)handleKeyEvent:(NSEvent *)event
405 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
406 SetupKeyEvent( wxevent, event );
407 impl->GetWXPeer()->HandleWindowEvent(wxevent);
411 - (void)setImplementation: (wxWidgetImpl *) theImplementation
413 impl = theImplementation;
416 - (wxWidgetImpl*) implementation
426 - (BOOL) becomeFirstResponder
428 BOOL r = [super becomeFirstResponder];
435 - (BOOL) resignFirstResponder
437 BOOL r = [super resignFirstResponder];
444 - (BOOL) canBecomeKeyView
451 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
453 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
454 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
458 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
462 void wxWidgetCocoaImpl::Init()
467 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
469 if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] )
470 [m_osxView setImplementation:NULL];
471 if ( !IsRootControl() )
473 NSView *sv = [m_osxView superview];
475 [m_osxView removeFromSuperview];
480 bool wxWidgetCocoaImpl::IsVisible() const
482 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
485 void wxWidgetCocoaImpl::SetVisibility( bool visible )
487 [m_osxView setHidden:(visible ? NO:YES)];
490 void wxWidgetCocoaImpl::Raise()
494 void wxWidgetCocoaImpl::Lower()
498 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
502 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
504 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
505 [m_osxView setFrame:r];
508 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
510 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
515 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
517 NSRect rect = [m_osxView frame];
518 width = rect.size.width;
519 height = rect.size.height;
522 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
525 GetSize( width, height );
528 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
531 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
533 [m_osxView setNeedsDisplay:YES];
536 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
538 return [m_osxView needsDisplay];
541 bool wxWidgetCocoaImpl::CanFocus() const
543 return [m_osxView canBecomeKeyView] == YES;
546 bool wxWidgetCocoaImpl::HasFocus() const
548 return ( [[m_osxView window] firstResponder] == m_osxView );
551 bool wxWidgetCocoaImpl::SetFocus()
553 if ( [m_osxView canBecomeKeyView] == NO )
556 [[m_osxView window] makeFirstResponder: m_osxView] ;
561 void wxWidgetCocoaImpl::RemoveFromParent()
563 [m_osxView removeFromSuperview];
566 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
568 NSView* container = parent->GetWXWidget() ;
569 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
570 [container addSubview:m_osxView];
573 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
575 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
578 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
580 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
582 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
583 [m_osxView setTitle:cf.AsNSString()];
588 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
590 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
591 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
592 *pt = wxFromNSPoint( to->GetWXWidget(), p );
595 wxInt32 wxWidgetCocoaImpl::GetValue() const
597 return [(NSControl*)m_osxView intValue];
600 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
602 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
604 [m_osxView setIntValue:v];
606 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
608 [m_osxView setFloatValue:(double)v];
610 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
612 [m_osxView setDoubleValue:(double)v];
616 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
618 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
620 [m_osxView setMinValue:(double)v];
624 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
626 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
628 [m_osxView setMaxValue:(double)v];
632 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
634 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
636 [m_osxView setImage:bitmap.GetNSImage()];
640 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
642 // implementation in subclass
645 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
647 r->x = r->y = r->width = r->height = 0;
648 // if ( [m_osxView isKindOfClass:[NSControl class]] )
649 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
651 NSRect former = [m_osxView frame];
652 [m_osxView sizeToFit];
653 NSRect best = [m_osxView frame];
654 [m_osxView setFrame:former];
655 r->width = best.size.width;
656 r->height = best.size.height;
660 bool wxWidgetCocoaImpl::IsEnabled() const
662 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
663 return [m_osxView isEnabled];
667 void wxWidgetCocoaImpl::Enable( bool enable )
669 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
670 [m_osxView setEnabled:enable];
673 void wxWidgetCocoaImpl::PulseGauge()
677 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
681 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
683 NSControlSize size = NSRegularControlSize;
687 case wxWINDOW_VARIANT_NORMAL :
688 size = NSRegularControlSize;
691 case wxWINDOW_VARIANT_SMALL :
692 size = NSSmallControlSize;
695 case wxWINDOW_VARIANT_MINI :
696 size = NSMiniControlSize;
699 case wxWINDOW_VARIANT_LARGE :
700 size = NSRegularControlSize;
704 wxFAIL_MSG(_T("unexpected window variant"));
707 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
708 [m_osxView setControlSize:size];
711 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
720 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
721 long style, long extraStyle)
723 NSView* sv = (wxpeer->GetParent()->GetHandle() );
725 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
726 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
728 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
729 [v setImplementation:c];
733 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
735 NSWindow* tlw = now->GetWXWindow();
736 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
737 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
738 [v setImplementation:c];
739 [tlw setContentView:v];