]> git.saurik.com Git - wxWidgets.git/blame - src/generic/spinctlg.cpp
Erase the column label background in wxGrid to avoid display artefacts.
[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:
7e4952db
VZ
61 wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase *spin, const wxString& value, long style=0)
62 : wxTextCtrl(spin->GetParent(), wxID_ANY, value, wxDefaultPosition, wxDefaultSize,
63 ( style & wxALIGN_MASK ) | 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 {
3a712105
VZ
96 if ( m_spin->SyncSpinToText() )
97 m_spin->DoSendEvent();
8cd6a9ad
VZ
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,
18e45a88
VZ
189 (style & ~wxBORDER_MASK) | wxBORDER_NONE,
190 wxDefaultValidator, name) )
21709999 191 {
3fb79396 192 return false;
21709999
JS
193 }
194
8cd6a9ad
VZ
195 m_value = initial;
196 m_min = min;
197 m_max = max;
198 m_increment = increment;
199
7e4952db 200 m_textCtrl = new wxSpinCtrlTextGeneric(this, value, style);
405f0fef 201 m_spinButton = new wxSpinCtrlButtonGeneric(this, style);
8cd6a9ad
VZ
202
203 m_spin_value = m_spinButton->GetValue();
204
71e03035
VZ
205 // the string value overrides the numeric one (for backwards compatibility
206 // reasons and also because it is simpler to satisfy the string value which
207 // comes much sooner in the list of arguments and leave the initial
208 // parameter unspecified)
209 if ( !value.empty() )
210 {
8cd6a9ad
VZ
211 double d;
212 if ( value.ToDouble(&d) )
213 {
214 m_value = d;
215 m_textCtrl->SetValue(wxString::Format(m_format, m_value));
216 }
71e03035
VZ
217 }
218
170acdc9 219 SetInitialSize(size);
9cb6c9a0 220 Move(pos);
ca65c044 221
21709999
JS
222 // have to disable this window to avoid interfering it with message
223 // processing to the text and the button... but pretend it is enabled to
3fb79396
WS
224 // make IsEnabled() return true
225 wxControl::Enable(false); // don't use non virtual Disable() here!
226 m_isEnabled = true;
21709999
JS
227
228 // we don't even need to show this window itself - and not doing it avoids
229 // that it overwrites the text control
3fb79396
WS
230 wxControl::Show(false);
231 m_isShown = true;
232 return true;
21709999
JS
233}
234
8cd6a9ad 235wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase()
21709999 236{
e459a794 237 // delete the controls now, don't leave them alive even though they would
21709999
JS
238 // still be eventually deleted by our parent - but it will be too late, the
239 // user code expects them to be gone now
8cd6a9ad
VZ
240
241 if (m_textCtrl)
242 {
405f0fef
VZ
243 // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric
244 wxDynamicCast(m_textCtrl, wxSpinCtrlTextGeneric)->m_spin = NULL;
8cd6a9ad 245
405f0fef 246 wxSpinCtrlTextGeneric *text = (wxSpinCtrlTextGeneric*)m_textCtrl;
8cd6a9ad
VZ
247 m_textCtrl = NULL;
248 delete text;
249 }
250
251 delete m_spinButton;
252 m_spinButton = NULL;
21709999
JS
253}
254
255// ----------------------------------------------------------------------------
256// geometry
257// ----------------------------------------------------------------------------
258
8cd6a9ad 259wxSize wxSpinCtrlGenericBase::DoGetBestSize() const
21709999 260{
8cd6a9ad
VZ
261 wxSize sizeBtn = m_spinButton->GetBestSize(),
262 sizeText = m_textCtrl->GetBestSize();
21709999
JS
263
264 return wxSize(sizeBtn.x + sizeText.x + MARGIN, sizeText.y);
265}
266
8cd6a9ad 267void wxSpinCtrlGenericBase::DoMoveWindow(int x, int y, int width, int height)
21709999
JS
268{
269 wxControl::DoMoveWindow(x, y, width, height);
270
271 // position the subcontrols inside the client area
8cd6a9ad 272 wxSize sizeBtn = m_spinButton->GetSize();
21709999
JS
273
274 wxCoord wText = width - sizeBtn.x;
8cd6a9ad
VZ
275 m_textCtrl->SetSize(x, y, wText, height);
276 m_spinButton->SetSize(x + wText + MARGIN, y, wxDefaultCoord, height);
21709999
JS
277}
278
279// ----------------------------------------------------------------------------
280// operations forwarded to the subcontrols
281// ----------------------------------------------------------------------------
282
8cd6a9ad 283bool wxSpinCtrlGenericBase::Enable(bool enable)
21709999 284{
3f77c76c
VZ
285 // Notice that we never enable this control itself, it must stay disabled
286 // to avoid interfering with the siblings event handling (see e.g. #12045
287 // for the kind of problems which arise otherwise).
288 if ( enable == m_isEnabled )
3fb79396 289 return false;
21709999 290
3f77c76c
VZ
291 m_isEnabled = enable;
292
8cd6a9ad
VZ
293 m_spinButton->Enable(enable);
294 m_textCtrl->Enable(enable);
21709999 295
3fb79396 296 return true;
21709999
JS
297}
298
8cd6a9ad 299bool wxSpinCtrlGenericBase::Show(bool show)
21709999
JS
300{
301 if ( !wxControl::Show(show) )
3fb79396 302 return false;
21709999 303
3379ed37
VZ
304 // under GTK Show() is called the first time before we are fully
305 // constructed
8cd6a9ad 306 if ( m_spinButton )
3379ed37 307 {
8cd6a9ad
VZ
308 m_spinButton->Show(show);
309 m_textCtrl->Show(show);
3379ed37 310 }
21709999 311
3fb79396 312 return true;
21709999
JS
313}
314
7f0cbaaa 315bool wxSpinCtrlGenericBase::Reparent(wxWindowBase *newParent)
03d4194d 316{
8cd6a9ad 317 if ( m_spinButton )
03d4194d 318 {
8cd6a9ad
VZ
319 m_spinButton->Reparent(newParent);
320 m_textCtrl->Reparent(newParent);
03d4194d
VZ
321 }
322
323 return true;
324}
325
21709999 326// ----------------------------------------------------------------------------
8cd6a9ad 327// Handle sub controls events
21709999
JS
328// ----------------------------------------------------------------------------
329
8cd6a9ad 330void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent& event)
21709999 331{
8cd6a9ad
VZ
332 event.Skip();
333
334 // Sync the textctrl since the user expects that the button will modify
335 // what they see in the textctrl.
3a712105 336 SyncSpinToText();
8cd6a9ad
VZ
337
338 int spin_value = event.GetPosition();
339 double step = (event.GetEventType() == wxEVT_SCROLL_LINEUP) ? 1 : -1;
340
341 // Use the spinbutton's acceleration, if any, but not if wrapping around
342 if (((spin_value >= 0) && (m_spin_value >= 0)) || ((spin_value <= 0) && (m_spin_value <= 0)))
343 step *= abs(spin_value - m_spin_value);
21709999 344
70c14728 345 double value = AdjustToFitInRange(m_value + step*m_increment);
8cd6a9ad
VZ
346
347 // Ignore the edges when it wraps since the up/down event may be opposite
348 // They are in GTK and Mac
349 if (abs(spin_value - m_spin_value) > SPINCTRLBUT_MAX)
21709999 350 {
8cd6a9ad
VZ
351 m_spin_value = spin_value;
352 return;
21709999
JS
353 }
354
8cd6a9ad 355 m_spin_value = spin_value;
21709999 356
70c14728 357 if ( DoSetValue(value) )
8cd6a9ad 358 DoSendEvent();
21709999
JS
359}
360
8cd6a9ad 361void wxSpinCtrlGenericBase::OnTextEnter(wxCommandEvent& event)
21709999 362{
8cd6a9ad
VZ
363 SyncSpinToText();
364 DoSendEvent();
365 event.Skip();
21709999
JS
366}
367
8cd6a9ad 368void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent& event)
21709999 369{
fd07e185
VZ
370 if ( !HasFlag(wxSP_ARROW_KEYS) )
371 {
372 event.Skip();
373 return;
374 }
375
8cd6a9ad
VZ
376 double value = m_value;
377 switch ( event.GetKeyCode() )
378 {
379 case WXK_UP :
380 value += m_increment;
381 break;
382
383 case WXK_DOWN :
384 value -= m_increment;
385 break;
386
387 case WXK_PAGEUP :
388 value += m_increment * 10.0;
389 break;
390
391 case WXK_PAGEDOWN :
392 value -= m_increment * 10.0;
393 break;
394
395 default:
396 event.Skip();
397 return;
398 }
399
70c14728
VZ
400 value = AdjustToFitInRange(value);
401
3a712105 402 SyncSpinToText();
8cd6a9ad
VZ
403
404 if ( DoSetValue(value) )
405 DoSendEvent();
21709999
JS
406}
407
8cd6a9ad
VZ
408// ----------------------------------------------------------------------------
409// Textctrl functions
410// ----------------------------------------------------------------------------
411
3a712105 412bool wxSpinCtrlGenericBase::SyncSpinToText()
21709999 413{
3a712105
VZ
414 if ( !m_textCtrl || !m_textCtrl->IsModified() )
415 return false;
8cd6a9ad
VZ
416
417 double textValue;
418 if ( m_textCtrl->GetValue().ToDouble(&textValue) )
419 {
420 if (textValue > m_max)
421 textValue = m_max;
422 else if (textValue < m_min)
423 textValue = m_min;
8cd6a9ad 424 }
3a712105 425 else // text contents is not a valid number at all
8cd6a9ad 426 {
3a712105
VZ
427 // replace its contents with the last valid value
428 textValue = m_value;
8cd6a9ad 429 }
3a712105
VZ
430
431 // we must always set the value here, even if it's equal to m_value, as
432 // otherwise we could be left with an out of range value when leaving the
433 // text control and the current value is already m_max for example
434 return DoSetValue(textValue);
21709999
JS
435}
436
437// ----------------------------------------------------------------------------
438// changing value and range
439// ----------------------------------------------------------------------------
440
8cd6a9ad
VZ
441void wxSpinCtrlGenericBase::SetValue(const wxString& text)
442{
9a83f860 443 wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetValue") );
8cd6a9ad
VZ
444
445 double val;
446 if ( text.ToDouble(&val) && InRange(val) )
447 {
448 DoSetValue(val);
449 }
450 else // not a number at all or out of range
451 {
452 m_textCtrl->SetValue(text);
453 m_textCtrl->SetSelection(0, -1);
b78ffab0 454 m_textCtrl->SetInsertionPointEnd();
8cd6a9ad
VZ
455 }
456}
457
458bool wxSpinCtrlGenericBase::DoSetValue(double val)
21709999 459{
9a83f860 460 wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") );
21709999 461
8cd6a9ad
VZ
462 if (!InRange(val))
463 return false;
21709999 464
8cd6a9ad
VZ
465 if ( m_snap_to_ticks && (m_increment != 0) )
466 {
467 double snap_value = val / m_increment;
21709999 468
8cd6a9ad
VZ
469 if (wxFinite(snap_value)) // FIXME what to do about a failure?
470 {
471 if ((snap_value - floor(snap_value)) < (ceil(snap_value) - snap_value))
472 val = floor(snap_value) * m_increment;
473 else
474 val = ceil(snap_value) * m_increment;
475 }
476 }
477
478 wxString str(wxString::Format(m_format.c_str(), val));
479
480 if ((val != m_value) || (str != m_textCtrl->GetValue()))
481 {
482 m_value = val;
483 str.ToDouble( &m_value ); // wysiwyg for textctrl
484 m_textCtrl->SetValue( str );
b78ffab0 485 m_textCtrl->SetInsertionPointEnd();
8cd6a9ad
VZ
486 m_textCtrl->DiscardEdits();
487 return true;
488 }
489
490 return false;
21709999
JS
491}
492
70c14728
VZ
493double wxSpinCtrlGenericBase::AdjustToFitInRange(double value) const
494{
495 if (value < m_min)
496 value = HasFlag(wxSP_WRAP) ? m_max : m_min;
497 if (value > m_max)
498 value = HasFlag(wxSP_WRAP) ? m_min : m_max;
499
500 return value;
501}
502
8cd6a9ad 503void wxSpinCtrlGenericBase::DoSetRange(double min, double max)
21709999 504{
8cd6a9ad
VZ
505 m_min = min;
506 m_max = max;
507}
21709999 508
8cd6a9ad
VZ
509void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
510{
511 m_increment = inc;
512}
21709999 513
8cd6a9ad
VZ
514void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks)
515{
516 m_snap_to_ticks = snap_to_ticks;
517 DoSetValue(m_value);
21709999
JS
518}
519
8cd6a9ad 520void wxSpinCtrlGenericBase::SetSelection(long from, long to)
21709999 521{
9a83f860 522 wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetSelection") );
21709999 523
8cd6a9ad 524 m_textCtrl->SetSelection(from, to);
21709999
JS
525}
526
8cd6a9ad
VZ
527#ifndef wxHAS_NATIVE_SPINCTRL
528
529//-----------------------------------------------------------------------------
530// wxSpinCtrl
531//-----------------------------------------------------------------------------
532
533IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxSpinCtrlGenericBase)
534
535void wxSpinCtrl::DoSendEvent()
21709999 536{
8cd6a9ad
VZ
537 wxSpinEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
538 event.SetEventObject( this );
539 event.SetPosition((int)(m_value + 0.5)); // FIXME should be SetValue
540 event.SetString(m_textCtrl->GetValue());
541 GetEventHandler()->ProcessEvent( event );
542}
543
544#endif // !wxHAS_NATIVE_SPINCTRL
545
546//-----------------------------------------------------------------------------
547// wxSpinCtrlDouble
548//-----------------------------------------------------------------------------
21709999 549
8cd6a9ad
VZ
550IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGenericBase)
551
552void wxSpinCtrlDouble::DoSendEvent()
553{
554 wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, GetId());
555 event.SetEventObject( this );
556 event.SetValue(m_value);
557 event.SetString(m_textCtrl->GetValue());
558 GetEventHandler()->ProcessEvent( event );
21709999
JS
559}
560
8cd6a9ad 561void wxSpinCtrlDouble::SetDigits(unsigned digits)
739555e3 562{
8cd6a9ad 563 wxCHECK_RET( digits <= 20, "too many digits for wxSpinCtrlDouble" );
739555e3 564
8cd6a9ad
VZ
565 m_format.Printf(wxT("%%0.%ulf"), digits);
566
567 DoSetValue(m_value);
739555e3
VZ
568}
569
8cd6a9ad
VZ
570#endif // wxUSE_SPINBTN
571
3379ed37 572#endif // !wxPort-with-native-spinctrl
8cd6a9ad
VZ
573
574#endif // wxUSE_SPINCTRL