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
29 @implementation wxNSScroller
33 static BOOL initialized = NO;
37 wxOSXCocoaClassAddWXMethods(self);
43 class wxOSXScrollBarCocoaImpl : public wxWidgetCocoaImpl
46 wxOSXScrollBarCocoaImpl( wxWindowMac* peer, WXWidget w) : wxWidgetCocoaImpl( peer, w )
51 void SetMaximum(wxInt32 v)
53 m_maximum = (v == 0) ? 1 : v;
56 void SetScrollThumb( wxInt32 value, wxInt32 thumbSize )
58 double v = ((double) value)/m_maximum;
59 double t = ((double) thumbSize)/(m_maximum+thumbSize);
60 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
61 [(wxNSScroller*) m_osxView setFloatValue:v knobProportion:t];
63 [(wxNSScroller*) m_osxView setDoubleValue:v];
64 [(wxNSScroller*) m_osxView setKnobProportion:t];
68 virtual wxInt32 GetValue() const
70 return [(wxNSScroller*) m_osxView floatValue] * m_maximum;
73 virtual wxInt32 GetMaximum() const
78 virtual void controlAction(WXWidget slf, void* _cmd, void *sender);
79 virtual void mouseEvent(WX_NSEvent event, WXWidget slf, void* _cmd);
84 // we will have a mouseDown, then in the native
85 // implementation of mouseDown the tracking code
86 // is calling clickedAction, therefore we wire this
87 // to thumbtrack and only after super mouseDown
88 // returns we will call the thumbrelease
90 void wxOSXScrollBarCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
92 wxEventType scrollEvent = wxEVT_NULL;
93 switch ([(NSScroller*)m_osxView hitPart])
95 case NSScrollerIncrementLine:
96 scrollEvent = wxEVT_SCROLL_LINEDOWN;
98 case NSScrollerIncrementPage:
99 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
101 case NSScrollerDecrementLine:
102 scrollEvent = wxEVT_SCROLL_LINEUP;
104 case NSScrollerDecrementPage:
105 scrollEvent = wxEVT_SCROLL_PAGEUP;
108 case NSScrollerKnobSlot:
109 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
111 case NSScrollerNoPart:
116 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
118 wxpeer->TriggerScrollEvent(scrollEvent);
121 void wxOSXScrollBarCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
123 wxWidgetCocoaImpl::mouseEvent(event, slf, _cmd);
125 // send a release event in case we've been tracking the thumb
126 if ( strcmp( sel_getName((SEL) _cmd) , "mouseDown:") == 0 )
128 NSScrollerPart hit = [(NSScroller*)m_osxView hitPart];
129 if ( (hit == NSScrollerKnob || hit == NSScrollerKnobSlot) )
131 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
133 wxpeer->OSXHandleClicked(0);
138 wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer,
139 wxWindowMac* WXUNUSED(parent),
140 wxWindowID WXUNUSED(id),
144 long WXUNUSED(extraStyle))
146 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
147 // the creation rect defines the orientation
148 NSRect createRect = ( style & wxSB_HORIZONTAL ) ? NSMakeRect(r.origin.x, r.origin.y , 17, 16) :
149 NSMakeRect(r.origin.x, r.origin.y , 16, 17);
150 wxNSScroller* v = [[wxNSScroller alloc] initWithFrame:createRect];
153 wxWidgetCocoaImpl* c = new wxOSXScrollBarCocoaImpl( wxpeer, v );