]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/spinctlg.cpp | |
3 | // Purpose: implements wxSpinCtrl as a composite control | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29.01.01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // License: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/textctrl.h" | |
29 | #endif //WX_PRECOMP | |
30 | ||
31 | #include "wx/spinctrl.h" | |
32 | ||
33 | #if wxUSE_SPINCTRL | |
34 | ||
35 | IMPLEMENT_DYNAMIC_CLASS(wxSpinDoubleEvent, wxNotifyEvent) | |
36 | ||
37 | // There are port-specific versions for the wxSpinCtrl, so exclude the | |
38 | // contents of this file in those cases | |
39 | #if !defined(wxHAS_NATIVE_SPINCTRL) || !defined(wxHAS_NATIVE_SPINCTRLDOUBLE) | |
40 | ||
41 | #include "wx/spinbutt.h" | |
42 | ||
43 | #if wxUSE_SPINBTN | |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // constants | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | // the margin between the text control and the spin | |
50 | static const wxCoord MARGIN = 2; | |
51 | ||
52 | #define SPINCTRLBUT_MAX 32000 // large to avoid wrap around trouble | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // wxSpinCtrlTextGeneric: text control used by spin control | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | class wxSpinCtrlTextGeneric : public wxTextCtrl | |
59 | { | |
60 | public: | |
61 | wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase *spin, const wxString& value) | |
62 | : wxTextCtrl(spin->GetParent(), wxID_ANY, value, wxDefaultPosition, | |
63 | wxDefaultSize, wxTE_NOHIDESEL|wxTE_PROCESS_ENTER) | |
64 | { | |
65 | m_spin = spin; | |
66 | ||
67 | // remove the default minsize, the spinctrl will have one instead | |
68 | SetSizeHints(wxDefaultCoord, wxDefaultCoord); | |
69 | } | |
70 | ||
71 | virtual ~wxSpinCtrlTextGeneric() | |
72 | { | |
73 | // MSW sends extra kill focus event on destroy | |
74 | if (m_spin) | |
75 | m_spin->m_textCtrl = NULL; | |
76 | ||
77 | m_spin = NULL; | |
78 | } | |
79 | ||
80 | void OnTextEnter(wxCommandEvent& event) | |
81 | { | |
82 | if (m_spin) | |
83 | m_spin->OnTextEnter(event); | |
84 | } | |
85 | ||
86 | void OnChar( wxKeyEvent &event ) | |
87 | { | |
88 | if (m_spin) | |
89 | m_spin->OnTextChar(event); | |
90 | } | |
91 | ||
92 | void OnKillFocus(wxFocusEvent& event) | |
93 | { | |
94 | if (m_spin) | |
95 | { | |
96 | m_spin->SyncSpinToText(); | |
97 | m_spin->DoSendEvent(); | |
98 | } | |
99 | ||
100 | event.Skip(); | |
101 | } | |
102 | ||
103 | wxSpinCtrlGenericBase *m_spin; | |
104 | ||
105 | private: | |
106 | DECLARE_EVENT_TABLE() | |
107 | }; | |
108 | ||
109 | BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric, wxTextCtrl) | |
110 | EVT_TEXT_ENTER(wxID_ANY, wxSpinCtrlTextGeneric::OnTextEnter) | |
111 | ||
112 | EVT_CHAR(wxSpinCtrlTextGeneric::OnChar) | |
113 | ||
114 | EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus) | |
115 | END_EVENT_TABLE() | |
116 | ||
117 | // ---------------------------------------------------------------------------- | |
118 | // wxSpinCtrlButtonGeneric: spin button used by spin control | |
119 | // ---------------------------------------------------------------------------- | |
120 | ||
121 | class wxSpinCtrlButtonGeneric : public wxSpinButton | |
122 | { | |
123 | public: | |
124 | wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase *spin, int style) | |
125 | : wxSpinButton(spin->GetParent(), wxID_ANY, wxDefaultPosition, | |
126 | wxDefaultSize, style | wxSP_VERTICAL) | |
127 | { | |
128 | m_spin = spin; | |
129 | ||
130 | SetRange(-SPINCTRLBUT_MAX, SPINCTRLBUT_MAX); | |
131 | ||
132 | // remove the default minsize, the spinctrl will have one instead | |
133 | SetSizeHints(wxDefaultCoord, wxDefaultCoord); | |
134 | } | |
135 | ||
136 | void OnSpinButton(wxSpinEvent& event) | |
137 | { | |
138 | if (m_spin) | |
139 | m_spin->OnSpinButton(event); | |
140 | } | |
141 | ||
142 | wxSpinCtrlGenericBase *m_spin; | |
143 | ||
144 | private: | |
145 | DECLARE_EVENT_TABLE() | |
146 | }; | |
147 | ||
148 | BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric, wxSpinButton) | |
149 | EVT_SPIN_UP( wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton) | |
150 | EVT_SPIN_DOWN(wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton) | |
151 | END_EVENT_TABLE() | |
152 | ||
153 | // ============================================================================ | |
154 | // wxSpinCtrlGenericBase | |
155 | // ============================================================================ | |
156 | ||
157 | // ---------------------------------------------------------------------------- | |
158 | // wxSpinCtrlGenericBase creation | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | void wxSpinCtrlGenericBase::Init() | |
162 | { | |
163 | m_value = 0; | |
164 | m_min = 0; | |
165 | m_max = 100; | |
166 | m_increment = 1; | |
167 | m_snap_to_ticks = false; | |
168 | m_format = wxS("%g"); | |
169 | ||
170 | m_spin_value = 0; | |
171 | ||
172 | m_textCtrl = NULL; | |
173 | m_spinButton = NULL; | |
174 | } | |
175 | ||
176 | bool wxSpinCtrlGenericBase::Create(wxWindow *parent, | |
177 | wxWindowID id, | |
178 | const wxString& value, | |
179 | const wxPoint& pos, const wxSize& size, | |
180 | long style, | |
181 | double min, double max, double initial, | |
182 | double increment, | |
183 | const wxString& name) | |
184 | { | |
185 | // don't use borders for this control itself, it wouldn't look good with | |
186 | // the text control borders (but we might want to use style border bits to | |
187 | // select the text control style) | |
188 | if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, | |
189 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, | |
190 | wxDefaultValidator, name) ) | |
191 | { | |
192 | return false; | |
193 | } | |
194 | ||
195 | m_value = initial; | |
196 | m_min = min; | |
197 | m_max = max; | |
198 | m_increment = increment; | |
199 | ||
200 | m_textCtrl = new wxSpinCtrlTextGeneric(this, value); | |
201 | m_spinButton = new wxSpinCtrlButtonGeneric(this, style); | |
202 | ||
203 | m_spin_value = m_spinButton->GetValue(); | |
204 | ||
205 | // the string value overrides the numeric one (for backwards compatibility | |
206 | // reasons and also because it is simpler to satisfy the string value which | |
207 | // comes much sooner in the list of arguments and leave the initial | |
208 | // parameter unspecified) | |
209 | if ( !value.empty() ) | |
210 | { | |
211 | double d; | |
212 | if ( value.ToDouble(&d) ) | |
213 | { | |
214 | m_value = d; | |
215 | m_textCtrl->SetValue(wxString::Format(m_format, m_value)); | |
216 | } | |
217 | } | |
218 | ||
219 | SetInitialSize(size); | |
220 | Move(pos); | |
221 | ||
222 | // have to disable this window to avoid interfering it with message | |
223 | // processing to the text and the button... but pretend it is enabled to | |
224 | // make IsEnabled() return true | |
225 | wxControl::Enable(false); // don't use non virtual Disable() here! | |
226 | m_isEnabled = true; | |
227 | ||
228 | // we don't even need to show this window itself - and not doing it avoids | |
229 | // that it overwrites the text control | |
230 | wxControl::Show(false); | |
231 | m_isShown = true; | |
232 | return true; | |
233 | } | |
234 | ||
235 | wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase() | |
236 | { | |
237 | // delete the controls now, don't leave them alive even though they would | |
238 | // still be eventually deleted by our parent - but it will be too late, the | |
239 | // user code expects them to be gone now | |
240 | ||
241 | if (m_textCtrl) | |
242 | { | |
243 | // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric | |
244 | wxDynamicCast(m_textCtrl, wxSpinCtrlTextGeneric)->m_spin = NULL; | |
245 | ||
246 | wxSpinCtrlTextGeneric *text = (wxSpinCtrlTextGeneric*)m_textCtrl; | |
247 | m_textCtrl = NULL; | |
248 | delete text; | |
249 | } | |
250 | ||
251 | delete m_spinButton; | |
252 | m_spinButton = NULL; | |
253 | } | |
254 | ||
255 | // ---------------------------------------------------------------------------- | |
256 | // geometry | |
257 | // ---------------------------------------------------------------------------- | |
258 | ||
259 | wxSize wxSpinCtrlGenericBase::DoGetBestSize() const | |
260 | { | |
261 | wxSize sizeBtn = m_spinButton->GetBestSize(), | |
262 | sizeText = m_textCtrl->GetBestSize(); | |
263 | ||
264 | return wxSize(sizeBtn.x + sizeText.x + MARGIN, sizeText.y); | |
265 | } | |
266 | ||
267 | void wxSpinCtrlGenericBase::DoMoveWindow(int x, int y, int width, int height) | |
268 | { | |
269 | wxControl::DoMoveWindow(x, y, width, height); | |
270 | ||
271 | // position the subcontrols inside the client area | |
272 | wxSize sizeBtn = m_spinButton->GetSize(); | |
273 | ||
274 | wxCoord wText = width - sizeBtn.x; | |
275 | m_textCtrl->SetSize(x, y, wText, height); | |
276 | m_spinButton->SetSize(x + wText + MARGIN, y, wxDefaultCoord, height); | |
277 | } | |
278 | ||
279 | // ---------------------------------------------------------------------------- | |
280 | // operations forwarded to the subcontrols | |
281 | // ---------------------------------------------------------------------------- | |
282 | ||
283 | bool wxSpinCtrlGenericBase::Enable(bool enable) | |
284 | { | |
285 | if ( !wxControl::Enable(enable) ) | |
286 | return false; | |
287 | ||
288 | m_spinButton->Enable(enable); | |
289 | m_textCtrl->Enable(enable); | |
290 | ||
291 | return true; | |
292 | } | |
293 | ||
294 | bool wxSpinCtrlGenericBase::Show(bool show) | |
295 | { | |
296 | if ( !wxControl::Show(show) ) | |
297 | return false; | |
298 | ||
299 | // under GTK Show() is called the first time before we are fully | |
300 | // constructed | |
301 | if ( m_spinButton ) | |
302 | { | |
303 | m_spinButton->Show(show); | |
304 | m_textCtrl->Show(show); | |
305 | } | |
306 | ||
307 | return true; | |
308 | } | |
309 | ||
310 | bool wxSpinCtrlGenericBase::Reparent(wxWindowBase *newParent) | |
311 | { | |
312 | if ( m_spinButton ) | |
313 | { | |
314 | m_spinButton->Reparent(newParent); | |
315 | m_textCtrl->Reparent(newParent); | |
316 | } | |
317 | ||
318 | return true; | |
319 | } | |
320 | ||
321 | // ---------------------------------------------------------------------------- | |
322 | // Handle sub controls events | |
323 | // ---------------------------------------------------------------------------- | |
324 | ||
325 | void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent& event) | |
326 | { | |
327 | event.Skip(); | |
328 | ||
329 | // Sync the textctrl since the user expects that the button will modify | |
330 | // what they see in the textctrl. | |
331 | if ( m_textCtrl && m_textCtrl->IsModified() ) | |
332 | SyncSpinToText(); | |
333 | ||
334 | int spin_value = event.GetPosition(); | |
335 | double step = (event.GetEventType() == wxEVT_SCROLL_LINEUP) ? 1 : -1; | |
336 | ||
337 | // Use the spinbutton's acceleration, if any, but not if wrapping around | |
338 | if (((spin_value >= 0) && (m_spin_value >= 0)) || ((spin_value <= 0) && (m_spin_value <= 0))) | |
339 | step *= abs(spin_value - m_spin_value); | |
340 | ||
341 | double value = m_value + step*m_increment; | |
342 | ||
343 | // Check for over/underflow wrapping around if necessary | |
344 | if (value < m_min) | |
345 | value = HasFlag(wxSP_WRAP) ? m_max : m_min; | |
346 | if (value > m_max) | |
347 | value = HasFlag(wxSP_WRAP) ? m_min : m_max; | |
348 | ||
349 | // Ignore the edges when it wraps since the up/down event may be opposite | |
350 | // They are in GTK and Mac | |
351 | if (abs(spin_value - m_spin_value) > SPINCTRLBUT_MAX) | |
352 | { | |
353 | m_spin_value = spin_value; | |
354 | return; | |
355 | } | |
356 | ||
357 | m_spin_value = spin_value; | |
358 | ||
359 | if (InRange(value) && DoSetValue(value)) | |
360 | DoSendEvent(); | |
361 | } | |
362 | ||
363 | void wxSpinCtrlGenericBase::OnTextEnter(wxCommandEvent& event) | |
364 | { | |
365 | SyncSpinToText(); | |
366 | DoSendEvent(); | |
367 | event.Skip(); | |
368 | } | |
369 | ||
370 | void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent& event) | |
371 | { | |
372 | double value = m_value; | |
373 | switch ( event.GetKeyCode() ) | |
374 | { | |
375 | case WXK_UP : | |
376 | value += m_increment; | |
377 | break; | |
378 | ||
379 | case WXK_DOWN : | |
380 | value -= m_increment; | |
381 | break; | |
382 | ||
383 | case WXK_PAGEUP : | |
384 | value += m_increment * 10.0; | |
385 | break; | |
386 | ||
387 | case WXK_PAGEDOWN : | |
388 | value -= m_increment * 10.0; | |
389 | break; | |
390 | ||
391 | default: | |
392 | event.Skip(); | |
393 | return; | |
394 | } | |
395 | ||
396 | if ( m_textCtrl && m_textCtrl->IsModified() ) | |
397 | SyncSpinToText(); | |
398 | ||
399 | if ( DoSetValue(value) ) | |
400 | DoSendEvent(); | |
401 | } | |
402 | ||
403 | // ---------------------------------------------------------------------------- | |
404 | // Textctrl functions | |
405 | // ---------------------------------------------------------------------------- | |
406 | ||
407 | void wxSpinCtrlGenericBase::SyncSpinToText() | |
408 | { | |
409 | if (!m_textCtrl) | |
410 | return; | |
411 | ||
412 | double textValue; | |
413 | if ( m_textCtrl->GetValue().ToDouble(&textValue) ) | |
414 | { | |
415 | if (textValue > m_max) | |
416 | textValue = m_max; | |
417 | else if (textValue < m_min) | |
418 | textValue = m_min; | |
419 | ||
420 | if (m_value != textValue) | |
421 | { | |
422 | DoSetValue(textValue); | |
423 | } | |
424 | } | |
425 | else | |
426 | { | |
427 | // textctrl is out of sync, discard and reset | |
428 | DoSetValue(m_value); | |
429 | } | |
430 | } | |
431 | ||
432 | // ---------------------------------------------------------------------------- | |
433 | // changing value and range | |
434 | // ---------------------------------------------------------------------------- | |
435 | ||
436 | void wxSpinCtrlGenericBase::SetValue(const wxString& text) | |
437 | { | |
438 | wxCHECK_RET( m_textCtrl, _T("invalid call to wxSpinCtrl::SetValue") ); | |
439 | ||
440 | double val; | |
441 | if ( text.ToDouble(&val) && InRange(val) ) | |
442 | { | |
443 | DoSetValue(val); | |
444 | } | |
445 | else // not a number at all or out of range | |
446 | { | |
447 | m_textCtrl->SetValue(text); | |
448 | m_textCtrl->SetSelection(0, -1); | |
449 | } | |
450 | } | |
451 | ||
452 | bool wxSpinCtrlGenericBase::DoSetValue(double val) | |
453 | { | |
454 | wxCHECK_MSG( m_textCtrl, false, _T("invalid call to wxSpinCtrl::SetValue") ); | |
455 | ||
456 | if (!InRange(val)) | |
457 | return false; | |
458 | ||
459 | if ( m_snap_to_ticks && (m_increment != 0) ) | |
460 | { | |
461 | double snap_value = val / m_increment; | |
462 | ||
463 | if (wxFinite(snap_value)) // FIXME what to do about a failure? | |
464 | { | |
465 | if ((snap_value - floor(snap_value)) < (ceil(snap_value) - snap_value)) | |
466 | val = floor(snap_value) * m_increment; | |
467 | else | |
468 | val = ceil(snap_value) * m_increment; | |
469 | } | |
470 | } | |
471 | ||
472 | wxString str(wxString::Format(m_format.c_str(), val)); | |
473 | ||
474 | if ((val != m_value) || (str != m_textCtrl->GetValue())) | |
475 | { | |
476 | m_value = val; | |
477 | str.ToDouble( &m_value ); // wysiwyg for textctrl | |
478 | m_textCtrl->SetValue( str ); | |
479 | m_textCtrl->DiscardEdits(); | |
480 | return true; | |
481 | } | |
482 | ||
483 | return false; | |
484 | } | |
485 | ||
486 | void wxSpinCtrlGenericBase::DoSetRange(double min, double max) | |
487 | { | |
488 | m_min = min; | |
489 | m_max = max; | |
490 | } | |
491 | ||
492 | void wxSpinCtrlGenericBase::DoSetIncrement(double inc) | |
493 | { | |
494 | m_increment = inc; | |
495 | } | |
496 | ||
497 | void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks) | |
498 | { | |
499 | m_snap_to_ticks = snap_to_ticks; | |
500 | DoSetValue(m_value); | |
501 | } | |
502 | ||
503 | void wxSpinCtrlGenericBase::SetSelection(long from, long to) | |
504 | { | |
505 | wxCHECK_RET( m_textCtrl, _T("invalid call to wxSpinCtrl::SetSelection") ); | |
506 | ||
507 | m_textCtrl->SetSelection(from, to); | |
508 | } | |
509 | ||
510 | #ifndef wxHAS_NATIVE_SPINCTRL | |
511 | ||
512 | //----------------------------------------------------------------------------- | |
513 | // wxSpinCtrl | |
514 | //----------------------------------------------------------------------------- | |
515 | ||
516 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxSpinCtrlGenericBase) | |
517 | ||
518 | void wxSpinCtrl::DoSendEvent() | |
519 | { | |
520 | wxSpinEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, GetId()); | |
521 | event.SetEventObject( this ); | |
522 | event.SetPosition((int)(m_value + 0.5)); // FIXME should be SetValue | |
523 | event.SetString(m_textCtrl->GetValue()); | |
524 | GetEventHandler()->ProcessEvent( event ); | |
525 | } | |
526 | ||
527 | #endif // !wxHAS_NATIVE_SPINCTRL | |
528 | ||
529 | //----------------------------------------------------------------------------- | |
530 | // wxSpinCtrlDouble | |
531 | //----------------------------------------------------------------------------- | |
532 | ||
533 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGenericBase) | |
534 | ||
535 | void wxSpinCtrlDouble::DoSendEvent() | |
536 | { | |
537 | wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, GetId()); | |
538 | event.SetEventObject( this ); | |
539 | event.SetValue(m_value); | |
540 | event.SetString(m_textCtrl->GetValue()); | |
541 | GetEventHandler()->ProcessEvent( event ); | |
542 | } | |
543 | ||
544 | void wxSpinCtrlDouble::SetDigits(unsigned digits) | |
545 | { | |
546 | wxCHECK_RET( digits <= 20, "too many digits for wxSpinCtrlDouble" ); | |
547 | ||
548 | m_format.Printf(wxT("%%0.%ulf"), digits); | |
549 | ||
550 | DoSetValue(m_value); | |
551 | } | |
552 | ||
553 | #endif // wxUSE_SPINBTN | |
554 | ||
555 | #endif // !wxPort-with-native-spinctrl | |
556 | ||
557 | #endif // wxUSE_SPINCTRL |