]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/spinctrl.cpp
getting out for 'addded' events early enough
[wxWidgets.git] / src / mac / carbon / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/spinbutt.cpp
3 // Purpose: wxSpinCtrl
4 // Author: Robert
5 // Modified by: Mark Newsam (Based on GTK file)
6 // RCS-ID: $Id$
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_SPINCTRL
14
15 #include "wx/spinctrl.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/textctrl.h"
19 #include "wx/containr.h"
20 #endif
21
22 #include "wx/spinbutt.h"
23
24 // ----------------------------------------------------------------------------
25 // constants
26 // ----------------------------------------------------------------------------
27
28 // the focus rect around a text may have 4 pixels in each direction
29 // we handle these problems right now in an extended vis region of a window
30 static const wxCoord TEXTBORDER = 4 ;
31 // the margin between the text control and the spin
32 static const wxCoord MARGIN = 8 - TEXTBORDER;
33
34 // ----------------------------------------------------------------------------
35 // wxSpinCtrlText: text control used by spin control
36 // ----------------------------------------------------------------------------
37
38 class wxSpinCtrlText : public wxTextCtrl
39 {
40 public:
41 wxSpinCtrlText(wxSpinCtrl *spin, const wxString& value)
42 : wxTextCtrl(spin , wxID_ANY, value, wxDefaultPosition, wxSize(40, wxDefaultCoord))
43 {
44 m_spin = spin;
45
46 // remove the default minsize, the spinctrl will have one instead
47 SetSizeHints(-1,-1);
48 }
49
50 bool ProcessEvent(wxEvent &event)
51 {
52 // Hand button down events to wxSpinCtrl. Doesn't work.
53 if (event.GetEventType() == wxEVT_LEFT_DOWN && m_spin->ProcessEvent( event ))
54 return true;
55
56 return wxTextCtrl::ProcessEvent( event );
57 }
58
59 protected:
60 void OnTextChange(wxCommandEvent& event)
61 {
62 int val;
63 if ( m_spin->GetTextValue(&val) )
64 {
65 m_spin->GetSpinButton()->SetValue(val);
66
67 // If we're already processing a text update from m_spin,
68 // don't send it again, since we could end up recursing
69 // infinitely.
70 if (event.GetId() == m_spin->GetId())
71 {
72 event.Skip();
73 return;
74 }
75
76 // Send event that the text was manually changed
77 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_spin->GetId());
78 event.SetEventObject(m_spin);
79 event.SetString(m_spin->GetText()->GetValue());
80 event.SetInt(val);
81
82 m_spin->GetEventHandler()->ProcessEvent(event);
83 }
84
85 event.Skip();
86 }
87
88 private:
89 wxSpinCtrl *m_spin;
90
91 DECLARE_EVENT_TABLE()
92 };
93
94 BEGIN_EVENT_TABLE(wxSpinCtrlText, wxTextCtrl)
95 EVT_TEXT(wxID_ANY, wxSpinCtrlText::OnTextChange)
96 END_EVENT_TABLE()
97
98 // ----------------------------------------------------------------------------
99 // wxSpinCtrlButton: spin button used by spin control
100 // ----------------------------------------------------------------------------
101
102 class wxSpinCtrlButton : public wxSpinButton
103 {
104 public:
105 wxSpinCtrlButton(wxSpinCtrl *spin, int style)
106 : wxSpinButton(spin )
107 {
108 m_spin = spin;
109 SetWindowStyle(style | wxSP_VERTICAL);
110
111 // TODO: The spin button gets truncated a little bit due to size
112 // differences so change it's default size a bit. SMALL still gets a
113 // bit truncated, but MINI seems to be too small... Readdress this
114 // when the textctrl issues are all sorted out.
115 //SetWindowVariant(wxWINDOW_VARIANT_SMALL);
116
117 // remove the default minsize, the spinctrl will have one instead
118 SetSizeHints(-1,-1);
119 }
120
121 protected:
122 void OnSpinButton(wxSpinEvent& eventSpin)
123 {
124 m_spin->SetTextValue(eventSpin.GetPosition());
125
126 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, m_spin->GetId());
127 event.SetEventObject(m_spin);
128 event.SetInt(eventSpin.GetPosition());
129
130 m_spin->GetEventHandler()->ProcessEvent(event);
131 }
132
133 private:
134 wxSpinCtrl *m_spin;
135
136 DECLARE_EVENT_TABLE()
137 };
138
139 BEGIN_EVENT_TABLE(wxSpinCtrlButton, wxSpinButton)
140 EVT_SPIN(wxID_ANY, wxSpinCtrlButton::OnSpinButton)
141 END_EVENT_TABLE()
142
143 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
144
145 BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
146 WX_EVENT_TABLE_CONTROL_CONTAINER(wxSpinCtrl)
147 END_EVENT_TABLE()
148
149 WX_DELEGATE_TO_CONTROL_CONTAINER(wxSpinCtrl, wxControl)
150
151
152 // ============================================================================
153 // implementation
154 // ============================================================================
155
156 // ----------------------------------------------------------------------------
157 // wxSpinCtrl creation
158 // ----------------------------------------------------------------------------
159
160 void wxSpinCtrl::Init()
161 {
162 m_text = NULL;
163 m_btn = NULL;
164 m_container.SetContainerWindow(this);
165 }
166
167 bool wxSpinCtrl::Create(wxWindow *parent,
168 wxWindowID id,
169 const wxString& value,
170 const wxPoint& pos,
171 const wxSize& size,
172 long style,
173 int min,
174 int max,
175 int initial,
176 const wxString& name)
177 {
178 m_macIsUserPane = true;
179 if ( !wxControl::Create(parent, id, pos, size, style,
180 wxDefaultValidator, name) )
181 {
182 return false;
183 }
184
185 // the string value overrides the numeric one (for backwards compatibility
186 // reasons and also because it is simpler to satisfy the string value which
187 // comes much sooner in the list of arguments and leave the initial
188 // parameter unspecified)
189 if ( !value.empty() )
190 {
191 long l;
192 if ( value.ToLong(&l) )
193 initial = l;
194 }
195
196 wxSize csize = size ;
197 m_text = new wxSpinCtrlText(this, value);
198 m_btn = new wxSpinCtrlButton(this, style);
199
200 m_btn->SetRange(min, max);
201 m_btn->SetValue(initial);
202
203 if ( size.x == wxDefaultCoord ){
204 csize.x = m_text->GetSize().x + MARGIN + m_btn->GetSize().x ;
205 }
206
207 if ( size.y == wxDefaultCoord ) {
208 csize.y = m_text->GetSize().y + 2 * TEXTBORDER ; //allow for text border highlights
209 if ( m_btn->GetSize().y > csize.y )
210 csize.y = m_btn->GetSize().y ;
211 }
212
213 //SetSize(csize);
214
215 //MacPostControlCreate(pos, csize);
216 SetInitialBestSize(csize);
217
218 return true;
219 }
220
221 wxSpinCtrl::~wxSpinCtrl()
222 {
223 // delete the controls now, don't leave them alive even though they would
224 // still be eventually deleted by our parent - but it will be too late, the
225 // user code expects them to be gone now
226 delete m_text;
227 m_text = NULL ;
228 delete m_btn;
229 m_btn = NULL ;
230 }
231
232 // ----------------------------------------------------------------------------
233 // geometry
234 // ----------------------------------------------------------------------------
235
236 wxSize wxSpinCtrl::DoGetBestSize() const
237 {
238 if (!m_btn || !m_text)
239 return GetSize();
240
241 wxSize sizeBtn = m_btn->GetBestSize(),
242 sizeText = m_text->GetBestSize();
243
244 sizeText.y += 2 * TEXTBORDER ;
245 sizeText.x += 2 * TEXTBORDER ;
246
247 int height;
248 if (sizeText.y > sizeBtn.y)
249 height = sizeText.y;
250 else
251 height = sizeBtn.y;
252
253 return wxSize(sizeBtn.x + sizeText.x + MARGIN, height );
254 }
255
256 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
257 {
258 // position the subcontrols inside the client area
259 wxSize sizeBtn = m_btn->GetSize();
260 wxSize sizeText = m_text->GetSize();
261
262 wxControl::DoMoveWindow(x, y, width, height);
263
264 wxCoord wText = width - sizeBtn.x - MARGIN - 2 * TEXTBORDER;
265
266 m_text->SetSize(TEXTBORDER, (height - sizeText.y) / 2, wText, -1);
267 m_btn->SetSize(0 + wText + MARGIN + 2 * TEXTBORDER , (height - sizeBtn.y) / 2 , -1, -1 );
268 }
269
270 // ----------------------------------------------------------------------------
271 // operations forwarded to the subcontrols
272 // ----------------------------------------------------------------------------
273
274 bool wxSpinCtrl::Enable(bool enable)
275 {
276 if ( !wxControl::Enable(enable) )
277 return false;
278 return true;
279 }
280
281 bool wxSpinCtrl::Show(bool show)
282 {
283 if ( !wxControl::Show(show) )
284 return false;
285 return true;
286 }
287
288 // ----------------------------------------------------------------------------
289 // value and range access
290 // ----------------------------------------------------------------------------
291
292 bool wxSpinCtrl::GetTextValue(int *val) const
293 {
294 long l;
295 if ( !m_text->GetValue().ToLong(&l) )
296 {
297 // not a number at all
298 return false;
299 }
300
301 if ( l < GetMin() || l > GetMax() )
302 {
303 // out of range
304 return false;
305 }
306
307 *val = l;
308
309 return true;
310 }
311
312 int wxSpinCtrl::GetValue() const
313 {
314 return m_btn ? m_btn->GetValue() : 0;
315 }
316
317 int wxSpinCtrl::GetMin() const
318 {
319 return m_btn ? m_btn->GetMin() : 0;
320 }
321
322 int wxSpinCtrl::GetMax() const
323 {
324 return m_btn ? m_btn->GetMax() : 0;
325 }
326
327 // ----------------------------------------------------------------------------
328 // changing value and range
329 // ----------------------------------------------------------------------------
330
331 void wxSpinCtrl::SetTextValue(int val)
332 {
333 wxCHECK_RET( m_text, _T("invalid call to wxSpinCtrl::SetTextValue") );
334
335 m_text->SetValue(wxString::Format(_T("%d"), val));
336
337 // select all text
338 m_text->SetSelection(0, -1);
339
340 // and give focus to the control!
341 // m_text->SetFocus(); Why???? TODO.
342 }
343
344 void wxSpinCtrl::SetValue(int val)
345 {
346 wxCHECK_RET( m_btn, _T("invalid call to wxSpinCtrl::SetValue") );
347
348 SetTextValue(val);
349
350 m_btn->SetValue(val);
351 }
352
353 void wxSpinCtrl::SetValue(const wxString& text)
354 {
355 wxCHECK_RET( m_text, _T("invalid call to wxSpinCtrl::SetValue") );
356
357 long val;
358 if ( text.ToLong(&val) && ((val > INT_MIN) && (val < INT_MAX)) )
359 {
360 SetValue((int)val);
361 }
362 else // not a number at all or out of range
363 {
364 m_text->SetValue(text);
365 m_text->SetSelection(0, -1);
366 }
367 }
368
369 void wxSpinCtrl::SetRange(int min, int max)
370 {
371 wxCHECK_RET( m_btn, _T("invalid call to wxSpinCtrl::SetRange") );
372
373 m_btn->SetRange(min, max);
374 }
375
376 void wxSpinCtrl::SetSelection(long from, long to)
377 {
378 // if from and to are both -1, it means (in wxWidgets) that all text should
379 // be selected
380 if ( (from == -1) && (to == -1) )
381 {
382 from = 0;
383 }
384 m_text->SetSelection(from, to);
385 }
386
387 #endif // wxUSE_SPINCTRL