mouse and cursor additions for cocoa, see #10361

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58198 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Stefan Csomor
2009-01-18 16:10:46 +00:00
parent 9b66a1d31a
commit 54f1106086
9 changed files with 208 additions and 63 deletions

View File

@@ -329,6 +329,10 @@ public :
void PerformClick(); void PerformClick();
void SetLabel( const wxString& title, wxFontEncoding encoding ); void SetLabel( const wxString& title, wxFontEncoding encoding );
void SetCursor( const wxCursor & cursor );
void CaptureMouse();
void ReleaseMouse();
wxInt32 GetValue() const; wxInt32 GetValue() const;
void SetValue( wxInt32 v ); void SetValue( wxInt32 v );
void SetBitmap( const wxBitmap& bitmap ); void SetBitmap( const wxBitmap& bitmap );

View File

@@ -100,6 +100,10 @@ public :
void PerformClick(); void PerformClick();
void SetLabel(const wxString& title, wxFontEncoding encoding); void SetLabel(const wxString& title, wxFontEncoding encoding);
void SetCursor( const wxCursor & cursor );
void CaptureMouse();
void ReleaseMouse();
wxInt32 GetValue() const; wxInt32 GetValue() const;
void SetValue( wxInt32 v ); void SetValue( wxInt32 v );
void SetBitmap( const wxBitmap& bitmap ); void SetBitmap( const wxBitmap& bitmap );
@@ -201,11 +205,18 @@ protected :
// later to be done using injection in method table // later to be done using injection in method table
#define WXCOCOAIMPL_COMMON_EVENTS_INTERFACE -(void)mouseDown:(NSEvent *)event ;\ #define WXCOCOAIMPL_COMMON_EVENTS_INTERFACE -(void)mouseDown:(NSEvent *)event ;\
-(void)rightMouseDown:(NSEvent *)event ;\ - (void)rightMouseDown:(NSEvent *)event ;\
-(void)otherMouseDown:(NSEvent *)event ;\ - (void)otherMouseDown:(NSEvent *)event ;\
-(void)mouseUp:(NSEvent *)event ;\ - (void)mouseUp:(NSEvent *)event ;\
-(void)rightMouseUp:(NSEvent *)event ;\ - (void)rightMouseUp:(NSEvent *)event ;\
-(void)otherMouseUp:(NSEvent *)event ;\ - (void)otherMouseUp:(NSEvent *)event ;\
- (void)mouseMoved:(NSEvent *)event;\
- (void)mouseDragged:(NSEvent *)event;\
- (void)rightMouseDragged:(NSEvent *)event;\
- (void)otherMouseDragged:(NSEvent *)event;\
- (void)scrollWheel:(NSEvent *)theEvent;\
- (void)mouseEntered:(NSEvent *)event;\
- (void)mouseExited:(NSEvent *)event;\
- (void)keyDown:(NSEvent *)event;\ - (void)keyDown:(NSEvent *)event;\
- (void)keyUp:(NSEvent *)event;\ - (void)keyUp:(NSEvent *)event;\
- (void)flagsChanged:(NSEvent *)event;\ - (void)flagsChanged:(NSEvent *)event;\
@@ -242,6 +253,41 @@ protected :
if ( !impl->DoHandleMouseEvent(event) )\ if ( !impl->DoHandleMouseEvent(event) )\
[super otherMouseUp:event];\ [super otherMouseUp:event];\
}\ }\
-(void)mouseMoved:(NSEvent *)event\
{\
if ( !impl->DoHandleMouseEvent(event) )\
[super mouseMoved:event];\
}\
-(void)mouseDragged:(NSEvent *)event\
{\
if ( !impl->DoHandleMouseEvent(event) )\
[super mouseDragged:event];\
}\
-(void)rightMouseDragged:(NSEvent *)event\
{\
if ( !impl->DoHandleMouseEvent(event) )\
[super rightMouseDragged:event];\
}\
-(void)otherMouseDragged:(NSEvent *)event\
{\
if ( !impl->DoHandleMouseEvent(event) )\
[super otherMouseDragged:event];\
}\
-(void)scrollWheel:(NSEvent *)event\
{\
if ( !impl->DoHandleMouseEvent(event) )\
[super scrollWheel:event];\
}\
-(void)mouseEntered:(NSEvent *)event\
{\
if ( !impl->DoHandleMouseEvent(event) )\
[super mouseEntered:event];\
}\
-(void)mouseExited:(NSEvent *)event\
{\
if ( !impl->DoHandleMouseEvent(event) )\
[super mouseExited:event];\
}\
-(void)keyDown:(NSEvent *)event\ -(void)keyDown:(NSEvent *)event\
{\ {\
if ( !impl->DoHandleKeyEvent(event) )\ if ( !impl->DoHandleKeyEvent(event) )\

View File

@@ -220,6 +220,10 @@ public :
virtual void PerformClick() = 0; virtual void PerformClick() = 0;
virtual void SetLabel( const wxString& title, wxFontEncoding encoding ) = 0; virtual void SetLabel( const wxString& title, wxFontEncoding encoding ) = 0;
virtual void SetCursor( const wxCursor & cursor ) = 0;
virtual void CaptureMouse() = 0;
virtual void ReleaseMouse() = 0;
virtual wxInt32 GetValue() const = 0; virtual wxInt32 GetValue() const = 0;
virtual void SetValue( wxInt32 v ) = 0; virtual void SetValue( wxInt32 v ) = 0;
virtual void SetBitmap( const wxBitmap& bitmap ) = 0; virtual void SetBitmap( const wxBitmap& bitmap ) = 0;

View File

@@ -1038,6 +1038,47 @@ bool wxMacControl::HasFocus() const
return control == m_controlRef; return control == m_controlRef;
} }
void wxMacControl::SetCursor(const wxCursor& cursor)
{
wxWindowMac *mouseWin = 0 ;
WindowRef window = GetControlOwner( m_controlRef ) ;
wxNonOwnedWindow* tlwwx = wxNonOwnedWindow::GetFromWXWindow( (WXWindow) window ) ;
if ( tlwwx != NULL )
{
ControlPartCode part ;
ControlRef control ;
Point pt ;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
HIPoint hiPoint ;
HIGetMousePosition(kHICoordSpaceWindow, window, &hiPoint);
pt.h = hiPoint.x;
pt.v = hiPoint.y;
#else
GetGlobalMouse( &pt );
int x = pt.h;
int y = pt.v;
tlwwx->ScreenToClient(&x, &y);
pt.h = x;
pt.v = y;
#endif
control = FindControlUnderMouse( pt , window , &part ) ;
if ( control )
mouseWin = wxFindWindowFromWXWidget( (WXWidget) control ) ;
}
if ( mouseWin == tlwwx && !wxIsBusy() )
cursor.MacInstall() ;
}
void wxMacControl::CaptureMouse()
{
}
void wxMacControl::ReleaseMouse()
{
}
// //
// subclass specifics // subclass specifics
// //

View File

@@ -426,7 +426,8 @@ bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height) void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
{ {
NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) ); NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
[m_macWindow setFrame:r display:YES]; // do not trigger refreshes upon invisible and possible partly created objects
[m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
} }
void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
@@ -534,12 +535,10 @@ void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y ) void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
{ {
wxPoint p((x ? *x : 0), (y ? *y : 0) ); wxPoint p((x ? *x : 0), (y ? *y : 0) );
/*
NSPoint nspt = wxToNSPoint( NULL, p ); NSPoint nspt = wxToNSPoint( NULL, p );
nspt = [m_macWindow convertScreenToBase:nspt];
nspt = [[m_macWindow contentView] convertPoint:p toV:nil]; nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
p = wxFromNSPoint( p = wxFromNSPoint([m_macWindow contentView], nspt);
*/
if ( x ) if ( x )
*x = p.x; *x = p.x;
if ( y ) if ( y )
@@ -548,10 +547,11 @@ void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y ) void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
{ {
wxPoint p( (x ? *x : 0), (y ? *y : 0) ); wxPoint p((x ? *x : 0), (y ? *y : 0) );
/* NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
p = [m_macWindow convertPoint:p toWindow:nil]; nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
*/ nspt = [m_macWindow convertBaseToScreen:nspt];
p = wxFromNSPoint( NULL, nspt);
if ( x ) if ( x )
*x = p.x; *x = p.x;
if ( y ) if ( y )

View File

@@ -62,11 +62,12 @@ class wxOSXScrollBarCocoaImpl : public wxWidgetCocoaImpl
public : public :
wxOSXScrollBarCocoaImpl( wxWindowMac* peer, WXWidget w) : wxWidgetCocoaImpl( peer, w ) wxOSXScrollBarCocoaImpl( wxWindowMac* peer, WXWidget w) : wxWidgetCocoaImpl( peer, w )
{ {
m_maximum = 1;
} }
void SetMaximum(wxInt32 v) void SetMaximum(wxInt32 v)
{ {
m_maximum = v; m_maximum = (v == 0) ? 1 : v;
} }
void SetScrollThumb( wxInt32 value, wxInt32 thumbSize ) void SetScrollThumb( wxInt32 value, wxInt32 thumbSize )
@@ -102,5 +103,6 @@ wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer,
wxWidgetCocoaImpl* c = new wxOSXScrollBarCocoaImpl( wxpeer, v ); wxWidgetCocoaImpl* c = new wxOSXScrollBarCocoaImpl( wxpeer, v );
[v setImplementation:c]; [v setImplementation:c];
[v setEnabled:YES];
return c; return c;
} }

View File

@@ -177,6 +177,10 @@ void wxClientDisplayRect(int *x, int *y, int *width, int *height)
void wxGetMousePosition( int* x, int* y ) void wxGetMousePosition( int* x, int* y )
{ {
wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]); wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
if ( x )
*x = pt.x;
if ( y )
*y = pt.y;
}; };
wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer) wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
@@ -267,6 +271,4 @@ wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
#endif // wxUSE_GUI #endif // wxUSE_GUI
#endif // wxOSX_USE_COCOA #endif // wxOSX_USE_COCOA

View File

@@ -41,6 +41,8 @@ NSRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const
} }
- (void)drawRect: (NSRect) rect; - (void)drawRect: (NSRect) rect;
// TODO should the be moved to COMMON ?
- (void)resetCursorRects;
WXCOCOAIMPL_COMMON_INTERFACE WXCOCOAIMPL_COMMON_INTERFACE
@@ -167,6 +169,9 @@ void SetupKeyEvent( wxKeyEvent &wxevent , NSEvent * nsEvent )
} }
} }
UInt32 g_lastButton = 0 ;
bool g_lastButtonWasFakeRight = false ;
void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent ) void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
{ {
UInt32 modifiers = [nsEvent modifierFlags] ; UInt32 modifiers = [nsEvent modifierFlags] ;
@@ -184,26 +189,48 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
wxevent.m_metaDown = modifiers & NSCommandKeyMask; wxevent.m_metaDown = modifiers & NSCommandKeyMask;
wxevent.m_clickCount = clickCount; wxevent.m_clickCount = clickCount;
wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ; wxevent.SetTimestamp( [nsEvent timestamp] * 1000.0 ) ;
/*
UInt32 mouseChord = 0; // TODO does this exist for cocoa 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 // a control click is interpreted as a right click
bool thisButtonIsFakeRight = false ; bool thisButtonIsFakeRight = false ;
if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) ) if ( button == 0 && (modifiers & NSControlKeyMask) )
{ {
button = kEventMouseButtonSecondary ; button = 1 ;
thisButtonIsFakeRight = true ; thisButtonIsFakeRight = true ;
} }
// otherwise we report double clicks by connecting a left click with a ctrl-left click // otherwise we report double clicks by connecting a left click with a ctrl-left click
if ( clickCount > 1 && button != g_lastButton ) if ( clickCount > 1 && button != g_lastButton )
clickCount = 1 ; clickCount = 1 ;
// we must make sure that our synthetic 'right' button corresponds in // 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 // mouse down, moved and mouse up, and does not deliver a right down and left up
switch (eventType)
if ( cEvent.GetKind() == kEventMouseDown )
{ {
case NSLeftMouseDown :
case NSRightMouseDown :
case NSOtherMouseDown :
g_lastButton = button ; g_lastButton = button ;
g_lastButtonWasFakeRight = thisButtonIsFakeRight ; g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
break;
} }
if ( button == 0 ) if ( button == 0 )
@@ -211,7 +238,7 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
g_lastButton = 0 ; g_lastButton = 0 ;
g_lastButtonWasFakeRight = false ; g_lastButtonWasFakeRight = false ;
} }
else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight ) else if ( g_lastButton == 1 && g_lastButtonWasFakeRight )
button = g_lastButton ; button = g_lastButton ;
// Adjust the chord mask to remove the primary button and add the // Adjust the chord mask to remove the primary button and add the
@@ -228,9 +255,7 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
if(mouseChord & 4U) if(mouseChord & 4U)
wxevent.m_middleDown = true ; wxevent.m_middleDown = true ;
*/
// translate into wx types // translate into wx types
int eventType = [nsEvent type];
switch (eventType) switch (eventType)
{ {
case NSLeftMouseDown : case NSLeftMouseDown :
@@ -359,6 +384,15 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSEvent * nsEvent )
} }
} }
- (void) resetCursorRects
{
[super resetCursorRects];
wxWindow* wxpeer = impl->GetWXPeer();
NSCursor *cursor = (NSCursor*)wxpeer->GetCursor().GetHCURSOR();
[self addCursorRect: [self bounds]
cursor: cursor];
}
WXCOCOAIMPL_COMMON_IMPLEMENTATION WXCOCOAIMPL_COMMON_IMPLEMENTATION
- (BOOL) canBecomeKeyView - (BOOL) canBecomeKeyView
@@ -417,6 +451,18 @@ void wxWidgetCocoaImpl::Lower()
void wxWidgetCocoaImpl::ScrollRect( const wxRect *rect, int dx, int dy ) 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) void wxWidgetCocoaImpl::Move(int x, int y, int width, int height)
@@ -727,6 +773,28 @@ void wxWidgetCocoaImpl::DoNotifyFocusEvent(bool receivedFocus)
} }
} }
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 // Factory methods

View File

@@ -459,6 +459,11 @@ void wxWindowMac::SetFocus()
void wxWindowMac::DoCaptureMouse() void wxWindowMac::DoCaptureMouse()
{ {
wxApp::s_captureWindow = (wxWindow*) this ; wxApp::s_captureWindow = (wxWindow*) this ;
#ifdef wxOSX_USE_COCOA
// TODO do we really need this ?
m_peer->SetFocus() ;
#endif
m_peer->CaptureMouse() ;
} }
wxWindow * wxWindowBase::GetCapture() wxWindow * wxWindowBase::GetCapture()
@@ -469,6 +474,8 @@ wxWindow * wxWindowBase::GetCapture()
void wxWindowMac::DoReleaseMouse() void wxWindowMac::DoReleaseMouse()
{ {
wxApp::s_captureWindow = NULL ; wxApp::s_captureWindow = NULL ;
m_peer->ReleaseMouse() ;
} }
#if wxUSE_DRAG_AND_DROP #if wxUSE_DRAG_AND_DROP
@@ -723,37 +730,8 @@ bool wxWindowMac::SetCursor(const wxCursor& cursor)
wxASSERT_MSG( m_cursor.Ok(), wxASSERT_MSG( m_cursor.Ok(),
wxT("cursor must be valid after call to the base version")); wxT("cursor must be valid after call to the base version"));
wxWindowMac *mouseWin = 0 ; if ( GetPeer() != NULL )
#if wxOSX_USE_CARBON GetPeer()->SetCursor( m_cursor );
{
wxNonOwnedWindow *tlw = MacGetTopLevelWindow() ;
WindowRef window = (WindowRef) ( tlw ? tlw->GetWXWindow() : 0 ) ;
ControlPartCode part ;
ControlRef control ;
Point pt ;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
HIPoint hiPoint ;
HIGetMousePosition(kHICoordSpaceWindow, window, &hiPoint);
pt.h = hiPoint.x;
pt.v = hiPoint.y;
#else
GetGlobalMouse( &pt );
int x = pt.h;
int y = pt.v;
ScreenToClient(&x, &y);
pt.h = x;
pt.v = y;
#endif
control = FindControlUnderMouse( pt , window , &part ) ;
if ( control )
mouseWin = wxFindWindowFromWXWidget( (WXWidget) control ) ;
}
#endif
if ( mouseWin == this && !wxIsBusy() )
m_cursor.MacInstall() ;
return true ; return true ;
} }