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