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