]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/osx/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 | // HIG says 2px between text and stepper control, | |
33 | // but a value of 3 leads to the same look as the | |
34 | // spin controls in Apple's apps | |
35 | static const wxCoord MARGIN = 3; | |
36 | ||
37 | // ---------------------------------------------------------------------------- | |
38 | // wxSpinCtrlText: text control used by spin control | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | class wxSpinCtrlText : public wxTextCtrl | |
42 | { | |
43 | public: | |
44 | wxSpinCtrlText(wxSpinCtrl *spin, const wxString& value, int style) | |
45 | : wxTextCtrl(spin , wxID_ANY, value, wxDefaultPosition, wxSize(40, wxDefaultCoord), style ) | |
46 | { | |
47 | m_spin = spin; | |
48 | ||
49 | // remove the default minsize, the spinctrl will have one instead | |
50 | SetMinSize(wxDefaultSize); | |
51 | } | |
52 | ||
53 | bool ProcessEvent(wxEvent &event) | |
54 | { | |
55 | // Hand button down events to wxSpinCtrl. Doesn't work. | |
56 | if (event.GetEventType() == wxEVT_LEFT_DOWN && m_spin->ProcessEvent( event )) | |
57 | return true; | |
58 | ||
59 | return wxTextCtrl::ProcessEvent( event ); | |
60 | } | |
61 | ||
62 | protected: | |
63 | void OnSetFocus(wxFocusEvent& event) | |
64 | { | |
65 | // delegate to parent control | |
66 | event.SetEventObject( GetParent() ); | |
67 | GetParent()->HandleWindowEvent(event); | |
68 | } | |
69 | ||
70 | void OnKillFocus(wxFocusEvent& event) | |
71 | { | |
72 | long l; | |
73 | if ( !GetValue().ToLong(&l) ) | |
74 | { | |
75 | // not a number at all | |
76 | return; | |
77 | } | |
78 | ||
79 | // is within range | |
80 | if (l < m_spin->GetMin()) | |
81 | l = m_spin->GetMin(); | |
82 | if (l > m_spin->GetMax()) | |
83 | l = m_spin->GetMax(); | |
84 | ||
85 | // Update text control | |
86 | wxString str; | |
87 | str.Printf( wxT("%d"), (int)l ); | |
88 | if (str != GetValue()) | |
89 | SetValue( str ); | |
90 | ||
91 | if (l != m_spin->m_oldValue) | |
92 | { | |
93 | // set value in spin button | |
94 | // does that trigger an event? | |
95 | m_spin->m_btn->SetValue( l ); | |
96 | ||
97 | // if not | |
98 | wxCommandEvent cevent(wxEVT_COMMAND_SPINCTRL_UPDATED, m_spin->GetId()); | |
99 | cevent.SetEventObject(m_spin); | |
100 | cevent.SetInt(l); | |
101 | m_spin->HandleWindowEvent(cevent); | |
102 | ||
103 | m_spin->m_oldValue = l; | |
104 | } | |
105 | ||
106 | // delegate to parent control | |
107 | event.SetEventObject( GetParent() ); | |
108 | GetParent()->HandleWindowEvent(event); | |
109 | } | |
110 | ||
111 | void OnTextChange(wxCommandEvent& event) | |
112 | { | |
113 | int val; | |
114 | if ( m_spin->GetTextValue(&val) ) | |
115 | { | |
116 | m_spin->GetSpinButton()->SetValue(val); | |
117 | ||
118 | // If we're already processing a text update from m_spin, | |
119 | // don't send it again, since we could end up recursing | |
120 | // infinitely. | |
121 | if (event.GetId() == m_spin->GetId()) | |
122 | { | |
123 | event.Skip(); | |
124 | return; | |
125 | } | |
126 | ||
127 | // Send event that the text was manually changed | |
128 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_spin->GetId()); | |
129 | event.SetEventObject(m_spin); | |
130 | event.SetString(m_spin->GetText()->GetValue()); | |
131 | event.SetInt(val); | |
132 | ||
133 | m_spin->HandleWindowEvent(event); | |
134 | } | |
135 | ||
136 | event.Skip(); | |
137 | } | |
138 | ||
139 | private: | |
140 | wxSpinCtrl *m_spin; | |
141 | ||
142 | DECLARE_EVENT_TABLE() | |
143 | }; | |
144 | ||
145 | BEGIN_EVENT_TABLE(wxSpinCtrlText, wxTextCtrl) | |
146 | EVT_TEXT(wxID_ANY, wxSpinCtrlText::OnTextChange) | |
147 | EVT_SET_FOCUS(wxSpinCtrlText::OnSetFocus) | |
148 | EVT_KILL_FOCUS(wxSpinCtrlText::OnKillFocus) | |
149 | END_EVENT_TABLE() | |
150 | ||
151 | // ---------------------------------------------------------------------------- | |
152 | // wxSpinCtrlButton: spin button used by spin control | |
153 | // ---------------------------------------------------------------------------- | |
154 | ||
155 | class wxSpinCtrlButton : public wxSpinButton | |
156 | { | |
157 | public: | |
158 | wxSpinCtrlButton(wxSpinCtrl *spin, int style) | |
159 | : wxSpinButton(spin ) | |
160 | { | |
161 | m_spin = spin; | |
162 | SetWindowStyle(style | wxSP_VERTICAL); | |
163 | ||
164 | // TODO: The spin button gets truncated a little bit due to size | |
165 | // differences so change it's default size a bit. SMALL still gets a | |
166 | // bit truncated, but MINI seems to be too small... Readdress this | |
167 | // when the textctrl issues are all sorted out. | |
168 | //SetWindowVariant(wxWINDOW_VARIANT_SMALL); | |
169 | ||
170 | // remove the default minsize, the spinctrl will have one instead | |
171 | SetMinSize(wxDefaultSize); | |
172 | } | |
173 | ||
174 | protected: | |
175 | void OnSpinButton(wxSpinEvent& eventSpin) | |
176 | { | |
177 | int pos = eventSpin.GetPosition(); | |
178 | m_spin->SetTextValue(pos); | |
179 | ||
180 | wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, m_spin->GetId()); | |
181 | event.SetEventObject(m_spin); | |
182 | event.SetInt(pos); | |
183 | ||
184 | m_spin->HandleWindowEvent(event); | |
185 | ||
186 | m_spin->m_oldValue = pos; | |
187 | } | |
188 | ||
189 | private: | |
190 | wxSpinCtrl *m_spin; | |
191 | ||
192 | DECLARE_EVENT_TABLE() | |
193 | }; | |
194 | ||
195 | BEGIN_EVENT_TABLE(wxSpinCtrlButton, wxSpinButton) | |
196 | EVT_SPIN(wxID_ANY, wxSpinCtrlButton::OnSpinButton) | |
197 | END_EVENT_TABLE() | |
198 | ||
199 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl) | |
200 | ||
201 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl) | |
202 | WX_EVENT_TABLE_CONTROL_CONTAINER(wxSpinCtrl) | |
203 | END_EVENT_TABLE() | |
204 | ||
205 | WX_DELEGATE_TO_CONTROL_CONTAINER(wxSpinCtrl, wxControl) | |
206 | ||
207 | ||
208 | // ============================================================================ | |
209 | // implementation | |
210 | // ============================================================================ | |
211 | ||
212 | // ---------------------------------------------------------------------------- | |
213 | // wxSpinCtrl creation | |
214 | // ---------------------------------------------------------------------------- | |
215 | ||
216 | void wxSpinCtrl::Init() | |
217 | { | |
218 | m_text = NULL; | |
219 | m_btn = NULL; | |
220 | WX_INIT_CONTROL_CONTAINER(); | |
221 | } | |
222 | ||
223 | bool wxSpinCtrl::Create(wxWindow *parent, | |
224 | wxWindowID id, | |
225 | const wxString& value, | |
226 | const wxPoint& pos, | |
227 | const wxSize& size, | |
228 | long style, | |
229 | int min, | |
230 | int max, | |
231 | int initial, | |
232 | const wxString& name) | |
233 | { | |
234 | m_macIsUserPane = true; | |
235 | if ( !wxControl::Create(parent, id, pos, size, style, | |
236 | wxDefaultValidator, name) ) | |
237 | { | |
238 | return false; | |
239 | } | |
240 | ||
241 | // the string value overrides the numeric one (for backwards compatibility | |
242 | // reasons and also because it is simpler to satisfy the string value which | |
243 | // comes much sooner in the list of arguments and leave the initial | |
244 | // parameter unspecified) | |
245 | if ( !value.empty() ) | |
246 | { | |
247 | long l; | |
248 | if ( value.ToLong(&l) ) | |
249 | initial = l; | |
250 | } | |
251 | ||
252 | wxSize csize = size ; | |
253 | m_text = new wxSpinCtrlText(this, value, style & ( wxTE_PROCESS_ENTER | wxALIGN_MASK ) ); | |
254 | m_btn = new wxSpinCtrlButton(this, style); | |
255 | ||
256 | m_btn->SetRange(min, max); | |
257 | m_btn->SetValue(initial); | |
258 | // make it different | |
259 | m_oldValue = GetMin()-1; | |
260 | ||
261 | if ( size.x == wxDefaultCoord ){ | |
262 | csize.x = m_text->GetSize().x + MARGIN + m_btn->GetSize().x ; | |
263 | } | |
264 | ||
265 | if ( size.y == wxDefaultCoord ) { | |
266 | csize.y = m_text->GetSize().y + 2 * TEXTBORDER ; //allow for text border highlights | |
267 | if ( m_btn->GetSize().y > csize.y ) | |
268 | csize.y = m_btn->GetSize().y ; | |
269 | } | |
270 | ||
271 | //SetSize(csize); | |
272 | ||
273 | //MacPostControlCreate(pos, csize); | |
274 | SetInitialSize(csize); | |
275 | ||
276 | return true; | |
277 | } | |
278 | ||
279 | wxSpinCtrl::~wxSpinCtrl() | |
280 | { | |
281 | // delete the controls now, don't leave them alive even though they would | |
282 | // still be eventually deleted by our parent - but it will be too late, the | |
283 | // user code expects them to be gone now | |
284 | delete m_text; | |
285 | m_text = NULL ; | |
286 | delete m_btn; | |
287 | m_btn = NULL ; | |
288 | } | |
289 | ||
290 | // ---------------------------------------------------------------------------- | |
291 | // geometry | |
292 | // ---------------------------------------------------------------------------- | |
293 | ||
294 | wxSize wxSpinCtrl::DoGetBestSize() const | |
295 | { | |
296 | if (!m_btn || !m_text) | |
297 | return GetSize(); | |
298 | ||
299 | wxSize sizeBtn = m_btn->GetBestSize(), | |
300 | sizeText = m_text->GetBestSize(); | |
301 | ||
302 | sizeText.y += 2 * TEXTBORDER ; | |
303 | sizeText.x += 2 * TEXTBORDER ; | |
304 | ||
305 | int height; | |
306 | if (sizeText.y > sizeBtn.y) | |
307 | height = sizeText.y; | |
308 | else | |
309 | height = sizeBtn.y; | |
310 | ||
311 | return wxSize(sizeBtn.x + sizeText.x + MARGIN, height ); | |
312 | } | |
313 | ||
314 | void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height) | |
315 | { | |
316 | // position the subcontrols inside the client area | |
317 | wxSize sizeBtn = m_btn->GetSize(); | |
318 | wxSize sizeText = m_text->GetSize(); | |
319 | ||
320 | wxControl::DoMoveWindow(x, y, width, height); | |
321 | ||
322 | wxCoord wText = width - sizeBtn.x - MARGIN - 2 * TEXTBORDER; | |
323 | ||
324 | m_text->SetSize(TEXTBORDER, (height - sizeText.y) / 2, wText, -1); | |
325 | m_btn->SetSize(0 + wText + MARGIN + TEXTBORDER , (height - sizeBtn.y) / 2 , -1, -1 ); | |
326 | } | |
327 | ||
328 | // ---------------------------------------------------------------------------- | |
329 | // operations forwarded to the subcontrols | |
330 | // ---------------------------------------------------------------------------- | |
331 | ||
332 | bool wxSpinCtrl::Enable(bool enable) | |
333 | { | |
334 | if ( !wxControl::Enable(enable) ) | |
335 | return false; | |
336 | return true; | |
337 | } | |
338 | ||
339 | bool wxSpinCtrl::Show(bool show) | |
340 | { | |
341 | if ( !wxControl::Show(show) ) | |
342 | return false; | |
343 | return true; | |
344 | } | |
345 | ||
346 | // ---------------------------------------------------------------------------- | |
347 | // value and range access | |
348 | // ---------------------------------------------------------------------------- | |
349 | ||
350 | bool wxSpinCtrl::GetTextValue(int *val) const | |
351 | { | |
352 | long l; | |
353 | if ( !m_text->GetValue().ToLong(&l) ) | |
354 | { | |
355 | // not a number at all | |
356 | return false; | |
357 | } | |
358 | ||
359 | if ( l < GetMin() || l > GetMax() ) | |
360 | { | |
361 | // out of range | |
362 | return false; | |
363 | } | |
364 | ||
365 | *val = l; | |
366 | ||
367 | return true; | |
368 | } | |
369 | ||
370 | int wxSpinCtrl::GetValue() const | |
371 | { | |
372 | return m_btn ? m_btn->GetValue() : 0; | |
373 | } | |
374 | ||
375 | int wxSpinCtrl::GetMin() const | |
376 | { | |
377 | return m_btn ? m_btn->GetMin() : 0; | |
378 | } | |
379 | ||
380 | int wxSpinCtrl::GetMax() const | |
381 | { | |
382 | return m_btn ? m_btn->GetMax() : 0; | |
383 | } | |
384 | ||
385 | // ---------------------------------------------------------------------------- | |
386 | // changing value and range | |
387 | // ---------------------------------------------------------------------------- | |
388 | ||
389 | void wxSpinCtrl::SetTextValue(int val) | |
390 | { | |
391 | wxCHECK_RET( m_text, _T("invalid call to wxSpinCtrl::SetTextValue") ); | |
392 | ||
393 | m_text->SetValue(wxString::Format(_T("%d"), val)); | |
394 | ||
395 | // select all text | |
396 | m_text->SetSelection(0, -1); | |
397 | ||
398 | // and give focus to the control! | |
399 | // m_text->SetFocus(); Why???? TODO. | |
400 | } | |
401 | ||
402 | void wxSpinCtrl::SetValue(int val) | |
403 | { | |
404 | wxCHECK_RET( m_btn, _T("invalid call to wxSpinCtrl::SetValue") ); | |
405 | ||
406 | SetTextValue(val); | |
407 | ||
408 | m_btn->SetValue(val); | |
409 | m_oldValue = val; | |
410 | } | |
411 | ||
412 | void wxSpinCtrl::SetValue(const wxString& text) | |
413 | { | |
414 | wxCHECK_RET( m_text, _T("invalid call to wxSpinCtrl::SetValue") ); | |
415 | ||
416 | long val; | |
417 | if ( text.ToLong(&val) && ((val > INT_MIN) && (val < INT_MAX)) ) | |
418 | { | |
419 | SetValue((int)val); | |
420 | } | |
421 | else // not a number at all or out of range | |
422 | { | |
423 | m_text->SetValue(text); | |
424 | m_text->SetSelection(0, -1); | |
425 | } | |
426 | } | |
427 | ||
428 | void wxSpinCtrl::SetRange(int min, int max) | |
429 | { | |
430 | wxCHECK_RET( m_btn, _T("invalid call to wxSpinCtrl::SetRange") ); | |
431 | ||
432 | m_btn->SetRange(min, max); | |
433 | } | |
434 | ||
435 | void wxSpinCtrl::SetSelection(long from, long to) | |
436 | { | |
437 | // if from and to are both -1, it means (in wxWidgets) that all text should | |
438 | // be selected | |
439 | if ( (from == -1) && (to == -1) ) | |
440 | { | |
441 | from = 0; | |
442 | } | |
443 | m_text->SetSelection(from, to); | |
444 | } | |
445 | ||
446 | #endif // wxUSE_SPINCTRL |