]> git.saurik.com Git - wxWidgets.git/blame - src/generic/spinctlg.cpp
remove always-true test of unsigned >= 0
[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
VZ
31#include "wx/spinctrl.h"
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
VZ
65 wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase *spin, const wxString& value, long style=0)
66 : wxTextCtrl(spin->GetParent(), wxID_ANY, value, wxDefaultPosition, wxDefaultSize,
67 ( style & wxALIGN_MASK ) | wxTE_NOHIDESEL | wxTE_PROCESS_ENTER)
21709999
JS
68 {
69 m_spin = spin;
ca65c044 70
e459a794 71 // remove the default minsize, the spinctrl will have one instead
8cd6a9ad 72 SetSizeHints(wxDefaultCoord, wxDefaultCoord);
21709999
JS
73 }
74
405f0fef 75 virtual ~wxSpinCtrlTextGeneric()
21709999 76 {
8cd6a9ad
VZ
77 // MSW sends extra kill focus event on destroy
78 if (m_spin)
79 m_spin->m_textCtrl = NULL;
21709999 80
8cd6a9ad 81 m_spin = NULL;
21709999 82 }
71e03035 83
8cd6a9ad 84 void OnTextEnter(wxCommandEvent& event)
23d416bf 85 {
8cd6a9ad
VZ
86 if (m_spin)
87 m_spin->OnTextEnter(event);
88 }
71e03035 89
8cd6a9ad
VZ
90 void OnChar( wxKeyEvent &event )
91 {
92 if (m_spin)
93 m_spin->OnTextChar(event);
23d416bf 94 }
21709999 95
8cd6a9ad
VZ
96 void OnKillFocus(wxFocusEvent& event)
97 {
98 if (m_spin)
99 {
3a712105
VZ
100 if ( m_spin->SyncSpinToText() )
101 m_spin->DoSendEvent();
8cd6a9ad
VZ
102 }
103
104 event.Skip();
105 }
21709999 106
8cd6a9ad
VZ
107 wxSpinCtrlGenericBase *m_spin;
108
109private:
21709999
JS
110 DECLARE_EVENT_TABLE()
111};
112
405f0fef
VZ
113BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric, wxTextCtrl)
114 EVT_TEXT_ENTER(wxID_ANY, wxSpinCtrlTextGeneric::OnTextEnter)
8cd6a9ad 115
405f0fef 116 EVT_CHAR(wxSpinCtrlTextGeneric::OnChar)
8cd6a9ad 117
405f0fef 118 EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus)
21709999
JS
119END_EVENT_TABLE()
120
121// ----------------------------------------------------------------------------
405f0fef 122// wxSpinCtrlButtonGeneric: spin button used by spin control
21709999
JS
123// ----------------------------------------------------------------------------
124
405f0fef 125class wxSpinCtrlButtonGeneric : public wxSpinButton
21709999
JS
126{
127public:
405f0fef 128 wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase *spin, int style)
8cd6a9ad
VZ
129 : wxSpinButton(spin->GetParent(), wxID_ANY, wxDefaultPosition,
130 wxDefaultSize, style | wxSP_VERTICAL)
21709999
JS
131 {
132 m_spin = spin;
133
8cd6a9ad 134 SetRange(-SPINCTRLBUT_MAX, SPINCTRLBUT_MAX);
e459a794
RD
135
136 // remove the default minsize, the spinctrl will have one instead
8cd6a9ad 137 SetSizeHints(wxDefaultCoord, wxDefaultCoord);
21709999
JS
138 }
139
8cd6a9ad 140 void OnSpinButton(wxSpinEvent& event)
21709999 141 {
8cd6a9ad
VZ
142 if (m_spin)
143 m_spin->OnSpinButton(event);
21709999
JS
144 }
145
8cd6a9ad 146 wxSpinCtrlGenericBase *m_spin;
21709999 147
8cd6a9ad 148private:
21709999
JS
149 DECLARE_EVENT_TABLE()
150};
151
405f0fef
VZ
152BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric, wxSpinButton)
153 EVT_SPIN_UP( wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton)
154 EVT_SPIN_DOWN(wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton)
21709999
JS
155END_EVENT_TABLE()
156
21709999 157// ============================================================================
8cd6a9ad 158// wxSpinCtrlGenericBase
21709999
JS
159// ============================================================================
160
161// ----------------------------------------------------------------------------
8cd6a9ad 162// wxSpinCtrlGenericBase creation
21709999
JS
163// ----------------------------------------------------------------------------
164
8cd6a9ad 165void wxSpinCtrlGenericBase::Init()
21709999 166{
8cd6a9ad
VZ
167 m_value = 0;
168 m_min = 0;
169 m_max = 100;
170 m_increment = 1;
171 m_snap_to_ticks = false;
172 m_format = wxS("%g");
173
174 m_spin_value = 0;
175
176 m_textCtrl = NULL;
177 m_spinButton = NULL;
21709999
JS
178}
179
8cd6a9ad
VZ
180bool wxSpinCtrlGenericBase::Create(wxWindow *parent,
181 wxWindowID id,
182 const wxString& value,
183 const wxPoint& pos, const wxSize& size,
184 long style,
185 double min, double max, double initial,
186 double increment,
187 const wxString& name)
21709999 188{
8cd6a9ad
VZ
189 // don't use borders for this control itself, it wouldn't look good with
190 // the text control borders (but we might want to use style border bits to
191 // select the text control style)
192 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize,
18e45a88
VZ
193 (style & ~wxBORDER_MASK) | wxBORDER_NONE,
194 wxDefaultValidator, name) )
21709999 195 {
3fb79396 196 return false;
21709999
JS
197 }
198
8cd6a9ad
VZ
199 m_value = initial;
200 m_min = min;
201 m_max = max;
202 m_increment = increment;
203
7e4952db 204 m_textCtrl = new wxSpinCtrlTextGeneric(this, value, style);
405f0fef 205 m_spinButton = new wxSpinCtrlButtonGeneric(this, style);
8cd6a9ad
VZ
206
207 m_spin_value = m_spinButton->GetValue();
208
71e03035
VZ
209 // the string value overrides the numeric one (for backwards compatibility
210 // reasons and also because it is simpler to satisfy the string value which
211 // comes much sooner in the list of arguments and leave the initial
212 // parameter unspecified)
213 if ( !value.empty() )
214 {
8cd6a9ad
VZ
215 double d;
216 if ( value.ToDouble(&d) )
217 {
218 m_value = d;
219 m_textCtrl->SetValue(wxString::Format(m_format, m_value));
220 }
71e03035
VZ
221 }
222
170acdc9 223 SetInitialSize(size);
9cb6c9a0 224 Move(pos);
ca65c044 225
21709999
JS
226 // have to disable this window to avoid interfering it with message
227 // processing to the text and the button... but pretend it is enabled to
3fb79396
WS
228 // make IsEnabled() return true
229 wxControl::Enable(false); // don't use non virtual Disable() here!
230 m_isEnabled = true;
21709999
JS
231
232 // we don't even need to show this window itself - and not doing it avoids
233 // that it overwrites the text control
3fb79396
WS
234 wxControl::Show(false);
235 m_isShown = true;
236 return true;
21709999
JS
237}
238
8cd6a9ad 239wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase()
21709999 240{
e459a794 241 // delete the controls now, don't leave them alive even though they would
21709999
JS
242 // still be eventually deleted by our parent - but it will be too late, the
243 // user code expects them to be gone now
8cd6a9ad
VZ
244
245 if (m_textCtrl)
246 {
405f0fef
VZ
247 // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric
248 wxDynamicCast(m_textCtrl, wxSpinCtrlTextGeneric)->m_spin = NULL;
8cd6a9ad 249
405f0fef 250 wxSpinCtrlTextGeneric *text = (wxSpinCtrlTextGeneric*)m_textCtrl;
8cd6a9ad
VZ
251 m_textCtrl = NULL;
252 delete text;
253 }
254
5276b0a5 255 wxDELETE(m_spinButton);
21709999
JS
256}
257
258// ----------------------------------------------------------------------------
259// geometry
260// ----------------------------------------------------------------------------
261
8cd6a9ad 262wxSize wxSpinCtrlGenericBase::DoGetBestSize() const
21709999 263{
8cd6a9ad
VZ
264 wxSize sizeBtn = m_spinButton->GetBestSize(),
265 sizeText = m_textCtrl->GetBestSize();
21709999
JS
266
267 return wxSize(sizeBtn.x + sizeText.x + MARGIN, sizeText.y);
268}
269
8cd6a9ad 270void wxSpinCtrlGenericBase::DoMoveWindow(int x, int y, int width, int height)
21709999
JS
271{
272 wxControl::DoMoveWindow(x, y, width, height);
273
274 // position the subcontrols inside the client area
8cd6a9ad 275 wxSize sizeBtn = m_spinButton->GetSize();
21709999 276
6f0b6fd1 277 wxCoord wText = width - sizeBtn.x - MARGIN;
8cd6a9ad
VZ
278 m_textCtrl->SetSize(x, y, wText, height);
279 m_spinButton->SetSize(x + wText + MARGIN, y, wxDefaultCoord, height);
21709999
JS
280}
281
282// ----------------------------------------------------------------------------
283// operations forwarded to the subcontrols
284// ----------------------------------------------------------------------------
285
fda43a0e
VZ
286void wxSpinCtrlGenericBase::SetFocus()
287{
288 if ( m_textCtrl )
289 m_textCtrl->SetFocus();
290}
291
8cd6a9ad 292bool wxSpinCtrlGenericBase::Enable(bool enable)
21709999 293{
3f77c76c
VZ
294 // Notice that we never enable this control itself, it must stay disabled
295 // to avoid interfering with the siblings event handling (see e.g. #12045
296 // for the kind of problems which arise otherwise).
297 if ( enable == m_isEnabled )
3fb79396 298 return false;
21709999 299
3f77c76c
VZ
300 m_isEnabled = enable;
301
8cd6a9ad
VZ
302 m_spinButton->Enable(enable);
303 m_textCtrl->Enable(enable);
21709999 304
3fb79396 305 return true;
21709999
JS
306}
307
8cd6a9ad 308bool wxSpinCtrlGenericBase::Show(bool show)
21709999
JS
309{
310 if ( !wxControl::Show(show) )
3fb79396 311 return false;
21709999 312
3379ed37
VZ
313 // under GTK Show() is called the first time before we are fully
314 // constructed
8cd6a9ad 315 if ( m_spinButton )
3379ed37 316 {
8cd6a9ad
VZ
317 m_spinButton->Show(show);
318 m_textCtrl->Show(show);
3379ed37 319 }
21709999 320
3fb79396 321 return true;
21709999
JS
322}
323
7f0cbaaa 324bool wxSpinCtrlGenericBase::Reparent(wxWindowBase *newParent)
03d4194d 325{
8cd6a9ad 326 if ( m_spinButton )
03d4194d 327 {
8cd6a9ad
VZ
328 m_spinButton->Reparent(newParent);
329 m_textCtrl->Reparent(newParent);
03d4194d
VZ
330 }
331
332 return true;
333}
334
21709999 335// ----------------------------------------------------------------------------
8cd6a9ad 336// Handle sub controls events
21709999
JS
337// ----------------------------------------------------------------------------
338
8cd6a9ad 339void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent& event)
21709999 340{
8cd6a9ad
VZ
341 event.Skip();
342
343 // Sync the textctrl since the user expects that the button will modify
344 // what they see in the textctrl.
3a712105 345 SyncSpinToText();
8cd6a9ad
VZ
346
347 int spin_value = event.GetPosition();
348 double step = (event.GetEventType() == wxEVT_SCROLL_LINEUP) ? 1 : -1;
349
350 // Use the spinbutton's acceleration, if any, but not if wrapping around
351 if (((spin_value >= 0) && (m_spin_value >= 0)) || ((spin_value <= 0) && (m_spin_value <= 0)))
352 step *= abs(spin_value - m_spin_value);
21709999 353
70c14728 354 double value = AdjustToFitInRange(m_value + step*m_increment);
8cd6a9ad
VZ
355
356 // Ignore the edges when it wraps since the up/down event may be opposite
357 // They are in GTK and Mac
358 if (abs(spin_value - m_spin_value) > SPINCTRLBUT_MAX)
21709999 359 {
8cd6a9ad
VZ
360 m_spin_value = spin_value;
361 return;
21709999
JS
362 }
363
8cd6a9ad 364 m_spin_value = spin_value;
21709999 365
70c14728 366 if ( DoSetValue(value) )
8cd6a9ad 367 DoSendEvent();
21709999
JS
368}
369
8cd6a9ad 370void wxSpinCtrlGenericBase::OnTextEnter(wxCommandEvent& event)
21709999 371{
8cd6a9ad
VZ
372 SyncSpinToText();
373 DoSendEvent();
374 event.Skip();
21709999
JS
375}
376
8cd6a9ad 377void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent& event)
21709999 378{
fd07e185
VZ
379 if ( !HasFlag(wxSP_ARROW_KEYS) )
380 {
381 event.Skip();
382 return;
383 }
384
8cd6a9ad
VZ
385 double value = m_value;
386 switch ( event.GetKeyCode() )
387 {
388 case WXK_UP :
389 value += m_increment;
390 break;
391
392 case WXK_DOWN :
393 value -= m_increment;
394 break;
395
396 case WXK_PAGEUP :
397 value += m_increment * 10.0;
398 break;
399
400 case WXK_PAGEDOWN :
401 value -= m_increment * 10.0;
402 break;
403
404 default:
405 event.Skip();
406 return;
407 }
408
70c14728
VZ
409 value = AdjustToFitInRange(value);
410
3a712105 411 SyncSpinToText();
8cd6a9ad
VZ
412
413 if ( DoSetValue(value) )
414 DoSendEvent();
21709999
JS
415}
416
8cd6a9ad
VZ
417// ----------------------------------------------------------------------------
418// Textctrl functions
419// ----------------------------------------------------------------------------
420
3a712105 421bool wxSpinCtrlGenericBase::SyncSpinToText()
21709999 422{
3a712105
VZ
423 if ( !m_textCtrl || !m_textCtrl->IsModified() )
424 return false;
8cd6a9ad
VZ
425
426 double textValue;
427 if ( m_textCtrl->GetValue().ToDouble(&textValue) )
428 {
429 if (textValue > m_max)
430 textValue = m_max;
431 else if (textValue < m_min)
432 textValue = m_min;
8cd6a9ad 433 }
3a712105 434 else // text contents is not a valid number at all
8cd6a9ad 435 {
3a712105
VZ
436 // replace its contents with the last valid value
437 textValue = m_value;
8cd6a9ad 438 }
3a712105
VZ
439
440 // we must always set the value here, even if it's equal to m_value, as
441 // otherwise we could be left with an out of range value when leaving the
442 // text control and the current value is already m_max for example
443 return DoSetValue(textValue);
21709999
JS
444}
445
446// ----------------------------------------------------------------------------
447// changing value and range
448// ----------------------------------------------------------------------------
449
8cd6a9ad
VZ
450void wxSpinCtrlGenericBase::SetValue(const wxString& text)
451{
9a83f860 452 wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetValue") );
8cd6a9ad
VZ
453
454 double val;
455 if ( text.ToDouble(&val) && InRange(val) )
456 {
457 DoSetValue(val);
458 }
459 else // not a number at all or out of range
460 {
461 m_textCtrl->SetValue(text);
462 m_textCtrl->SetSelection(0, -1);
b78ffab0 463 m_textCtrl->SetInsertionPointEnd();
8cd6a9ad
VZ
464 }
465}
466
467bool wxSpinCtrlGenericBase::DoSetValue(double val)
21709999 468{
9a83f860 469 wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") );
21709999 470
8cd6a9ad
VZ
471 if (!InRange(val))
472 return false;
21709999 473
8cd6a9ad
VZ
474 if ( m_snap_to_ticks && (m_increment != 0) )
475 {
476 double snap_value = val / m_increment;
21709999 477
8cd6a9ad
VZ
478 if (wxFinite(snap_value)) // FIXME what to do about a failure?
479 {
480 if ((snap_value - floor(snap_value)) < (ceil(snap_value) - snap_value))
481 val = floor(snap_value) * m_increment;
482 else
483 val = ceil(snap_value) * m_increment;
484 }
485 }
486
487 wxString str(wxString::Format(m_format.c_str(), val));
488
489 if ((val != m_value) || (str != m_textCtrl->GetValue()))
490 {
491 m_value = val;
492 str.ToDouble( &m_value ); // wysiwyg for textctrl
493 m_textCtrl->SetValue( str );
b78ffab0 494 m_textCtrl->SetInsertionPointEnd();
8cd6a9ad
VZ
495 m_textCtrl->DiscardEdits();
496 return true;
497 }
498
499 return false;
21709999
JS
500}
501
70c14728
VZ
502double wxSpinCtrlGenericBase::AdjustToFitInRange(double value) const
503{
504 if (value < m_min)
505 value = HasFlag(wxSP_WRAP) ? m_max : m_min;
506 if (value > m_max)
507 value = HasFlag(wxSP_WRAP) ? m_min : m_max;
508
509 return value;
510}
511
8cd6a9ad 512void wxSpinCtrlGenericBase::DoSetRange(double min, double max)
21709999 513{
8cd6a9ad
VZ
514 m_min = min;
515 m_max = max;
516}
21709999 517
8cd6a9ad
VZ
518void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
519{
520 m_increment = inc;
521}
21709999 522
8cd6a9ad
VZ
523void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks)
524{
525 m_snap_to_ticks = snap_to_ticks;
526 DoSetValue(m_value);
21709999
JS
527}
528
8cd6a9ad 529void wxSpinCtrlGenericBase::SetSelection(long from, long to)
21709999 530{
9a83f860 531 wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetSelection") );
21709999 532
8cd6a9ad 533 m_textCtrl->SetSelection(from, to);
21709999
JS
534}
535
8cd6a9ad
VZ
536#ifndef wxHAS_NATIVE_SPINCTRL
537
538//-----------------------------------------------------------------------------
539// wxSpinCtrl
540//-----------------------------------------------------------------------------
541
8cd6a9ad 542void wxSpinCtrl::DoSendEvent()
21709999 543{
8cd6a9ad
VZ
544 wxSpinEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
545 event.SetEventObject( this );
546 event.SetPosition((int)(m_value + 0.5)); // FIXME should be SetValue
547 event.SetString(m_textCtrl->GetValue());
548 GetEventHandler()->ProcessEvent( event );
549}
550
551#endif // !wxHAS_NATIVE_SPINCTRL
552
553//-----------------------------------------------------------------------------
554// wxSpinCtrlDouble
555//-----------------------------------------------------------------------------
21709999 556
8cd6a9ad
VZ
557IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGenericBase)
558
559void wxSpinCtrlDouble::DoSendEvent()
560{
561 wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, GetId());
562 event.SetEventObject( this );
563 event.SetValue(m_value);
564 event.SetString(m_textCtrl->GetValue());
565 GetEventHandler()->ProcessEvent( event );
21709999
JS
566}
567
8cd6a9ad 568void wxSpinCtrlDouble::SetDigits(unsigned digits)
739555e3 569{
8cd6a9ad 570 wxCHECK_RET( digits <= 20, "too many digits for wxSpinCtrlDouble" );
739555e3 571
9bce7b7b
VZ
572 if ( digits == m_digits )
573 return;
574
575 m_digits = digits;
576
8cd6a9ad
VZ
577 m_format.Printf(wxT("%%0.%ulf"), digits);
578
579 DoSetValue(m_value);
739555e3
VZ
580}
581
8cd6a9ad
VZ
582#endif // wxUSE_SPINBTN
583
3379ed37 584#endif // !wxPort-with-native-spinctrl
8cd6a9ad
VZ
585
586#endif // wxUSE_SPINCTRL