reworking event handling to redirect to c++ virtual functions
[wxWidgets.git] / src / osx / cocoa / scrolbar.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/osx/cocoa/scrolbar.mm
3 // Purpose:     wxScrollBar
4 // Author:      Stefan Csomor
5 // Modified by:
6 // Created:     1998-01-01
7 // RCS-ID:      $Id: scrolbar.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright:   (c) Stefan Csomor
9 // Licence:       wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/scrolbar.h"
15
16 #ifndef WX_PRECOMP
17     #include "wx/intl.h"
18     #include "wx/log.h"
19     #include "wx/settings.h"
20 #endif
21
22 #include "wx/osx/private.h"
23
24 @interface wxNSScroller : NSScroller
25 {
26 }
27 @end
28
29 @implementation wxNSScroller
30
31 + (void)initialize
32 {
33     static BOOL initialized = NO;
34     if (!initialized) 
35     {
36         initialized = YES;
37         wxOSXCocoaClassAddWXMethods(self);
38     }
39 }
40
41 @end
42
43 class wxOSXScrollBarCocoaImpl : public wxWidgetCocoaImpl
44 {
45 public :
46     wxOSXScrollBarCocoaImpl( wxWindowMac* peer, WXWidget w) : wxWidgetCocoaImpl( peer, w )
47     {
48         m_maximum = 1;
49     }
50     
51     void SetMaximum(wxInt32 v)
52     {
53         m_maximum = (v == 0) ? 1 : v;
54     }
55     
56     void    SetScrollThumb( wxInt32 value, wxInt32 thumbSize ) 
57     {
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];
62 #else
63         [(wxNSScroller*) m_osxView setDoubleValue:v];
64         [(wxNSScroller*) m_osxView setKnobProportion:t];
65 #endif
66     }
67     
68     virtual wxInt32 GetValue() const
69     {
70         return [(wxNSScroller*) m_osxView floatValue] * m_maximum;
71     }
72     
73     virtual wxInt32 GetMaximum() const
74     {
75         return m_maximum;
76     }
77
78     virtual void clickedAction(WXWidget slf, void* _cmd, void *sender);
79     virtual void mouseEvent(WX_NSEvent event, WXWidget slf, void* _cmd);
80 protected:
81     wxInt32 m_maximum;
82 };
83
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
89
90 void wxOSXScrollBarCocoaImpl::clickedAction( WXWidget slf, void *_cmd, void *sender)
91 {
92     wxEventType scrollEvent = wxEVT_NULL;
93     switch ([(NSScroller*)m_osxView hitPart]) 
94     {
95     case NSScrollerIncrementLine:
96         scrollEvent = wxEVT_SCROLL_LINEDOWN;
97         break;
98     case NSScrollerIncrementPage:
99         scrollEvent = wxEVT_SCROLL_PAGEDOWN;
100         break;
101     case NSScrollerDecrementLine:
102         scrollEvent = wxEVT_SCROLL_LINEUP;
103         break;
104     case NSScrollerDecrementPage:
105         scrollEvent = wxEVT_SCROLL_PAGEUP;
106         break;
107     case NSScrollerKnob:
108     case NSScrollerKnobSlot:
109         scrollEvent = wxEVT_SCROLL_THUMBTRACK;
110         break;
111     case NSScrollerNoPart:
112     default:
113         return;
114     }
115
116     wxWindow* wxpeer = (wxWindow*) GetWXPeer();
117     if ( wxpeer )
118         wxpeer->TriggerScrollEvent(scrollEvent);
119 }
120
121 void wxOSXScrollBarCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
122 {
123     wxWidgetCocoaImpl::mouseEvent(event, slf, _cmd);
124     
125     // send a release event in case we've been tracking the thumb
126     if ( strcmp( sel_getName((SEL) _cmd) , "mouseDown:") == 0 )
127     {
128         NSScrollerPart hit = [(NSScroller*)m_osxView hitPart];
129         if ( (hit == NSScrollerKnob || hit == NSScrollerKnobSlot) )
130         {
131             wxWindow* wxpeer = (wxWindow*) GetWXPeer();
132             if ( wxpeer )
133                 wxpeer->OSXHandleClicked(0);
134         }
135     }
136 }
137
138 wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer, 
139                                     wxWindowMac* parent, 
140                                     wxWindowID id, 
141                                     const wxPoint& pos, 
142                                     const wxSize& size,
143                                     long style, 
144                                     long extraStyle)
145 {
146     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
147     wxNSScroller* v = [[wxNSScroller alloc] initWithFrame:r];
148
149     wxWidgetCocoaImpl* c = new wxOSXScrollBarCocoaImpl( wxpeer, v );
150     [v setEnabled:YES];
151     return c;
152 }