X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4850cc8b2e1037cd394806db4ddcf73868588a4c..6cab4fcac7fe26d9ae5a1d29066e0893d689bb38:/src/osx/cocoa/window.mm diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index ce2fa679f8..44b70b7f01 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -12,13 +12,18 @@ #include "wx/wxprec.h" #ifndef WX_PRECOMP -#include "wx/nonownedwnd.h" + #include "wx/nonownedwnd.h" + #include "wx/log.h" #endif #ifdef __WXMAC__ #include "wx/osx/private.h" #endif +#if wxUSE_CARET + #include "wx/caret.h" +#endif + NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin ) { int x, y, w, h ; @@ -32,30 +37,44 @@ NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const @interface wxNSView : NSView { - wxWidgetCocoaImpl* impl; + WXCOCOAIMPL_COMMON_MEMBERS } - (void)drawRect: (NSRect) rect; -WXCOCOAIMPL_COMMON_MOUSE_INTERFACE - -- (void)keyDown:(NSEvent *)event; -- (void)keyUp:(NSEvent *)event; -- (void)flagsChanged:(NSEvent *)event; -- (void)handleKeyEvent:(NSEvent *)event; +WXCOCOAIMPL_COMMON_INTERFACE -- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; -- (wxWidgetCocoaImpl*) implementation; -- (BOOL) isFlipped; -- (BOOL) becomeFirstResponder; -- (BOOL) resignFirstResponder; - (BOOL) canBecomeKeyView; @end // wxNSView -long wxOSXTranslateCocoaKey(unsigned short code, int unichar ) +@interface NSView(PossibleMethods) +- (void)setImplementation:(wxWidgetCocoaImpl *)theImplementation; +- (void)setTitle:(NSString *)aString; +- (void)setStringValue:(NSString *)aString; +- (void)setIntValue:(int)anInt; +- (void)setFloatValue:(float)aFloat; +- (void)setDoubleValue:(double)aDouble; + +- (double)minValue; +- (double)maxValue; +- (void)setMinValue:(double)aDouble; +- (void)setMaxValue:(double)aDouble; + +- (void)sizeToFit; + +- (BOOL)isEnabled; +- (void)setEnabled:(BOOL)flag; + +- (void)setImage:(NSImage *)image; +- (void)setControlSize:(NSControlSize)size; + +- (id)contentView; +@end + +long wxOSXTranslateCocoaKey( int unichar ) { - long retval = code; + long retval = unichar; switch( unichar ) { case NSUpArrowFunctionKey : @@ -112,25 +131,8 @@ void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent ) wxevent.m_controlDown = modifiers & NSControlKeyMask; wxevent.m_altDown = modifiers & NSAlternateKeyMask; wxevent.m_metaDown = modifiers & NSCommandKeyMask; - - wxString chars; - if ( eventType != NSFlagsChanged ) - { - NSString* nschars = [nsEvent characters]; - if ( nschars ) - { - wxCFStringRef cfchars((CFStringRef)[nschars retain]); - chars = cfchars.AsString(); - } - } - int unichar = chars.Length() > 0 ? chars[0] : 0; - -#if wxUSE_UNICODE - wxevent.m_uniChar = unichar; -#endif - wxevent.m_keyCode = wxOSXTranslateCocoaKey( [nsEvent keyCode], unichar ) ; -// wxevent.m_rawCode = keymessage; + wxevent.m_rawCode = [nsEvent keyCode]; wxevent.m_rawFlags = modifiers; wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ; @@ -148,8 +150,32 @@ void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent ) default : break ; } + + wxString chars; + if ( eventType != NSFlagsChanged ) + { + NSString* nschars = [nsEvent characters]; + if ( nschars ) + { + wxCFStringRef cfchars((CFStringRef)[nschars retain]); + chars = cfchars.AsString(); + } + } + + int unichar = chars.Length() > 0 ? chars[0] : 0; + long keyval = wxOSXTranslateCocoaKey(unichar) ; + if ( keyval == unichar && ( wxevent.GetEventType() == wxEVT_KEY_UP || wxevent.GetEventType() == wxEVT_KEY_DOWN ) ) + keyval = wxToupper( keyval ) ; + +#if wxUSE_UNICODE + wxevent.m_uniChar = unichar; +#endif + wxevent.m_keyCode = keyval; } +UInt32 g_lastButton = 0 ; +bool g_lastButtonWasFakeRight = false ; + void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) { UInt32 modifiers = [nsEvent modifierFlags] ; @@ -158,7 +184,6 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) // these parameters are not given for all events UInt32 button = [nsEvent buttonNumber]; UInt32 clickCount = [nsEvent clickCount]; - UInt32 mouseChord = 0; // TODO does this exist for cocoa wxevent.m_x = screenMouseLocation.x; wxevent.m_y = screenMouseLocation.y; @@ -168,33 +193,56 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) wxevent.m_metaDown = modifiers & NSCommandKeyMask; wxevent.m_clickCount = clickCount; wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ; -/* + + UInt32 mouseChord = 0; + int eventType = [nsEvent type]; + + switch (eventType) + { + case NSLeftMouseDown : + case NSLeftMouseDragged : + mouseChord = 1U; + break; + case NSRightMouseDown : + case NSRightMouseDragged : + mouseChord = 2U; + break; + case NSOtherMouseDown : + case NSOtherMouseDragged : + mouseChord = 4U; + break; + } + // a control click is interpreted as a right click bool thisButtonIsFakeRight = false ; - if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) ) + if ( button == 0 && (modifiers & NSControlKeyMask) ) { - button = kEventMouseButtonSecondary ; + button = 1 ; thisButtonIsFakeRight = true ; } // otherwise we report double clicks by connecting a left click with a ctrl-left click if ( clickCount > 1 && button != g_lastButton ) clickCount = 1 ; + // we must make sure that our synthetic 'right' button corresponds in // mouse down, moved and mouse up, and does not deliver a right down and left up - - if ( cEvent.GetKind() == kEventMouseDown ) + switch (eventType) { - g_lastButton = button ; - g_lastButtonWasFakeRight = thisButtonIsFakeRight ; - } + case NSLeftMouseDown : + case NSRightMouseDown : + case NSOtherMouseDown : + g_lastButton = button ; + g_lastButtonWasFakeRight = thisButtonIsFakeRight ; + break; + } if ( button == 0 ) { g_lastButton = 0 ; g_lastButtonWasFakeRight = false ; } - else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight ) + else if ( g_lastButton == 1 && g_lastButtonWasFakeRight ) button = g_lastButton ; // Adjust the chord mask to remove the primary button and add the @@ -211,9 +259,7 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) if(mouseChord & 4U) wxevent.m_middleDown = true ; -*/ // translate into wx types - int eventType = [nsEvent type]; switch (eventType) { case NSLeftMouseDown : @@ -342,63 +388,7 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) } } -WXCOCOAIMPL_COMMON_MOUSE_IMPLEMENTATION - -- (void)keyDown:(NSEvent *)event -{ - [self handleKeyEvent:event]; -} - -- (void)keyUp:(NSEvent *)event -{ - [self handleKeyEvent:event]; -} - -- (void)flagsChanged:(NSEvent *)event -{ - [self handleKeyEvent:event]; -} - -- (void)handleKeyEvent:(NSEvent *)event -{ - wxKeyEvent wxevent(wxEVT_KEY_DOWN); - SetupKeyEvent( wxevent, event ); - impl->GetWXPeer()->HandleWindowEvent(wxevent); -} - - -- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation -{ - impl = theImplementation; -} - -- (wxWidgetCocoaImpl*) implementation -{ - return impl; -} - -- (BOOL) isFlipped -{ - return YES; -} - -- (BOOL) becomeFirstResponder -{ - BOOL r = [super becomeFirstResponder]; - if ( r ) - { - } - return r; -} - -- (BOOL) resignFirstResponder -{ - BOOL r = [super resignFirstResponder]; - if ( r ) - { - } - return r; -} +WXCOCOAIMPL_COMMON_IMPLEMENTATION - (BOOL) canBecomeKeyView { @@ -456,10 +446,31 @@ void wxWidgetCocoaImpl::Lower() void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy ) { +#if 1 + SetNeedsDisplay() ; +#else + // We should do something like this, but it wasn't working in 10.4. + if (GetNeedsDisplay() ) + { + SetNeedsDisplay() ; + } + NSRect r = wxToNSRect( [m_osxView superview], *rect ); + NSSize offset = NSMakeSize((float)dx, (float)dy); + [m_osxView scrollRect:r by:offset]; +#endif } void wxWidgetCocoaImpl::Move(int x, int y, int width, int height) { + wxWindowMac* parent = GetWXPeer()->GetParent(); + // under Cocoa we might have a contentView in the wxParent to which we have to + // adjust the coordinates + if (parent) + { + wxPoint pt(parent->GetClientAreaOrigin()); + x -= pt.x; + y -= pt.y; + } NSRect r = wxToNSRect( [m_osxView superview], wxRect(x,y,width, height) ); [m_osxView setFrame:r]; } @@ -480,8 +491,27 @@ void wxWidgetCocoaImpl::GetSize( int &width, int &height ) const void wxWidgetCocoaImpl::GetContentArea( int&left, int &top, int &width, int &height ) const { - left = top = 0; - GetSize( width, height ); + if ( [m_osxView respondsToSelector:@selector(contentView) ] ) + { + NSView* cv = [m_osxView contentView]; + + NSRect bounds = [m_osxView bounds]; + NSRect rect = [cv frame]; + + int y = rect.origin.y; + int x = rect.origin.x; + if ( ![ m_osxView isFlipped ] ) + y = bounds.size.height - (rect.origin.y + rect.size.height); + left = x; + top = y; + width = rect.size.width; + height = rect.size.height; + } + else + { + left = top = 0; + GetSize( width, height ); + } } void wxWidgetCocoaImpl::SetNeedsDisplay( const wxRect* where ) @@ -588,6 +618,24 @@ void wxWidgetCocoaImpl::SetMaximum( wxInt32 v ) } } +wxInt32 wxWidgetCocoaImpl::GetMinimum() const +{ + if ( [m_osxView respondsToSelector:@selector(getMinValue:)] ) + { + return [m_osxView minValue]; + } + return 0; +} + +wxInt32 wxWidgetCocoaImpl::GetMaximum() const +{ + if ( [m_osxView respondsToSelector:@selector(getMaxValue:)] ) + { + return [m_osxView maxValue]; + } + return 0; +} + void wxWidgetCocoaImpl::SetBitmap( const wxBitmap& bitmap ) { if ( [m_osxView respondsToSelector:@selector(setImage:)] ) @@ -676,7 +724,15 @@ void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control ) { } -void wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event) +bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event) +{ + wxKeyEvent wxevent(wxEVT_KEY_DOWN); + SetupKeyEvent( wxevent, event ); + + return GetWXPeer()->OSXHandleKeyEvent(wxevent); +} + +bool wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event) { NSPoint clickLocation; clickLocation = [m_osxView convertPoint:[event locationInWindow] fromView:nil]; @@ -685,9 +741,74 @@ void wxWidgetCocoaImpl::DoHandleMouseEvent(NSEvent *event) SetupMouseEvent( wxevent , event ) ; wxevent.m_x = pt.x; wxevent.m_y = pt.y; - GetWXPeer()->HandleWindowEvent(wxevent); + + return GetWXPeer()->HandleWindowEvent(wxevent); } +void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus) +{ + wxWindow* thisWindow = GetWXPeer(); + if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() ) + { + thisWindow->MacInvalidateBorders(); + } + + if ( receivedFocus ) + { + wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast(thisWindow)); + wxChildFocusEvent eventFocus((wxWindow*)thisWindow); + thisWindow->HandleWindowEvent(eventFocus); + +#if wxUSE_CARET + if ( thisWindow->GetCaret() ) + thisWindow->GetCaret()->OnSetFocus(); +#endif + + wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId()); + event.SetEventObject(thisWindow); + // TODO how to find out the targetFocusWindow ? + // event.SetWindow(targetFocusWindow); + thisWindow->HandleWindowEvent(event) ; + } + else // !receivedFocuss + { +#if wxUSE_CARET + if ( thisWindow->GetCaret() ) + thisWindow->GetCaret()->OnKillFocus(); +#endif + + wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast(thisWindow)); + + wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId()); + event.SetEventObject(thisWindow); + // TODO how to find out the targetFocusWindow ? + // event.SetWindow(targetFocusWindow); + thisWindow->HandleWindowEvent(event) ; + } +} + +void wxWidgetCocoaImpl::SetCursor(const wxCursor& cursor) +{ + NSPoint location = [NSEvent mouseLocation]; + location = [[m_osxView window] convertScreenToBase:location]; + NSPoint locationInView = [m_osxView convertPoint:location fromView:nil]; + + if( NSMouseInRect(locationInView, [m_osxView bounds], YES) ) + { + [(NSCursor*)cursor.GetHCURSOR() set]; + } + [[m_osxView window] invalidateCursorRectsForView:m_osxView]; +} + +void wxWidgetCocoaImpl::CaptureMouse() +{ + [[m_osxView window] disableCursorRects]; +} + +void wxWidgetCocoaImpl::ReleaseMouse() +{ + [[m_osxView window] enableCursorRects]; +} // // Factory methods