]> git.saurik.com Git - wxWidgets.git/blob - src/generic/spinctlg.cpp
Define __VISUALC__ for ICC under Windows again.
[wxWidgets.git] / src / generic / spinctlg.cpp
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 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/textctrl.h"
28 #endif //WX_PRECOMP
29
30 #include "wx/spinctrl.h"
31 #include "wx/tooltip.h"
32
33 #if wxUSE_SPINCTRL
34
35 IMPLEMENT_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
41 #include "wx/spinbutt.h"
42
43 #if wxUSE_SPINBTN
44
45 // ----------------------------------------------------------------------------
46 // constants
47 // ----------------------------------------------------------------------------
48
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?).
54 static const wxCoord MARGIN = 1;
55
56 #define SPINCTRLBUT_MAX 32000 // large to avoid wrap around trouble
57
58 // ----------------------------------------------------------------------------
59 // wxSpinCtrlTextGeneric: text control used by spin control
60 // ----------------------------------------------------------------------------
61
62 class wxSpinCtrlTextGeneric : public wxTextCtrl
63 {
64 public:
65 wxSpinCtrlTextGeneric(wxSpinCtrlGenericBase *spin, const wxString& value, long style=0)
66 : wxTextCtrl(spin, wxID_ANY, value, wxDefaultPosition, wxDefaultSize,
67 // This is tricky: we want to honour any alignment flags
68 // but not wxALIGN_CENTER_VERTICAL because it's the same
69 // as wxTE_PASSWORD and we definitely don't want to show
70 // asterisks in spin control.
71 style & (wxALIGN_MASK | wxTE_PROCESS_ENTER) & ~wxTE_PASSWORD)
72 {
73 m_spin = spin;
74
75 // remove the default minsize, the spinctrl will have one instead
76 SetSizeHints(wxDefaultCoord, wxDefaultCoord);
77 }
78
79 virtual ~wxSpinCtrlTextGeneric()
80 {
81 // MSW sends extra kill focus event on destroy
82 if (m_spin)
83 m_spin->m_textCtrl = NULL;
84
85 m_spin = NULL;
86 }
87
88 void OnChar( wxKeyEvent &event )
89 {
90 if ( !m_spin->ProcessWindowEvent(event) )
91 event.Skip();
92 }
93
94 void OnTextEvent(wxCommandEvent& event)
95 {
96 wxCommandEvent eventCopy(event);
97 eventCopy.SetEventObject(m_spin);
98 eventCopy.SetId(m_spin->GetId());
99 m_spin->ProcessWindowEvent(eventCopy);
100 }
101
102 void OnKillFocus(wxFocusEvent& event)
103 {
104 if (m_spin)
105 m_spin->ProcessWindowEvent(event);
106
107 event.Skip();
108 }
109
110 wxSpinCtrlGenericBase *m_spin;
111
112 private:
113 DECLARE_EVENT_TABLE()
114 };
115
116 BEGIN_EVENT_TABLE(wxSpinCtrlTextGeneric, wxTextCtrl)
117 EVT_CHAR(wxSpinCtrlTextGeneric::OnChar)
118
119 // Forward the text events to wxSpinCtrl itself adjusting them slightly in
120 // the process.
121 EVT_TEXT(wxID_ANY, wxSpinCtrlTextGeneric::OnTextEvent)
122
123 // And we need to forward this one too as wxSpinCtrl is supposed to
124 // generate it if wxTE_PROCESS_ENTER is used with it (and if it isn't,
125 // we're never going to get EVT_TEXT_ENTER in the first place).
126 EVT_TEXT_ENTER(wxID_ANY, wxSpinCtrlTextGeneric::OnTextEvent)
127
128 EVT_KILL_FOCUS(wxSpinCtrlTextGeneric::OnKillFocus)
129 END_EVENT_TABLE()
130
131 // ----------------------------------------------------------------------------
132 // wxSpinCtrlButtonGeneric: spin button used by spin control
133 // ----------------------------------------------------------------------------
134
135 class wxSpinCtrlButtonGeneric : public wxSpinButton
136 {
137 public:
138 wxSpinCtrlButtonGeneric(wxSpinCtrlGenericBase *spin, int style)
139 : wxSpinButton(spin, wxID_ANY, wxDefaultPosition,
140 wxDefaultSize, style | wxSP_VERTICAL)
141 {
142 m_spin = spin;
143
144 SetRange(-SPINCTRLBUT_MAX, SPINCTRLBUT_MAX);
145
146 // remove the default minsize, the spinctrl will have one instead
147 SetSizeHints(wxDefaultCoord, wxDefaultCoord);
148 }
149
150 void OnSpinButton(wxSpinEvent& event)
151 {
152 if (m_spin)
153 m_spin->OnSpinButton(event);
154 }
155
156 wxSpinCtrlGenericBase *m_spin;
157
158 private:
159 DECLARE_EVENT_TABLE()
160 };
161
162 BEGIN_EVENT_TABLE(wxSpinCtrlButtonGeneric, wxSpinButton)
163 EVT_SPIN_UP( wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton)
164 EVT_SPIN_DOWN(wxID_ANY, wxSpinCtrlButtonGeneric::OnSpinButton)
165 END_EVENT_TABLE()
166
167 // ============================================================================
168 // wxSpinCtrlGenericBase
169 // ============================================================================
170
171 // ----------------------------------------------------------------------------
172 // wxSpinCtrlGenericBase creation
173 // ----------------------------------------------------------------------------
174
175 void wxSpinCtrlGenericBase::Init()
176 {
177 m_value = 0;
178 m_min = 0;
179 m_max = 100;
180 m_increment = 1;
181 m_snap_to_ticks = false;
182
183 m_spin_value = 0;
184
185 m_textCtrl = NULL;
186 m_spinButton = NULL;
187 }
188
189 bool wxSpinCtrlGenericBase::Create(wxWindow *parent,
190 wxWindowID id,
191 const wxString& value,
192 const wxPoint& pos, const wxSize& size,
193 long style,
194 double min, double max, double initial,
195 double increment,
196 const wxString& name)
197 {
198 // don't use borders for this control itself, it wouldn't look good with
199 // the text control borders (but we might want to use style border bits to
200 // select the text control style)
201 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize,
202 (style & ~wxBORDER_MASK) | wxBORDER_NONE,
203 wxDefaultValidator, name) )
204 {
205 return false;
206 }
207
208 m_value = initial;
209 m_min = min;
210 m_max = max;
211 m_increment = increment;
212
213 m_textCtrl = new wxSpinCtrlTextGeneric(this, value, style);
214 m_spinButton = new wxSpinCtrlButtonGeneric(this, style);
215
216 #if wxUSE_TOOLTIPS
217 m_textCtrl->SetToolTip(GetToolTipText());
218 m_spinButton->SetToolTip(GetToolTipText());
219 #endif // wxUSE_TOOLTIPS
220
221 m_spin_value = m_spinButton->GetValue();
222
223 // the string value overrides the numeric one (for backwards compatibility
224 // reasons and also because it is simpler to satisfy the string value which
225 // comes much sooner in the list of arguments and leave the initial
226 // parameter unspecified)
227 if ( !value.empty() )
228 {
229 double d;
230 if ( DoTextToValue(value, &d) )
231 {
232 m_value = d;
233 m_textCtrl->ChangeValue(DoValueToText(m_value));
234 }
235 }
236
237 SetInitialSize(size);
238 Move(pos);
239
240 return true;
241 }
242
243 wxSpinCtrlGenericBase::~wxSpinCtrlGenericBase()
244 {
245 // delete the controls now, don't leave them alive even though they would
246 // still be eventually deleted by our parent - but it will be too late, the
247 // user code expects them to be gone now
248
249 if (m_textCtrl)
250 {
251 // null this since MSW sends KILL_FOCUS on deletion, see ~wxSpinCtrlTextGeneric
252 wxDynamicCast(m_textCtrl, wxSpinCtrlTextGeneric)->m_spin = NULL;
253
254 wxSpinCtrlTextGeneric *text = (wxSpinCtrlTextGeneric*)m_textCtrl;
255 m_textCtrl = NULL;
256 delete text;
257 }
258
259 wxDELETE(m_spinButton);
260 }
261
262 wxWindowList wxSpinCtrlGenericBase::GetCompositeWindowParts() const
263 {
264 wxWindowList parts;
265 parts.push_back(m_textCtrl);
266 parts.push_back(m_spinButton);
267 return parts;
268 }
269
270 // ----------------------------------------------------------------------------
271 // geometry
272 // ----------------------------------------------------------------------------
273
274 wxSize wxSpinCtrlGenericBase::DoGetBestSize() const
275 {
276 return DoGetSizeFromTextSize(m_textCtrl->GetBestSize().x, -1);
277 }
278
279 wxSize wxSpinCtrlGenericBase::DoGetSizeFromTextSize(int xlen, int ylen) const
280 {
281 wxSize sizeBtn = m_spinButton->GetBestSize();
282 wxSize totalS( m_textCtrl->GetBestSize() );
283
284 wxSize tsize(xlen + sizeBtn.x + MARGIN, totalS.y);
285 #if defined(__WXMSW__)
286 tsize.IncBy(4*totalS.y/10 + 4, 0);
287 #elif defined(__WXGTK__)
288 tsize.IncBy(totalS.y + 10, 0);
289 #endif // MSW GTK
290
291 // Check if the user requested a non-standard height.
292 if ( ylen > 0 )
293 tsize.IncBy(0, ylen - GetCharHeight());
294
295 return tsize;
296 }
297
298 void wxSpinCtrlGenericBase::DoMoveWindow(int x, int y, int width, int height)
299 {
300 wxControl::DoMoveWindow(x, y, width, height);
301
302 // position the subcontrols inside the client area
303 wxSize sizeBtn = m_spinButton->GetSize();
304
305 wxCoord wText = width - sizeBtn.x - MARGIN;
306 m_textCtrl->SetSize(0, 0, wText, height);
307 m_spinButton->SetSize(0 + wText + MARGIN, 0, wxDefaultCoord, height);
308 }
309
310 // ----------------------------------------------------------------------------
311 // operations forwarded to the subcontrols
312 // ----------------------------------------------------------------------------
313
314 void wxSpinCtrlGenericBase::SetFocus()
315 {
316 if ( m_textCtrl )
317 m_textCtrl->SetFocus();
318 }
319
320 #ifdef __WXMSW__
321
322 void wxSpinCtrlGenericBase::DoEnable(bool enable)
323 {
324 wxSpinCtrlBase::DoEnable(enable);
325 }
326
327 #endif // __WXMSW__
328
329 bool wxSpinCtrlGenericBase::Enable(bool enable)
330 {
331 if ( !wxSpinCtrlBase::Enable(enable) )
332 return false;
333
334 m_spinButton->Enable(enable);
335 m_textCtrl->Enable(enable);
336
337 return true;
338 }
339
340 bool wxSpinCtrlGenericBase::Show(bool show)
341 {
342 if ( !wxControl::Show(show) )
343 return false;
344
345 // under GTK Show() is called the first time before we are fully
346 // constructed
347 if ( m_spinButton )
348 {
349 m_spinButton->Show(show);
350 m_textCtrl->Show(show);
351 }
352
353 return true;
354 }
355
356 #if wxUSE_TOOLTIPS
357 void wxSpinCtrlGenericBase::DoSetToolTip(wxToolTip *tip)
358 {
359 // Notice that we must check for the subcontrols not being NULL (as they
360 // could be if we were created with the default ctor and this is called
361 // before Create() for some reason) and that we can't call SetToolTip(tip)
362 // because this would take ownership of the wxToolTip object (twice).
363 if ( m_textCtrl )
364 {
365 if ( tip )
366 m_textCtrl->SetToolTip(tip->GetTip());
367 else
368 m_textCtrl->SetToolTip(NULL);
369 }
370
371 if ( m_spinButton )
372 {
373 if( tip )
374 m_spinButton->SetToolTip(tip->GetTip());
375 else
376 m_spinButton->SetToolTip(NULL);
377 }
378
379 wxWindowBase::DoSetToolTip(tip);
380 }
381 #endif // wxUSE_TOOLTIPS
382
383 bool wxSpinCtrlGenericBase::SetBackgroundColour(const wxColour& colour)
384 {
385 // We need to provide this otherwise the entire composite window
386 // background and therefore the between component spaces
387 // will be changed.
388 if ( m_textCtrl )
389 return m_textCtrl->SetBackgroundColour(colour);
390
391 return true;
392 }
393
394 // ----------------------------------------------------------------------------
395 // Handle sub controls events
396 // ----------------------------------------------------------------------------
397
398 BEGIN_EVENT_TABLE(wxSpinCtrlGenericBase, wxSpinCtrlBase)
399 EVT_CHAR(wxSpinCtrlGenericBase::OnTextChar)
400 EVT_KILL_FOCUS(wxSpinCtrlGenericBase::OnTextLostFocus)
401 END_EVENT_TABLE()
402
403 void wxSpinCtrlGenericBase::OnSpinButton(wxSpinEvent& event)
404 {
405 event.Skip();
406
407 // Sync the textctrl since the user expects that the button will modify
408 // what they see in the textctrl.
409 SyncSpinToText(SendEvent_None);
410
411 int spin_value = event.GetPosition();
412 double step = (event.GetEventType() == wxEVT_SCROLL_LINEUP) ? 1 : -1;
413
414 // Use the spinbutton's acceleration, if any, but not if wrapping around
415 if (((spin_value >= 0) && (m_spin_value >= 0)) || ((spin_value <= 0) && (m_spin_value <= 0)))
416 step *= abs(spin_value - m_spin_value);
417
418 double value = AdjustToFitInRange(m_value + step*m_increment);
419
420 // Ignore the edges when it wraps since the up/down event may be opposite
421 // They are in GTK and Mac
422 if (abs(spin_value - m_spin_value) > SPINCTRLBUT_MAX)
423 {
424 m_spin_value = spin_value;
425 return;
426 }
427
428 m_spin_value = spin_value;
429
430 // Notify about the change in wxTextCtrl too.
431 if ( DoSetValue(value, SendEvent_Text) )
432 DoSendEvent();
433 }
434
435 void wxSpinCtrlGenericBase::OnTextLostFocus(wxFocusEvent& event)
436 {
437 SyncSpinToText(SendEvent_Text);
438 DoSendEvent();
439
440 event.Skip();
441 }
442
443 void wxSpinCtrlGenericBase::OnTextChar(wxKeyEvent& event)
444 {
445 if ( !HasFlag(wxSP_ARROW_KEYS) )
446 {
447 event.Skip();
448 return;
449 }
450
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
475 value = AdjustToFitInRange(value);
476
477 SyncSpinToText(SendEvent_None);
478
479 // No need to send event, it was already generated by wxTextCtrl itself.
480 if ( DoSetValue(value, SendEvent_None) )
481 DoSendEvent();
482 }
483
484 // ----------------------------------------------------------------------------
485 // Textctrl functions
486 // ----------------------------------------------------------------------------
487
488 bool wxSpinCtrlGenericBase::SyncSpinToText(SendEvent sendEvent)
489 {
490 if ( !m_textCtrl || !m_textCtrl->IsModified() )
491 return false;
492
493 double textValue;
494 if ( DoTextToValue(m_textCtrl->GetValue(), &textValue) )
495 {
496 if (textValue > m_max)
497 textValue = m_max;
498 else if (textValue < m_min)
499 textValue = m_min;
500 }
501 else // text contents is not a valid number at all
502 {
503 // replace its contents with the last valid value
504 textValue = m_value;
505 }
506
507 // we must always set the value here, even if it's equal to m_value, as
508 // otherwise we could be left with an out of range value when leaving the
509 // text control and the current value is already m_max for example
510 return DoSetValue(textValue, sendEvent);
511 }
512
513 // ----------------------------------------------------------------------------
514 // changing value and range
515 // ----------------------------------------------------------------------------
516
517 void wxSpinCtrlGenericBase::SetValue(const wxString& text)
518 {
519 wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetValue") );
520
521 double val;
522 if ( DoTextToValue(text, &val) && InRange(val) )
523 {
524 DoSetValue(val, SendEvent_None);
525 }
526 else // not a number at all or out of range
527 {
528 m_textCtrl->ChangeValue(text);
529 m_textCtrl->SelectAll();
530 }
531 }
532
533 bool wxSpinCtrlGenericBase::DoSetValue(double val, SendEvent sendEvent)
534 {
535 wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") );
536
537 if ( val < m_min )
538 val = m_min;
539 if ( val > m_max )
540 val = m_max;
541
542 if ( m_snap_to_ticks && (m_increment != 0) )
543 {
544 double snap_value = val / m_increment;
545
546 if (wxFinite(snap_value)) // FIXME what to do about a failure?
547 {
548 if ((snap_value - floor(snap_value)) < (ceil(snap_value) - snap_value))
549 val = floor(snap_value) * m_increment;
550 else
551 val = ceil(snap_value) * m_increment;
552 }
553 }
554
555 wxString str(DoValueToText(val));
556
557 if ((val != m_value) || (str != m_textCtrl->GetValue()))
558 {
559 if ( !DoTextToValue(str, &m_value ) ) // wysiwyg for textctrl
560 m_value = val;
561
562 switch ( sendEvent )
563 {
564 case SendEvent_None:
565 m_textCtrl->ChangeValue(str);
566 break;
567
568 case SendEvent_Text:
569 m_textCtrl->SetValue(str);
570 break;
571 }
572
573 m_textCtrl->SelectAll();
574 m_textCtrl->DiscardEdits();
575 return true;
576 }
577
578 return false;
579 }
580
581 double wxSpinCtrlGenericBase::AdjustToFitInRange(double value) const
582 {
583 if (value < m_min)
584 value = HasFlag(wxSP_WRAP) ? m_max : m_min;
585 if (value > m_max)
586 value = HasFlag(wxSP_WRAP) ? m_min : m_max;
587
588 return value;
589 }
590
591 void wxSpinCtrlGenericBase::DoSetRange(double min, double max)
592 {
593 m_min = min;
594 if ( m_value < m_min )
595 DoSetValue(m_min, SendEvent_None);
596 m_max = max;
597 if ( m_value > m_max )
598 DoSetValue(m_max, SendEvent_None);
599 }
600
601 void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
602 {
603 m_increment = inc;
604 }
605
606 void wxSpinCtrlGenericBase::SetSnapToTicks(bool snap_to_ticks)
607 {
608 m_snap_to_ticks = snap_to_ticks;
609 DoSetValue(m_value, SendEvent_None);
610 }
611
612 void wxSpinCtrlGenericBase::SetSelection(long from, long to)
613 {
614 wxCHECK_RET( m_textCtrl, wxT("invalid call to wxSpinCtrl::SetSelection") );
615
616 m_textCtrl->SetSelection(from, to);
617 }
618
619 #ifndef wxHAS_NATIVE_SPINCTRL
620
621 //-----------------------------------------------------------------------------
622 // wxSpinCtrl
623 //-----------------------------------------------------------------------------
624
625 bool wxSpinCtrl::SetBase(int base)
626 {
627 // Currently we only support base 10 and 16. We could add support for base
628 // 8 quite easily but wxMSW doesn't support it natively so don't bother.
629 if ( base != 10 && base != 16 )
630 return false;
631
632 if ( base == m_base )
633 return true;
634
635 // Update the current control contents to show in the new base: be careful
636 // to call DoTextToValue() before changing the base...
637 double val;
638 const bool hasValidVal = DoTextToValue(m_textCtrl->GetValue(), &val);
639
640 m_base = base;
641
642 // ... but DoValueToText() after doing it.
643 if ( hasValidVal )
644 m_textCtrl->ChangeValue(DoValueToText(val));
645
646 return true;
647 }
648
649 void wxSpinCtrl::DoSendEvent()
650 {
651 wxSpinEvent event( wxEVT_SPINCTRL, GetId());
652 event.SetEventObject( this );
653 event.SetPosition((int)(m_value + 0.5)); // FIXME should be SetValue
654 event.SetString(m_textCtrl->GetValue());
655 GetEventHandler()->ProcessEvent( event );
656 }
657
658 bool wxSpinCtrl::DoTextToValue(const wxString& text, double *val)
659 {
660 long lval;
661 if ( !text.ToLong(&lval, GetBase()) )
662 return false;
663
664 *val = static_cast<double>(lval);
665
666 return true;
667 }
668
669 wxString wxSpinCtrl::DoValueToText(double val)
670 {
671 switch ( GetBase() )
672 {
673 case 16:
674 return wxPrivate::wxSpinCtrlFormatAsHex(static_cast<long>(val),
675 GetMax());
676
677 default:
678 wxFAIL_MSG( wxS("Unsupported spin control base") );
679 // Fall through
680
681 case 10:
682 return wxString::Format("%ld", static_cast<long>(val));
683 }
684 }
685
686 #endif // !wxHAS_NATIVE_SPINCTRL
687
688 //-----------------------------------------------------------------------------
689 // wxSpinCtrlDouble
690 //-----------------------------------------------------------------------------
691
692 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGenericBase)
693
694 void wxSpinCtrlDouble::DoSendEvent()
695 {
696 wxSpinDoubleEvent event( wxEVT_SPINCTRLDOUBLE, GetId());
697 event.SetEventObject( this );
698 event.SetValue(m_value);
699 event.SetString(m_textCtrl->GetValue());
700 GetEventHandler()->ProcessEvent( event );
701 }
702
703 bool wxSpinCtrlDouble::DoTextToValue(const wxString& text, double *val)
704 {
705 return text.ToDouble(val);
706 }
707
708 wxString wxSpinCtrlDouble::DoValueToText(double val)
709 {
710 return wxString::Format(m_format, val);
711 }
712
713 void wxSpinCtrlDouble::SetDigits(unsigned digits)
714 {
715 wxCHECK_RET( digits <= 20, "too many digits for wxSpinCtrlDouble" );
716
717 if ( digits == m_digits )
718 return;
719
720 m_digits = digits;
721
722 m_format.Printf(wxT("%%0.%ulf"), digits);
723
724 DoSetValue(m_value, SendEvent_None);
725 }
726
727 #endif // wxUSE_SPINBTN
728
729 #endif // !wxPort-with-native-spinctrl
730
731 #endif // wxUSE_SPINCTRL