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