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"
15 #include "wx/nonownedwnd.h"
19 #include "wx/osx/private.h"
22 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
26 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
27 wxRect bounds(x,y,w,h);
28 NSView* sv = (window->GetParent()->GetHandle() );
30 return wxToNSRect( sv, bounds );
33 @interface wxNSView : NSView
35 WXCOCOAIMPL_COMMON_MEMBERS
38 - (void)drawRect: (NSRect) rect;
40 - (void)keyDown:(NSEvent *)event;
41 - (void)keyUp:(NSEvent *)event;
42 - (void)flagsChanged:(NSEvent *)event;
43 - (void)handleKeyEvent:(NSEvent *)event;
45 WXCOCOAIMPL_COMMON_INTERFACE
47 - (BOOL) becomeFirstResponder;
48 - (BOOL) resignFirstResponder;
49 - (BOOL) canBecomeKeyView;
53 long wxOSXTranslateCocoaKey(unsigned short code, int unichar )
58 case NSUpArrowFunctionKey :
61 case NSDownArrowFunctionKey :
64 case NSLeftArrowFunctionKey :
67 case NSRightArrowFunctionKey :
70 case NSInsertFunctionKey :
73 case NSDeleteFunctionKey :
76 case NSHomeFunctionKey :
79 // case NSBeginFunctionKey :
80 // retval = WXK_BEGIN;
82 case NSEndFunctionKey :
85 case NSPageUpFunctionKey :
88 case NSPageDownFunctionKey :
89 retval = WXK_PAGEDOWN;
91 case NSHelpFunctionKey :
96 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
97 retval = WXK_F1 + (unichar - NSF1FunctionKey );
103 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
105 UInt32 modifiers = [nsEvent modifierFlags] ;
106 int eventType = [nsEvent type];
108 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
109 wxevent.m_controlDown = modifiers & NSControlKeyMask;
110 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
111 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
114 if ( eventType != NSFlagsChanged )
116 NSString* nschars = [nsEvent characters];
119 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
120 chars = cfchars.AsString();
124 int unichar = chars.Length() > 0 ? chars[0] : 0;
127 wxevent.m_uniChar = unichar;
129 wxevent.m_keyCode = wxOSXTranslateCocoaKey( [nsEvent keyCode], unichar ) ;
130 // wxevent.m_rawCode = keymessage;
131 wxevent.m_rawFlags = modifiers;
133 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
137 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
140 wxevent.SetEventType( wxEVT_KEY_UP ) ;
142 case NSFlagsChanged :
143 // setup common code here
150 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
152 UInt32 modifiers = [nsEvent modifierFlags] ;
153 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
155 // these parameters are not given for all events
156 UInt32 button = [nsEvent buttonNumber];
157 UInt32 clickCount = [nsEvent clickCount];
158 UInt32 mouseChord = 0; // TODO does this exist for cocoa
160 wxevent.m_x = screenMouseLocation.x;
161 wxevent.m_y = screenMouseLocation.y;
162 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
163 wxevent.m_controlDown = modifiers & NSControlKeyMask;
164 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
165 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
166 wxevent.m_clickCount = clickCount;
167 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
169 // a control click is interpreted as a right click
170 bool thisButtonIsFakeRight = false ;
171 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
173 button = kEventMouseButtonSecondary ;
174 thisButtonIsFakeRight = true ;
177 // otherwise we report double clicks by connecting a left click with a ctrl-left click
178 if ( clickCount > 1 && button != g_lastButton )
180 // we must make sure that our synthetic 'right' button corresponds in
181 // mouse down, moved and mouse up, and does not deliver a right down and left up
183 if ( cEvent.GetKind() == kEventMouseDown )
185 g_lastButton = button ;
186 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
192 g_lastButtonWasFakeRight = false ;
194 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
195 button = g_lastButton ;
197 // Adjust the chord mask to remove the primary button and add the
198 // secondary button. It is possible that the secondary button is
199 // already pressed, e.g. on a mouse connected to a laptop, but this
200 // possibility is ignored here:
201 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
202 mouseChord = ((mouseChord & ~1U) | 2U);
205 wxevent.m_leftDown = true ;
207 wxevent.m_rightDown = true ;
209 wxevent.m_middleDown = true ;
212 // translate into wx types
213 int eventType = [nsEvent type];
216 case NSLeftMouseDown :
217 case NSRightMouseDown :
218 case NSOtherMouseDown :
222 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
226 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
230 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
239 case NSRightMouseUp :
240 case NSOtherMouseUp :
244 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
248 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
252 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
262 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
264 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
265 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
267 wxevent.m_wheelRotation = 10; // delta;
268 wxevent.m_wheelDelta = 1;
269 wxevent.m_linesPerAction = 1;
270 if ( 0 /* axis == kEventMouseWheelAxisX*/ )
271 wxevent.m_wheelAxis = 1;
275 case NSMouseEntered :
277 case NSLeftMouseDragged :
278 case NSRightMouseDragged :
279 case NSOtherMouseDragged :
281 wxevent.SetEventType( wxEVT_MOTION ) ;
288 @implementation wxNSView
290 #define OSX_DEBUG_DRAWING 0
292 - (void)drawRect: (NSRect) rect
296 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
297 CGContextSaveGState( context );
298 #if OSX_DEBUG_DRAWING
299 CGContextBeginPath( context );
300 CGContextMoveToPoint(context, 0, 0);
301 NSRect bounds = [self bounds];
302 CGContextAddLineToPoint(context, 10, 0);
303 CGContextMoveToPoint(context, 0, 0);
304 CGContextAddLineToPoint(context, 0, 10);
305 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
306 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
307 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
308 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
309 CGContextClosePath( context );
310 CGContextStrokePath(context);
313 if ( [ self isFlipped ] == NO )
315 CGContextTranslateCTM( context, 0, [self bounds].size.height );
316 CGContextScaleCTM( context, 1, -1 );
323 [self getRectsBeingDrawn:&rects count:&count];
324 for ( int i = 0 ; i < count ; ++i )
326 updateRgn.Union(wxFromNSRect(self, rects[i]) );
329 wxWindow* wxpeer = impl->GetWXPeer();
330 wxpeer->GetUpdateRegion() = updateRgn;
331 wxpeer->MacSetCGContextRef( context );
334 event.SetTimestamp(0); // todo
335 event.SetEventObject(wxpeer);
336 wxpeer->HandleWindowEvent(event);
338 CGContextRestoreGState( context );
342 WXCOCOAIMPL_COMMON_IMPLEMENTATION
344 - (void)keyDown:(NSEvent *)event
346 [self handleKeyEvent:event];
349 - (void)keyUp:(NSEvent *)event
351 [self handleKeyEvent:event];
354 - (void)flagsChanged:(NSEvent *)event
356 [self handleKeyEvent:event];
359 - (void)handleKeyEvent:(NSEvent *)event
361 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
362 SetupKeyEvent( wxevent, event );
363 impl->GetWXPeer()->HandleWindowEvent(wxevent);
366 - (BOOL) becomeFirstResponder
368 BOOL r = [super becomeFirstResponder];
375 - (BOOL) resignFirstResponder
377 BOOL r = [super resignFirstResponder];
384 - (BOOL) canBecomeKeyView
391 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
393 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
394 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
398 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
402 void wxWidgetCocoaImpl::Init()
407 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
409 if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] )
410 [m_osxView setImplementation:NULL];
411 if ( !IsRootControl() )
413 NSView *sv = [m_osxView superview];
415 [m_osxView removeFromSuperview];
420 bool wxWidgetCocoaImpl::IsVisible() const
422 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
425 void wxWidgetCocoaImpl::SetVisibility( bool visible )
427 [m_osxView setHidden:(visible ? NO:YES)];
430 void wxWidgetCocoaImpl::Raise()
434 void wxWidgetCocoaImpl::Lower()
438 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
442 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
444 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
445 [m_osxView setFrame:r];
448 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
450 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
455 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
457 NSRect rect = [m_osxView frame];
458 width = rect.size.width;
459 height = rect.size.height;
462 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
465 GetSize( width, height );
468 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
471 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
473 [m_osxView setNeedsDisplay:YES];
476 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
478 return [m_osxView needsDisplay];
481 bool wxWidgetCocoaImpl::CanFocus() const
483 return [m_osxView canBecomeKeyView] == YES;
486 bool wxWidgetCocoaImpl::HasFocus() const
488 return ( [[m_osxView window] firstResponder] == m_osxView );
491 bool wxWidgetCocoaImpl::SetFocus()
493 if ( [m_osxView canBecomeKeyView] == NO )
496 [[m_osxView window] makeFirstResponder: m_osxView] ;
501 void wxWidgetCocoaImpl::RemoveFromParent()
503 [m_osxView removeFromSuperview];
506 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
508 NSView* container = parent->GetWXWidget() ;
509 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
510 [container addSubview:m_osxView];
513 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
515 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
518 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
520 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
522 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
523 [m_osxView setTitle:cf.AsNSString()];
528 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
530 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
531 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
532 *pt = wxFromNSPoint( to->GetWXWidget(), p );
535 wxInt32 wxWidgetCocoaImpl::GetValue() const
537 return [(NSControl*)m_osxView intValue];
540 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
542 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
544 [m_osxView setIntValue:v];
546 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
548 [m_osxView setFloatValue:(double)v];
550 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
552 [m_osxView setDoubleValue:(double)v];
556 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
558 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
560 [m_osxView setMinValue:(double)v];
564 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
566 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
568 [m_osxView setMaxValue:(double)v];
572 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
574 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
576 [m_osxView setImage:bitmap.GetNSImage()];
580 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
582 // implementation in subclass
585 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
587 r->x = r->y = r->width = r->height = 0;
588 // if ( [m_osxView isKindOfClass:[NSControl class]] )
589 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
591 NSRect former = [m_osxView frame];
592 [m_osxView sizeToFit];
593 NSRect best = [m_osxView frame];
594 [m_osxView setFrame:former];
595 r->width = best.size.width;
596 r->height = best.size.height;
600 bool wxWidgetCocoaImpl::IsEnabled() const
602 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
603 return [m_osxView isEnabled];
607 void wxWidgetCocoaImpl::Enable( bool enable )
609 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
610 [m_osxView setEnabled:enable];
613 void wxWidgetCocoaImpl::PulseGauge()
617 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
621 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
623 NSControlSize size = NSRegularControlSize;
627 case wxWINDOW_VARIANT_NORMAL :
628 size = NSRegularControlSize;
631 case wxWINDOW_VARIANT_SMALL :
632 size = NSSmallControlSize;
635 case wxWINDOW_VARIANT_MINI :
636 size = NSMiniControlSize;
639 case wxWINDOW_VARIANT_LARGE :
640 size = NSRegularControlSize;
644 wxFAIL_MSG(_T("unexpected window variant"));
647 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
648 [m_osxView setControlSize:size];
651 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
656 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
660 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
662 NSPoint clickLocation;
663 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
664 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
665 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
666 SetupMouseEvent( wxevent , event ) ;
670 return GetWXPeer()->HandleWindowEvent(wxevent);
678 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
679 long style, long extraStyle)
681 NSView* sv = (wxpeer->GetParent()->GetHandle() );
683 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
684 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
686 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
687 [v setImplementation:c];
691 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
693 NSWindow* tlw = now->GetWXWindow();
694 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
695 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
696 [v setImplementation:c];
697 [tlw setContentView:v];