1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/scrolbar.mm
3 // Purpose: wxScrollBar
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: scrolbar.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/scrolbar.h"
19 #include "wx/settings.h"
22 #include "wx/osx/private.h"
24 @interface wxNSScroller : NSScroller
26 WXCOCOAIMPL_COMMON_MEMBERS
29 WXCOCOAIMPL_COMMON_INTERFACE
31 - (void) clickedAction: (id) sender;
35 @implementation wxNSScroller
37 - (id)initWithFrame:(NSRect)frame
39 [super initWithFrame:frame];
41 [self setTarget: self];
42 [self setAction: @selector(clickedAction:)];
46 WXCOCOAIMPL_COMMON_IMPLEMENTATION_NO_MOUSEDOWN
48 // we will have a mouseDown, then in the native
49 // implementation of mouseDown the tracking code
50 // is calling clickedAction, therefore we wire this
51 // to thumbtrack and only after super mouseDown
52 // returns we will call the thumbrelease
54 - (void) clickedAction: (id) sender
58 wxEventType scrollEvent = wxEVT_NULL;
59 switch ([self hitPart])
61 case NSScrollerIncrementLine:
62 scrollEvent = wxEVT_SCROLL_LINEDOWN;
64 case NSScrollerIncrementPage:
65 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
67 case NSScrollerDecrementLine:
68 scrollEvent = wxEVT_SCROLL_LINEUP;
70 case NSScrollerDecrementPage:
71 scrollEvent = wxEVT_SCROLL_PAGEUP;
74 case NSScrollerKnobSlot:
75 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
77 case NSScrollerNoPart:
82 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
84 wxpeer->TriggerScrollEvent(scrollEvent);
88 -(void)mouseDown:(NSEvent *)event
90 if ( !impl->DoHandleMouseEvent(event) )
91 [super mouseDown:event];
93 // send a release event in case we've been tracking the thumb
94 NSScrollerPart hit = [self hitPart];
95 if ( impl && (hit == NSScrollerKnob || hit == NSScrollerKnobSlot) )
97 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
99 wxpeer->TriggerScrollEvent(wxEVT_SCROLL_THUMBRELEASE);
105 class wxOSXScrollBarCocoaImpl : public wxWidgetCocoaImpl
108 wxOSXScrollBarCocoaImpl( wxWindowMac* peer, WXWidget w) : wxWidgetCocoaImpl( peer, w )
113 void SetMaximum(wxInt32 v)
115 m_maximum = (v == 0) ? 1 : v;
118 void SetScrollThumb( wxInt32 value, wxInt32 thumbSize )
120 double v = ((double) value)/m_maximum;
121 double t = ((double) thumbSize)/m_maximum;
122 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
123 [(wxNSScroller*) m_osxView setFloatValue:v knobProportion:t];
125 [(wxNSScroller*) m_osxView setDoubleValue:v];
126 [(wxNSScroller*) m_osxView setKnobProportion:t];
130 wxInt32 GetValue() const
132 return [(wxNSScroller*) m_osxView floatValue] * m_maximum;
135 wxInt32 GetMaximum() const
143 wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer,
151 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
152 wxNSScroller* v = [[wxNSScroller alloc] initWithFrame:r];
154 wxWidgetCocoaImpl* c = new wxOSXScrollBarCocoaImpl( wxpeer, v );
155 [v setImplementation:c];