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