Update OpenVMS makefile
[wxWidgets.git] / src / osx / cocoa / spinbutt.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/osx/cocoa/spinbutt.mm
3 // Purpose:     wxSpinButton
4 // Author:      Stefan Csomor
5 // Modified by:
6 // Created:     1998-01-01
7 // Copyright:   (c) Stefan Csomor
8 // Licence:       wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_SPINBTN
14
15 #include "wx/spinbutt.h"
16 #include "wx/osx/private.h"
17
18 @interface wxNSStepper : NSStepper
19 {
20 }
21 @end
22
23 @implementation wxNSStepper
24
25 + (void)initialize
26 {
27     static BOOL initialized = NO;
28     if (!initialized)
29     {
30         initialized = YES;
31         wxOSXCocoaClassAddWXMethods(self);
32     }
33 }
34
35 @end
36
37 class wxSpinButtonCocoaImpl : public wxWidgetCocoaImpl
38 {
39 public :
40     wxSpinButtonCocoaImpl(wxWindowMac* peer , WXWidget w) :
41         wxWidgetCocoaImpl(peer, w)
42     {
43         m_formerValue = 0;
44     }
45
46     ~wxSpinButtonCocoaImpl()
47     {
48     }
49
50     virtual void controlAction(WXWidget slf, void* _cmd, void *sender);
51     virtual void mouseEvent(WX_NSEvent event, WXWidget slf, void* _cmd);
52 private:
53     int m_formerValue;
54 };
55
56 void wxSpinButtonCocoaImpl::mouseEvent(WX_NSEvent event, WXWidget slf, void *_cmd)
57 {
58
59     // send a release event in case we've been tracking the thumb
60     if ( strcmp( sel_getName((SEL) _cmd) , "mouseDown:") == 0 )
61     {
62         m_formerValue = [(NSStepper*)m_osxView intValue];
63     }
64
65     wxWidgetCocoaImpl::mouseEvent(event, slf, _cmd);
66 }
67
68 void wxSpinButtonCocoaImpl::controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
69 {
70     wxWindow* wxpeer = (wxWindow*) GetWXPeer();
71     if ( wxpeer )
72     {
73         // because wx expects to be able to veto
74         // a change we must revert the value change
75         // and expose it
76         int currentValue = [(NSStepper*)m_osxView intValue];
77         [(NSStepper*)m_osxView setIntValue:m_formerValue];
78         int inc = currentValue-m_formerValue;
79
80         // adjust for wrap arounds
81         if ( inc > 1 )
82             inc = -1;
83         else if (inc < -1 )
84             inc = 1;
85
86         if ( inc == 1 )
87             wxpeer->TriggerScrollEvent(wxEVT_SCROLL_LINEUP);
88         else if ( inc == -1 )
89             wxpeer->TriggerScrollEvent(wxEVT_SCROLL_LINEDOWN);
90
91         m_formerValue = [(NSStepper*)m_osxView intValue];
92     }
93 }
94
95 wxWidgetImplType* wxWidgetImpl::CreateSpinButton( wxWindowMac* wxpeer,
96                                     wxWindowMac* WXUNUSED(parent),
97                                     wxWindowID WXUNUSED(id),
98                                     wxInt32 value,
99                                     wxInt32 minimum,
100                                     wxInt32 maximum,
101                                     const wxPoint& pos,
102                                     const wxSize& size,
103                                     long style,
104                                     long WXUNUSED(extraStyle))
105 {
106     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
107     wxNSStepper* v = [[wxNSStepper alloc] initWithFrame:r];
108
109     [v setMinValue: minimum];
110     [v setMaxValue: maximum];
111     [v setIntValue: value];
112
113     if ( style & wxSP_HORIZONTAL )
114         [v rotateByAngle:90.0];
115
116     BOOL wrap = NO;
117     if ( style & wxSP_WRAP )
118         wrap = YES;
119     [v setValueWraps:wrap];
120     
121     wxWidgetCocoaImpl* c = new wxSpinButtonCocoaImpl( wxpeer, v );
122     return c;
123 }
124
125 #endif // wxUSE_SPINBTN