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)setImplementation: (wxWidgetImpl *) theImplementation;
47 - (wxWidgetImpl*) implementation;
49 - (BOOL) becomeFirstResponder;
50 - (BOOL) resignFirstResponder;
54 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
56 UInt32 modifiers = [nsEvent modifierFlags] ;
57 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
59 // these parameters are not given for all events
60 UInt32 button = [nsEvent buttonNumber];
61 UInt32 clickCount = [nsEvent clickCount];
62 UInt32 mouseChord = 0; // TODO does this exist for cocoa
64 wxevent.m_x = screenMouseLocation.x;
65 wxevent.m_y = screenMouseLocation.y;
66 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
67 wxevent.m_controlDown = modifiers & NSControlKeyMask;
68 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
69 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
70 wxevent.m_clickCount = clickCount;
71 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
73 // a control click is interpreted as a right click
74 bool thisButtonIsFakeRight = false ;
75 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
77 button = kEventMouseButtonSecondary ;
78 thisButtonIsFakeRight = true ;
81 // otherwise we report double clicks by connecting a left click with a ctrl-left click
82 if ( clickCount > 1 && button != g_lastButton )
84 // we must make sure that our synthetic 'right' button corresponds in
85 // mouse down, moved and mouse up, and does not deliver a right down and left up
87 if ( cEvent.GetKind() == kEventMouseDown )
89 g_lastButton = button ;
90 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
96 g_lastButtonWasFakeRight = false ;
98 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
99 button = g_lastButton ;
101 // Adjust the chord mask to remove the primary button and add the
102 // secondary button. It is possible that the secondary button is
103 // already pressed, e.g. on a mouse connected to a laptop, but this
104 // possibility is ignored here:
105 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
106 mouseChord = ((mouseChord & ~1U) | 2U);
109 wxevent.m_leftDown = true ;
111 wxevent.m_rightDown = true ;
113 wxevent.m_middleDown = true ;
116 // translate into wx types
117 int eventType = [nsEvent type];
120 case NSLeftMouseDown :
121 case NSRightMouseDown :
122 case NSOtherMouseDown :
126 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
130 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
134 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
143 case NSRightMouseUp :
144 case NSOtherMouseUp :
148 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
152 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
156 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
166 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
168 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
169 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
171 wxevent.m_wheelRotation = 10; // delta;
172 wxevent.m_wheelDelta = 1;
173 wxevent.m_linesPerAction = 1;
174 if ( 0 /* axis == kEventMouseWheelAxisX*/ )
175 wxevent.m_wheelAxis = 1;
179 case NSMouseEntered :
181 case NSLeftMouseDragged :
182 case NSRightMouseDragged :
183 case NSOtherMouseDragged :
185 wxevent.SetEventType( wxEVT_MOTION ) ;
192 @implementation wxNSView
194 #define OSX_DEBUG_DRAWING 0
196 - (void)drawRect: (NSRect) rect
200 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
201 CGContextSaveGState( context );
202 #if OSX_DEBUG_DRAWING
203 CGContextBeginPath( context );
204 CGContextMoveToPoint(context, 0, 0);
205 NSRect bounds = [self bounds];
206 CGContextAddLineToPoint(context, 10, 0);
207 CGContextMoveToPoint(context, 0, 0);
208 CGContextAddLineToPoint(context, 0, 10);
209 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
210 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
211 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
212 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
213 CGContextClosePath( context );
214 CGContextStrokePath(context);
217 if ( [ self isFlipped ] == NO )
219 CGContextTranslateCTM( context, 0, [self bounds].size.height );
220 CGContextScaleCTM( context, 1, -1 );
227 [self getRectsBeingDrawn:&rects count:&count];
228 for ( int i = 0 ; i < count ; ++i )
230 updateRgn.Union(wxFromNSRect(self, rects[i]) );
233 wxWindow* wxpeer = impl->GetWXPeer();
234 wxpeer->GetUpdateRegion() = updateRgn;
235 wxpeer->MacSetCGContextRef( context );
238 event.SetTimestamp(0); // todo
239 event.SetEventObject(wxpeer);
240 wxpeer->HandleWindowEvent(event);
242 CGContextRestoreGState( context );
246 -(void)mouseDown:(NSEvent *)event
248 [self handleMouseEvent:event];
251 -(void)rightMouseDown:(NSEvent *)event
253 [self handleMouseEvent:event];
256 -(void)otherMouseDown:(NSEvent *)event
258 [self handleMouseEvent:event];
261 -(void)mouseUp:(NSEvent *)event
263 [self handleMouseEvent:event];
266 -(void)rightMouseUp:(NSEvent *)event
268 [self handleMouseEvent:event];
271 -(void)otherMouseUp:(NSEvent *)event
273 [self handleMouseEvent:event];
276 -(void)handleMouseEvent:(NSEvent *)event
278 NSPoint clickLocation;
279 clickLocation = [self convertPoint:[event locationInWindow] fromView:nil];
280 wxPoint pt = wxFromNSPoint( self, clickLocation );
281 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
282 SetupMouseEvent( wxevent , event ) ;
285 impl->GetWXPeer()->HandleWindowEvent(wxevent);
288 - (void)setImplementation: (wxWidgetImpl *) theImplementation
290 impl = theImplementation;
293 - (wxWidgetImpl*) implementation
303 - (BOOL) becomeFirstResponder
305 BOOL r = [super becomeFirstResponder];
312 - (BOOL) resignFirstResponder
314 BOOL r = [super resignFirstResponder];
324 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
326 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
327 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
331 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
335 void wxWidgetCocoaImpl::Init()
340 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
342 if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] )
343 [m_osxView setImplementation:NULL];
344 if ( !IsRootControl() )
346 NSView *sv = [m_osxView superview];
348 [m_osxView removeFromSuperview];
353 bool wxWidgetCocoaImpl::IsVisible() const
355 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
358 void wxWidgetCocoaImpl::SetVisibility( bool visible )
360 [m_osxView setHidden:(visible ? NO:YES)];
363 void wxWidgetCocoaImpl::Raise()
367 void wxWidgetCocoaImpl::Lower()
371 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
375 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
377 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
378 [m_osxView setFrame:r];
381 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
383 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
388 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
390 NSRect rect = [m_osxView frame];
391 width = rect.size.width;
392 height = rect.size.height;
395 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
398 GetSize( width, height );
401 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
404 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
406 [m_osxView setNeedsDisplay:YES];
409 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
411 return [m_osxView needsDisplay];
414 bool wxWidgetCocoaImpl::CanFocus() const
416 return [m_osxView canBecomeKeyView] == YES;
419 bool wxWidgetCocoaImpl::HasFocus() const
421 return ( [[m_osxView window] firstResponder] == m_osxView );
424 bool wxWidgetCocoaImpl::SetFocus()
426 if ( [m_osxView canBecomeKeyView] == NO )
429 [[m_osxView window] makeFirstResponder: m_osxView] ;
434 void wxWidgetCocoaImpl::RemoveFromParent()
436 [m_osxView removeFromSuperview];
439 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
441 NSView* container = parent->GetWXWidget() ;
442 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
443 [container addSubview:m_osxView];
446 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
448 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
451 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
453 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
455 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
456 [m_osxView setTitle:cf.AsNSString()];
461 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
463 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
464 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
465 *pt = wxFromNSPoint( to->GetWXWidget(), p );
468 wxInt32 wxWidgetCocoaImpl::GetValue() const
470 return [(NSControl*)m_osxView intValue];
473 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
475 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
477 [m_osxView setIntValue:v];
479 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
481 [m_osxView setFloatValue:(double)v];
483 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
485 [m_osxView setDoubleValue:(double)v];
489 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
491 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
493 [m_osxView setMinValue:(double)v];
497 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
499 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
501 [m_osxView setMaxValue:(double)v];
505 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
507 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
509 [m_osxView setImage:bitmap.GetNSImage()];
513 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
515 // implementation in subclass
518 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
520 r->x = r->y = r->width = r->height = 0;
521 // if ( [m_osxView isKindOfClass:[NSControl class]] )
522 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
524 NSRect former = [m_osxView frame];
525 [m_osxView sizeToFit];
526 NSRect best = [m_osxView frame];
527 [m_osxView setFrame:former];
528 r->width = best.size.width;
529 r->height = best.size.height;
533 bool wxWidgetCocoaImpl::IsEnabled() const
535 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
536 return [m_osxView isEnabled];
540 void wxWidgetCocoaImpl::Enable( bool enable )
542 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
543 [m_osxView setEnabled:enable];
546 void wxWidgetCocoaImpl::PulseGauge()
550 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
554 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
556 NSControlSize size = NSRegularControlSize;
560 case wxWINDOW_VARIANT_NORMAL :
561 size = NSRegularControlSize;
564 case wxWINDOW_VARIANT_SMALL :
565 size = NSSmallControlSize;
568 case wxWINDOW_VARIANT_MINI :
569 size = NSMiniControlSize;
572 case wxWINDOW_VARIANT_LARGE :
573 size = NSRegularControlSize;
577 wxFAIL_MSG(_T("unexpected window variant"));
580 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
581 [m_osxView setControlSize:size];
588 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
589 long style, long extraStyle)
591 NSView* sv = (wxpeer->GetParent()->GetHandle() );
593 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
594 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
596 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
597 [v setImplementation:c];
601 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
603 NSWindow* tlw = now->GetWXWindow();
604 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
605 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
606 [v setImplementation:c];
607 [tlw setContentView:v];