]> git.saurik.com Git - wxWidgets.git/blame - src/generic/spinctlg.cpp
Document that wxDC::FloodFill() is not implemented under wxOSX.
[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
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
36IMPLEMENT_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?).
55static 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 63class wxSpinCtrlTextGeneric : public wxTextCtrl
21709999
JS
64{
65public:
7e4952db 66 wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase *spin, const wxString& value, long style=0)
29cc4cc9 67 : wxTextCtrl(spin, 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
a0e55920 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
113private:
21709999
JS
114 DECLARE_EVENT_TABLE()
115};
116
405f0fef 117BEGIN_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
130END_EVENT_TABLE()
131
132// ----------------------------------------------------------------------------
405f0fef 133// wxSpinCtrlButtonGeneric: spin button used by spin control
21709999
JS
134// ----------------------------------------------------------------------------
135
405f0fef 136class wxSpinCtrlButtonGeneric : public wxSpinButton
21709999
JS
137{
138public:
405f0fef 139 wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase *spin, int style)
29cc4cc9 140 : wxSpinButton(spin, wxID_ANY, wxDefaultPosition,
8cd6a9ad 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 159private:
21709999
JS
160 DECLARE_EVENT_TABLE()
161};
162
405f0fef
VZ
163BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric, wxSpinButton)
164 EVT_SPIN_UP( wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton)
165 EVT_SPIN_DOWN(wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton)
21709999
JS
166END_EVENT_TABLE()
167
21709999 168// ============================================================================
8cd6a9ad 169// wxSpinCtrlGenericBase
21709999
JS
170// ============================================================================
171
172// ----------------------------------------------------------------------------
8cd6a9ad 173// wxSpinCtrlGenericBase creation
21709999
JS
174// ----------------------------------------------------------------------------
175
8cd6a9ad 176void 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
190bool 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);
29cc4cc9 216
cb7ef329
VZ
217#if wxUSE_TOOLTIPS
218 m_textCtrl->SetToolTip(GetToolTipText());
219 m_spinButton->SetToolTip(GetToolTipText());
220#endif // wxUSE_TOOLTIPS
8cd6a9ad
VZ
221
222 m_spin_value = m_spinButton->GetValue();
223
71e03035
VZ
224 // the string value overrides the numeric one (for backwards compatibility
225 // reasons and also because it is simpler to satisfy the string value which
226 // comes much sooner in the list of arguments and leave the initial
227 // parameter unspecified)
228 if ( !value.empty() )
229 {
8cd6a9ad 230 double d;
62f96636 231 if ( DoTextToValue(value, &d) )
8cd6a9ad
VZ
232 {
233 m_value = d;
62f96636 234 m_textCtrl->SetValue(DoValueToText(m_value));
8cd6a9ad 235 }
71e03035
VZ
236 }
237
170acdc9 238 SetInitialSize(size);
9cb6c9a0 239 Move(pos);
ca65c044 240
3fb79396 241 return true;
21709999
JS
242}
243
8cd6a9ad 244wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase()
21709999 245{
e459a794 246 // delete the controls now, don't leave them alive even though they would
21709999
JS
247 // still be eventually deleted by our parent - but it will be too late, the
248 // user code expects them to be gone now
8cd6a9ad
VZ
249
250 if (m_textCtrl)
251 {
405f0fef
VZ
252 // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric
253 wxDynamicCast(m_textCtrl, wxSpinCtrlTextGeneric)->m_spin = NULL;
8cd6a9ad 254
405f0fef 255 wxSpinCtrlTextGeneric *text = (wxSpinCtrlTextGeneric*)m_textCtrl;
8cd6a9ad
VZ
256 m_textCtrl = NULL;
257 delete text;
258 }
259
5276b0a5 260 wxDELETE(m_spinButton);
21709999
JS
261}
262
29604c85
VZ
263wxWindowList wxSpinCtrlGenericBase::GetCompositeWindowParts() const
264{
265 wxWindowList parts;
266 parts.push_back(m_textCtrl);
267 parts.push_back(m_spinButton);
268 return parts;
269}
270
21709999
JS
271// ----------------------------------------------------------------------------
272// geometry
273// ----------------------------------------------------------------------------
274
8cd6a9ad 275wxSize wxSpinCtrlGenericBase::DoGetBestSize() const
21709999 276{
40aa1a7e
VZ
277 return DoGetSizeFromTextSize(m_textCtrl->GetBestSize().x, -1);
278}
279
280wxSize wxSpinCtrlGenericBase::DoGetSizeFromTextSize(int xlen, int ylen) const
281{
282 wxSize sizeBtn = m_spinButton->GetBestSize();
283 wxSize totalS( m_textCtrl->GetBestSize() );
284
285 wxSize tsize(xlen + sizeBtn.x + MARGIN, totalS.y);
286#if defined(__WXMSW__)
287 tsize.IncBy(0.4 * totalS.y + 4, 0);
288#elif defined(__WXGTK__)
289 tsize.IncBy(totalS.y + 10, 0);
290#endif // MSW GTK
291
292 // Check if the user requested a non-standard height.
293 if ( ylen > 0 )
294 tsize.IncBy(0, ylen - GetCharHeight());
21709999 295
40aa1a7e 296 return tsize;
21709999
JS
297}
298
8cd6a9ad 299void wxSpinCtrlGenericBase::DoMoveWindow(int x, int y, int width, int height)
21709999
JS
300{
301 wxControl::DoMoveWindow(x, y, width, height);
302
303 // position the subcontrols inside the client area
8cd6a9ad 304 wxSize sizeBtn = m_spinButton->GetSize();
21709999 305
6f0b6fd1 306 wxCoord wText = width - sizeBtn.x - MARGIN;
29cc4cc9
VZ
307 m_textCtrl->SetSize(0, 0, wText, height);
308 m_spinButton->SetSize(0 + wText + MARGIN, 0, wxDefaultCoord, height);
21709999
JS
309}
310
311// ----------------------------------------------------------------------------
312// operations forwarded to the subcontrols
313// ----------------------------------------------------------------------------
314
fda43a0e
VZ
315void wxSpinCtrlGenericBase::SetFocus()
316{
317 if ( m_textCtrl )
318 m_textCtrl->SetFocus();
319}
320
a2fb9138
VZ
321#ifdef __WXMSW__
322
323void wxSpinCtrlGenericBase::DoEnable(bool enable)
324{
29cc4cc9 325 wxSpinCtrlBase::DoEnable(enable);
a2fb9138
VZ
326}
327
328#endif // __WXMSW__
329
8cd6a9ad 330bool wxSpinCtrlGenericBase::Enable(bool enable)
21709999 331{
a2fb9138 332 if ( !wxSpinCtrlBase::Enable(enable) )
3fb79396 333 return false;
21709999 334
8cd6a9ad
VZ
335 m_spinButton->Enable(enable);
336 m_textCtrl->Enable(enable);
21709999 337
3fb79396 338 return true;
21709999
JS
339}
340
8cd6a9ad 341bool wxSpinCtrlGenericBase::Show(bool show)
21709999
JS
342{
343 if ( !wxControl::Show(show) )
3fb79396 344 return false;
21709999 345
3379ed37
VZ
346 // under GTK Show() is called the first time before we are fully
347 // constructed
8cd6a9ad 348 if ( m_spinButton )
3379ed37 349 {
8cd6a9ad
VZ
350 m_spinButton->Show(show);
351 m_textCtrl->Show(show);
3379ed37 352 }
21709999 353
3fb79396 354 return true;
21709999
JS
355}
356
cb7ef329
VZ
357#if wxUSE_TOOLTIPS
358void wxSpinCtrlGenericBase::DoSetToolTip(wxToolTip *tip)
359{
360 // Notice that we must check for the subcontrols not being NULL (as they
361 // could be if we were created with the default ctor and this is called
362 // before Create() for some reason) and that we can't call SetToolTip(tip)
363 // because this would take ownership of the wxToolTip object (twice).
364 if ( m_textCtrl )
365 {
366 if ( tip )
367 m_textCtrl->SetToolTip(tip->GetTip());
368 else
369 m_textCtrl->SetToolTip(NULL);
370 }
371
372 if ( m_spinButton )
373 {
374 if( tip )
375 m_spinButton->SetToolTip(tip->GetTip());
376 else
377 m_spinButton->SetToolTip(NULL);
378 }
379
380 wxWindowBase::DoSetToolTip(tip);
381}
382#endif // wxUSE_TOOLTIPS
383
29cc4cc9
VZ
384bool wxSpinCtrlGenericBase::SetBackgroundColour(const wxColour& colour)
385{
386 // We need to provide this otherwise the entire composite window
387 // background and therefore the between component spaces
388 // will be changed.
389 if ( m_textCtrl )
390 return m_textCtrl->SetBackgroundColour(colour);
391
392 return true;
393}
394
21709999 395// ----------------------------------------------------------------------------
8cd6a9ad 396// Handle sub controls events
21709999
JS
397// ----------------------------------------------------------------------------
398
c8139480
VS
399BEGIN_EVENT_TABLE(wxSpinCtrlGenericBase, wxSpinCtrlBase)
400 EVT_CHAR(wxSpinCtrlGenericBase::OnTextChar)
401 EVT_KILL_FOCUS(wxSpinCtrlGenericBase::OnTextLostFocus)
402END_EVENT_TABLE()
403
8cd6a9ad 404void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent& event)
21709999 405{
8cd6a9ad
VZ
406 event.Skip();
407
408 // Sync the textctrl since the user expects that the button will modify
409 // what they see in the textctrl.
3a712105 410 SyncSpinToText();
8cd6a9ad
VZ
411
412 int spin_value = event.GetPosition();
413 double step = (event.GetEventType() == wxEVT_SCROLL_LINEUP) ? 1 : -1;
414
415 // Use the spinbutton's acceleration, if any, but not if wrapping around
416 if (((spin_value >= 0) && (m_spin_value >= 0)) || ((spin_value <= 0) && (m_spin_value <= 0)))
417 step *= abs(spin_value - m_spin_value);
21709999 418
70c14728 419 double value = AdjustToFitInRange(m_value + step*m_increment);
8cd6a9ad
VZ
420
421 // Ignore the edges when it wraps since the up/down event may be opposite
422 // They are in GTK and Mac
423 if (abs(spin_value - m_spin_value) > SPINCTRLBUT_MAX)
21709999 424 {
8cd6a9ad
VZ
425 m_spin_value = spin_value;
426 return;
21709999
JS
427 }
428
8cd6a9ad 429 m_spin_value = spin_value;
21709999 430
70c14728 431 if ( DoSetValue(value) )
8cd6a9ad 432 DoSendEvent();
21709999
JS
433}
434
c8139480 435void wxSpinCtrlGenericBase::OnTextLostFocus(wxFocusEvent& event)
21709999 436{
8cd6a9ad
VZ
437 SyncSpinToText();
438 DoSendEvent();
c8139480
VS
439
440 event.Skip();
21709999
JS
441}
442
8cd6a9ad 443void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent& event)
21709999 444{
fd07e185
VZ
445 if ( !HasFlag(wxSP_ARROW_KEYS) )
446 {
447 event.Skip();
448 return;
449 }
450
8cd6a9ad
VZ
451 double value = m_value;
452 switch ( event.GetKeyCode() )
453 {
454 case WXK_UP :
455 value += m_increment;
456 break;
457
458 case WXK_DOWN :
459 value -= m_increment;
460 break;
461
462 case WXK_PAGEUP :
463 value += m_increment * 10.0;
464 break;
465
466 case WXK_PAGEDOWN :
467 value -= m_increment * 10.0;
468 break;
469
470 default:
471 event.Skip();
472 return;
473 }
474
70c14728
VZ
475 value = AdjustToFitInRange(value);
476
3a712105 477 SyncSpinToText();
8cd6a9ad
VZ
478
479 if ( DoSetValue(value) )
480 DoSendEvent();
21709999
JS
481}
482
8cd6a9ad
VZ
483// ----------------------------------------------------------------------------
484// Textctrl functions
485// ----------------------------------------------------------------------------
486
3a712105 487bool wxSpinCtrlGenericBase::SyncSpinToText()
21709999 488{
3a712105
VZ
489 if ( !m_textCtrl || !m_textCtrl->IsModified() )
490 return false;
8cd6a9ad
VZ
491
492 double textValue;
62f96636 493 if ( DoTextToValue(m_textCtrl->GetValue(), &textValue) )
8cd6a9ad
VZ
494 {
495 if (textValue > m_max)
496 textValue = m_max;
497 else if (textValue < m_min)
498 textValue = m_min;
8cd6a9ad 499 }
3a712105 500 else // text contents is not a valid number at all
8cd6a9ad 501 {
3a712105
VZ
502 // replace its contents with the last valid value
503 textValue = m_value;
8cd6a9ad 504 }
3a712105
VZ
505
506 // we must always set the value here, even if it's equal to m_value, as
507 // otherwise we could be left with an out of range value when leaving the
508 // text control and the current value is already m_max for example
509 return DoSetValue(textValue);
21709999
JS
510}
511
512// ----------------------------------------------------------------------------
513// changing value and range
514// ----------------------------------------------------------------------------
515
8cd6a9ad
VZ
516void wxSpinCtrlGenericBase::SetValue(const wxString& text)
517{
9a83f860 518 wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetValue") );
8cd6a9ad
VZ
519
520 double val;
62f96636 521 if ( DoTextToValue(text, &val) && InRange(val) )
8cd6a9ad
VZ
522 {
523 DoSetValue(val);
524 }
525 else // not a number at all or out of range
526 {
527 m_textCtrl->SetValue(text);
6ceeeafd 528 m_textCtrl->SelectAll();
8cd6a9ad
VZ
529 }
530}
531
532bool wxSpinCtrlGenericBase::DoSetValue(double val)
21709999 533{
9a83f860 534 wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") );
21709999 535
f66c0d49
SC
536 if ( val < m_min )
537 val = m_min;
538 if ( val > m_max )
539 val = m_max;
540
8cd6a9ad
VZ
541 if ( m_snap_to_ticks && (m_increment != 0) )
542 {
543 double snap_value = val / m_increment;
21709999 544
8cd6a9ad
VZ
545 if (wxFinite(snap_value)) // FIXME what to do about a failure?
546 {
547 if ((snap_value - floor(snap_value)) < (ceil(snap_value) - snap_value))
548 val = floor(snap_value) * m_increment;
549 else
550 val = ceil(snap_value) * m_increment;
551 }
552 }
553
62f96636 554 wxString str(DoValueToText(val));
8cd6a9ad
VZ
555
556 if ((val != m_value) || (str != m_textCtrl->GetValue()))
557 {
62f96636
VZ
558 if ( !DoTextToValue(str, &m_value ) ) // wysiwyg for textctrl
559 m_value = val;
8cd6a9ad 560 m_textCtrl->SetValue( str );
6ceeeafd 561 m_textCtrl->SelectAll();
8cd6a9ad
VZ
562 m_textCtrl->DiscardEdits();
563 return true;
564 }
565
566 return false;
21709999
JS
567}
568
70c14728
VZ
569double wxSpinCtrlGenericBase::AdjustToFitInRange(double value) const
570{
571 if (value < m_min)
572 value = HasFlag(wxSP_WRAP) ? m_max : m_min;
573 if (value > m_max)
574 value = HasFlag(wxSP_WRAP) ? m_min : m_max;
575
576 return value;
577}
578
8cd6a9ad 579void wxSpinCtrlGenericBase::DoSetRange(double min, double max)
21709999 580{
8cd6a9ad 581 m_min = min;
f66c0d49
SC
582 if ( m_value < m_min )
583 DoSetValue(m_min);
8cd6a9ad 584 m_max = max;
f66c0d49
SC
585 if ( m_value > m_max )
586 DoSetValue(m_max);
8cd6a9ad 587}
21709999 588
8cd6a9ad
VZ
589void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
590{
591 m_increment = inc;
592}
21709999 593
8cd6a9ad
VZ
594void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks)
595{
596 m_snap_to_ticks = snap_to_ticks;
597 DoSetValue(m_value);
21709999
JS
598}
599
8cd6a9ad 600void wxSpinCtrlGenericBase::SetSelection(long from, long to)
21709999 601{
9a83f860 602 wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetSelection") );
21709999 603
8cd6a9ad 604 m_textCtrl->SetSelection(from, to);
21709999
JS
605}
606
8cd6a9ad
VZ
607#ifndef wxHAS_NATIVE_SPINCTRL
608
609//-----------------------------------------------------------------------------
610// wxSpinCtrl
611//-----------------------------------------------------------------------------
612
9e565667
VZ
613bool wxSpinCtrl::SetBase(int base)
614{
615 // Currently we only support base 10 and 16. We could add support for base
616 // 8 quite easily but wxMSW doesn't support it natively so don't bother.
617 if ( base != 10 && base != 16 )
618 return false;
619
620 if ( base == m_base )
621 return true;
622
623 // Update the current control contents to show in the new base: be careful
624 // to call DoTextToValue() before changing the base...
625 double val;
626 const bool hasValidVal = DoTextToValue(m_textCtrl->GetValue(), &val);
627
628 m_base = base;
629
630 // ... but DoValueToText() after doing it.
631 if ( hasValidVal )
632 m_textCtrl->SetValue(DoValueToText(val));
633
634 return true;
635}
636
8cd6a9ad 637void wxSpinCtrl::DoSendEvent()
21709999 638{
ce7fe42e 639 wxSpinEvent event( wxEVT_SPINCTRL, GetId());
8cd6a9ad
VZ
640 event.SetEventObject( this );
641 event.SetPosition((int)(m_value + 0.5)); // FIXME should be SetValue
642 event.SetString(m_textCtrl->GetValue());
643 GetEventHandler()->ProcessEvent( event );
644}
645
62f96636
VZ
646bool wxSpinCtrl::DoTextToValue(const wxString& text, double *val)
647{
648 long lval;
9e565667 649 if ( !text.ToLong(&lval, GetBase()) )
62f96636
VZ
650 return false;
651
652 *val = static_cast<double>(lval);
653
654 return true;
655}
656
657wxString wxSpinCtrl::DoValueToText(double val)
658{
9e565667
VZ
659 switch ( GetBase() )
660 {
661 case 16:
662 return wxPrivate::wxSpinCtrlFormatAsHex(static_cast<long>(val),
663 GetMax());
664
665 default:
666 wxFAIL_MSG( wxS("Unsupported spin control base") );
667 // Fall through
668
669 case 10:
670 return wxString::Format("%ld", static_cast<long>(val));
671 }
62f96636
VZ
672}
673
8cd6a9ad
VZ
674#endif // !wxHAS_NATIVE_SPINCTRL
675
676//-----------------------------------------------------------------------------
677// wxSpinCtrlDouble
678//-----------------------------------------------------------------------------
21709999 679
8cd6a9ad
VZ
680IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGenericBase)
681
682void wxSpinCtrlDouble::DoSendEvent()
683{
ce7fe42e 684 wxSpinDoubleEvent event( wxEVT_SPINCTRLDOUBLE, GetId());
8cd6a9ad
VZ
685 event.SetEventObject( this );
686 event.SetValue(m_value);
687 event.SetString(m_textCtrl->GetValue());
688 GetEventHandler()->ProcessEvent( event );
21709999
JS
689}
690
62f96636
VZ
691bool wxSpinCtrlDouble::DoTextToValue(const wxString& text, double *val)
692{
693 return text.ToDouble(val);
694}
695
696wxString wxSpinCtrlDouble::DoValueToText(double val)
697{
698 return wxString::Format(m_format, val);
699}
700
8cd6a9ad 701void wxSpinCtrlDouble::SetDigits(unsigned digits)
739555e3 702{
8cd6a9ad 703 wxCHECK_RET( digits <= 20, "too many digits for wxSpinCtrlDouble" );
739555e3 704
9bce7b7b
VZ
705 if ( digits == m_digits )
706 return;
707
708 m_digits = digits;
709
8cd6a9ad
VZ
710 m_format.Printf(wxT("%%0.%ulf"), digits);
711
712 DoSetValue(m_value);
739555e3
VZ
713}
714
8cd6a9ad
VZ
715#endif // wxUSE_SPINBTN
716
3379ed37 717#endif // !wxPort-with-native-spinctrl
8cd6a9ad
VZ
718
719#endif // wxUSE_SPINCTRL