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"
26 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
30 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
31 wxRect bounds(x,y,w,h);
32 NSView* sv = (window->GetParent()->GetHandle() );
34 return wxToNSRect( sv, bounds );
37 @interface wxNSView : NSView
39 WXCOCOAIMPL_COMMON_MEMBERS
42 - (void)drawRect: (NSRect) rect;
44 WXCOCOAIMPL_COMMON_INTERFACE
46 - (BOOL) canBecomeKeyView;
50 @interface NSView(PossibleMethods)
51 - (void)setImplementation:(wxWidgetCocoaImpl *)theImplementation;
52 - (void)setTitle:(NSString *)aString;
53 - (void)setStringValue:(NSString *)aString;
54 - (void)setIntValue:(int)anInt;
55 - (void)setFloatValue:(float)aFloat;
56 - (void)setDoubleValue:(double)aDouble;
58 - (void)setMinValue:(double)aDouble;
59 - (void)setMaxValue:(double)aDouble;
64 - (void)setEnabled:(BOOL)flag;
66 - (void)setImage:(NSImage *)image;
67 - (void)setControlSize:(NSControlSize)size;
72 long wxOSXTranslateCocoaKey(unsigned short code, int unichar )
77 case NSUpArrowFunctionKey :
80 case NSDownArrowFunctionKey :
83 case NSLeftArrowFunctionKey :
86 case NSRightArrowFunctionKey :
89 case NSInsertFunctionKey :
92 case NSDeleteFunctionKey :
95 case NSHomeFunctionKey :
98 // case NSBeginFunctionKey :
99 // retval = WXK_BEGIN;
101 case NSEndFunctionKey :
104 case NSPageUpFunctionKey :
107 case NSPageDownFunctionKey :
108 retval = WXK_PAGEDOWN;
110 case NSHelpFunctionKey :
115 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
116 retval = WXK_F1 + (unichar - NSF1FunctionKey );
122 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
124 UInt32 modifiers = [nsEvent modifierFlags] ;
125 int eventType = [nsEvent type];
127 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
128 wxevent.m_controlDown = modifiers & NSControlKeyMask;
129 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
130 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
133 if ( eventType != NSFlagsChanged )
135 NSString* nschars = [nsEvent characters];
138 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
139 chars = cfchars.AsString();
143 int unichar = chars.Length() > 0 ? chars[0] : 0;
146 wxevent.m_uniChar = unichar;
148 wxevent.m_keyCode = wxOSXTranslateCocoaKey( [nsEvent keyCode], unichar ) ;
149 // wxevent.m_rawCode = keymessage;
150 wxevent.m_rawFlags = modifiers;
152 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
156 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
159 wxevent.SetEventType( wxEVT_KEY_UP ) ;
161 case NSFlagsChanged :
162 // setup common code here
169 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
171 UInt32 modifiers = [nsEvent modifierFlags] ;
172 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
174 // these parameters are not given for all events
175 UInt32 button = [nsEvent buttonNumber];
176 UInt32 clickCount = [nsEvent clickCount];
178 wxevent.m_x = screenMouseLocation.x;
179 wxevent.m_y = screenMouseLocation.y;
180 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
181 wxevent.m_controlDown = modifiers & NSControlKeyMask;
182 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
183 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
184 wxevent.m_clickCount = clickCount;
185 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
187 UInt32 mouseChord = 0; // TODO does this exist for cocoa
188 // a control click is interpreted as a right click
189 bool thisButtonIsFakeRight = false ;
190 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
192 button = kEventMouseButtonSecondary ;
193 thisButtonIsFakeRight = true ;
196 // otherwise we report double clicks by connecting a left click with a ctrl-left click
197 if ( clickCount > 1 && button != g_lastButton )
199 // we must make sure that our synthetic 'right' button corresponds in
200 // mouse down, moved and mouse up, and does not deliver a right down and left up
202 if ( cEvent.GetKind() == kEventMouseDown )
204 g_lastButton = button ;
205 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
211 g_lastButtonWasFakeRight = false ;
213 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
214 button = g_lastButton ;
216 // Adjust the chord mask to remove the primary button and add the
217 // secondary button. It is possible that the secondary button is
218 // already pressed, e.g. on a mouse connected to a laptop, but this
219 // possibility is ignored here:
220 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
221 mouseChord = ((mouseChord & ~1U) | 2U);
224 wxevent.m_leftDown = true ;
226 wxevent.m_rightDown = true ;
228 wxevent.m_middleDown = true ;
231 // translate into wx types
232 int eventType = [nsEvent type];
235 case NSLeftMouseDown :
236 case NSRightMouseDown :
237 case NSOtherMouseDown :
241 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
245 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
249 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
258 case NSRightMouseUp :
259 case NSOtherMouseUp :
263 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
267 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
271 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
281 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
283 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
284 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
286 wxevent.m_wheelRotation = 10; // delta;
287 wxevent.m_wheelDelta = 1;
288 wxevent.m_linesPerAction = 1;
289 if ( 0 /* axis == kEventMouseWheelAxisX*/ )
290 wxevent.m_wheelAxis = 1;
294 case NSMouseEntered :
296 case NSLeftMouseDragged :
297 case NSRightMouseDragged :
298 case NSOtherMouseDragged :
300 wxevent.SetEventType( wxEVT_MOTION ) ;
307 @implementation wxNSView
309 #define OSX_DEBUG_DRAWING 0
311 - (void)drawRect: (NSRect) rect
315 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
316 CGContextSaveGState( context );
317 #if OSX_DEBUG_DRAWING
318 CGContextBeginPath( context );
319 CGContextMoveToPoint(context, 0, 0);
320 NSRect bounds = [self bounds];
321 CGContextAddLineToPoint(context, 10, 0);
322 CGContextMoveToPoint(context, 0, 0);
323 CGContextAddLineToPoint(context, 0, 10);
324 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
325 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
326 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
327 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
328 CGContextClosePath( context );
329 CGContextStrokePath(context);
332 if ( [ self isFlipped ] == NO )
334 CGContextTranslateCTM( context, 0, [self bounds].size.height );
335 CGContextScaleCTM( context, 1, -1 );
342 [self getRectsBeingDrawn:&rects count:&count];
343 for ( int i = 0 ; i < count ; ++i )
345 updateRgn.Union(wxFromNSRect(self, rects[i]) );
348 wxWindow* wxpeer = impl->GetWXPeer();
349 wxpeer->GetUpdateRegion() = updateRgn;
350 wxpeer->MacSetCGContextRef( context );
353 event.SetTimestamp(0); // todo
354 event.SetEventObject(wxpeer);
355 wxpeer->HandleWindowEvent(event);
357 CGContextRestoreGState( context );
361 WXCOCOAIMPL_COMMON_IMPLEMENTATION
363 - (BOOL) canBecomeKeyView
370 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
372 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
373 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
377 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
381 void wxWidgetCocoaImpl::Init()
386 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
388 if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] )
389 [m_osxView setImplementation:NULL];
390 if ( !IsRootControl() )
392 NSView *sv = [m_osxView superview];
394 [m_osxView removeFromSuperview];
399 bool wxWidgetCocoaImpl::IsVisible() const
401 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
404 void wxWidgetCocoaImpl::SetVisibility( bool visible )
406 [m_osxView setHidden:(visible ? NO:YES)];
409 void wxWidgetCocoaImpl::Raise()
413 void wxWidgetCocoaImpl::Lower()
417 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
421 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
423 wxWindowMac* parent = GetWXPeer()->GetParent();
424 // under Cocoa we might have a contentView in the wxParent to which we have to
425 // adjust the coordinates
428 wxPoint pt(parent->GetClientAreaOrigin());
432 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
433 [m_osxView setFrame:r];
436 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
438 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
443 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
445 NSRect rect = [m_osxView frame];
446 width = rect.size.width;
447 height = rect.size.height;
450 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
452 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
454 NSView* cv = [m_osxView contentView];
456 NSRect bounds = [m_osxView bounds];
457 NSRect rect = [cv frame];
459 int y = rect.origin.y;
460 int x = rect.origin.x;
461 if ( ![ m_osxView isFlipped ] )
462 y = bounds.size.height - (rect.origin.y + rect.size.height);
465 width = rect.size.width;
466 height = rect.size.height;
471 GetSize( width, height );
475 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
478 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
480 [m_osxView setNeedsDisplay:YES];
483 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
485 return [m_osxView needsDisplay];
488 bool wxWidgetCocoaImpl::CanFocus() const
490 return [m_osxView canBecomeKeyView] == YES;
493 bool wxWidgetCocoaImpl::HasFocus() const
495 return ( [[m_osxView window] firstResponder] == m_osxView );
498 bool wxWidgetCocoaImpl::SetFocus()
500 if ( [m_osxView canBecomeKeyView] == NO )
503 [[m_osxView window] makeFirstResponder: m_osxView] ;
508 void wxWidgetCocoaImpl::RemoveFromParent()
510 [m_osxView removeFromSuperview];
513 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
515 NSView* container = parent->GetWXWidget() ;
516 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
517 [container addSubview:m_osxView];
520 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
522 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
525 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
527 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
529 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
530 [m_osxView setTitle:cf.AsNSString()];
535 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
537 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
538 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
539 *pt = wxFromNSPoint( to->GetWXWidget(), p );
542 wxInt32 wxWidgetCocoaImpl::GetValue() const
544 return [(NSControl*)m_osxView intValue];
547 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
549 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
551 [m_osxView setIntValue:v];
553 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
555 [m_osxView setFloatValue:(double)v];
557 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
559 [m_osxView setDoubleValue:(double)v];
563 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
565 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
567 [m_osxView setMinValue:(double)v];
571 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
573 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
575 [m_osxView setMaxValue:(double)v];
579 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
581 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
583 [m_osxView setImage:bitmap.GetNSImage()];
587 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
589 // implementation in subclass
592 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
594 r->x = r->y = r->width = r->height = 0;
595 // if ( [m_osxView isKindOfClass:[NSControl class]] )
596 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
598 NSRect former = [m_osxView frame];
599 [m_osxView sizeToFit];
600 NSRect best = [m_osxView frame];
601 [m_osxView setFrame:former];
602 r->width = best.size.width;
603 r->height = best.size.height;
607 bool wxWidgetCocoaImpl::IsEnabled() const
609 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
610 return [m_osxView isEnabled];
614 void wxWidgetCocoaImpl::Enable( bool enable )
616 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
617 [m_osxView setEnabled:enable];
620 void wxWidgetCocoaImpl::PulseGauge()
624 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
628 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
630 NSControlSize size = NSRegularControlSize;
634 case wxWINDOW_VARIANT_NORMAL :
635 size = NSRegularControlSize;
638 case wxWINDOW_VARIANT_SMALL :
639 size = NSSmallControlSize;
642 case wxWINDOW_VARIANT_MINI :
643 size = NSMiniControlSize;
646 case wxWINDOW_VARIANT_LARGE :
647 size = NSRegularControlSize;
651 wxFAIL_MSG(_T("unexpected window variant"));
654 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
655 [m_osxView setControlSize:size];
658 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
663 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
667 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
669 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
670 SetupKeyEvent( wxevent, event );
671 return GetWXPeer()->HandleWindowEvent(wxevent);
674 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
676 NSPoint clickLocation;
677 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
678 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
679 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
680 SetupMouseEvent( wxevent , event ) ;
684 return GetWXPeer()->HandleWindowEvent(wxevent);
687 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
689 wxWindow* thisWindow = GetWXPeer();
690 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
692 thisWindow->MacInvalidateBorders();
697 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
698 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
699 thisWindow->HandleWindowEvent(eventFocus);
702 if ( thisWindow->GetCaret() )
703 thisWindow->GetCaret()->OnSetFocus();
706 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
707 event.SetEventObject(thisWindow);
708 // TODO how to find out the targetFocusWindow ?
709 // event.SetWindow(targetFocusWindow);
710 thisWindow->HandleWindowEvent(event) ;
712 else // !receivedFocuss
715 if ( thisWindow->GetCaret() )
716 thisWindow->GetCaret()->OnKillFocus();
719 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
721 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
722 event.SetEventObject(thisWindow);
723 // TODO how to find out the targetFocusWindow ?
724 // event.SetWindow(targetFocusWindow);
725 thisWindow->HandleWindowEvent(event) ;
734 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
735 long style, long extraStyle)
737 NSView* sv = (wxpeer->GetParent()->GetHandle() );
739 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
740 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
742 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
743 [v setImplementation:c];
747 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
749 NSWindow* tlw = now->GetWXWindow();
750 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
751 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
752 [v setImplementation:c];
753 [tlw setContentView:v];