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 wxWidgetCocoaImpl* impl;
38 - (void)drawRect: (NSRect) rect;
40 WXCOCOAIMPL_COMMON_MOUSE_INTERFACE
42 - (void)keyDown:(NSEvent *)event;
43 - (void)keyUp:(NSEvent *)event;
44 - (void)flagsChanged:(NSEvent *)event;
45 - (void)handleKeyEvent:(NSEvent *)event;
47 - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation;
48 - (wxWidgetCocoaImpl*) implementation;
50 - (BOOL) becomeFirstResponder;
51 - (BOOL) resignFirstResponder;
52 - (BOOL) canBecomeKeyView;
56 long wxOSXTranslateCocoaKey(unsigned short code, int unichar )
61 case NSUpArrowFunctionKey :
64 case NSDownArrowFunctionKey :
67 case NSLeftArrowFunctionKey :
70 case NSRightArrowFunctionKey :
73 case NSInsertFunctionKey :
76 case NSDeleteFunctionKey :
79 case NSHomeFunctionKey :
82 // case NSBeginFunctionKey :
83 // retval = WXK_BEGIN;
85 case NSEndFunctionKey :
88 case NSPageUpFunctionKey :
91 case NSPageDownFunctionKey :
92 retval = WXK_PAGEDOWN;
94 case NSHelpFunctionKey :
99 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
100 retval = WXK_F1 + (unichar - NSF1FunctionKey );
106 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
108 UInt32 modifiers = [nsEvent modifierFlags] ;
109 int eventType = [nsEvent type];
111 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
112 wxevent.m_controlDown = modifiers & NSControlKeyMask;
113 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
114 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
117 if ( eventType != NSFlagsChanged )
119 NSString* nschars = [nsEvent characters];
122 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
123 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 ) ;
140 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
143 wxevent.SetEventType( wxEVT_KEY_UP ) ;
145 case NSFlagsChanged :
146 // setup common code here
153 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
155 UInt32 modifiers = [nsEvent modifierFlags] ;
156 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
158 // these parameters are not given for all events
159 UInt32 button = [nsEvent buttonNumber];
160 UInt32 clickCount = [nsEvent clickCount];
161 UInt32 mouseChord = 0; // TODO does this exist for cocoa
163 wxevent.m_x = screenMouseLocation.x;
164 wxevent.m_y = screenMouseLocation.y;
165 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
166 wxevent.m_controlDown = modifiers & NSControlKeyMask;
167 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
168 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
169 wxevent.m_clickCount = clickCount;
170 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
172 // a control click is interpreted as a right click
173 bool thisButtonIsFakeRight = false ;
174 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
176 button = kEventMouseButtonSecondary ;
177 thisButtonIsFakeRight = true ;
180 // otherwise we report double clicks by connecting a left click with a ctrl-left click
181 if ( clickCount > 1 && button != g_lastButton )
183 // we must make sure that our synthetic 'right' button corresponds in
184 // mouse down, moved and mouse up, and does not deliver a right down and left up
186 if ( cEvent.GetKind() == kEventMouseDown )
188 g_lastButton = button ;
189 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
195 g_lastButtonWasFakeRight = false ;
197 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
198 button = g_lastButton ;
200 // Adjust the chord mask to remove the primary button and add the
201 // secondary button. It is possible that the secondary button is
202 // already pressed, e.g. on a mouse connected to a laptop, but this
203 // possibility is ignored here:
204 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
205 mouseChord = ((mouseChord & ~1U) | 2U);
208 wxevent.m_leftDown = true ;
210 wxevent.m_rightDown = true ;
212 wxevent.m_middleDown = true ;
215 // translate into wx types
216 int eventType = [nsEvent type];
219 case NSLeftMouseDown :
220 case NSRightMouseDown :
221 case NSOtherMouseDown :
225 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
229 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
233 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
242 case NSRightMouseUp :
243 case NSOtherMouseUp :
247 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
251 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
255 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
265 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
267 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
268 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
270 wxevent.m_wheelRotation = 10; // delta;
271 wxevent.m_wheelDelta = 1;
272 wxevent.m_linesPerAction = 1;
273 if ( 0 /* axis == kEventMouseWheelAxisX*/ )
274 wxevent.m_wheelAxis = 1;
278 case NSMouseEntered :
280 case NSLeftMouseDragged :
281 case NSRightMouseDragged :
282 case NSOtherMouseDragged :
284 wxevent.SetEventType( wxEVT_MOTION ) ;
291 @implementation wxNSView
293 #define OSX_DEBUG_DRAWING 0
295 - (void)drawRect: (NSRect) rect
299 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
300 CGContextSaveGState( context );
301 #if OSX_DEBUG_DRAWING
302 CGContextBeginPath( context );
303 CGContextMoveToPoint(context, 0, 0);
304 NSRect bounds = [self bounds];
305 CGContextAddLineToPoint(context, 10, 0);
306 CGContextMoveToPoint(context, 0, 0);
307 CGContextAddLineToPoint(context, 0, 10);
308 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
309 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
310 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
311 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
312 CGContextClosePath( context );
313 CGContextStrokePath(context);
316 if ( [ self isFlipped ] == NO )
318 CGContextTranslateCTM( context, 0, [self bounds].size.height );
319 CGContextScaleCTM( context, 1, -1 );
326 [self getRectsBeingDrawn:&rects count:&count];
327 for ( int i = 0 ; i < count ; ++i )
329 updateRgn.Union(wxFromNSRect(self, rects[i]) );
332 wxWindow* wxpeer = impl->GetWXPeer();
333 wxpeer->GetUpdateRegion() = updateRgn;
334 wxpeer->MacSetCGContextRef( context );
337 event.SetTimestamp(0); // todo
338 event.SetEventObject(wxpeer);
339 wxpeer->HandleWindowEvent(event);
341 CGContextRestoreGState( context );
345 WXCOCOAIMPL_COMMON_MOUSE_IMPLEMENTATION
347 - (void)keyDown:(NSEvent *)event
349 [self handleKeyEvent:event];
352 - (void)keyUp:(NSEvent *)event
354 [self handleKeyEvent:event];
357 - (void)flagsChanged:(NSEvent *)event
359 [self handleKeyEvent:event];
362 - (void)handleKeyEvent:(NSEvent *)event
364 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
365 SetupKeyEvent( wxevent, event );
366 impl->GetWXPeer()->HandleWindowEvent(wxevent);
370 - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation
372 impl = theImplementation;
375 - (wxWidgetCocoaImpl*) implementation
385 - (BOOL) becomeFirstResponder
387 BOOL r = [super becomeFirstResponder];
394 - (BOOL) resignFirstResponder
396 BOOL r = [super resignFirstResponder];
403 - (BOOL) canBecomeKeyView
410 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
412 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
413 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
417 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
421 void wxWidgetCocoaImpl::Init()
426 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
428 if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] )
429 [m_osxView setImplementation:NULL];
430 if ( !IsRootControl() )
432 NSView *sv = [m_osxView superview];
434 [m_osxView removeFromSuperview];
439 bool wxWidgetCocoaImpl::IsVisible() const
441 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
444 void wxWidgetCocoaImpl::SetVisibility( bool visible )
446 [m_osxView setHidden:(visible ? NO:YES)];
449 void wxWidgetCocoaImpl::Raise()
453 void wxWidgetCocoaImpl::Lower()
457 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
461 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
463 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
464 [m_osxView setFrame:r];
467 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
469 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
474 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
476 NSRect rect = [m_osxView frame];
477 width = rect.size.width;
478 height = rect.size.height;
481 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
484 GetSize( width, height );
487 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
490 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
492 [m_osxView setNeedsDisplay:YES];
495 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
497 return [m_osxView needsDisplay];
500 bool wxWidgetCocoaImpl::CanFocus() const
502 return [m_osxView canBecomeKeyView] == YES;
505 bool wxWidgetCocoaImpl::HasFocus() const
507 return ( [[m_osxView window] firstResponder] == m_osxView );
510 bool wxWidgetCocoaImpl::SetFocus()
512 if ( [m_osxView canBecomeKeyView] == NO )
515 [[m_osxView window] makeFirstResponder: m_osxView] ;
520 void wxWidgetCocoaImpl::RemoveFromParent()
522 [m_osxView removeFromSuperview];
525 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
527 NSView* container = parent->GetWXWidget() ;
528 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
529 [container addSubview:m_osxView];
532 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
534 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
537 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
539 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
541 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
542 [m_osxView setTitle:cf.AsNSString()];
547 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
549 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
550 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
551 *pt = wxFromNSPoint( to->GetWXWidget(), p );
554 wxInt32 wxWidgetCocoaImpl::GetValue() const
556 return [(NSControl*)m_osxView intValue];
559 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
561 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
563 [m_osxView setIntValue:v];
565 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
567 [m_osxView setFloatValue:(double)v];
569 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
571 [m_osxView setDoubleValue:(double)v];
575 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
577 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
579 [m_osxView setMinValue:(double)v];
583 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
585 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
587 [m_osxView setMaxValue:(double)v];
591 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
593 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
595 [m_osxView setImage:bitmap.GetNSImage()];
599 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
601 // implementation in subclass
604 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
606 r->x = r->y = r->width = r->height = 0;
607 // if ( [m_osxView isKindOfClass:[NSControl class]] )
608 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
610 NSRect former = [m_osxView frame];
611 [m_osxView sizeToFit];
612 NSRect best = [m_osxView frame];
613 [m_osxView setFrame:former];
614 r->width = best.size.width;
615 r->height = best.size.height;
619 bool wxWidgetCocoaImpl::IsEnabled() const
621 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
622 return [m_osxView isEnabled];
626 void wxWidgetCocoaImpl::Enable( bool enable )
628 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
629 [m_osxView setEnabled:enable];
632 void wxWidgetCocoaImpl::PulseGauge()
636 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
640 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
642 NSControlSize size = NSRegularControlSize;
646 case wxWINDOW_VARIANT_NORMAL :
647 size = NSRegularControlSize;
650 case wxWINDOW_VARIANT_SMALL :
651 size = NSSmallControlSize;
654 case wxWINDOW_VARIANT_MINI :
655 size = NSMiniControlSize;
658 case wxWINDOW_VARIANT_LARGE :
659 size = NSRegularControlSize;
663 wxFAIL_MSG(_T("unexpected window variant"));
666 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
667 [m_osxView setControlSize:size];
670 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
675 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
679 void wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
681 NSPoint clickLocation;
682 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
683 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
684 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
685 SetupMouseEvent( wxevent , event ) ;
688 GetWXPeer()->HandleWindowEvent(wxevent);
696 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
697 long style, long extraStyle)
699 NSView* sv = (wxpeer->GetParent()->GetHandle() );
701 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
702 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
704 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
705 [v setImplementation:c];
709 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
711 NSWindow* tlw = now->GetWXWindow();
712 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
713 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
714 [v setImplementation:c];
715 [tlw setContentView:v];