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