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"
20 #include "wx/osx/private.h"
27 NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
31 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
32 wxRect bounds(x,y,w,h);
33 NSView* sv = (window->GetParent()->GetHandle() );
35 return wxToNSRect( sv, bounds );
38 @interface wxNSView : NSView
40 WXCOCOAIMPL_COMMON_MEMBERS
43 - (void)drawRect: (NSRect) rect;
45 WXCOCOAIMPL_COMMON_INTERFACE
47 - (BOOL) canBecomeKeyView;
51 @interface NSView(PossibleMethods)
52 - (void)setImplementation:(wxWidgetCocoaImpl *)theImplementation;
53 - (void)setTitle:(NSString *)aString;
54 - (void)setStringValue:(NSString *)aString;
55 - (void)setIntValue:(int)anInt;
56 - (void)setFloatValue:(float)aFloat;
57 - (void)setDoubleValue:(double)aDouble;
59 - (void)setMinValue:(double)aDouble;
60 - (void)setMaxValue:(double)aDouble;
65 - (void)setEnabled:(BOOL)flag;
67 - (void)setImage:(NSImage *)image;
68 - (void)setControlSize:(NSControlSize)size;
73 long wxOSXTranslateCocoaKey(unsigned short code, int unichar )
78 case NSUpArrowFunctionKey :
81 case NSDownArrowFunctionKey :
84 case NSLeftArrowFunctionKey :
87 case NSRightArrowFunctionKey :
90 case NSInsertFunctionKey :
93 case NSDeleteFunctionKey :
96 case NSHomeFunctionKey :
99 // case NSBeginFunctionKey :
100 // retval = WXK_BEGIN;
102 case NSEndFunctionKey :
105 case NSPageUpFunctionKey :
108 case NSPageDownFunctionKey :
109 retval = WXK_PAGEDOWN;
111 case NSHelpFunctionKey :
116 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
117 retval = WXK_F1 + (unichar - NSF1FunctionKey );
123 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
125 UInt32 modifiers = [nsEvent modifierFlags] ;
126 int eventType = [nsEvent type];
128 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
129 wxevent.m_controlDown = modifiers & NSControlKeyMask;
130 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
131 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
134 if ( eventType != NSFlagsChanged )
136 NSString* nschars = [nsEvent characters];
139 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
140 chars = cfchars.AsString();
144 int unichar = chars.Length() > 0 ? chars[0] : 0;
147 wxevent.m_uniChar = unichar;
149 wxevent.m_keyCode = wxOSXTranslateCocoaKey( [nsEvent keyCode], unichar ) ;
150 // wxevent.m_rawCode = keymessage;
151 wxevent.m_rawFlags = modifiers;
153 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
157 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
160 wxevent.SetEventType( wxEVT_KEY_UP ) ;
162 case NSFlagsChanged :
163 // setup common code here
170 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
172 UInt32 modifiers = [nsEvent modifierFlags] ;
173 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
175 // these parameters are not given for all events
176 UInt32 button = [nsEvent buttonNumber];
177 UInt32 clickCount = [nsEvent clickCount];
179 wxevent.m_x = screenMouseLocation.x;
180 wxevent.m_y = screenMouseLocation.y;
181 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
182 wxevent.m_controlDown = modifiers & NSControlKeyMask;
183 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
184 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
185 wxevent.m_clickCount = clickCount;
186 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
188 UInt32 mouseChord = 0; // TODO does this exist for cocoa
189 // a control click is interpreted as a right click
190 bool thisButtonIsFakeRight = false ;
191 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
193 button = kEventMouseButtonSecondary ;
194 thisButtonIsFakeRight = true ;
197 // otherwise we report double clicks by connecting a left click with a ctrl-left click
198 if ( clickCount > 1 && button != g_lastButton )
200 // we must make sure that our synthetic 'right' button corresponds in
201 // mouse down, moved and mouse up, and does not deliver a right down and left up
203 if ( cEvent.GetKind() == kEventMouseDown )
205 g_lastButton = button ;
206 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
212 g_lastButtonWasFakeRight = false ;
214 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
215 button = g_lastButton ;
217 // Adjust the chord mask to remove the primary button and add the
218 // secondary button. It is possible that the secondary button is
219 // already pressed, e.g. on a mouse connected to a laptop, but this
220 // possibility is ignored here:
221 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
222 mouseChord = ((mouseChord & ~1U) | 2U);
225 wxevent.m_leftDown = true ;
227 wxevent.m_rightDown = true ;
229 wxevent.m_middleDown = true ;
232 // translate into wx types
233 int eventType = [nsEvent type];
236 case NSLeftMouseDown :
237 case NSRightMouseDown :
238 case NSOtherMouseDown :
242 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
246 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
250 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
259 case NSRightMouseUp :
260 case NSOtherMouseUp :
264 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
268 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
272 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
282 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
284 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
285 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
287 wxevent.m_wheelRotation = 10; // delta;
288 wxevent.m_wheelDelta = 1;
289 wxevent.m_linesPerAction = 1;
290 if ( 0 /* axis == kEventMouseWheelAxisX*/ )
291 wxevent.m_wheelAxis = 1;
295 case NSMouseEntered :
297 case NSLeftMouseDragged :
298 case NSRightMouseDragged :
299 case NSOtherMouseDragged :
301 wxevent.SetEventType( wxEVT_MOTION ) ;
308 @implementation wxNSView
310 #define OSX_DEBUG_DRAWING 0
312 - (void)drawRect: (NSRect) rect
316 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
317 CGContextSaveGState( context );
318 #if OSX_DEBUG_DRAWING
319 CGContextBeginPath( context );
320 CGContextMoveToPoint(context, 0, 0);
321 NSRect bounds = [self bounds];
322 CGContextAddLineToPoint(context, 10, 0);
323 CGContextMoveToPoint(context, 0, 0);
324 CGContextAddLineToPoint(context, 0, 10);
325 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
326 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
327 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
328 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
329 CGContextClosePath( context );
330 CGContextStrokePath(context);
333 if ( [ self isFlipped ] == NO )
335 CGContextTranslateCTM( context, 0, [self bounds].size.height );
336 CGContextScaleCTM( context, 1, -1 );
343 [self getRectsBeingDrawn:&rects count:&count];
344 for ( int i = 0 ; i < count ; ++i )
346 updateRgn.Union(wxFromNSRect(self, rects[i]) );
349 wxWindow* wxpeer = impl->GetWXPeer();
350 wxpeer->GetUpdateRegion() = updateRgn;
351 wxpeer->MacSetCGContextRef( context );
354 event.SetTimestamp(0); // todo
355 event.SetEventObject(wxpeer);
356 wxpeer->HandleWindowEvent(event);
358 CGContextRestoreGState( context );
362 WXCOCOAIMPL_COMMON_IMPLEMENTATION
364 - (BOOL) canBecomeKeyView
371 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
373 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
374 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
378 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
382 void wxWidgetCocoaImpl::Init()
387 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
389 if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] )
390 [m_osxView setImplementation:NULL];
391 if ( !IsRootControl() )
393 NSView *sv = [m_osxView superview];
395 [m_osxView removeFromSuperview];
400 bool wxWidgetCocoaImpl::IsVisible() const
402 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
405 void wxWidgetCocoaImpl::SetVisibility( bool visible )
407 [m_osxView setHidden:(visible ? NO:YES)];
410 void wxWidgetCocoaImpl::Raise()
414 void wxWidgetCocoaImpl::Lower()
418 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
422 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
424 wxWindowMac* parent = GetWXPeer()->GetParent();
425 // under Cocoa we might have a contentView in the wxParent to which we have to
426 // adjust the coordinates
429 wxPoint pt(parent->GetClientAreaOrigin());
433 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
434 [m_osxView setFrame:r];
437 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
439 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
444 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
446 NSRect rect = [m_osxView frame];
447 width = rect.size.width;
448 height = rect.size.height;
451 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
453 if ( [m_osxView respondsToSelector:@selector(contentView) ] )
455 NSView* cv = [m_osxView contentView];
457 NSRect bounds = [m_osxView bounds];
458 NSRect rect = [cv frame];
460 int y = rect.origin.y;
461 int x = rect.origin.x;
462 if ( ![ m_osxView isFlipped ] )
463 y = bounds.size.height - (rect.origin.y + rect.size.height);
466 width = rect.size.width;
467 height = rect.size.height;
472 GetSize( width, height );
476 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
479 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
481 [m_osxView setNeedsDisplay:YES];
484 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
486 return [m_osxView needsDisplay];
489 bool wxWidgetCocoaImpl::CanFocus() const
491 return [m_osxView canBecomeKeyView] == YES;
494 bool wxWidgetCocoaImpl::HasFocus() const
496 return ( [[m_osxView window] firstResponder] == m_osxView );
499 bool wxWidgetCocoaImpl::SetFocus()
501 if ( [m_osxView canBecomeKeyView] == NO )
504 [[m_osxView window] makeFirstResponder: m_osxView] ;
509 void wxWidgetCocoaImpl::RemoveFromParent()
511 [m_osxView removeFromSuperview];
514 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
516 NSView* container = parent->GetWXWidget() ;
517 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
518 [container addSubview:m_osxView];
521 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
523 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
526 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
528 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
530 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
531 [m_osxView setTitle:cf.AsNSString()];
536 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
538 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
539 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
540 *pt = wxFromNSPoint( to->GetWXWidget(), p );
543 wxInt32 wxWidgetCocoaImpl::GetValue() const
545 return [(NSControl*)m_osxView intValue];
548 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
550 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
552 [m_osxView setIntValue:v];
554 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
556 [m_osxView setFloatValue:(double)v];
558 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
560 [m_osxView setDoubleValue:(double)v];
564 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
566 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
568 [m_osxView setMinValue:(double)v];
572 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
574 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
576 [m_osxView setMaxValue:(double)v];
580 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
582 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
584 [m_osxView setImage:bitmap.GetNSImage()];
588 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
590 // implementation in subclass
593 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
595 r->x = r->y = r->width = r->height = 0;
596 // if ( [m_osxView isKindOfClass:[NSControl class]] )
597 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
599 NSRect former = [m_osxView frame];
600 [m_osxView sizeToFit];
601 NSRect best = [m_osxView frame];
602 [m_osxView setFrame:former];
603 r->width = best.size.width;
604 r->height = best.size.height;
608 bool wxWidgetCocoaImpl::IsEnabled() const
610 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
611 return [m_osxView isEnabled];
615 void wxWidgetCocoaImpl::Enable( bool enable )
617 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
618 [m_osxView setEnabled:enable];
621 void wxWidgetCocoaImpl::PulseGauge()
625 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
629 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
631 NSControlSize size = NSRegularControlSize;
635 case wxWINDOW_VARIANT_NORMAL :
636 size = NSRegularControlSize;
639 case wxWINDOW_VARIANT_SMALL :
640 size = NSSmallControlSize;
643 case wxWINDOW_VARIANT_MINI :
644 size = NSMiniControlSize;
647 case wxWINDOW_VARIANT_LARGE :
648 size = NSRegularControlSize;
652 wxFAIL_MSG(_T("unexpected window variant"));
655 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
656 [m_osxView setControlSize:size];
659 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
664 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
668 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
670 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
671 SetupKeyEvent( wxevent, event );
672 return GetWXPeer()->HandleWindowEvent(wxevent);
675 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
677 NSPoint clickLocation;
678 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
679 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
680 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
681 SetupMouseEvent( wxevent , event ) ;
685 return GetWXPeer()->HandleWindowEvent(wxevent);
688 void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
690 wxWindow* thisWindow = GetWXPeer();
691 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
693 thisWindow->MacInvalidateBorders();
698 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
699 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
700 thisWindow->HandleWindowEvent(eventFocus);
703 if ( thisWindow->GetCaret() )
704 thisWindow->GetCaret()->OnSetFocus();
707 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
708 event.SetEventObject(thisWindow);
709 // TODO how to find out the targetFocusWindow ?
710 // event.SetWindow(targetFocusWindow);
711 thisWindow->HandleWindowEvent(event) ;
713 else // !receivedFocuss
716 if ( thisWindow->GetCaret() )
717 thisWindow->GetCaret()->OnKillFocus();
720 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
722 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
723 event.SetEventObject(thisWindow);
724 // TODO how to find out the targetFocusWindow ?
725 // event.SetWindow(targetFocusWindow);
726 thisWindow->HandleWindowEvent(event) ;
735 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
736 long style, long extraStyle)
738 NSView* sv = (wxpeer->GetParent()->GetHandle() );
740 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
741 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
743 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
744 [v setImplementation:c];
748 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
750 NSWindow* tlw = now->GetWXWindow();
751 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
752 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
753 [v setImplementation:c];
754 [tlw setContentView:v];