streamlining slider, scroller, spinbutton for osx carbon and cocoa
[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     WXCOCOAIMPL_COMMON_MEMBERS
27 }
28
29 WXCOCOAIMPL_COMMON_INTERFACE
30
31  - (void) clickedAction: (id) sender;
32
33 @end
34
35 @implementation wxNSScroller
36
37 - (id)initWithFrame:(NSRect)frame
38 {
39     [super initWithFrame:frame];
40     impl = NULL;
41     [self setTarget: self];
42     [self setAction: @selector(clickedAction:)];
43     return self;
44 }
45
46 WXCOCOAIMPL_COMMON_IMPLEMENTATION_NO_MOUSEDOWN
47
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
53
54 - (void) clickedAction: (id) sender
55 {
56     if ( impl )
57     {
58         wxEventType scrollEvent = wxEVT_NULL;
59         switch ([self hitPart]) 
60         {
61         case NSScrollerIncrementLine:
62             scrollEvent = wxEVT_SCROLL_LINEDOWN;
63             break;
64         case NSScrollerIncrementPage:
65             scrollEvent = wxEVT_SCROLL_PAGEDOWN;
66             break;
67         case NSScrollerDecrementLine:
68             scrollEvent = wxEVT_SCROLL_LINEUP;
69             break;
70         case NSScrollerDecrementPage:
71             scrollEvent = wxEVT_SCROLL_PAGEUP;
72             break;
73         case NSScrollerKnob:
74         case NSScrollerKnobSlot:
75             scrollEvent = wxEVT_SCROLL_THUMBTRACK;
76             break;
77         case NSScrollerNoPart:
78         default:
79             return;
80         }
81
82         wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
83         if ( wxpeer )
84             wxpeer->TriggerScrollEvent(scrollEvent);
85     }
86 }
87
88 -(void)mouseDown:(NSEvent *)event 
89 {
90     if ( !impl->DoHandleMouseEvent(event) )
91         [super mouseDown:event];
92
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) )
96     {
97         wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
98         if ( wxpeer )
99             wxpeer->TriggerScrollEvent(wxEVT_SCROLL_THUMBRELEASE);
100     }
101 }
102
103 @end
104
105 class wxOSXScrollBarCocoaImpl : public wxWidgetCocoaImpl
106 {
107 public :
108     wxOSXScrollBarCocoaImpl( wxWindowMac* peer, WXWidget w) : wxWidgetCocoaImpl( peer, w )
109     {
110         m_maximum = 1;
111     }
112     
113     void SetMaximum(wxInt32 v)
114     {
115         m_maximum = (v == 0) ? 1 : v;
116     }
117     
118     void    SetScrollThumb( wxInt32 value, wxInt32 thumbSize ) 
119     {
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];
124 #else
125         [(wxNSScroller*) m_osxView setDoubleValue:v];
126         [(wxNSScroller*) m_osxView setKnobProportion:t];
127 #endif
128     }
129     
130     wxInt32 GetValue() const
131     {
132         return [(wxNSScroller*) m_osxView floatValue] * m_maximum;
133     }
134     
135     wxInt32 GetMaximum() const
136     {
137         return m_maximum;
138     }
139 protected:
140     wxInt32 m_maximum;
141 };
142
143 wxWidgetImplType* wxWidgetImpl::CreateScrollBar( wxWindowMac* wxpeer, 
144                                     wxWindowMac* parent, 
145                                     wxWindowID id, 
146                                     const wxPoint& pos, 
147                                     const wxSize& size,
148                                     long style, 
149                                     long extraStyle)
150 {
151     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
152     wxNSScroller* v = [[wxNSScroller alloc] initWithFrame:r];
153
154     wxWidgetCocoaImpl* c = new wxOSXScrollBarCocoaImpl( wxpeer, v );
155     [v setImplementation:c];
156     [v setEnabled:YES];
157     return c;
158 }