X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4850cc8b2e1037cd394806db4ddcf73868588a4c..0afeb753e0a6a3fdba290bf3612bb2f012d44d95:/src/osx/cocoa/scrolbar.mm diff --git a/src/osx/cocoa/scrolbar.mm b/src/osx/cocoa/scrolbar.mm index e2a7b01f98..10399d7619 100644 --- a/src/osx/cocoa/scrolbar.mm +++ b/src/osx/cocoa/scrolbar.mm @@ -4,7 +4,7 @@ // Author: Stefan Csomor // Modified by: // Created: 1998-01-01 -// RCS-ID: $Id: scrolbar.cpp 54129 2008-06-11 19:30:52Z SC $ +// RCS-ID: $Id$ // Copyright: (c) Stefan Csomor // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -19,56 +19,26 @@ #include "wx/settings.h" #endif +#include "wx/math.h" #include "wx/osx/private.h" @interface wxNSScroller : NSScroller { - wxWidgetCocoaImpl* impl; } - -- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation; -- (wxWidgetCocoaImpl*) implementation; -- (BOOL) isFlipped; - - (void) clickedAction: (id) sender; - @end @implementation wxNSScroller -- (id)initWithFrame:(NSRect)frame -{ - [super initWithFrame:frame]; - impl = NULL; - [self setTarget: self]; - [self setAction: @selector(clickedAction:)]; - return self; -} - -- (void) clickedAction: (id) sender ++ (void)initialize { - if ( impl ) + static BOOL initialized = NO; + if (!initialized) { - wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); - if ( wxpeer ) - wxpeer->HandleClicked(0); + initialized = YES; + wxOSXCocoaClassAddWXMethods(self); } } -- (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation -{ - impl = theImplementation; -} - -- (wxWidgetCocoaImpl*) implementation -{ - return impl ; -} - -- (BOOL) isFlipped -{ - return YES; -} - @end class wxOSXScrollBarCocoaImpl : public wxWidgetCocoaImpl @@ -76,17 +46,18 @@ class wxOSXScrollBarCocoaImpl : public wxWidgetCocoaImpl public : wxOSXScrollBarCocoaImpl( wxWindowMac* peer, WXWidget w) : wxWidgetCocoaImpl( peer, w ) { + m_maximum = 1; } - + 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 ) { double v = ((double) value)/m_maximum; - double t = ((double) thumbSize)/m_maximum; + double t = ((double) thumbSize)/(m_maximum+thumbSize); #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 [(wxNSScroller*) m_osxView setFloatValue:v knobProportion:t]; #else @@ -94,27 +65,93 @@ public : [(wxNSScroller*) m_osxView setKnobProportion:t]; #endif } - - wxInt32 GetValue() const + + virtual wxInt32 GetValue() const + { + return wxRound([(wxNSScroller*) m_osxView floatValue] * m_maximum); + } + + virtual wxInt32 GetMaximum() const { - return [(wxNSScroller*) m_osxView floatValue] * m_maximum; + return m_maximum; } + + virtual void controlAction(WXWidget slf, void* _cmd, void *sender); + virtual void mouseEvent(WX_NSEvent event, WXWidget slf, void* _cmd); protected: wxInt32 m_maximum; }; -wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer, - wxWindowMac* parent, - wxWindowID id, - const wxPoint& pos, +// we will have a mouseDown, then in the native +// implementation of mouseDown the tracking code +// is calling clickedAction, therefore we wire this +// to thumbtrack and only after super mouseDown +// returns we will call the thumbrelease + +void wxOSXScrollBarCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender)) +{ + wxEventType scrollEvent = wxEVT_NULL; + switch ([(NSScroller*)m_osxView hitPart]) + { + case NSScrollerIncrementLine: + scrollEvent = wxEVT_SCROLL_LINEDOWN; + break; + case NSScrollerIncrementPage: + scrollEvent = wxEVT_SCROLL_PAGEDOWN; + break; + case NSScrollerDecrementLine: + scrollEvent = wxEVT_SCROLL_LINEUP; + break; + case NSScrollerDecrementPage: + scrollEvent = wxEVT_SCROLL_PAGEUP; + break; + case NSScrollerKnob: + case NSScrollerKnobSlot: + scrollEvent = wxEVT_SCROLL_THUMBTRACK; + break; + case NSScrollerNoPart: + default: + return; + } + + wxWindow* wxpeer = (wxWindow*) GetWXPeer(); + if ( wxpeer ) + wxpeer->TriggerScrollEvent(scrollEvent); +} + +void wxOSXScrollBarCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd) +{ + wxWidgetCocoaImpl::mouseEvent(event, slf, _cmd); + + // send a release event in case we've been tracking the thumb + if ( strcmp( sel_getName((SEL) _cmd) , "mouseDown:") == 0 ) + { + NSScrollerPart hit = [(NSScroller*)m_osxView hitPart]; + if ( (hit == NSScrollerKnob || hit == NSScrollerKnobSlot) ) + { + wxWindow* wxpeer = (wxWindow*) GetWXPeer(); + if ( wxpeer ) + wxpeer->OSXHandleClicked(0); + } + } +} + +wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer, + wxWindowMac* WXUNUSED(parent), + wxWindowID WXUNUSED(id), + const wxPoint& pos, const wxSize& size, - long style, - long extraStyle) + long style, + long WXUNUSED(extraStyle)) { NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; - wxNSScroller* v = [[wxNSScroller alloc] initWithFrame:r]; + // the creation rect defines the orientation + NSRect createRect = ( style & wxSB_HORIZONTAL ) ? NSMakeRect(r.origin.x, r.origin.y , 17, 16) : + NSMakeRect(r.origin.x, r.origin.y , 16, 17); + wxNSScroller* v = [[wxNSScroller alloc] initWithFrame:createRect]; + [v setFrame:r]; wxWidgetCocoaImpl* c = new wxOSXScrollBarCocoaImpl( wxpeer, v ); - [v setImplementation:c]; + [v setEnabled:YES]; return c; }