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
38 - (void)drawRect: (NSRect) rect;
40 -(void)mouseDown:(NSEvent *)event ;
41 -(void)rightMouseDown:(NSEvent *)event ;
42 -(void)otherMouseDown:(NSEvent *)event ;
43 -(void)mouseUp:(NSEvent *)event ;
44 -(void)rightMouseUp:(NSEvent *)event ;
45 -(void)otherMouseUp:(NSEvent *)event ;
46 -(void)handleMouseEvent:(NSEvent *)event;
48 - (void)keyDown:(NSEvent *)event;
49 - (void)keyUp:(NSEvent *)event;
50 - (void)flagsChanged:(NSEvent *)event;
51 - (void)handleKeyEvent:(NSEvent *)event;
53 - (void)setImplementation: (wxWidgetImpl *) theImplementation;
54 - (wxWidgetImpl*) implementation;
56 - (BOOL) becomeFirstResponder;
57 - (BOOL) resignFirstResponder;
58 - (BOOL) canBecomeKeyView;
62 long wxOSXTranslateCocoaKey(unsigned short code, int unichar )
67 case NSUpArrowFunctionKey :
70 case NSDownArrowFunctionKey :
73 case NSLeftArrowFunctionKey :
76 case NSRightArrowFunctionKey :
79 case NSInsertFunctionKey :
82 case NSDeleteFunctionKey :
85 case NSHomeFunctionKey :
88 // case NSBeginFunctionKey :
89 // retval = WXK_BEGIN;
91 case NSEndFunctionKey :
94 case NSPageUpFunctionKey :
97 case NSPageDownFunctionKey :
98 retval = WXK_PAGEDOWN;
100 case NSHelpFunctionKey :
105 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
106 retval = WXK_F1 + (unichar - NSF1FunctionKey );
112 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
114 UInt32 modifiers = [nsEvent modifierFlags] ;
115 int eventType = [nsEvent type];
117 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
118 wxevent.m_controlDown = modifiers & NSControlKeyMask;
119 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
120 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
123 if ( eventType != NSFlagsChanged )
125 NSString* nschars = [nsEvent characters];
128 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
129 chars = cfchars.AsString();
133 int unichar = chars.Length() > 0 ? chars[0] : 0;
136 wxevent.m_uniChar = unichar;
138 wxevent.m_keyCode = wxOSXTranslateCocoaKey( [nsEvent keyCode], unichar ) ;
139 // wxevent.m_rawCode = keymessage;
140 wxevent.m_rawFlags = modifiers;
142 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
146 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
149 wxevent.SetEventType( wxEVT_KEY_UP ) ;
151 case NSFlagsChanged :
152 // setup common code here
159 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
161 UInt32 modifiers = [nsEvent modifierFlags] ;
162 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
164 // these parameters are not given for all events
165 UInt32 button = [nsEvent buttonNumber];
166 UInt32 clickCount = [nsEvent clickCount];
167 UInt32 mouseChord = 0; // TODO does this exist for cocoa
169 wxevent.m_x = screenMouseLocation.x;
170 wxevent.m_y = screenMouseLocation.y;
171 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
172 wxevent.m_controlDown = modifiers & NSControlKeyMask;
173 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
174 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
175 wxevent.m_clickCount = clickCount;
176 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
178 // a control click is interpreted as a right click
179 bool thisButtonIsFakeRight = false ;
180 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
182 button = kEventMouseButtonSecondary ;
183 thisButtonIsFakeRight = true ;
186 // otherwise we report double clicks by connecting a left click with a ctrl-left click
187 if ( clickCount > 1 && button != g_lastButton )
189 // we must make sure that our synthetic 'right' button corresponds in
190 // mouse down, moved and mouse up, and does not deliver a right down and left up
192 if ( cEvent.GetKind() == kEventMouseDown )
194 g_lastButton = button ;
195 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
201 g_lastButtonWasFakeRight = false ;
203 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
204 button = g_lastButton ;
206 // Adjust the chord mask to remove the primary button and add the
207 // secondary button. It is possible that the secondary button is
208 // already pressed, e.g. on a mouse connected to a laptop, but this
209 // possibility is ignored here:
210 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
211 mouseChord = ((mouseChord & ~1U) | 2U);
214 wxevent.m_leftDown = true ;
216 wxevent.m_rightDown = true ;
218 wxevent.m_middleDown = true ;
221 // translate into wx types
222 int eventType = [nsEvent type];
225 case NSLeftMouseDown :
226 case NSRightMouseDown :
227 case NSOtherMouseDown :
231 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
235 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
239 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
248 case NSRightMouseUp :
249 case NSOtherMouseUp :
253 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
257 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
261 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
271 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
273 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
274 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
276 wxevent.m_wheelRotation = 10; // delta;
277 wxevent.m_wheelDelta = 1;
278 wxevent.m_linesPerAction = 1;
279 if ( 0 /* axis == kEventMouseWheelAxisX*/ )
280 wxevent.m_wheelAxis = 1;
284 case NSMouseEntered :
286 case NSLeftMouseDragged :
287 case NSRightMouseDragged :
288 case NSOtherMouseDragged :
290 wxevent.SetEventType( wxEVT_MOTION ) ;
297 @implementation wxNSView
299 #define OSX_DEBUG_DRAWING 0
301 - (void)drawRect: (NSRect) rect
305 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
306 CGContextSaveGState( context );
307 #if OSX_DEBUG_DRAWING
308 CGContextBeginPath( context );
309 CGContextMoveToPoint(context, 0, 0);
310 NSRect bounds = [self bounds];
311 CGContextAddLineToPoint(context, 10, 0);
312 CGContextMoveToPoint(context, 0, 0);
313 CGContextAddLineToPoint(context, 0, 10);
314 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
315 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
316 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
317 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
318 CGContextClosePath( context );
319 CGContextStrokePath(context);
322 if ( [ self isFlipped ] == NO )
324 CGContextTranslateCTM( context, 0, [self bounds].size.height );
325 CGContextScaleCTM( context, 1, -1 );
332 [self getRectsBeingDrawn:&rects count:&count];
333 for ( int i = 0 ; i < count ; ++i )
335 updateRgn.Union(wxFromNSRect(self, rects[i]) );
338 wxWindow* wxpeer = impl->GetWXPeer();
339 wxpeer->GetUpdateRegion() = updateRgn;
340 wxpeer->MacSetCGContextRef( context );
343 event.SetTimestamp(0); // todo
344 event.SetEventObject(wxpeer);
345 wxpeer->HandleWindowEvent(event);
347 CGContextRestoreGState( context );
351 -(void)mouseDown:(NSEvent *)event
353 [self handleMouseEvent:event];
356 -(void)rightMouseDown:(NSEvent *)event
358 [self handleMouseEvent:event];
361 -(void)otherMouseDown:(NSEvent *)event
363 [self handleMouseEvent:event];
366 -(void)mouseUp:(NSEvent *)event
368 [self handleMouseEvent:event];
371 -(void)rightMouseUp:(NSEvent *)event
373 [self handleMouseEvent:event];
376 -(void)otherMouseUp:(NSEvent *)event
378 [self handleMouseEvent:event];
381 -(void)handleMouseEvent:(NSEvent *)event
383 NSPoint clickLocation;
384 clickLocation = [self convertPoint:[event locationInWindow] fromView:nil];
385 wxPoint pt = wxFromNSPoint( self, clickLocation );
386 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
387 SetupMouseEvent( wxevent , event ) ;
390 impl->GetWXPeer()->HandleWindowEvent(wxevent);
393 - (void)keyDown:(NSEvent *)event
395 [self handleKeyEvent:event];
398 - (void)keyUp:(NSEvent *)event
400 [self handleKeyEvent:event];
403 - (void)flagsChanged:(NSEvent *)event
405 [self handleKeyEvent:event];
408 - (void)handleKeyEvent:(NSEvent *)event
410 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
411 SetupKeyEvent( wxevent, event );
412 impl->GetWXPeer()->HandleWindowEvent(wxevent);
416 - (void)setImplementation: (wxWidgetImpl *) theImplementation
418 impl = theImplementation;
421 - (wxWidgetImpl*) implementation
431 - (BOOL) becomeFirstResponder
433 BOOL r = [super becomeFirstResponder];
440 - (BOOL) resignFirstResponder
442 BOOL r = [super resignFirstResponder];
449 - (BOOL) canBecomeKeyView
456 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
458 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
459 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
463 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
467 void wxWidgetCocoaImpl::Init()
472 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
474 if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] )
475 [m_osxView setImplementation:NULL];
476 if ( !IsRootControl() )
478 NSView *sv = [m_osxView superview];
480 [m_osxView removeFromSuperview];
485 bool wxWidgetCocoaImpl::IsVisible() const
487 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
490 void wxWidgetCocoaImpl::SetVisibility( bool visible )
492 [m_osxView setHidden:(visible ? NO:YES)];
495 void wxWidgetCocoaImpl::Raise()
499 void wxWidgetCocoaImpl::Lower()
503 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
507 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
509 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
510 [m_osxView setFrame:r];
513 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
515 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
520 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
522 NSRect rect = [m_osxView frame];
523 width = rect.size.width;
524 height = rect.size.height;
527 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
530 GetSize( width, height );
533 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
536 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
538 [m_osxView setNeedsDisplay:YES];
541 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
543 return [m_osxView needsDisplay];
546 bool wxWidgetCocoaImpl::CanFocus() const
548 return [m_osxView canBecomeKeyView] == YES;
551 bool wxWidgetCocoaImpl::HasFocus() const
553 return ( [[m_osxView window] firstResponder] == m_osxView );
556 bool wxWidgetCocoaImpl::SetFocus()
558 if ( [m_osxView canBecomeKeyView] == NO )
561 [[m_osxView window] makeFirstResponder: m_osxView] ;
566 void wxWidgetCocoaImpl::RemoveFromParent()
568 [m_osxView removeFromSuperview];
571 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
573 NSView* container = parent->GetWXWidget() ;
574 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
575 [container addSubview:m_osxView];
578 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
580 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
583 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
585 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
587 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
588 [m_osxView setTitle:cf.AsNSString()];
593 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
595 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
596 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
597 *pt = wxFromNSPoint( to->GetWXWidget(), p );
600 wxInt32 wxWidgetCocoaImpl::GetValue() const
602 return [(NSControl*)m_osxView intValue];
605 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
607 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
609 [m_osxView setIntValue:v];
611 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
613 [m_osxView setFloatValue:(double)v];
615 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
617 [m_osxView setDoubleValue:(double)v];
621 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
623 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
625 [m_osxView setMinValue:(double)v];
629 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
631 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
633 [m_osxView setMaxValue:(double)v];
637 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
639 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
641 [m_osxView setImage:bitmap.GetNSImage()];
645 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
647 // implementation in subclass
650 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
652 r->x = r->y = r->width = r->height = 0;
653 // if ( [m_osxView isKindOfClass:[NSControl class]] )
654 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
656 NSRect former = [m_osxView frame];
657 [m_osxView sizeToFit];
658 NSRect best = [m_osxView frame];
659 [m_osxView setFrame:former];
660 r->width = best.size.width;
661 r->height = best.size.height;
665 bool wxWidgetCocoaImpl::IsEnabled() const
667 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
668 return [m_osxView isEnabled];
672 void wxWidgetCocoaImpl::Enable( bool enable )
674 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
675 [m_osxView setEnabled:enable];
678 void wxWidgetCocoaImpl::PulseGauge()
682 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
686 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
688 NSControlSize size = NSRegularControlSize;
692 case wxWINDOW_VARIANT_NORMAL :
693 size = NSRegularControlSize;
696 case wxWINDOW_VARIANT_SMALL :
697 size = NSSmallControlSize;
700 case wxWINDOW_VARIANT_MINI :
701 size = NSMiniControlSize;
704 case wxWINDOW_VARIANT_LARGE :
705 size = NSRegularControlSize;
709 wxFAIL_MSG(_T("unexpected window variant"));
712 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
713 [m_osxView setControlSize:size];
716 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
725 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
726 long style, long extraStyle)
728 NSView* sv = (wxpeer->GetParent()->GetHandle() );
730 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
731 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
733 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
734 [v setImplementation:c];
738 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
740 NSWindow* tlw = now->GetWXWindow();
741 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
742 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
743 [v setImplementation:c];
744 [tlw setContentView:v];