force showing 0 in the spin control instead of leaving it empty as the native spin...
[wxWidgets.git] / src / msw / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for Win32
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 22.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999-2005 Vadim Zeitlin
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/wx.h"
29 #endif
30
31 #if wxUSE_SPINCTRL
32
33 #include "wx/spinctrl.h"
34 #include "wx/msw/private.h"
35 #include "wx/msw/wrapcctl.h"
36
37 #if wxUSE_TOOLTIPS
38 #include "wx/tooltip.h"
39 #endif // wxUSE_TOOLTIPS
40
41 #include <limits.h> // for INT_MIN
42
43 // ----------------------------------------------------------------------------
44 // macros
45 // ----------------------------------------------------------------------------
46
47 #if wxUSE_EXTENDED_RTTI
48 WX_DEFINE_FLAGS( wxSpinCtrlStyle )
49
50 wxBEGIN_FLAGS( wxSpinCtrlStyle )
51 // new style border flags, we put them first to
52 // use them for streaming out
53 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
54 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
55 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
56 wxFLAGS_MEMBER(wxBORDER_RAISED)
57 wxFLAGS_MEMBER(wxBORDER_STATIC)
58 wxFLAGS_MEMBER(wxBORDER_NONE)
59
60 // old style border flags
61 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
62 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
63 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
64 wxFLAGS_MEMBER(wxRAISED_BORDER)
65 wxFLAGS_MEMBER(wxSTATIC_BORDER)
66 wxFLAGS_MEMBER(wxBORDER)
67
68 // standard window styles
69 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
70 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
71 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
72 wxFLAGS_MEMBER(wxWANTS_CHARS)
73 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
74 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
75 wxFLAGS_MEMBER(wxVSCROLL)
76 wxFLAGS_MEMBER(wxHSCROLL)
77
78 wxFLAGS_MEMBER(wxSP_HORIZONTAL)
79 wxFLAGS_MEMBER(wxSP_VERTICAL)
80 wxFLAGS_MEMBER(wxSP_ARROW_KEYS)
81 wxFLAGS_MEMBER(wxSP_WRAP)
82
83 wxEND_FLAGS( wxSpinCtrlStyle )
84
85 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl, wxControl,"wx/spinbut.h")
86
87 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl)
88 wxEVENT_RANGE_PROPERTY( Spin , wxEVT_SCROLL_TOP , wxEVT_SCROLL_ENDSCROLL , wxSpinEvent )
89 wxEVENT_PROPERTY( Updated , wxEVT_COMMAND_SPINCTRL_UPDATED , wxCommandEvent )
90 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
91 wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent )
92
93 wxPROPERTY( ValueString , wxString , SetValue , GetValue , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
94 wxPROPERTY( Value , int , SetValue, GetValue, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
95 wxPROPERTY( Min , int , SetMin, GetMin, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
96 wxPROPERTY( Max , int , SetMax, GetMax, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
97 wxPROPERTY_FLAGS( WindowStyle , wxSpinCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
98 /*
99 TODO PROPERTIES
100 style wxSP_ARROW_KEYS
101 */
102 wxEND_PROPERTIES_TABLE()
103
104 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl)
105 wxEND_HANDLERS_TABLE()
106
107 wxCONSTRUCTOR_6( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , ValueString , wxPoint , Position , wxSize , Size , long , WindowStyle )
108 #else
109 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
110 #endif
111
112 //pmg EVT_KILL_FOCUS
113 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
114 EVT_CHAR(wxSpinCtrl::OnChar)
115
116 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
117 EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus)
118 EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange)
119 END_EVENT_TABLE()
120
121 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
122
123 // ----------------------------------------------------------------------------
124 // constants
125 // ----------------------------------------------------------------------------
126
127 // the margin between the up-down control and its buddy (can be arbitrary,
128 // choose what you like - or may be decide during run-time depending on the
129 // font size?)
130 static const int MARGIN_BETWEEN = 1;
131
132 // ============================================================================
133 // implementation
134 // ============================================================================
135
136 wxArraySpins wxSpinCtrl::ms_allSpins;
137
138 // ----------------------------------------------------------------------------
139 // wnd proc for the buddy text ctrl
140 // ----------------------------------------------------------------------------
141
142 LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
143 UINT message,
144 WPARAM wParam,
145 LPARAM lParam)
146 {
147 wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData(hwnd);
148
149 // forward some messages (the key and focus ones only so far) to
150 // the spin ctrl
151 switch ( message )
152 {
153 case WM_SETFOCUS:
154 // if the focus comes from the spin control itself, don't set it
155 // back to it -- we don't want to go into an infinite loop
156 if ( (WXHWND)wParam == spin->GetHWND() )
157 break;
158 //else: fall through
159
160 case WM_KILLFOCUS:
161 case WM_CHAR:
162 case WM_DEADCHAR:
163 case WM_KEYUP:
164 case WM_KEYDOWN:
165 spin->MSWWindowProc(message, wParam, lParam);
166
167 // The control may have been deleted at this point, so check.
168 if ( !::IsWindow(hwnd) || wxGetWindowUserData(hwnd) != spin )
169 return 0;
170 break;
171
172 case WM_GETDLGCODE:
173 // we want to get WXK_RETURN in order to generate the event for it
174 return DLGC_WANTCHARS;
175 }
176
177 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
178 hwnd, message, wParam, lParam);
179 }
180
181 /* static */
182 wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
183 {
184 wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData((HWND)hwndBuddy);
185
186 int i = ms_allSpins.Index(spin);
187
188 if ( i == wxNOT_FOUND )
189 return NULL;
190
191 // sanity check
192 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
193 _T("wxSpinCtrl has incorrect buddy HWND!") );
194
195 return spin;
196 }
197
198 // process a WM_COMMAND generated by the buddy text control
199 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
200 {
201 switch (cmd)
202 {
203 case EN_CHANGE:
204 {
205 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
206 event.SetEventObject(this);
207 wxString val = wxGetWindowText(m_hwndBuddy);
208 event.SetString(val);
209 event.SetInt(GetValue());
210 return GetEventHandler()->ProcessEvent(event);
211 }
212 case EN_SETFOCUS:
213 case EN_KILLFOCUS:
214 {
215 wxFocusEvent event(cmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
216 : wxEVT_SET_FOCUS,
217 m_windowId);
218 event.SetEventObject( this );
219 return GetEventHandler()->ProcessEvent(event);
220 }
221 default:
222 break;
223 }
224
225 // not processed
226 return false;
227 }
228
229 void wxSpinCtrl::OnChar(wxKeyEvent& event)
230 {
231 switch ( event.GetKeyCode() )
232 {
233 case WXK_RETURN:
234 {
235 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
236 InitCommandEvent(event);
237 wxString val = wxGetWindowText(m_hwndBuddy);
238 event.SetString(val);
239 event.SetInt(GetValue());
240 if ( GetEventHandler()->ProcessEvent(event) )
241 return;
242 break;
243 }
244
245 case WXK_TAB:
246 // always produce navigation event - even if we process TAB
247 // ourselves the fact that we got here means that the user code
248 // decided to skip processing of this TAB - probably to let it
249 // do its default job.
250 {
251 wxNavigationKeyEvent eventNav;
252 eventNav.SetDirection(!event.ShiftDown());
253 eventNav.SetWindowChange(event.ControlDown());
254 eventNav.SetEventObject(this);
255
256 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
257 return;
258 }
259 break;
260 }
261
262 // no, we didn't process it
263 event.Skip();
264 }
265
266 void wxSpinCtrl::OnKillFocus(wxFocusEvent& event)
267 {
268 // ensure that the value is shown correctly
269 SetValue(GetValue()) ;
270 event.Skip();
271 }
272
273 void wxSpinCtrl::OnSetFocus(wxFocusEvent& event)
274 {
275 // when we get focus, give it to our buddy window as it needs it more than
276 // we do
277 ::SetFocus((HWND)m_hwndBuddy);
278
279 event.Skip();
280 }
281
282 // ----------------------------------------------------------------------------
283 // construction
284 // ----------------------------------------------------------------------------
285
286 bool wxSpinCtrl::Create(wxWindow *parent,
287 wxWindowID id,
288 const wxString& value,
289 const wxPoint& pos,
290 const wxSize& size,
291 long style,
292 int min, int max, int initial,
293 const wxString& name)
294 {
295 // before using DoGetBestSize(), have to set style to let the base class
296 // know whether this is a horizontal or vertical control (we're always
297 // vertical)
298 style |= wxSP_VERTICAL;
299
300 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
301 #ifdef __WXWINCE__
302 style |= wxBORDER_SIMPLE;
303 #else
304 style |= wxBORDER_SUNKEN;
305 #endif
306
307 SetWindowStyle(style);
308
309 WXDWORD exStyle = 0;
310 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
311
312 // calculate the sizes: the size given is the toal size for both controls
313 // and we need to fit them both in the given width (height is the same)
314 wxSize sizeText(size), sizeBtn(size);
315 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
316 if ( sizeText.x <= 0 )
317 {
318 // DEFAULT_ITEM_WIDTH is the default width for the text control
319 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
320 }
321
322 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
323 if ( sizeText.x <= 0 )
324 {
325 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
326 }
327
328 wxPoint posBtn(pos);
329 posBtn.x += sizeText.x + MARGIN_BETWEEN;
330
331 // we must create the text control before the spin button for the purpose
332 // of the dialog navigation: if there is a static text just before the spin
333 // control, activating it by Alt-letter should give focus to the text
334 // control, not the spin and the dialog navigation code will give focus to
335 // the next control (at Windows level), not the one after it
336
337 // create the text window
338
339 m_hwndBuddy = (WXHWND)::CreateWindowEx
340 (
341 exStyle, // sunken border
342 _T("EDIT"), // window class
343 NULL, // no window title
344 msStyle, // style (will be shown later)
345 pos.x, pos.y, // position
346 0, 0, // size (will be set later)
347 GetHwndOf(parent), // parent
348 (HMENU)-1, // control id
349 wxGetInstance(), // app instance
350 NULL // unused client data
351 );
352
353 if ( !m_hwndBuddy )
354 {
355 wxLogLastError(wxT("CreateWindow(buddy text window)"));
356
357 return false;
358 }
359
360
361 // create the spin button
362 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
363 {
364 return false;
365 }
366
367 SetRange(min, max);
368 SetValue(initial);
369
370 // subclass the text ctrl to be able to intercept some events
371 wxSetWindowUserData(GetBuddyHwnd(), this);
372 m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(),
373 wxBuddyTextWndProc);
374
375 // set up fonts and colours (This is nomally done in MSWCreateControl)
376 InheritAttributes();
377 if (!m_hasFont)
378 SetFont(GetDefaultAttributes().font);
379
380 // set the size of the text window - can do it only now, because we
381 // couldn't call DoGetBestSize() before as font wasn't set
382 if ( sizeText.y <= 0 )
383 {
384 int cx, cy;
385 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
386
387 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
388 }
389
390 SetBestSize(size);
391
392 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
393
394 // associate the text window with the spin button
395 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
396
397 if ( !value.empty() )
398 {
399 SetValue(value);
400 }
401
402 // do it after finishing with m_hwndBuddy creation to avoid generating
403 // initial wxEVT_COMMAND_TEXT_UPDATED message
404 ms_allSpins.Add(this);
405
406 return true;
407 }
408
409 wxSpinCtrl::~wxSpinCtrl()
410 {
411 ms_allSpins.Remove(this);
412
413 // This removes spurious memory leak reporting
414 if (ms_allSpins.GetCount() == 0)
415 ms_allSpins.Clear();
416
417 // destroy the buddy window because this pointer which wxBuddyTextWndProc
418 // uses will not soon be valid any more
419 ::DestroyWindow(GetBuddyHwnd());
420 }
421
422 // ----------------------------------------------------------------------------
423 // wxTextCtrl-like methods
424 // ----------------------------------------------------------------------------
425
426 void wxSpinCtrl::SetValue(const wxString& text)
427 {
428 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
429 {
430 wxLogLastError(wxT("SetWindowText(buddy)"));
431 }
432 }
433
434 void wxSpinCtrl::SetValue(int val)
435 {
436 wxSpinButton::SetValue(val);
437
438 // normally setting the value of the spin button is enough as it updates
439 // its buddy control automatically ...
440 if ( wxGetWindowText(m_hwndBuddy).empty() )
441 {
442 // ... but sometimes it doesn't, notably when the value is 0 and the
443 // text control is currently empty, the spin button seems to be happy
444 // to leave it like this, while we really want to always show the
445 // current value in the control, so do it manually
446 ::SetWindowText(GetBuddyHwnd(), wxString::Format(_T("%ld"), val));
447 }
448 }
449
450 int wxSpinCtrl::GetValue() const
451 {
452 wxString val = wxGetWindowText(m_hwndBuddy);
453
454 long n;
455 if ( (wxSscanf(val, wxT("%ld"), &n) != 1) )
456 n = INT_MIN;
457
458 if ( n < m_min )
459 n = m_min;
460 if ( n > m_max )
461 n = m_max;
462
463 return n;
464 }
465
466 void wxSpinCtrl::SetSelection(long from, long to)
467 {
468 // if from and to are both -1, it means (in wxWidgets) that all text should
469 // be selected - translate into Windows convention
470 if ( (from == -1) && (to == -1) )
471 {
472 from = 0;
473 }
474
475 ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to);
476 }
477
478 // ----------------------------------------------------------------------------
479 // forward some methods to subcontrols
480 // ----------------------------------------------------------------------------
481
482 bool wxSpinCtrl::SetFont(const wxFont& font)
483 {
484 if ( !wxWindowBase::SetFont(font) )
485 {
486 // nothing to do
487 return false;
488 }
489
490 WXHANDLE hFont = GetFont().GetResourceHandle();
491 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
492
493 return true;
494 }
495
496 bool wxSpinCtrl::Show(bool show)
497 {
498 if ( !wxControl::Show(show) )
499 {
500 return false;
501 }
502
503 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
504
505 return true;
506 }
507
508 bool wxSpinCtrl::Enable(bool enable)
509 {
510 if ( !wxControl::Enable(enable) )
511 {
512 return false;
513 }
514
515 ::EnableWindow(GetBuddyHwnd(), enable);
516
517 return true;
518 }
519
520 void wxSpinCtrl::SetFocus()
521 {
522 ::SetFocus(GetBuddyHwnd());
523 }
524
525 #if wxUSE_TOOLTIPS
526
527 void wxSpinCtrl::DoSetToolTip(wxToolTip *tip)
528 {
529 wxSpinButton::DoSetToolTip(tip);
530
531 if ( tip )
532 tip->Add(m_hwndBuddy);
533 }
534
535 #endif // wxUSE_TOOLTIPS
536
537 // ----------------------------------------------------------------------------
538 // event processing
539 // ----------------------------------------------------------------------------
540
541 void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin)
542 {
543 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
544 event.SetEventObject(this);
545 event.SetInt(eventSpin.GetPosition());
546
547 (void)GetEventHandler()->ProcessEvent(event);
548
549 if ( eventSpin.GetSkipped() )
550 {
551 event.Skip();
552 }
553 }
554
555 // ----------------------------------------------------------------------------
556 // size calculations
557 // ----------------------------------------------------------------------------
558
559 wxSize wxSpinCtrl::DoGetBestSize() const
560 {
561 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
562 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
563
564 int y;
565 wxGetCharSize(GetHWND(), NULL, &y, GetFont());
566 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
567
568 // JACS: we should always use the height calculated
569 // from above, because otherwise we'll get a spin control
570 // that's too big. So never use the height calculated
571 // from wxSpinButton::DoGetBestSize().
572
573 // if ( sizeBtn.y < y )
574 {
575 // make the text tall enough
576 sizeBtn.y = y;
577 }
578
579 return sizeBtn;
580 }
581
582 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
583 {
584 int widthBtn = wxSpinButton::DoGetBestSize().x;
585 int widthText = width - widthBtn - MARGIN_BETWEEN;
586 if ( widthText <= 0 )
587 {
588 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
589 }
590
591 // 1) The buddy window
592 DoMoveSibling(m_hwndBuddy, x, y, widthText, height);
593
594 // 2) The button window
595 x += widthText + MARGIN_BETWEEN;
596 wxSpinButton::DoMoveWindow(x, y, widthBtn, height);
597 }
598
599 // get total size of the control
600 void wxSpinCtrl::DoGetSize(int *x, int *y) const
601 {
602 RECT spinrect, textrect, ctrlrect;
603 GetWindowRect(GetHwnd(), &spinrect);
604 GetWindowRect(GetBuddyHwnd(), &textrect);
605 UnionRect(&ctrlrect,&textrect, &spinrect);
606
607 if ( x )
608 *x = ctrlrect.right - ctrlrect.left;
609 if ( y )
610 *y = ctrlrect.bottom - ctrlrect.top;
611 }
612
613 void wxSpinCtrl::DoGetPosition(int *x, int *y) const
614 {
615 // hack: pretend that our HWND is the text control just for a moment
616 WXHWND hWnd = GetHWND();
617 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
618
619 wxSpinButton::DoGetPosition(x, y);
620
621 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
622 }
623
624 #endif // wxUSE_SPINCTRL
625