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