]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: spinbutt.cpp | |
3 | // Purpose: wxSpinButton | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
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/mac/uma.h" | |
18 | ||
19 | ||
20 | IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl) | |
21 | IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent) | |
22 | ||
23 | ||
24 | wxSpinButton::wxSpinButton() | |
25 | : wxSpinButtonBase() | |
26 | { | |
27 | } | |
28 | ||
29 | bool wxSpinButton::Create( wxWindow *parent, | |
30 | wxWindowID id, const wxPoint& pos, const wxSize& size, | |
31 | long style, const wxString& name ) | |
32 | { | |
33 | m_macIsUserPane = false; | |
34 | ||
35 | if ( !wxSpinButtonBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) ) | |
36 | return false; | |
37 | ||
38 | m_min = 0; | |
39 | m_max = 100; | |
40 | ||
41 | if (!parent) | |
42 | return false; | |
43 | ||
44 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ); | |
45 | ||
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 ); | |
51 | ||
52 | m_peer->SetActionProc( GetwxMacLiveScrollbarActionProc() ); | |
53 | MacPostControlCreate( pos, size ); | |
54 | ||
55 | return true; | |
56 | } | |
57 | ||
58 | wxSpinButton::~wxSpinButton() | |
59 | { | |
60 | } | |
61 | ||
62 | int wxSpinButton::GetMin() const | |
63 | { | |
64 | return m_min; | |
65 | } | |
66 | ||
67 | int wxSpinButton::GetMax() const | |
68 | { | |
69 | return m_max; | |
70 | } | |
71 | ||
72 | int wxSpinButton::GetValue() const | |
73 | { | |
74 | int n = m_value; | |
75 | ||
76 | if (n < m_min) | |
77 | n = m_min; | |
78 | else if (n > m_max) | |
79 | n = m_max; | |
80 | ||
81 | return n; | |
82 | } | |
83 | ||
84 | void wxSpinButton::SetValue(int val) | |
85 | { | |
86 | m_value = val; | |
87 | } | |
88 | ||
89 | void wxSpinButton::SetRange(int minVal, int maxVal) | |
90 | { | |
91 | m_min = minVal; | |
92 | m_max = maxVal; | |
93 | m_peer->SetMaximum( maxVal ); | |
94 | m_peer->SetMinimum( minVal ); | |
95 | } | |
96 | ||
97 | void wxSpinButton::MacHandleValueChanged( int inc ) | |
98 | { | |
99 | wxEventType scrollEvent = wxEVT_NULL; | |
100 | int oldValue = m_value; | |
101 | ||
102 | m_value = oldValue + inc; | |
103 | ||
104 | if (m_value < m_min) | |
105 | { | |
106 | if ( m_windowStyle & wxSP_WRAP ) | |
107 | m_value = m_max; | |
108 | else | |
109 | m_value = m_min; | |
110 | } | |
111 | ||
112 | if (m_value > m_max) | |
113 | { | |
114 | if ( m_windowStyle & wxSP_WRAP ) | |
115 | m_value = m_min; | |
116 | else | |
117 | m_value = m_max; | |
118 | } | |
119 | ||
120 | if ( m_value - oldValue == -1 ) | |
121 | scrollEvent = wxEVT_SCROLL_LINEDOWN; | |
122 | else if ( m_value - oldValue == 1 ) | |
123 | scrollEvent = wxEVT_SCROLL_LINEUP; | |
124 | else | |
125 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; | |
126 | ||
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 | ||
132 | wxSpinEvent event( scrollEvent, m_windowId ); | |
133 | ||
134 | event.SetPosition( m_value ); | |
135 | event.SetEventObject( this ); | |
136 | if ((GetEventHandler()->ProcessEvent( event )) && !event.IsAllowed()) | |
137 | m_value = oldValue; | |
138 | ||
139 | m_peer->SetValue( m_value ); | |
140 | ||
141 | // always send a thumbtrack event | |
142 | if (scrollEvent != wxEVT_SCROLL_THUMBTRACK) | |
143 | { | |
144 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; | |
145 | wxSpinEvent event2( scrollEvent, GetId() ); | |
146 | event2.SetPosition( m_value ); | |
147 | event2.SetEventObject( this ); | |
148 | GetEventHandler()->ProcessEvent( event2 ); | |
149 | } | |
150 | } | |
151 | ||
152 | void wxSpinButton::MacHandleControlClick( WXWidget control, wxInt16 controlpart, bool mouseStillDown ) | |
153 | { | |
154 | int nScrollInc = 0; | |
155 | ||
156 | switch ( controlpart ) | |
157 | { | |
158 | case kControlUpButtonPart : | |
159 | nScrollInc = 1; | |
160 | break; | |
161 | ||
162 | case kControlDownButtonPart : | |
163 | nScrollInc = -1; | |
164 | break; | |
165 | ||
166 | default: | |
167 | break; | |
168 | } | |
169 | ||
170 | MacHandleValueChanged( nScrollInc ) ; | |
171 | } | |
172 | ||
173 | wxInt32 wxSpinButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF event ) | |
174 | { | |
175 | #if 0 | |
176 | // these have been handled by the live action proc already | |
177 | int nScrollInc = 0; | |
178 | wxMacCarbonEvent cEvent( (EventRef)event ); | |
179 | ||
180 | switch ( cEvent.GetParameter<ControlPartCode>(kEventParamControlPart, typeControlPartCode) ) | |
181 | { | |
182 | case kControlUpButtonPart : | |
183 | nScrollInc = 1; | |
184 | break; | |
185 | ||
186 | case kControlDownButtonPart : | |
187 | nScrollInc = -1; | |
188 | break; | |
189 | ||
190 | default : | |
191 | break; | |
192 | } | |
193 | ||
194 | MacHandleValueChanged( nScrollInc ) ; | |
195 | #endif | |
196 | ||
197 | return noErr; | |
198 | } | |
199 | ||
200 | wxSize wxSpinButton::DoGetBestSize() const | |
201 | { | |
202 | return wxSize( 16, 24 ); | |
203 | } | |
204 | ||
205 | #endif // wxUSE_SPINBTN |