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