Fix fatal bug in wxGetWindowFromHWND() for radio buttons.
[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/hashmap.h"
33 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
34 #include "wx/event.h"
35 #include "wx/textctrl.h"
36 #include "wx/wxcrtvararg.h"
37 #endif
38
39 #include "wx/msw/private.h"
40
41 #if wxUSE_TOOLTIPS
42 #include "wx/tooltip.h"
43 #endif // wxUSE_TOOLTIPS
44
45 #include <limits.h> // for INT_MIN
46
47 // ----------------------------------------------------------------------------
48 // macros
49 // ----------------------------------------------------------------------------
50
51 #if wxUSE_EXTENDED_RTTI
52 WX_DEFINE_FLAGS( wxSpinCtrlStyle )
53
54 wxBEGIN_FLAGS( wxSpinCtrlStyle )
55 // new style border flags, we put them first to
56 // use them for streaming out
57 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
58 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
59 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
60 wxFLAGS_MEMBER(wxBORDER_RAISED)
61 wxFLAGS_MEMBER(wxBORDER_STATIC)
62 wxFLAGS_MEMBER(wxBORDER_NONE)
63
64 // old style border flags
65 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
66 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
67 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
68 wxFLAGS_MEMBER(wxRAISED_BORDER)
69 wxFLAGS_MEMBER(wxSTATIC_BORDER)
70 wxFLAGS_MEMBER(wxBORDER)
71
72 // standard window styles
73 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
74 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
75 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
76 wxFLAGS_MEMBER(wxWANTS_CHARS)
77 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
78 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
79 wxFLAGS_MEMBER(wxVSCROLL)
80 wxFLAGS_MEMBER(wxHSCROLL)
81
82 wxFLAGS_MEMBER(wxSP_HORIZONTAL)
83 wxFLAGS_MEMBER(wxSP_VERTICAL)
84 wxFLAGS_MEMBER(wxSP_ARROW_KEYS)
85 wxFLAGS_MEMBER(wxSP_WRAP)
86
87 wxEND_FLAGS( wxSpinCtrlStyle )
88
89 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl, wxControl,"wx/spinbut.h")
90
91 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl)
92 wxEVENT_RANGE_PROPERTY( Spin , wxEVT_SCROLL_TOP , wxEVT_SCROLL_CHANGED , wxSpinEvent )
93 wxEVENT_PROPERTY( Updated , wxEVT_COMMAND_SPINCTRL_UPDATED , wxCommandEvent )
94 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
95 wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent )
96
97 wxPROPERTY( ValueString , wxString , SetValue , GetValue , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
98 wxPROPERTY( Value , int , SetValue, GetValue, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY( Min , int , SetMin, GetMin, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
100 wxPROPERTY( Max , int , SetMax, GetMax, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
101 wxPROPERTY_FLAGS( WindowStyle , wxSpinCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
102 /*
103 TODO PROPERTIES
104 style wxSP_ARROW_KEYS
105 */
106 wxEND_PROPERTIES_TABLE()
107
108 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl)
109 wxEND_HANDLERS_TABLE()
110
111 wxCONSTRUCTOR_6( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , ValueString , wxPoint , Position , wxSize , Size , long , WindowStyle )
112 #else
113 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
114 #endif
115
116 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
117 EVT_CHAR(wxSpinCtrl::OnChar)
118 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
119 EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus)
120 END_EVENT_TABLE()
121
122 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
123
124 // ----------------------------------------------------------------------------
125 // constants
126 // ----------------------------------------------------------------------------
127
128 // the margin between the up-down control and its buddy (can be arbitrary,
129 // choose what you like - or may be decide during run-time depending on the
130 // font size?)
131 static const int MARGIN_BETWEEN = 1;
132
133
134 // ---------------------------------------------------------------------------
135 // global vars
136 // ---------------------------------------------------------------------------
137
138 namespace
139 {
140
141 // Global hash used to find the spin control corresponding to the given buddy
142 // text control HWND.
143 WX_DECLARE_HASH_MAP(HWND, wxSpinCtrl *,
144 wxPointerHash, wxPointerEqual,
145 SpinForTextCtrl);
146
147 SpinForTextCtrl gs_spinForTextCtrl;
148
149 } // anonymous namespace
150
151 // ============================================================================
152 // implementation
153 // ============================================================================
154
155 // ----------------------------------------------------------------------------
156 // wnd proc for the buddy text ctrl
157 // ----------------------------------------------------------------------------
158
159 LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
160 UINT message,
161 WPARAM wParam,
162 LPARAM lParam)
163 {
164 wxSpinCtrl * const spin = wxSpinCtrl::GetSpinForTextCtrl(hwnd);
165
166 // forward some messages (mostly the key and focus ones) to the spin ctrl
167 switch ( message )
168 {
169 case WM_SETFOCUS:
170 // if the focus comes from the spin control itself, don't set it
171 // back to it -- we don't want to go into an infinite loop
172 if ( (WXHWND)wParam == spin->GetHWND() )
173 break;
174 //else: fall through
175
176 case WM_KILLFOCUS:
177 case WM_CHAR:
178 case WM_DEADCHAR:
179 case WM_KEYUP:
180 case WM_KEYDOWN:
181 #ifdef WM_HELP
182 // we need to forward WM_HELP too to ensure that the context help
183 // associated with wxSpinCtrl is shown when the text control part of it
184 // is clicked with the "?" cursor
185 case WM_HELP:
186 #endif
187 spin->MSWWindowProc(message, wParam, lParam);
188
189 // The control may have been deleted at this point, so check.
190 if ( !::IsWindow(hwnd) )
191 return 0;
192 break;
193
194 case WM_GETDLGCODE:
195 if ( spin->HasFlag(wxTE_PROCESS_ENTER) )
196 {
197 long dlgCode = ::CallWindowProc
198 (
199 CASTWNDPROC spin->GetBuddyWndProc(),
200 hwnd,
201 message,
202 wParam,
203 lParam
204 );
205 dlgCode |= DLGC_WANTMESSAGE;
206 return dlgCode;
207 }
208 break;
209 }
210
211 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
212 hwnd, message, wParam, lParam);
213 }
214
215 /* static */
216 wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
217 {
218 const SpinForTextCtrl::const_iterator
219 it = gs_spinForTextCtrl.find(hwndBuddy);
220 if ( it == gs_spinForTextCtrl.end() )
221 return NULL;
222
223 wxSpinCtrl * const spin = it->second;
224
225 // sanity check
226 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
227 wxT("wxSpinCtrl has incorrect buddy HWND!") );
228
229 return spin;
230 }
231
232 // process a WM_COMMAND generated by the buddy text control
233 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
234 {
235 if ( (cmd == EN_CHANGE) && (!m_blockEvent ))
236 {
237 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
238 event.SetEventObject(this);
239 wxString val = wxGetWindowText(m_hwndBuddy);
240 event.SetString(val);
241 event.SetInt(GetValue());
242 return HandleWindowEvent(event);
243 }
244
245 // not processed
246 return false;
247 }
248
249 void wxSpinCtrl::OnChar(wxKeyEvent& event)
250 {
251 switch ( event.GetKeyCode() )
252 {
253 case WXK_RETURN:
254 {
255 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
256 InitCommandEvent(event);
257 wxString val = wxGetWindowText(m_hwndBuddy);
258 event.SetString(val);
259 event.SetInt(GetValue());
260 if ( HandleWindowEvent(event) )
261 return;
262 break;
263 }
264
265 case WXK_TAB:
266 // always produce navigation event - even if we process TAB
267 // ourselves the fact that we got here means that the user code
268 // decided to skip processing of this TAB - probably to let it
269 // do its default job.
270 {
271 wxNavigationKeyEvent eventNav;
272 eventNav.SetDirection(!event.ShiftDown());
273 eventNav.SetWindowChange(event.ControlDown());
274 eventNav.SetEventObject(this);
275
276 if ( GetParent()->HandleWindowEvent(eventNav) )
277 return;
278 }
279 break;
280 }
281
282 // no, we didn't process it
283 event.Skip();
284 }
285
286 void wxSpinCtrl::OnKillFocus(wxFocusEvent& event)
287 {
288 // ensure that a correct value is shown by the control
289 NormalizeValue();
290 event.Skip();
291 }
292
293 void wxSpinCtrl::OnSetFocus(wxFocusEvent& event)
294 {
295 // when we get focus, give it to our buddy window as it needs it more than
296 // we do
297 ::SetFocus((HWND)m_hwndBuddy);
298
299 event.Skip();
300 }
301
302 void wxSpinCtrl::NormalizeValue()
303 {
304 const int value = GetValue();
305 const bool changed = value != m_oldValue;
306
307 // notice that we have to call SetValue() even if the value didn't change
308 // because otherwise we could be left with empty buddy control when value
309 // is 0, see comment in SetValue()
310 SetValue(value);
311
312 if ( changed )
313 {
314 SendSpinUpdate(value);
315 }
316 }
317
318 // ----------------------------------------------------------------------------
319 // construction
320 // ----------------------------------------------------------------------------
321
322 bool wxSpinCtrl::Create(wxWindow *parent,
323 wxWindowID id,
324 const wxString& value,
325 const wxPoint& pos,
326 const wxSize& size,
327 long style,
328 int min, int max, int initial,
329 const wxString& name)
330 {
331 m_blockEvent = false;
332
333 // this should be in ctor/init function but I don't want to add one to 2.8
334 // to avoid problems with default ctor which can be inlined in the user
335 // code and so might not get this fix without recompilation
336 m_oldValue = INT_MIN;
337
338 // before using DoGetBestSize(), have to set style to let the base class
339 // know whether this is a horizontal or vertical control (we're always
340 // vertical)
341 style |= wxSP_VERTICAL;
342
343 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
344 #ifdef __WXWINCE__
345 style |= wxBORDER_SIMPLE;
346 #else
347 style |= wxBORDER_SUNKEN;
348 #endif
349
350 SetWindowStyle(style);
351
352 WXDWORD exStyle = 0;
353 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
354
355 // propagate text alignment style to text ctrl
356 if ( style & wxALIGN_RIGHT )
357 msStyle |= ES_RIGHT;
358 else if ( style & wxALIGN_CENTER )
359 msStyle |= ES_CENTER;
360
361 // calculate the sizes: the size given is the total size for both controls
362 // and we need to fit them both in the given width (height is the same)
363 wxSize sizeText(size), sizeBtn(size);
364 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
365 if ( sizeText.x <= 0 )
366 {
367 // DEFAULT_ITEM_WIDTH is the default width for the text control
368 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
369 }
370
371 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
372 if ( sizeText.x <= 0 )
373 {
374 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
375 }
376
377 wxPoint posBtn(pos);
378 posBtn.x += sizeText.x + MARGIN_BETWEEN;
379
380 // we must create the text control before the spin button for the purpose
381 // of the dialog navigation: if there is a static text just before the spin
382 // control, activating it by Alt-letter should give focus to the text
383 // control, not the spin and the dialog navigation code will give focus to
384 // the next control (at Windows level), not the one after it
385
386 // create the text window
387
388 m_hwndBuddy = (WXHWND)::CreateWindowEx
389 (
390 exStyle, // sunken border
391 wxT("EDIT"), // window class
392 NULL, // no window title
393 msStyle, // style (will be shown later)
394 pos.x, pos.y, // position
395 0, 0, // size (will be set later)
396 GetHwndOf(parent), // parent
397 (HMENU)-1, // control id
398 wxGetInstance(), // app instance
399 NULL // unused client data
400 );
401
402 if ( !m_hwndBuddy )
403 {
404 wxLogLastError(wxT("CreateWindow(buddy text window)"));
405
406 return false;
407 }
408
409
410 // create the spin button
411 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
412 {
413 return false;
414 }
415
416 wxSpinButtonBase::SetRange(min, max);
417
418 // subclass the text ctrl to be able to intercept some events
419 gs_spinForTextCtrl[GetBuddyHwnd()] = this;
420
421 m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(),
422 wxBuddyTextWndProc);
423
424 // set up fonts and colours (This is nomally done in MSWCreateControl)
425 InheritAttributes();
426 if (!m_hasFont)
427 SetFont(GetDefaultAttributes().font);
428
429 // set the size of the text window - can do it only now, because we
430 // couldn't call DoGetBestSize() before as font wasn't set
431 if ( sizeText.y <= 0 )
432 {
433 int cx, cy;
434 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
435
436 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
437 }
438
439 SetInitialSize(size);
440
441 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
442
443 // associate the text window with the spin button
444 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
445
446 SetValue(initial);
447
448 // Set the range in the native control
449 SetRange(min, max);
450
451 if ( !value.empty() )
452 {
453 SetValue(value);
454 m_oldValue = (int) wxAtol(value);
455 }
456 else
457 {
458 SetValue(wxString::Format(wxT("%d"), initial));
459 m_oldValue = initial;
460 }
461
462 return true;
463 }
464
465 wxSpinCtrl::~wxSpinCtrl()
466 {
467 // destroy the buddy window because this pointer which wxBuddyTextWndProc
468 // uses will not soon be valid any more
469 ::DestroyWindow( GetBuddyHwnd() );
470
471 gs_spinForTextCtrl.erase(GetBuddyHwnd());
472 }
473
474 // ----------------------------------------------------------------------------
475 // wxTextCtrl-like methods
476 // ----------------------------------------------------------------------------
477
478 void wxSpinCtrl::SetValue(const wxString& text)
479 {
480 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
481 {
482 wxLogLastError(wxT("SetWindowText(buddy)"));
483 }
484 }
485
486 void wxSpinCtrl::SetValue(int val)
487 {
488 m_blockEvent = true;
489
490 wxSpinButton::SetValue(val);
491
492 // normally setting the value of the spin button is enough as it updates
493 // its buddy control automatically ...
494 if ( wxGetWindowText(m_hwndBuddy).empty() )
495 {
496 // ... but sometimes it doesn't, notably when the value is 0 and the
497 // text control is currently empty, the spin button seems to be happy
498 // to leave it like this, while we really want to always show the
499 // current value in the control, so do it manually
500 ::SetWindowText(GetBuddyHwnd(),
501 wxString::Format(wxT("%d"), val).wx_str());
502 }
503
504 m_oldValue = GetValue();
505
506 m_blockEvent = false;
507 }
508
509 int wxSpinCtrl::GetValue() const
510 {
511 wxString val = wxGetWindowText(m_hwndBuddy);
512
513 long n;
514 if ( (wxSscanf(val, wxT("%ld"), &n) != 1) )
515 n = INT_MIN;
516
517 if ( n < m_min )
518 n = m_min;
519 if ( n > m_max )
520 n = m_max;
521
522 return n;
523 }
524
525 void wxSpinCtrl::SetSelection(long from, long to)
526 {
527 // if from and to are both -1, it means (in wxWidgets) that all text should
528 // be selected - translate into Windows convention
529 if ( (from == -1) && (to == -1) )
530 {
531 from = 0;
532 }
533
534 ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to);
535 }
536
537 // ----------------------------------------------------------------------------
538 // wxSpinButton methods
539 // ----------------------------------------------------------------------------
540
541 void wxSpinCtrl::SetRange(int minVal, int maxVal)
542 {
543 wxSpinButton::SetRange(minVal, maxVal);
544
545 // this control is used for numeric entry so restrict the input to numeric
546 // keys only -- but only if we don't need to be able to enter "-" in it as
547 // otherwise this would become impossible
548 const DWORD styleOld = ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE);
549 DWORD styleNew;
550 if ( minVal < 0 )
551 styleNew = styleOld & ~ES_NUMBER;
552 else
553 styleNew = styleOld | ES_NUMBER;
554
555 if ( styleNew != styleOld )
556 ::SetWindowLong(GetBuddyHwnd(), GWL_STYLE, styleNew);
557 }
558
559 // ----------------------------------------------------------------------------
560 // forward some methods to subcontrols
561 // ----------------------------------------------------------------------------
562
563 bool wxSpinCtrl::SetFont(const wxFont& font)
564 {
565 if ( !wxWindowBase::SetFont(font) )
566 {
567 // nothing to do
568 return false;
569 }
570
571 WXHANDLE hFont = GetFont().GetResourceHandle();
572 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
573
574 return true;
575 }
576
577 bool wxSpinCtrl::Show(bool show)
578 {
579 if ( !wxControl::Show(show) )
580 {
581 return false;
582 }
583
584 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
585
586 return true;
587 }
588
589 bool wxSpinCtrl::Reparent(wxWindowBase *newParent)
590 {
591 // Reparenting both the updown control and its buddy does not seem to work:
592 // they continue to be connected somehow, but visually there is no feedback
593 // on the buddy edit control. To avoid this problem, we reparent the buddy
594 // window normally, but we recreate the updown control and reassign its
595 // buddy.
596
597 if ( !wxWindowBase::Reparent(newParent) )
598 return false;
599
600 newParent->GetChildren().DeleteObject(this);
601
602 // preserve the old values
603 const wxSize size = GetSize();
604 int value = GetValue();
605 const wxRect btnRect = wxRectFromRECT(wxGetWindowRect(GetHwnd()));
606
607 // destroy the old spin button
608 UnsubclassWin();
609 if ( !::DestroyWindow(GetHwnd()) )
610 {
611 wxLogLastError(wxT("DestroyWindow"));
612 }
613
614 // create and initialize the new one
615 if ( !wxSpinButton::Create(GetParent(), GetId(),
616 btnRect.GetPosition(), btnRect.GetSize(),
617 GetWindowStyle(), GetName()) )
618 return false;
619
620 SetValue(value);
621 SetRange(m_min, m_max);
622 SetInitialSize(size);
623
624 // associate it with the buddy control again
625 ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent()));
626 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)GetBuddyHwnd(), 0);
627
628 return true;
629 }
630
631 bool wxSpinCtrl::Enable(bool enable)
632 {
633 if ( !wxControl::Enable(enable) )
634 {
635 return false;
636 }
637
638 MSWEnableHWND(GetBuddyHwnd(), enable);
639
640 return true;
641 }
642
643 void wxSpinCtrl::SetFocus()
644 {
645 ::SetFocus(GetBuddyHwnd());
646 }
647
648 #if wxUSE_TOOLTIPS
649
650 void wxSpinCtrl::DoSetToolTip(wxToolTip *tip)
651 {
652 wxSpinButton::DoSetToolTip(tip);
653
654 if ( tip )
655 tip->Add(m_hwndBuddy);
656 }
657
658 #endif // wxUSE_TOOLTIPS
659
660 // ----------------------------------------------------------------------------
661 // events processing and generation
662 // ----------------------------------------------------------------------------
663
664 void wxSpinCtrl::SendSpinUpdate(int value)
665 {
666 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
667 event.SetEventObject(this);
668 event.SetInt(value);
669
670 (void)HandleWindowEvent(event);
671
672 m_oldValue = value;
673 }
674
675 bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
676 WXWORD pos, WXHWND control)
677 {
678 wxCHECK_MSG( control, false, wxT("scrolling what?") );
679
680 if ( wParam != SB_THUMBPOSITION )
681 {
682 // probable SB_ENDSCROLL - we don't react to it
683 return false;
684 }
685
686 int new_value = (short) pos;
687 if (m_oldValue != new_value)
688 SendSpinUpdate( new_value );
689
690 return TRUE;
691 }
692
693 bool wxSpinCtrl::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
694 {
695 NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam;
696
697 if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control
698 return false;
699
700 *result = 0; // never reject UP and DOWN events
701
702 return TRUE;
703 }
704
705
706 // ----------------------------------------------------------------------------
707 // size calculations
708 // ----------------------------------------------------------------------------
709
710 wxSize wxSpinCtrl::DoGetBestSize() const
711 {
712 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
713 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
714
715 int y;
716 wxGetCharSize(GetHWND(), NULL, &y, GetFont());
717 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
718
719 // JACS: we should always use the height calculated
720 // from above, because otherwise we'll get a spin control
721 // that's too big. So never use the height calculated
722 // from wxSpinButton::DoGetBestSize().
723
724 // if ( sizeBtn.y < y )
725 {
726 // make the text tall enough
727 sizeBtn.y = y;
728 }
729
730 return sizeBtn;
731 }
732
733 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
734 {
735 int widthBtn = wxSpinButton::DoGetBestSize().x;
736 int widthText = width - widthBtn - MARGIN_BETWEEN;
737 if ( widthText <= 0 )
738 {
739 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
740 }
741
742 // 1) The buddy window
743 DoMoveSibling(m_hwndBuddy, x, y, widthText, height);
744
745 // 2) The button window
746 x += widthText + MARGIN_BETWEEN;
747 wxSpinButton::DoMoveWindow(x, y, widthBtn, height);
748 }
749
750 // get total size of the control
751 void wxSpinCtrl::DoGetSize(int *x, int *y) const
752 {
753 RECT spinrect, textrect, ctrlrect;
754 GetWindowRect(GetHwnd(), &spinrect);
755 GetWindowRect(GetBuddyHwnd(), &textrect);
756 UnionRect(&ctrlrect,&textrect, &spinrect);
757
758 if ( x )
759 *x = ctrlrect.right - ctrlrect.left;
760 if ( y )
761 *y = ctrlrect.bottom - ctrlrect.top;
762 }
763
764 void wxSpinCtrl::DoGetClientSize(int *x, int *y) const
765 {
766 RECT spinrect = wxGetClientRect(GetHwnd());
767 RECT textrect = wxGetClientRect(GetBuddyHwnd());
768 RECT ctrlrect;
769 UnionRect(&ctrlrect,&textrect, &spinrect);
770
771 if ( x )
772 *x = ctrlrect.right - ctrlrect.left;
773 if ( y )
774 *y = ctrlrect.bottom - ctrlrect.top;
775 }
776
777 void wxSpinCtrl::DoGetPosition(int *x, int *y) const
778 {
779 // hack: pretend that our HWND is the text control just for a moment
780 WXHWND hWnd = GetHWND();
781 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
782
783 wxSpinButton::DoGetPosition(x, y);
784
785 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
786 }
787
788 #endif // wxUSE_SPINCTRL