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 WXCOCOAIMPL_COMMON_MEMBERS
38 - (void)drawRect: (NSRect) rect;
40 WXCOCOAIMPL_COMMON_INTERFACE
42 - (BOOL) becomeFirstResponder;
43 - (BOOL) resignFirstResponder;
44 - (BOOL) canBecomeKeyView;
48 @interface NSView(PossibleMethods)
49 - (void)setImplementation:(wxWidgetCocoaImpl *)theImplementation;
50 - (void)setTitle:(NSString *)aString;
51 - (void)setStringValue:(NSString *)aString;
52 - (void)setIntValue:(int)anInt;
53 - (void)setFloatValue:(float)aFloat;
54 - (void)setDoubleValue:(double)aDouble;
56 - (void)setMinValue:(double)aDouble;
57 - (void)setMaxValue:(double)aDouble;
62 - (void)setEnabled:(BOOL)flag;
64 - (void)setImage:(NSImage *)image;
65 - (void)setControlSize:(NSControlSize)size;
68 long wxOSXTranslateCocoaKey(unsigned short code, int unichar )
73 case NSUpArrowFunctionKey :
76 case NSDownArrowFunctionKey :
79 case NSLeftArrowFunctionKey :
82 case NSRightArrowFunctionKey :
85 case NSInsertFunctionKey :
88 case NSDeleteFunctionKey :
91 case NSHomeFunctionKey :
94 // case NSBeginFunctionKey :
95 // retval = WXK_BEGIN;
97 case NSEndFunctionKey :
100 case NSPageUpFunctionKey :
103 case NSPageDownFunctionKey :
104 retval = WXK_PAGEDOWN;
106 case NSHelpFunctionKey :
111 if ( unichar >= NSF1FunctionKey && unichar >= NSF24FunctionKey )
112 retval = WXK_F1 + (unichar - NSF1FunctionKey );
118 void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
120 UInt32 modifiers = [nsEvent modifierFlags] ;
121 int eventType = [nsEvent type];
123 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
124 wxevent.m_controlDown = modifiers & NSControlKeyMask;
125 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
126 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
129 if ( eventType != NSFlagsChanged )
131 NSString* nschars = [nsEvent characters];
134 wxCFStringRef cfchars((CFStringRef)[nschars retain]);
135 chars = cfchars.AsString();
139 int unichar = chars.Length() > 0 ? chars[0] : 0;
142 wxevent.m_uniChar = unichar;
144 wxevent.m_keyCode = wxOSXTranslateCocoaKey( [nsEvent keyCode], unichar ) ;
145 // wxevent.m_rawCode = keymessage;
146 wxevent.m_rawFlags = modifiers;
148 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
152 wxevent.SetEventType( wxEVT_KEY_DOWN ) ;
155 wxevent.SetEventType( wxEVT_KEY_UP ) ;
157 case NSFlagsChanged :
158 // setup common code here
165 void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
167 UInt32 modifiers = [nsEvent modifierFlags] ;
168 wxPoint screenMouseLocation = wxFromNSPoint( NULL, [nsEvent locationInWindow]);
170 // these parameters are not given for all events
171 UInt32 button = [nsEvent buttonNumber];
172 UInt32 clickCount = [nsEvent clickCount];
174 wxevent.m_x = screenMouseLocation.x;
175 wxevent.m_y = screenMouseLocation.y;
176 wxevent.m_shiftDown = modifiers & NSShiftKeyMask;
177 wxevent.m_controlDown = modifiers & NSControlKeyMask;
178 wxevent.m_altDown = modifiers & NSAlternateKeyMask;
179 wxevent.m_metaDown = modifiers & NSCommandKeyMask;
180 wxevent.m_clickCount = clickCount;
181 wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
183 UInt32 mouseChord = 0; // TODO does this exist for cocoa
184 // a control click is interpreted as a right click
185 bool thisButtonIsFakeRight = false ;
186 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
188 button = kEventMouseButtonSecondary ;
189 thisButtonIsFakeRight = true ;
192 // otherwise we report double clicks by connecting a left click with a ctrl-left click
193 if ( clickCount > 1 && button != g_lastButton )
195 // we must make sure that our synthetic 'right' button corresponds in
196 // mouse down, moved and mouse up, and does not deliver a right down and left up
198 if ( cEvent.GetKind() == kEventMouseDown )
200 g_lastButton = button ;
201 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
207 g_lastButtonWasFakeRight = false ;
209 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
210 button = g_lastButton ;
212 // Adjust the chord mask to remove the primary button and add the
213 // secondary button. It is possible that the secondary button is
214 // already pressed, e.g. on a mouse connected to a laptop, but this
215 // possibility is ignored here:
216 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
217 mouseChord = ((mouseChord & ~1U) | 2U);
220 wxevent.m_leftDown = true ;
222 wxevent.m_rightDown = true ;
224 wxevent.m_middleDown = true ;
227 // translate into wx types
228 int eventType = [nsEvent type];
231 case NSLeftMouseDown :
232 case NSRightMouseDown :
233 case NSOtherMouseDown :
237 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
241 wxevent.SetEventType( clickCount > 1 ? wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN ) ;
245 wxevent.SetEventType( clickCount > 1 ? wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN ) ;
254 case NSRightMouseUp :
255 case NSOtherMouseUp :
259 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
263 wxevent.SetEventType( wxEVT_RIGHT_UP ) ;
267 wxevent.SetEventType( wxEVT_MIDDLE_UP ) ;
277 wxevent.SetEventType( wxEVT_MOUSEWHEEL ) ;
279 EventMouseWheelAxis axis = cEvent.GetParameter<EventMouseWheelAxis>(kEventParamMouseWheelAxis, typeMouseWheelAxis) ;
280 SInt32 delta = cEvent.GetParameter<SInt32>(kEventParamMouseWheelDelta, typeSInt32) ;
282 wxevent.m_wheelRotation = 10; // delta;
283 wxevent.m_wheelDelta = 1;
284 wxevent.m_linesPerAction = 1;
285 if ( 0 /* axis == kEventMouseWheelAxisX*/ )
286 wxevent.m_wheelAxis = 1;
290 case NSMouseEntered :
292 case NSLeftMouseDragged :
293 case NSRightMouseDragged :
294 case NSOtherMouseDragged :
296 wxevent.SetEventType( wxEVT_MOTION ) ;
303 @implementation wxNSView
305 #define OSX_DEBUG_DRAWING 0
307 - (void)drawRect: (NSRect) rect
311 CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
312 CGContextSaveGState( context );
313 #if OSX_DEBUG_DRAWING
314 CGContextBeginPath( context );
315 CGContextMoveToPoint(context, 0, 0);
316 NSRect bounds = [self bounds];
317 CGContextAddLineToPoint(context, 10, 0);
318 CGContextMoveToPoint(context, 0, 0);
319 CGContextAddLineToPoint(context, 0, 10);
320 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
321 CGContextAddLineToPoint(context, bounds.size.width, bounds.size.height-10);
322 CGContextMoveToPoint(context, bounds.size.width, bounds.size.height);
323 CGContextAddLineToPoint(context, bounds.size.width-10, bounds.size.height);
324 CGContextClosePath( context );
325 CGContextStrokePath(context);
328 if ( [ self isFlipped ] == NO )
330 CGContextTranslateCTM( context, 0, [self bounds].size.height );
331 CGContextScaleCTM( context, 1, -1 );
338 [self getRectsBeingDrawn:&rects count:&count];
339 for ( int i = 0 ; i < count ; ++i )
341 updateRgn.Union(wxFromNSRect(self, rects[i]) );
344 wxWindow* wxpeer = impl->GetWXPeer();
345 wxpeer->GetUpdateRegion() = updateRgn;
346 wxpeer->MacSetCGContextRef( context );
349 event.SetTimestamp(0); // todo
350 event.SetEventObject(wxpeer);
351 wxpeer->HandleWindowEvent(event);
353 CGContextRestoreGState( context );
357 WXCOCOAIMPL_COMMON_IMPLEMENTATION
359 - (BOOL) becomeFirstResponder
361 BOOL r = [super becomeFirstResponder];
368 - (BOOL) resignFirstResponder
370 BOOL r = [super resignFirstResponder];
377 - (BOOL) canBecomeKeyView
384 IMPLEMENT_DYNAMIC_CLASS( wxWidgetCocoaImpl , wxWidgetImpl )
386 wxWidgetCocoaImpl::wxWidgetCocoaImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
387 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
391 wxWidgetCocoaImpl::wxWidgetCocoaImpl()
395 void wxWidgetCocoaImpl::Init()
400 wxWidgetCocoaImpl::~wxWidgetCocoaImpl()
402 if ( [m_osxView respondsToSelector:@selector(setImplementation:) ] )
403 [m_osxView setImplementation:NULL];
404 if ( !IsRootControl() )
406 NSView *sv = [m_osxView superview];
408 [m_osxView removeFromSuperview];
413 bool wxWidgetCocoaImpl::IsVisible() const
415 return [m_osxView isHiddenOrHasHiddenAncestor] == NO;
418 void wxWidgetCocoaImpl::SetVisibility( bool visible )
420 [m_osxView setHidden:(visible ? NO:YES)];
423 void wxWidgetCocoaImpl::Raise()
427 void wxWidgetCocoaImpl::Lower()
431 void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy )
435 void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
437 NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) );
438 [m_osxView setFrame:r];
441 void wxWidgetCocoaImpl::GetPosition( int &x, int &y ) const
443 wxRect r = wxFromNSRect( [m_osxView superview], [m_osxView frame] );
448 void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const
450 NSRect rect = [m_osxView frame];
451 width = rect.size.width;
452 height = rect.size.height;
455 void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
458 GetSize( width, height );
461 void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where )
464 [m_osxView setNeedsDisplayInRect:wxToNSRect(m_osxView, *where )];
466 [m_osxView setNeedsDisplay:YES];
469 bool wxWidgetCocoaImpl::GetNeedsDisplay() const
471 return [m_osxView needsDisplay];
474 bool wxWidgetCocoaImpl::CanFocus() const
476 return [m_osxView canBecomeKeyView] == YES;
479 bool wxWidgetCocoaImpl::HasFocus() const
481 return ( [[m_osxView window] firstResponder] == m_osxView );
484 bool wxWidgetCocoaImpl::SetFocus()
486 if ( [m_osxView canBecomeKeyView] == NO )
489 [[m_osxView window] makeFirstResponder: m_osxView] ;
494 void wxWidgetCocoaImpl::RemoveFromParent()
496 [m_osxView removeFromSuperview];
499 void wxWidgetCocoaImpl::Embed( wxWidgetImpl *parent )
501 NSView* container = parent->GetWXWidget() ;
502 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
503 [container addSubview:m_osxView];
506 void wxWidgetCocoaImpl::SetBackgroundColour( const wxColour &WXUNUSED(col) )
508 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
511 void wxWidgetCocoaImpl::SetLabel( const wxString& title, wxFontEncoding encoding )
513 if ( [m_osxView respondsToSelector:@selector(setTitle:) ] )
515 wxCFStringRef cf( title , m_wxPeer->GetFont().GetEncoding() );
516 [m_osxView setTitle:cf.AsNSString()];
521 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
523 NSPoint p = wxToNSPoint( from->GetWXWidget(), *pt );
524 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
525 *pt = wxFromNSPoint( to->GetWXWidget(), p );
528 wxInt32 wxWidgetCocoaImpl::GetValue() const
530 return [(NSControl*)m_osxView intValue];
533 void wxWidgetCocoaImpl::SetValue( wxInt32 v )
535 if ( [m_osxView respondsToSelector:@selector(setIntValue:)] )
537 [m_osxView setIntValue:v];
539 else if ( [m_osxView respondsToSelector:@selector(setFloatValue:)] )
541 [m_osxView setFloatValue:(double)v];
543 else if ( [m_osxView respondsToSelector:@selector(setDoubleValue:)] )
545 [m_osxView setDoubleValue:(double)v];
549 void wxWidgetCocoaImpl::SetMinimum( wxInt32 v )
551 if ( [m_osxView respondsToSelector:@selector(setMinValue:)] )
553 [m_osxView setMinValue:(double)v];
557 void wxWidgetCocoaImpl::SetMaximum( wxInt32 v )
559 if ( [m_osxView respondsToSelector:@selector(setMaxValue:)] )
561 [m_osxView setMaxValue:(double)v];
565 void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap )
567 if ( [m_osxView respondsToSelector:@selector(setImage:)] )
569 [m_osxView setImage:bitmap.GetNSImage()];
573 void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook)
575 // implementation in subclass
578 void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const
580 r->x = r->y = r->width = r->height = 0;
581 // if ( [m_osxView isKindOfClass:[NSControl class]] )
582 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
584 NSRect former = [m_osxView frame];
585 [m_osxView sizeToFit];
586 NSRect best = [m_osxView frame];
587 [m_osxView setFrame:former];
588 r->width = best.size.width;
589 r->height = best.size.height;
593 bool wxWidgetCocoaImpl::IsEnabled() const
595 if ( [m_osxView respondsToSelector:@selector(isEnabled) ] )
596 return [m_osxView isEnabled];
600 void wxWidgetCocoaImpl::Enable( bool enable )
602 if ( [m_osxView respondsToSelector:@selector(setEnabled:) ] )
603 [m_osxView setEnabled:enable];
606 void wxWidgetCocoaImpl::PulseGauge()
610 void wxWidgetCocoaImpl::SetScrollThumb( wxInt32 val, wxInt32 view )
614 void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant )
616 NSControlSize size = NSRegularControlSize;
620 case wxWINDOW_VARIANT_NORMAL :
621 size = NSRegularControlSize;
624 case wxWINDOW_VARIANT_SMALL :
625 size = NSSmallControlSize;
628 case wxWINDOW_VARIANT_MINI :
629 size = NSMiniControlSize;
632 case wxWINDOW_VARIANT_LARGE :
633 size = NSRegularControlSize;
637 wxFAIL_MSG(_T("unexpected window variant"));
640 if ( [m_osxView respondsToSelector:@selector(setControlSize:)] )
641 [m_osxView setControlSize:size];
644 void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool)
649 void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control )
653 bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
655 wxKeyEvent wxevent(wxEVT_KEY_DOWN);
656 SetupKeyEvent( wxevent, event );
657 return GetWXPeer()->HandleWindowEvent(wxevent);
660 bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event)
662 NSPoint clickLocation;
663 clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil];
664 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
665 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
666 SetupMouseEvent( wxevent , event ) ;
670 return GetWXPeer()->HandleWindowEvent(wxevent);
678 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
679 long style, long extraStyle)
681 NSView* sv = (wxpeer->GetParent()->GetHandle() );
683 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
684 wxNSView* v = [[wxNSView alloc] initWithFrame:r];
686 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
687 [v setImplementation:c];
691 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
693 NSWindow* tlw = now->GetWXWindow();
694 wxNSView* v = [[wxNSView alloc] initWithFrame:[[tlw contentView] frame]];
695 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( now, v, true );
696 [v setImplementation:c];
697 [tlw setContentView:v];