1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/scrolbar.mm
3 // Purpose: wxScrollBar
4 // Author: Stefan Csomor
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"
23 #include "wx/osx/private.h"
25 @interface wxNSScroller : NSScroller
30 @implementation wxNSScroller
34 static BOOL initialized = NO;
38 wxOSXCocoaClassAddWXMethods(self);
44 class wxOSXScrollBarCocoaImpl : public wxWidgetCocoaImpl
47 wxOSXScrollBarCocoaImpl( wxWindowMac* peer, WXWidget w) : wxWidgetCocoaImpl( peer, w )
52 void SetMaximum(wxInt32 v)
54 m_maximum = (v == 0) ? 1 : v;
57 void SetScrollThumb( wxInt32 value, wxInt32 thumbSize )
59 double v = ((double) value)/m_maximum;
60 double t = ((double) thumbSize)/(m_maximum+thumbSize);
61 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
62 [(wxNSScroller*) m_osxView setFloatValue:v knobProportion:t];
64 [(wxNSScroller*) m_osxView setDoubleValue:v];
65 [(wxNSScroller*) m_osxView setKnobProportion:t];
69 virtual wxInt32 GetValue() const
71 return wxRound([(wxNSScroller*) m_osxView floatValue] * m_maximum);
74 virtual wxInt32 GetMaximum() const
79 virtual void controlAction(WXWidget slf, void* _cmd, void *sender);
80 virtual void mouseEvent(WX_NSEvent event, WXWidget slf, void* _cmd);
85 // we will have a mouseDown, then in the native
86 // implementation of mouseDown the tracking code
87 // is calling clickedAction, therefore we wire this
88 // to thumbtrack and only after super mouseDown
89 // returns we will call the thumbrelease
91 void wxOSXScrollBarCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
93 wxEventType scrollEvent = wxEVT_NULL;
94 switch ([(NSScroller*)m_osxView hitPart])
96 case NSScrollerIncrementLine:
97 scrollEvent = wxEVT_SCROLL_LINEDOWN;
99 case NSScrollerIncrementPage:
100 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
102 case NSScrollerDecrementLine:
103 scrollEvent = wxEVT_SCROLL_LINEUP;
105 case NSScrollerDecrementPage:
106 scrollEvent = wxEVT_SCROLL_PAGEUP;
109 case NSScrollerKnobSlot:
110 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
112 case NSScrollerNoPart:
117 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
119 wxpeer->TriggerScrollEvent(scrollEvent);
122 void wxOSXScrollBarCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
124 wxWidgetCocoaImpl::mouseEvent(event, slf, _cmd);
126 // send a release event in case we've been tracking the thumb
127 if ( strcmp( sel_getName((SEL) _cmd) , "mouseDown:") == 0 )
129 NSScrollerPart hit = [(NSScroller*)m_osxView hitPart];
130 if ( (hit == NSScrollerKnob || hit == NSScrollerKnobSlot) )
132 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
134 wxpeer->OSXHandleClicked(0);
139 wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer,
140 wxWindowMac* WXUNUSED(parent),
141 wxWindowID WXUNUSED(id),
145 long WXUNUSED(extraStyle))
147 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
148 // the creation rect defines the orientation
149 NSRect createRect = ( style & wxSB_HORIZONTAL ) ? NSMakeRect(r.origin.x, r.origin.y , 17, 16) :
150 NSMakeRect(r.origin.x, r.origin.y , 16, 17);
151 wxNSScroller* v = [[wxNSScroller alloc] initWithFrame:createRect];
154 wxWidgetCocoaImpl* c = new wxOSXScrollBarCocoaImpl( wxpeer, v );