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