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