]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/spinbutt.cpp
Test to avoid crashes in some circumstances
[wxWidgets.git] / src / mac / carbon / spinbutt.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: spinbutt.cpp
3// Purpose: wxSpinButton
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878 12#include "wx/wxprec.h"
312ebad4
WS
13
14#if wxUSE_SPINBTN
15
e9576ca5 16#include "wx/spinbutt.h"
519cb848 17#include "wx/mac/uma.h"
e9576ca5 18
e7549107 19
fd04970a
SC
20IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl)
21IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent)
e9576ca5 22
43524b15 23
03e11df5
GD
24wxSpinButton::wxSpinButton()
25 : wxSpinButtonBase()
26{
27}
28
43524b15
DS
29bool wxSpinButton::Create( wxWindow *parent,
30 wxWindowID id, const wxPoint& pos, const wxSize& size,
31 long style, const wxString& name )
e9576ca5 32{
43524b15 33 m_macIsUserPane = false;
312ebad4 34
43524b15 35 if ( !wxSpinButtonBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
b45ed7a2
VZ
36 return false;
37
e9576ca5
SC
38 m_min = 0;
39 m_max = 100;
312ebad4 40
e40298d5 41 if (!parent)
312ebad4
WS
42 return false;
43
43524b15 44 Rect bounds = wxMacGetBoundsForControl( this , pos , size );
312ebad4 45
43524b15
DS
46 m_peer = new wxMacControl( this );
47 OSStatus err = CreateLittleArrowsControl(
48 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, 0, m_min, m_max, 1,
49 m_peer->GetControlRefAddr() );
50 verify_noerr( err );
312ebad4 51
43524b15
DS
52 m_peer->SetActionProc( GetwxMacLiveScrollbarActionProc() );
53 MacPostControlCreate( pos, size );
312ebad4
WS
54
55 return true;
e9576ca5 56}
312ebad4 57
e9576ca5
SC
58wxSpinButton::~wxSpinButton()
59{
60}
61
03e11df5
GD
62int wxSpinButton::GetMin() const
63{
e40298d5 64 return m_min;
03e11df5
GD
65}
66
67int wxSpinButton::GetMax() const
68{
e40298d5 69 return m_max;
03e11df5
GD
70}
71
e9576ca5
SC
72int wxSpinButton::GetValue() const
73{
6511d307
RR
74 int n = m_value;
75
43524b15
DS
76 if (n < m_min)
77 n = m_min;
78 else if (n > m_max)
79 n = m_max;
6511d307
RR
80
81 return n;
e9576ca5
SC
82}
83
84void wxSpinButton::SetValue(int val)
85{
43524b15 86 m_value = val;
e9576ca5
SC
87}
88
89void wxSpinButton::SetRange(int minVal, int maxVal)
90{
e40298d5
JS
91 m_min = minVal;
92 m_max = maxVal;
43524b15
DS
93 m_peer->SetMaximum( maxVal );
94 m_peer->SetMinimum( minVal );
e9576ca5
SC
95}
96
a54b0880 97void wxSpinButton::MacHandleValueChanged( int inc )
519cb848 98{
a54b0880 99 wxEventType scrollEvent = wxEVT_NULL;
43524b15 100 int oldValue = m_value;
312ebad4 101
a54b0880 102 m_value = oldValue + inc;
312ebad4 103
a54b0880
SC
104 if (m_value < m_min)
105 {
e40298d5
JS
106 if ( m_windowStyle & wxSP_WRAP )
107 m_value = m_max;
108 else
109 m_value = m_min;
a54b0880 110 }
312ebad4 111
a54b0880
SC
112 if (m_value > m_max)
113 {
e40298d5
JS
114 if ( m_windowStyle & wxSP_WRAP )
115 m_value = m_min;
116 else
117 m_value = m_max;
a54b0880 118 }
312ebad4 119
92432aa1 120 if ( m_value - oldValue == -1 )
43524b15 121 scrollEvent = wxEVT_SCROLL_LINEDOWN;
92432aa1 122 else if ( m_value - oldValue == 1 )
43524b15 123 scrollEvent = wxEVT_SCROLL_LINEUP;
a54b0880 124 else
43524b15 125 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
312ebad4 126
e225fe17
KH
127 // Do not send an event if the value has not actually changed
128 // (Also works for wxSpinCtrl)
129 if ( m_value == oldValue )
130 return;
131
43524b15 132 wxSpinEvent event( scrollEvent, m_windowId );
312ebad4 133
43524b15 134 event.SetPosition( m_value );
a54b0880 135 event.SetEventObject( this );
43524b15
DS
136 if ((GetEventHandler()->ProcessEvent( event )) && !event.IsAllowed())
137 m_value = oldValue;
138
139 m_peer->SetValue( m_value );
312ebad4 140
43524b15 141 // always send a thumbtrack event
72d750d4
SC
142 if (scrollEvent != wxEVT_SCROLL_THUMBTRACK)
143 {
144 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
43524b15 145 wxSpinEvent event2( scrollEvent, GetId() );
72d750d4
SC
146 event2.SetPosition( m_value );
147 event2.SetEventObject( this );
148 GetEventHandler()->ProcessEvent( event2 );
149 }
519cb848
SC
150}
151
43524b15 152void wxSpinButton::MacHandleControlClick( WXWidget control, wxInt16 controlpart, bool mouseStillDown )
64be92e2
SC
153{
154 int nScrollInc = 0;
312ebad4 155
43524b15 156 switch ( controlpart )
64be92e2
SC
157 {
158 case kControlUpButtonPart :
159 nScrollInc = 1;
43524b15
DS
160 break;
161
64be92e2
SC
162 case kControlDownButtonPart :
163 nScrollInc = -1;
43524b15
DS
164 break;
165
166 default:
167 break;
64be92e2 168 }
43524b15 169
64be92e2
SC
170 MacHandleValueChanged( nScrollInc ) ;
171}
172
312ebad4 173wxInt32 wxSpinButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF event )
a54b0880 174{
43524b15 175#if 0
64be92e2 176 // these have been handled by the live action proc already
e40298d5 177 int nScrollInc = 0;
43524b15 178 wxMacCarbonEvent cEvent( (EventRef)event );
312ebad4 179
43524b15 180 switch ( cEvent.GetParameter<ControlPartCode>(kEventParamControlPart, typeControlPartCode) )
e40298d5
JS
181 {
182 case kControlUpButtonPart :
183 nScrollInc = 1;
43524b15
DS
184 break;
185
e40298d5
JS
186 case kControlDownButtonPart :
187 nScrollInc = -1;
43524b15
DS
188 break;
189
190 default :
191 break;
e40298d5 192 }
43524b15 193
e40298d5 194 MacHandleValueChanged( nScrollInc ) ;
43524b15 195#endif
a54b0880 196
43524b15
DS
197 return noErr;
198}
51abe921 199
37e2cb08 200wxSize wxSpinButton::DoGetBestSize() const
51abe921 201{
43524b15 202 return wxSize( 16, 24 );
51abe921 203}
519cb848 204
312ebad4 205#endif // wxUSE_SPINBTN