]>
Commit | Line | Data |
---|---|---|
21709999 JS |
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> | |
526954c5 | 9 | // Licence: wxWindows licence |
21709999 JS |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
21709999 JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
21709999 JS |
27 | #ifndef WX_PRECOMP |
28 | #include "wx/textctrl.h" | |
29 | #endif //WX_PRECOMP | |
30 | ||
8cd6a9ad | 31 | #include "wx/spinctrl.h" |
cb7ef329 | 32 | #include "wx/tooltip.h" |
8cd6a9ad | 33 | |
c67d6888 JS |
34 | #if wxUSE_SPINCTRL |
35 | ||
8cd6a9ad VZ |
36 | IMPLEMENT_DYNAMIC_CLASS(wxSpinDoubleEvent, wxNotifyEvent) |
37 | ||
38 | // There are port-specific versions for the wxSpinCtrl, so exclude the | |
39 | // contents of this file in those cases | |
40 | #if !defined(wxHAS_NATIVE_SPINCTRL) || !defined(wxHAS_NATIVE_SPINCTRLDOUBLE) | |
41 | ||
21709999 | 42 | #include "wx/spinbutt.h" |
8cd6a9ad VZ |
43 | |
44 | #if wxUSE_SPINBTN | |
21709999 JS |
45 | |
46 | // ---------------------------------------------------------------------------- | |
47 | // constants | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
30138611 VZ |
50 | // The margin between the text control and the spin: the value here is the same |
51 | // as the margin between the spin button and its "buddy" text control in wxMSW | |
52 | // so the generic control looks similarly to the native one there, we might | |
53 | // need to use different value for the other platforms (and maybe even | |
54 | // determine it dynamically?). | |
55 | static const wxCoord MARGIN = 1; | |
21709999 | 56 | |
8cd6a9ad VZ |
57 | #define SPINCTRLBUT_MAX 32000 // large to avoid wrap around trouble |
58 | ||
21709999 | 59 | // ---------------------------------------------------------------------------- |
405f0fef | 60 | // wxSpinCtrlTextGeneric: text control used by spin control |
21709999 JS |
61 | // ---------------------------------------------------------------------------- |
62 | ||
405f0fef | 63 | class wxSpinCtrlTextGeneric : public wxTextCtrl |
21709999 JS |
64 | { |
65 | public: | |
7e4952db VZ |
66 | wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase *spin, const wxString& value, long style=0) |
67 | : wxTextCtrl(spin->GetParent(), wxID_ANY, value, wxDefaultPosition, wxDefaultSize, | |
9ecc6bc2 VZ |
68 | // This is tricky: we want to honour any alignment flags |
69 | // but not wxALIGN_CENTER_VERTICAL because it's the same | |
70 | // as wxTE_PASSWORD and we definitely don't want to show | |
71 | // asterisks in spin control. | |
72 | style & (wxALIGN_MASK | wxTE_PROCESS_ENTER) & ~wxTE_PASSWORD) | |
21709999 JS |
73 | { |
74 | m_spin = spin; | |
ca65c044 | 75 | |
e459a794 | 76 | // remove the default minsize, the spinctrl will have one instead |
8cd6a9ad | 77 | SetSizeHints(wxDefaultCoord, wxDefaultCoord); |
21709999 JS |
78 | } |
79 | ||
405f0fef | 80 | virtual ~wxSpinCtrlTextGeneric() |
21709999 | 81 | { |
8cd6a9ad VZ |
82 | // MSW sends extra kill focus event on destroy |
83 | if (m_spin) | |
84 | m_spin->m_textCtrl = NULL; | |
21709999 | 85 | |
8cd6a9ad | 86 | m_spin = NULL; |
21709999 | 87 | } |
71e03035 | 88 | |
8cd6a9ad VZ |
89 | void OnChar( wxKeyEvent &event ) |
90 | { | |
9bd25c86 VZ |
91 | if ( !m_spin->ProcessWindowEvent(event) ) |
92 | event.Skip(); | |
93 | } | |
94 | ||
319faba7 | 95 | void OnTextEvent(wxCommandEvent& event) |
9bd25c86 | 96 | { |
9bd25c86 VZ |
97 | wxCommandEvent eventCopy(event); |
98 | eventCopy.SetEventObject(m_spin); | |
99 | eventCopy.SetId(m_spin->GetId()); | |
100 | m_spin->ProcessWindowEvent(eventCopy); | |
23d416bf | 101 | } |
21709999 | 102 | |
8cd6a9ad VZ |
103 | void OnKillFocus(wxFocusEvent& event) |
104 | { | |
105 | if (m_spin) | |
c8139480 | 106 | m_spin->ProcessWindowEvent(event); |
8cd6a9ad VZ |
107 | |
108 | event.Skip(); | |
109 | } | |
21709999 | 110 | |
8cd6a9ad VZ |
111 | wxSpinCtrlGenericBase *m_spin; |
112 | ||
113 | private: | |
21709999 JS |
114 | DECLARE_EVENT_TABLE() |
115 | }; | |
116 | ||
405f0fef | 117 | BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric, wxTextCtrl) |
405f0fef | 118 | EVT_CHAR(wxSpinCtrlTextGeneric::OnChar) |
319faba7 VZ |
119 | |
120 | // Forward the text events to wxSpinCtrl itself adjusting them slightly in | |
121 | // the process. | |
122 | EVT_TEXT(wxID_ANY, wxSpinCtrlTextGeneric::OnTextEvent) | |
123 | ||
124 | // And we need to forward this one too as wxSpinCtrl is supposed to | |
125 | // generate it if wxTE_PROCESS_ENTER is used with it (and if it isn't, | |
126 | // we're never going to get EVT_TEXT_ENTER in the first place). | |
127 | EVT_TEXT_ENTER(wxID_ANY, wxSpinCtrlTextGeneric::OnTextEvent) | |
8cd6a9ad | 128 | |
405f0fef | 129 | EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus) |
21709999 JS |
130 | END_EVENT_TABLE() |
131 | ||
132 | // ---------------------------------------------------------------------------- | |
405f0fef | 133 | // wxSpinCtrlButtonGeneric: spin button used by spin control |
21709999 JS |
134 | // ---------------------------------------------------------------------------- |
135 | ||
405f0fef | 136 | class wxSpinCtrlButtonGeneric : public wxSpinButton |
21709999 JS |
137 | { |
138 | public: | |
405f0fef | 139 | wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase *spin, int style) |
8cd6a9ad VZ |
140 | : wxSpinButton(spin->GetParent(), wxID_ANY, wxDefaultPosition, |
141 | wxDefaultSize, style | wxSP_VERTICAL) | |
21709999 JS |
142 | { |
143 | m_spin = spin; | |
144 | ||
8cd6a9ad | 145 | SetRange(-SPINCTRLBUT_MAX, SPINCTRLBUT_MAX); |
e459a794 RD |
146 | |
147 | // remove the default minsize, the spinctrl will have one instead | |
8cd6a9ad | 148 | SetSizeHints(wxDefaultCoord, wxDefaultCoord); |
21709999 JS |
149 | } |
150 | ||
8cd6a9ad | 151 | void OnSpinButton(wxSpinEvent& event) |
21709999 | 152 | { |
8cd6a9ad VZ |
153 | if (m_spin) |
154 | m_spin->OnSpinButton(event); | |
21709999 JS |
155 | } |
156 | ||
8cd6a9ad | 157 | wxSpinCtrlGenericBase *m_spin; |
21709999 | 158 | |
8cd6a9ad | 159 | private: |
21709999 JS |
160 | DECLARE_EVENT_TABLE() |
161 | }; | |
162 | ||
405f0fef VZ |
163 | BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric, wxSpinButton) |
164 | EVT_SPIN_UP( wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton) | |
165 | EVT_SPIN_DOWN(wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton) | |
21709999 JS |
166 | END_EVENT_TABLE() |
167 | ||
21709999 | 168 | // ============================================================================ |
8cd6a9ad | 169 | // wxSpinCtrlGenericBase |
21709999 JS |
170 | // ============================================================================ |
171 | ||
172 | // ---------------------------------------------------------------------------- | |
8cd6a9ad | 173 | // wxSpinCtrlGenericBase creation |
21709999 JS |
174 | // ---------------------------------------------------------------------------- |
175 | ||
8cd6a9ad | 176 | void wxSpinCtrlGenericBase::Init() |
21709999 | 177 | { |
8cd6a9ad VZ |
178 | m_value = 0; |
179 | m_min = 0; | |
180 | m_max = 100; | |
181 | m_increment = 1; | |
182 | m_snap_to_ticks = false; | |
8cd6a9ad VZ |
183 | |
184 | m_spin_value = 0; | |
185 | ||
186 | m_textCtrl = NULL; | |
187 | m_spinButton = NULL; | |
21709999 JS |
188 | } |
189 | ||
8cd6a9ad VZ |
190 | bool wxSpinCtrlGenericBase::Create(wxWindow *parent, |
191 | wxWindowID id, | |
192 | const wxString& value, | |
193 | const wxPoint& pos, const wxSize& size, | |
194 | long style, | |
195 | double min, double max, double initial, | |
196 | double increment, | |
197 | const wxString& name) | |
21709999 | 198 | { |
8cd6a9ad VZ |
199 | // don't use borders for this control itself, it wouldn't look good with |
200 | // the text control borders (but we might want to use style border bits to | |
201 | // select the text control style) | |
202 | if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, | |
18e45a88 VZ |
203 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, |
204 | wxDefaultValidator, name) ) | |
21709999 | 205 | { |
3fb79396 | 206 | return false; |
21709999 JS |
207 | } |
208 | ||
8cd6a9ad VZ |
209 | m_value = initial; |
210 | m_min = min; | |
211 | m_max = max; | |
212 | m_increment = increment; | |
213 | ||
7e4952db | 214 | m_textCtrl = new wxSpinCtrlTextGeneric(this, value, style); |
405f0fef | 215 | m_spinButton = new wxSpinCtrlButtonGeneric(this, style); |
cb7ef329 VZ |
216 | #if wxUSE_TOOLTIPS |
217 | m_textCtrl->SetToolTip(GetToolTipText()); | |
218 | m_spinButton->SetToolTip(GetToolTipText()); | |
219 | #endif // wxUSE_TOOLTIPS | |
8cd6a9ad VZ |
220 | |
221 | m_spin_value = m_spinButton->GetValue(); | |
222 | ||
71e03035 VZ |
223 | // the string value overrides the numeric one (for backwards compatibility |
224 | // reasons and also because it is simpler to satisfy the string value which | |
225 | // comes much sooner in the list of arguments and leave the initial | |
226 | // parameter unspecified) | |
227 | if ( !value.empty() ) | |
228 | { | |
8cd6a9ad | 229 | double d; |
62f96636 | 230 | if ( DoTextToValue(value, &d) ) |
8cd6a9ad VZ |
231 | { |
232 | m_value = d; | |
62f96636 | 233 | m_textCtrl->SetValue(DoValueToText(m_value)); |
8cd6a9ad | 234 | } |
71e03035 VZ |
235 | } |
236 | ||
170acdc9 | 237 | SetInitialSize(size); |
9cb6c9a0 | 238 | Move(pos); |
ca65c044 | 239 | |
21709999 JS |
240 | // have to disable this window to avoid interfering it with message |
241 | // processing to the text and the button... but pretend it is enabled to | |
3fb79396 WS |
242 | // make IsEnabled() return true |
243 | wxControl::Enable(false); // don't use non virtual Disable() here! | |
244 | m_isEnabled = true; | |
21709999 JS |
245 | |
246 | // we don't even need to show this window itself - and not doing it avoids | |
247 | // that it overwrites the text control | |
3fb79396 WS |
248 | wxControl::Show(false); |
249 | m_isShown = true; | |
250 | return true; | |
21709999 JS |
251 | } |
252 | ||
8cd6a9ad | 253 | wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase() |
21709999 | 254 | { |
e459a794 | 255 | // delete the controls now, don't leave them alive even though they would |
21709999 JS |
256 | // still be eventually deleted by our parent - but it will be too late, the |
257 | // user code expects them to be gone now | |
8cd6a9ad VZ |
258 | |
259 | if (m_textCtrl) | |
260 | { | |
405f0fef VZ |
261 | // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric |
262 | wxDynamicCast(m_textCtrl, wxSpinCtrlTextGeneric)->m_spin = NULL; | |
8cd6a9ad | 263 | |
405f0fef | 264 | wxSpinCtrlTextGeneric *text = (wxSpinCtrlTextGeneric*)m_textCtrl; |
8cd6a9ad VZ |
265 | m_textCtrl = NULL; |
266 | delete text; | |
267 | } | |
268 | ||
5276b0a5 | 269 | wxDELETE(m_spinButton); |
21709999 JS |
270 | } |
271 | ||
272 | // ---------------------------------------------------------------------------- | |
273 | // geometry | |
274 | // ---------------------------------------------------------------------------- | |
275 | ||
8cd6a9ad | 276 | wxSize wxSpinCtrlGenericBase::DoGetBestSize() const |
21709999 | 277 | { |
8cd6a9ad VZ |
278 | wxSize sizeBtn = m_spinButton->GetBestSize(), |
279 | sizeText = m_textCtrl->GetBestSize(); | |
21709999 JS |
280 | |
281 | return wxSize(sizeBtn.x + sizeText.x + MARGIN, sizeText.y); | |
282 | } | |
283 | ||
8cd6a9ad | 284 | void wxSpinCtrlGenericBase::DoMoveWindow(int x, int y, int width, int height) |
21709999 JS |
285 | { |
286 | wxControl::DoMoveWindow(x, y, width, height); | |
287 | ||
288 | // position the subcontrols inside the client area | |
8cd6a9ad | 289 | wxSize sizeBtn = m_spinButton->GetSize(); |
21709999 | 290 | |
6f0b6fd1 | 291 | wxCoord wText = width - sizeBtn.x - MARGIN; |
8cd6a9ad VZ |
292 | m_textCtrl->SetSize(x, y, wText, height); |
293 | m_spinButton->SetSize(x + wText + MARGIN, y, wxDefaultCoord, height); | |
21709999 JS |
294 | } |
295 | ||
296 | // ---------------------------------------------------------------------------- | |
297 | // operations forwarded to the subcontrols | |
298 | // ---------------------------------------------------------------------------- | |
299 | ||
fda43a0e VZ |
300 | void wxSpinCtrlGenericBase::SetFocus() |
301 | { | |
302 | if ( m_textCtrl ) | |
303 | m_textCtrl->SetFocus(); | |
304 | } | |
305 | ||
a2fb9138 VZ |
306 | #ifdef __WXMSW__ |
307 | ||
308 | void wxSpinCtrlGenericBase::DoEnable(bool enable) | |
309 | { | |
310 | // We never enable this control itself, it must stay disabled to avoid | |
311 | // interfering with the siblings event handling (see e.g. #12045 for the | |
312 | // kind of problems which arise otherwise). | |
313 | if ( !enable ) | |
314 | wxSpinCtrlBase::DoEnable(enable); | |
315 | } | |
316 | ||
317 | #endif // __WXMSW__ | |
318 | ||
8cd6a9ad | 319 | bool wxSpinCtrlGenericBase::Enable(bool enable) |
21709999 | 320 | { |
a2fb9138 | 321 | if ( !wxSpinCtrlBase::Enable(enable) ) |
3fb79396 | 322 | return false; |
21709999 | 323 | |
8cd6a9ad VZ |
324 | m_spinButton->Enable(enable); |
325 | m_textCtrl->Enable(enable); | |
21709999 | 326 | |
3fb79396 | 327 | return true; |
21709999 JS |
328 | } |
329 | ||
8cd6a9ad | 330 | bool wxSpinCtrlGenericBase::Show(bool show) |
21709999 JS |
331 | { |
332 | if ( !wxControl::Show(show) ) | |
3fb79396 | 333 | return false; |
21709999 | 334 | |
3379ed37 VZ |
335 | // under GTK Show() is called the first time before we are fully |
336 | // constructed | |
8cd6a9ad | 337 | if ( m_spinButton ) |
3379ed37 | 338 | { |
8cd6a9ad VZ |
339 | m_spinButton->Show(show); |
340 | m_textCtrl->Show(show); | |
3379ed37 | 341 | } |
21709999 | 342 | |
3fb79396 | 343 | return true; |
21709999 JS |
344 | } |
345 | ||
cb7ef329 VZ |
346 | #if wxUSE_TOOLTIPS |
347 | void wxSpinCtrlGenericBase::DoSetToolTip(wxToolTip *tip) | |
348 | { | |
349 | // Notice that we must check for the subcontrols not being NULL (as they | |
350 | // could be if we were created with the default ctor and this is called | |
351 | // before Create() for some reason) and that we can't call SetToolTip(tip) | |
352 | // because this would take ownership of the wxToolTip object (twice). | |
353 | if ( m_textCtrl ) | |
354 | { | |
355 | if ( tip ) | |
356 | m_textCtrl->SetToolTip(tip->GetTip()); | |
357 | else | |
358 | m_textCtrl->SetToolTip(NULL); | |
359 | } | |
360 | ||
361 | if ( m_spinButton ) | |
362 | { | |
363 | if( tip ) | |
364 | m_spinButton->SetToolTip(tip->GetTip()); | |
365 | else | |
366 | m_spinButton->SetToolTip(NULL); | |
367 | } | |
368 | ||
369 | wxWindowBase::DoSetToolTip(tip); | |
370 | } | |
371 | #endif // wxUSE_TOOLTIPS | |
372 | ||
21709999 | 373 | // ---------------------------------------------------------------------------- |
8cd6a9ad | 374 | // Handle sub controls events |
21709999 JS |
375 | // ---------------------------------------------------------------------------- |
376 | ||
c8139480 VS |
377 | BEGIN_EVENT_TABLE(wxSpinCtrlGenericBase, wxSpinCtrlBase) |
378 | EVT_CHAR(wxSpinCtrlGenericBase::OnTextChar) | |
379 | EVT_KILL_FOCUS(wxSpinCtrlGenericBase::OnTextLostFocus) | |
380 | END_EVENT_TABLE() | |
381 | ||
8cd6a9ad | 382 | void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent& event) |
21709999 | 383 | { |
8cd6a9ad VZ |
384 | event.Skip(); |
385 | ||
386 | // Sync the textctrl since the user expects that the button will modify | |
387 | // what they see in the textctrl. | |
3a712105 | 388 | SyncSpinToText(); |
8cd6a9ad VZ |
389 | |
390 | int spin_value = event.GetPosition(); | |
391 | double step = (event.GetEventType() == wxEVT_SCROLL_LINEUP) ? 1 : -1; | |
392 | ||
393 | // Use the spinbutton's acceleration, if any, but not if wrapping around | |
394 | if (((spin_value >= 0) && (m_spin_value >= 0)) || ((spin_value <= 0) && (m_spin_value <= 0))) | |
395 | step *= abs(spin_value - m_spin_value); | |
21709999 | 396 | |
70c14728 | 397 | double value = AdjustToFitInRange(m_value + step*m_increment); |
8cd6a9ad VZ |
398 | |
399 | // Ignore the edges when it wraps since the up/down event may be opposite | |
400 | // They are in GTK and Mac | |
401 | if (abs(spin_value - m_spin_value) > SPINCTRLBUT_MAX) | |
21709999 | 402 | { |
8cd6a9ad VZ |
403 | m_spin_value = spin_value; |
404 | return; | |
21709999 JS |
405 | } |
406 | ||
8cd6a9ad | 407 | m_spin_value = spin_value; |
21709999 | 408 | |
70c14728 | 409 | if ( DoSetValue(value) ) |
8cd6a9ad | 410 | DoSendEvent(); |
21709999 JS |
411 | } |
412 | ||
c8139480 | 413 | void wxSpinCtrlGenericBase::OnTextLostFocus(wxFocusEvent& event) |
21709999 | 414 | { |
8cd6a9ad VZ |
415 | SyncSpinToText(); |
416 | DoSendEvent(); | |
c8139480 VS |
417 | |
418 | event.Skip(); | |
21709999 JS |
419 | } |
420 | ||
8cd6a9ad | 421 | void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent& event) |
21709999 | 422 | { |
fd07e185 VZ |
423 | if ( !HasFlag(wxSP_ARROW_KEYS) ) |
424 | { | |
425 | event.Skip(); | |
426 | return; | |
427 | } | |
428 | ||
8cd6a9ad VZ |
429 | double value = m_value; |
430 | switch ( event.GetKeyCode() ) | |
431 | { | |
432 | case WXK_UP : | |
433 | value += m_increment; | |
434 | break; | |
435 | ||
436 | case WXK_DOWN : | |
437 | value -= m_increment; | |
438 | break; | |
439 | ||
440 | case WXK_PAGEUP : | |
441 | value += m_increment * 10.0; | |
442 | break; | |
443 | ||
444 | case WXK_PAGEDOWN : | |
445 | value -= m_increment * 10.0; | |
446 | break; | |
447 | ||
448 | default: | |
449 | event.Skip(); | |
450 | return; | |
451 | } | |
452 | ||
70c14728 VZ |
453 | value = AdjustToFitInRange(value); |
454 | ||
3a712105 | 455 | SyncSpinToText(); |
8cd6a9ad VZ |
456 | |
457 | if ( DoSetValue(value) ) | |
458 | DoSendEvent(); | |
21709999 JS |
459 | } |
460 | ||
8cd6a9ad VZ |
461 | // ---------------------------------------------------------------------------- |
462 | // Textctrl functions | |
463 | // ---------------------------------------------------------------------------- | |
464 | ||
3a712105 | 465 | bool wxSpinCtrlGenericBase::SyncSpinToText() |
21709999 | 466 | { |
3a712105 VZ |
467 | if ( !m_textCtrl || !m_textCtrl->IsModified() ) |
468 | return false; | |
8cd6a9ad VZ |
469 | |
470 | double textValue; | |
62f96636 | 471 | if ( DoTextToValue(m_textCtrl->GetValue(), &textValue) ) |
8cd6a9ad VZ |
472 | { |
473 | if (textValue > m_max) | |
474 | textValue = m_max; | |
475 | else if (textValue < m_min) | |
476 | textValue = m_min; | |
8cd6a9ad | 477 | } |
3a712105 | 478 | else // text contents is not a valid number at all |
8cd6a9ad | 479 | { |
3a712105 VZ |
480 | // replace its contents with the last valid value |
481 | textValue = m_value; | |
8cd6a9ad | 482 | } |
3a712105 VZ |
483 | |
484 | // we must always set the value here, even if it's equal to m_value, as | |
485 | // otherwise we could be left with an out of range value when leaving the | |
486 | // text control and the current value is already m_max for example | |
487 | return DoSetValue(textValue); | |
21709999 JS |
488 | } |
489 | ||
490 | // ---------------------------------------------------------------------------- | |
491 | // changing value and range | |
492 | // ---------------------------------------------------------------------------- | |
493 | ||
8cd6a9ad VZ |
494 | void wxSpinCtrlGenericBase::SetValue(const wxString& text) |
495 | { | |
9a83f860 | 496 | wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetValue") ); |
8cd6a9ad VZ |
497 | |
498 | double val; | |
62f96636 | 499 | if ( DoTextToValue(text, &val) && InRange(val) ) |
8cd6a9ad VZ |
500 | { |
501 | DoSetValue(val); | |
502 | } | |
503 | else // not a number at all or out of range | |
504 | { | |
505 | m_textCtrl->SetValue(text); | |
506 | m_textCtrl->SetSelection(0, -1); | |
b78ffab0 | 507 | m_textCtrl->SetInsertionPointEnd(); |
8cd6a9ad VZ |
508 | } |
509 | } | |
510 | ||
511 | bool wxSpinCtrlGenericBase::DoSetValue(double val) | |
21709999 | 512 | { |
9a83f860 | 513 | wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") ); |
21709999 | 514 | |
8cd6a9ad VZ |
515 | if (!InRange(val)) |
516 | return false; | |
21709999 | 517 | |
8cd6a9ad VZ |
518 | if ( m_snap_to_ticks && (m_increment != 0) ) |
519 | { | |
520 | double snap_value = val / m_increment; | |
21709999 | 521 | |
8cd6a9ad VZ |
522 | if (wxFinite(snap_value)) // FIXME what to do about a failure? |
523 | { | |
524 | if ((snap_value - floor(snap_value)) < (ceil(snap_value) - snap_value)) | |
525 | val = floor(snap_value) * m_increment; | |
526 | else | |
527 | val = ceil(snap_value) * m_increment; | |
528 | } | |
529 | } | |
530 | ||
62f96636 | 531 | wxString str(DoValueToText(val)); |
8cd6a9ad VZ |
532 | |
533 | if ((val != m_value) || (str != m_textCtrl->GetValue())) | |
534 | { | |
62f96636 VZ |
535 | if ( !DoTextToValue(str, &m_value ) ) // wysiwyg for textctrl |
536 | m_value = val; | |
8cd6a9ad | 537 | m_textCtrl->SetValue( str ); |
b78ffab0 | 538 | m_textCtrl->SetInsertionPointEnd(); |
8cd6a9ad VZ |
539 | m_textCtrl->DiscardEdits(); |
540 | return true; | |
541 | } | |
542 | ||
543 | return false; | |
21709999 JS |
544 | } |
545 | ||
70c14728 VZ |
546 | double wxSpinCtrlGenericBase::AdjustToFitInRange(double value) const |
547 | { | |
548 | if (value < m_min) | |
549 | value = HasFlag(wxSP_WRAP) ? m_max : m_min; | |
550 | if (value > m_max) | |
551 | value = HasFlag(wxSP_WRAP) ? m_min : m_max; | |
552 | ||
553 | return value; | |
554 | } | |
555 | ||
8cd6a9ad | 556 | void wxSpinCtrlGenericBase::DoSetRange(double min, double max) |
21709999 | 557 | { |
8cd6a9ad VZ |
558 | m_min = min; |
559 | m_max = max; | |
560 | } | |
21709999 | 561 | |
8cd6a9ad VZ |
562 | void wxSpinCtrlGenericBase::DoSetIncrement(double inc) |
563 | { | |
564 | m_increment = inc; | |
565 | } | |
21709999 | 566 | |
8cd6a9ad VZ |
567 | void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks) |
568 | { | |
569 | m_snap_to_ticks = snap_to_ticks; | |
570 | DoSetValue(m_value); | |
21709999 JS |
571 | } |
572 | ||
8cd6a9ad | 573 | void wxSpinCtrlGenericBase::SetSelection(long from, long to) |
21709999 | 574 | { |
9a83f860 | 575 | wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetSelection") ); |
21709999 | 576 | |
8cd6a9ad | 577 | m_textCtrl->SetSelection(from, to); |
21709999 JS |
578 | } |
579 | ||
8cd6a9ad VZ |
580 | #ifndef wxHAS_NATIVE_SPINCTRL |
581 | ||
582 | //----------------------------------------------------------------------------- | |
583 | // wxSpinCtrl | |
584 | //----------------------------------------------------------------------------- | |
585 | ||
9e565667 VZ |
586 | bool wxSpinCtrl::SetBase(int base) |
587 | { | |
588 | // Currently we only support base 10 and 16. We could add support for base | |
589 | // 8 quite easily but wxMSW doesn't support it natively so don't bother. | |
590 | if ( base != 10 && base != 16 ) | |
591 | return false; | |
592 | ||
593 | if ( base == m_base ) | |
594 | return true; | |
595 | ||
596 | // Update the current control contents to show in the new base: be careful | |
597 | // to call DoTextToValue() before changing the base... | |
598 | double val; | |
599 | const bool hasValidVal = DoTextToValue(m_textCtrl->GetValue(), &val); | |
600 | ||
601 | m_base = base; | |
602 | ||
603 | // ... but DoValueToText() after doing it. | |
604 | if ( hasValidVal ) | |
605 | m_textCtrl->SetValue(DoValueToText(val)); | |
606 | ||
607 | return true; | |
608 | } | |
609 | ||
8cd6a9ad | 610 | void wxSpinCtrl::DoSendEvent() |
21709999 | 611 | { |
8cd6a9ad VZ |
612 | wxSpinEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, GetId()); |
613 | event.SetEventObject( this ); | |
614 | event.SetPosition((int)(m_value + 0.5)); // FIXME should be SetValue | |
615 | event.SetString(m_textCtrl->GetValue()); | |
616 | GetEventHandler()->ProcessEvent( event ); | |
617 | } | |
618 | ||
62f96636 VZ |
619 | bool wxSpinCtrl::DoTextToValue(const wxString& text, double *val) |
620 | { | |
621 | long lval; | |
9e565667 | 622 | if ( !text.ToLong(&lval, GetBase()) ) |
62f96636 VZ |
623 | return false; |
624 | ||
625 | *val = static_cast<double>(lval); | |
626 | ||
627 | return true; | |
628 | } | |
629 | ||
630 | wxString wxSpinCtrl::DoValueToText(double val) | |
631 | { | |
9e565667 VZ |
632 | switch ( GetBase() ) |
633 | { | |
634 | case 16: | |
635 | return wxPrivate::wxSpinCtrlFormatAsHex(static_cast<long>(val), | |
636 | GetMax()); | |
637 | ||
638 | default: | |
639 | wxFAIL_MSG( wxS("Unsupported spin control base") ); | |
640 | // Fall through | |
641 | ||
642 | case 10: | |
643 | return wxString::Format("%ld", static_cast<long>(val)); | |
644 | } | |
62f96636 VZ |
645 | } |
646 | ||
8cd6a9ad VZ |
647 | #endif // !wxHAS_NATIVE_SPINCTRL |
648 | ||
649 | //----------------------------------------------------------------------------- | |
650 | // wxSpinCtrlDouble | |
651 | //----------------------------------------------------------------------------- | |
21709999 | 652 | |
8cd6a9ad VZ |
653 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGenericBase) |
654 | ||
655 | void wxSpinCtrlDouble::DoSendEvent() | |
656 | { | |
657 | wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, GetId()); | |
658 | event.SetEventObject( this ); | |
659 | event.SetValue(m_value); | |
660 | event.SetString(m_textCtrl->GetValue()); | |
661 | GetEventHandler()->ProcessEvent( event ); | |
21709999 JS |
662 | } |
663 | ||
62f96636 VZ |
664 | bool wxSpinCtrlDouble::DoTextToValue(const wxString& text, double *val) |
665 | { | |
666 | return text.ToDouble(val); | |
667 | } | |
668 | ||
669 | wxString wxSpinCtrlDouble::DoValueToText(double val) | |
670 | { | |
671 | return wxString::Format(m_format, val); | |
672 | } | |
673 | ||
8cd6a9ad | 674 | void wxSpinCtrlDouble::SetDigits(unsigned digits) |
739555e3 | 675 | { |
8cd6a9ad | 676 | wxCHECK_RET( digits <= 20, "too many digits for wxSpinCtrlDouble" ); |
739555e3 | 677 | |
9bce7b7b VZ |
678 | if ( digits == m_digits ) |
679 | return; | |
680 | ||
681 | m_digits = digits; | |
682 | ||
8cd6a9ad VZ |
683 | m_format.Printf(wxT("%%0.%ulf"), digits); |
684 | ||
685 | DoSetValue(m_value); | |
739555e3 VZ |
686 | } |
687 | ||
8cd6a9ad VZ |
688 | #endif // wxUSE_SPINBTN |
689 | ||
3379ed37 | 690 | #endif // !wxPort-with-native-spinctrl |
8cd6a9ad VZ |
691 | |
692 | #endif // wxUSE_SPINCTRL |