]> git.saurik.com Git - wxWidgets.git/blame - src/msw/spinctrl.cpp
Don't use DDEExec registry key in wxMSW wxExecute() if it's empty.
[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
74124ea9 7// Copyright: (c) 1999-2005 Vadim Zeitlin
65571936 8// Licence: wxWindows licence
b782f2e0
VZ
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
b782f2e0
VZ
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
0e528b99
JS
26#if wxUSE_SPINCTRL
27
b782f2e0 28#include "wx/spinctrl.h"
623d5f80
WS
29
30#ifndef WX_PRECOMP
7ee21e3a 31 #include "wx/hashmap.h"
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
9750fc42 50BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
e3582f7f 51 EVT_CHAR(wxSpinCtrl::OnChar)
c5c04fab 52 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
4a528443 53 EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus)
9750fc42 54END_EVENT_TABLE()
b782f2e0 55
6fe19057
VZ
56#define GetBuddyHwnd() (HWND)(m_hwndBuddy)
57
b782f2e0
VZ
58// ----------------------------------------------------------------------------
59// constants
60// ----------------------------------------------------------------------------
61
baccb514
VZ
62// the margin between the up-down control and its buddy (can be arbitrary,
63// choose what you like - or may be decide during run-time depending on the
64// font size?)
65static const int MARGIN_BETWEEN = 1;
b782f2e0 66
7ee21e3a
VZ
67
68// ---------------------------------------------------------------------------
69// global vars
70// ---------------------------------------------------------------------------
71
72namespace
73{
74
75// Global hash used to find the spin control corresponding to the given buddy
76// text control HWND.
77WX_DECLARE_HASH_MAP(HWND, wxSpinCtrl *,
78 wxPointerHash, wxPointerEqual,
79 SpinForTextCtrl);
80
81SpinForTextCtrl gs_spinForTextCtrl;
82
83} // anonymous namespace
84
b782f2e0
VZ
85// ============================================================================
86// implementation
87// ============================================================================
88
f6bcfd97
BP
89// ----------------------------------------------------------------------------
90// wnd proc for the buddy text ctrl
91// ----------------------------------------------------------------------------
92
93LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
94 UINT message,
95 WPARAM wParam,
96 LPARAM lParam)
97{
7ee21e3a 98 wxSpinCtrl * const spin = wxSpinCtrl::GetSpinForTextCtrl(hwnd);
f6bcfd97 99
5a0b1008 100 // forward some messages (mostly the key and focus ones) to the spin ctrl
f6bcfd97
BP
101 switch ( message )
102 {
93c4157c 103 case WM_SETFOCUS:
c5c04fab
VZ
104 // if the focus comes from the spin control itself, don't set it
105 // back to it -- we don't want to go into an infinite loop
c140b7e7 106 if ( (WXHWND)wParam == spin->GetHWND() )
c5c04fab
VZ
107 break;
108 //else: fall through
109
93c4157c 110 case WM_KILLFOCUS:
f6bcfd97
BP
111 case WM_CHAR:
112 case WM_DEADCHAR:
113 case WM_KEYUP:
114 case WM_KEYDOWN:
5a0b1008
VZ
115#ifdef WM_HELP
116 // we need to forward WM_HELP too to ensure that the context help
117 // associated with wxSpinCtrl is shown when the text control part of it
118 // is clicked with the "?" cursor
119 case WM_HELP:
120#endif
9f6e407c
VZ
121 {
122 WXLRESULT result;
123 if ( spin->MSWHandleMessage(&result, message, wParam, lParam) )
124 {
125 // Do not let the message be processed by the window proc
126 // of the text control if it had been already handled at wx
127 // level, this is consistent with what happens for normal,
128 // non-composite controls.
129 return 0;
130 }
131
132 // The control may have been deleted at this point, so check.
133 if ( !::IsWindow(hwnd) )
134 return 0;
135 }
f6bcfd97 136 break;
350ba193
VZ
137
138 case WM_GETDLGCODE:
242ec2f7
VZ
139 if ( spin->HasFlag(wxTE_PROCESS_ENTER) )
140 {
141 long dlgCode = ::CallWindowProc
142 (
143 CASTWNDPROC spin->GetBuddyWndProc(),
144 hwnd,
145 message,
146 wParam,
147 lParam
148 );
149 dlgCode |= DLGC_WANTMESSAGE;
150 return dlgCode;
151 }
152 break;
f6bcfd97 153 }
350ba193 154
f6bcfd97
BP
155 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
156 hwnd, message, wParam, lParam);
157}
158
6fe19057
VZ
159/* static */
160wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
161{
7ee21e3a
VZ
162 const SpinForTextCtrl::const_iterator
163 it = gs_spinForTextCtrl.find(hwndBuddy);
164 if ( it == gs_spinForTextCtrl.end() )
6fe19057
VZ
165 return NULL;
166
7ee21e3a
VZ
167 wxSpinCtrl * const spin = it->second;
168
6fe19057
VZ
169 // sanity check
170 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
9a83f860 171 wxT("wxSpinCtrl has incorrect buddy HWND!") );
6fe19057
VZ
172
173 return spin;
174}
175
176// process a WM_COMMAND generated by the buddy text control
177bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
178{
ea6cbf48 179 if ( (cmd == EN_CHANGE) && (!m_blockEvent ))
6fe19057 180 {
ce7fe42e 181 wxCommandEvent event(wxEVT_TEXT, GetId());
0ca3c231
VZ
182 event.SetEventObject(this);
183 wxString val = wxGetWindowText(m_hwndBuddy);
184 event.SetString(val);
185 event.SetInt(GetValue());
937013e0 186 return HandleWindowEvent(event);
6fe19057
VZ
187 }
188
189 // not processed
57f4f925 190 return false;
6fe19057
VZ
191}
192
e3582f7f
JS
193void wxSpinCtrl::OnChar(wxKeyEvent& event)
194{
77e00fe9 195 switch ( event.GetKeyCode() )
e3582f7f
JS
196 {
197 case WXK_RETURN:
198 {
ce7fe42e 199 wxCommandEvent event(wxEVT_TEXT_ENTER, m_windowId);
e3582f7f
JS
200 InitCommandEvent(event);
201 wxString val = wxGetWindowText(m_hwndBuddy);
202 event.SetString(val);
203 event.SetInt(GetValue());
937013e0 204 if ( HandleWindowEvent(event) )
e3582f7f
JS
205 return;
206 break;
207 }
208
209 case WXK_TAB:
210 // always produce navigation event - even if we process TAB
211 // ourselves the fact that we got here means that the user code
212 // decided to skip processing of this TAB - probably to let it
213 // do its default job.
214 {
215 wxNavigationKeyEvent eventNav;
216 eventNav.SetDirection(!event.ShiftDown());
217 eventNav.SetWindowChange(event.ControlDown());
218 eventNav.SetEventObject(this);
219
937013e0 220 if ( GetParent()->HandleWindowEvent(eventNav) )
e3582f7f
JS
221 return;
222 }
223 break;
224 }
225
226 // no, we didn't process it
227 event.Skip();
228}
229
4a528443
JS
230void wxSpinCtrl::OnKillFocus(wxFocusEvent& event)
231{
5ec60151
VZ
232 // ensure that a correct value is shown by the control
233 NormalizeValue();
4a528443
JS
234 event.Skip();
235}
236
c5c04fab
VZ
237void wxSpinCtrl::OnSetFocus(wxFocusEvent& event)
238{
239 // when we get focus, give it to our buddy window as it needs it more than
240 // we do
241 ::SetFocus((HWND)m_hwndBuddy);
242
243 event.Skip();
244}
245
1e8dba5e
RR
246void wxSpinCtrl::NormalizeValue()
247{
e816f5c7 248 const int value = GetValue();
d40e9e06 249 const bool changed = value != m_oldValue;
e816f5c7 250
b6d83018
VZ
251 // notice that we have to call SetValue() even if the value didn't change
252 // because otherwise we could be left with empty buddy control when value
253 // is 0, see comment in SetValue()
e816f5c7
VZ
254 SetValue(value);
255
b6d83018
VZ
256 if ( changed )
257 {
d40e9e06 258 SendSpinUpdate(value);
b6d83018 259 }
1e8dba5e
RR
260}
261
b782f2e0
VZ
262// ----------------------------------------------------------------------------
263// construction
264// ----------------------------------------------------------------------------
265
bcdeea5a
VZ
266void wxSpinCtrl::Init()
267{
268 m_blockEvent = false;
269 m_hwndBuddy = NULL;
270 m_wndProcBuddy = NULL;
271 m_oldValue = INT_MIN;
272}
273
b782f2e0
VZ
274bool wxSpinCtrl::Create(wxWindow *parent,
275 wxWindowID id,
678cd6de 276 const wxString& value,
b782f2e0
VZ
277 const wxPoint& pos,
278 const wxSize& size,
279 long style,
280 int min, int max, int initial,
281 const wxString& name)
282{
283 // before using DoGetBestSize(), have to set style to let the base class
284 // know whether this is a horizontal or vertical control (we're always
285 // vertical)
882a8f40 286 style |= wxSP_VERTICAL;
c76b1a30
JS
287
288 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
5d72f195
RR
289#ifdef __WXWINCE__
290 style |= wxBORDER_SIMPLE;
291#else
c76b1a30 292 style |= wxBORDER_SUNKEN;
5d72f195 293#endif
c76b1a30 294
882a8f40 295 SetWindowStyle(style);
b782f2e0 296
fe3d9123
JS
297 WXDWORD exStyle = 0;
298 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
299
36b0b090
VZ
300 // Scroll text automatically if there is not enough space to show all of
301 // it, this is better than not allowing to enter more digits at all.
302 msStyle |= ES_AUTOHSCROLL;
303
7e4952db 304 // propagate text alignment style to text ctrl
f1ddb476 305 if ( style & wxALIGN_RIGHT )
7e4952db 306 msStyle |= ES_RIGHT;
f1ddb476 307 else if ( style & wxALIGN_CENTER )
7e4952db 308 msStyle |= ES_CENTER;
f1ddb476 309
8e190f0e 310 // calculate the sizes: the size given is the total size for both controls
b782f2e0
VZ
311 // and we need to fit them both in the given width (height is the same)
312 wxSize sizeText(size), sizeBtn(size);
313 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
baccb514
VZ
314 if ( sizeText.x <= 0 )
315 {
316 // DEFAULT_ITEM_WIDTH is the default width for the text control
317 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
318 }
319
b782f2e0
VZ
320 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
321 if ( sizeText.x <= 0 )
322 {
f46e6a1e
VZ
323 wxLogDebug(wxS("wxSpinCtrl \"%s\": initial width %d is too small, ")
324 wxS("at least %d pixels needed."),
325 name, size.x, sizeBtn.x + MARGIN_BETWEEN + 1);
b782f2e0
VZ
326 }
327
328 wxPoint posBtn(pos);
329 posBtn.x += sizeText.x + MARGIN_BETWEEN;
330
c5c04fab
VZ
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
b782f2e0 336
c5c04fab 337 // create the text window
b782f2e0 338
b782f2e0
VZ
339 m_hwndBuddy = (WXHWND)::CreateWindowEx
340 (
c5c04fab 341 exStyle, // sunken border
9a83f860 342 wxT("EDIT"), // window class
baccb514 343 NULL, // no window title
c5c04fab 344 msStyle, // style (will be shown later)
baccb514
VZ
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
b782f2e0
VZ
351 );
352
353 if ( !m_hwndBuddy )
354 {
f6bcfd97 355 wxLogLastError(wxT("CreateWindow(buddy text window)"));
b782f2e0 356
57f4f925 357 return false;
b782f2e0
VZ
358 }
359
c5c04fab
VZ
360
361 // create the spin button
362 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
363 {
57f4f925 364 return false;
c5c04fab
VZ
365 }
366
23ec96e3 367 wxSpinButtonBase::SetRange(min, max);
e816f5c7 368
f6bcfd97 369 // subclass the text ctrl to be able to intercept some events
7ee21e3a
VZ
370 gs_spinForTextCtrl[GetBuddyHwnd()] = this;
371
975b6bcf
VZ
372 m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(),
373 wxBuddyTextWndProc);
f6bcfd97 374
8d2e831b
RD
375 // set up fonts and colours (This is nomally done in MSWCreateControl)
376 InheritAttributes();
e0176dd9
VZ
377 if (!m_hasFont)
378 SetFont(GetDefaultAttributes().font);
8d2e831b 379
baccb514
VZ
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 {
882a8f40 384 int cx, cy;
7a5e53ab 385 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
882a8f40
VZ
386
387 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
baccb514
VZ
388 }
389
170acdc9 390 SetInitialSize(size);
baccb514 391
6fe19057 392 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
b782f2e0
VZ
393
394 // associate the text window with the spin button
baccb514
VZ
395 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
396
6e36db5e
VZ
397 // If the initial text value is actually a number, it overrides the
398 // "initial" argument specified later.
399 long initialFromText;
400 if ( value.ToLong(&initialFromText) )
401 initial = initialFromText;
402
fe218f1e
VZ
403 // Set the range in the native control: notice that we must do it before
404 // calling SetValue() to use the correct validity checks for the initial
405 // value.
549cc3a8 406 SetRange(min, max);
fe218f1e 407 SetValue(initial);
549cc3a8 408
6e36db5e
VZ
409 // Also set the text part of the control if it was specified independently
410 // but don't generate an event for this, it would be unexpected.
411 m_blockEvent = true;
412 if ( !value.empty() )
678cd6de 413 SetValue(value);
6e36db5e 414 m_blockEvent = false;
678cd6de 415
57f4f925 416 return true;
baccb514
VZ
417}
418
f6bcfd97
BP
419wxSpinCtrl::~wxSpinCtrl()
420{
421 // destroy the buddy window because this pointer which wxBuddyTextWndProc
422 // uses will not soon be valid any more
7ee21e3a
VZ
423 ::DestroyWindow( GetBuddyHwnd() );
424
425 gs_spinForTextCtrl.erase(GetBuddyHwnd());
f6bcfd97
BP
426}
427
9e565667
VZ
428// ----------------------------------------------------------------------------
429// wxSpinCtrl-specific methods
430// ----------------------------------------------------------------------------
431
432int wxSpinCtrl::GetBase() const
433{
434 return ::SendMessage(GetHwnd(), UDM_GETBASE, 0, 0);
435}
436
437bool wxSpinCtrl::SetBase(int base)
438{
439 if ( !::SendMessage(GetHwnd(), UDM_SETBASE, base, 0) )
440 return false;
441
442 // Whether we need to be able enter "x" or not influences whether we should
443 // use ES_NUMBER for the buddy control.
444 UpdateBuddyStyle();
445
446 return true;
447}
448
678cd6de
VZ
449// ----------------------------------------------------------------------------
450// wxTextCtrl-like methods
451// ----------------------------------------------------------------------------
452
453void wxSpinCtrl::SetValue(const wxString& text)
454{
6fe19057 455 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
678cd6de 456 {
f6bcfd97 457 wxLogLastError(wxT("SetWindowText(buddy)"));
678cd6de
VZ
458 }
459}
460
eeea41ab
VZ
461void wxSpinCtrl::SetValue(int val)
462{
ea6cbf48 463 m_blockEvent = true;
f1ddb476 464
eeea41ab
VZ
465 wxSpinButton::SetValue(val);
466
9e565667
VZ
467 // Normally setting the value of the spin button is enough as it updates
468 // its buddy control automatically but in a couple of situations it doesn't
469 // do it, for whatever reason, do it explicitly then:
470 const wxString text = wxGetWindowText(m_hwndBuddy);
471
472 // First case is when the text control is empty and the value is 0: the
473 // spin button just leaves it empty in this case, while we want to show 0
474 // in it.
475 if ( text.empty() && !val )
476 {
477 ::SetWindowText(GetBuddyHwnd(), wxT("0"));
478 }
479
480 // Another one is when we're using hexadecimal base but the user input
481 // doesn't start with "0x" -- we prefer to show it to avoid ambiguity
482 // between decimal and hexadecimal.
483 if ( GetBase() == 16 &&
484 (text.length() < 3 || text[0] != '0' ||
485 (text[1] != 'x' && text[1] != 'X')) )
eeea41ab 486 {
e0a050e3 487 ::SetWindowText(GetBuddyHwnd(),
9e565667 488 wxPrivate::wxSpinCtrlFormatAsHex(val, m_max).t_str());
eeea41ab 489 }
e816f5c7 490
25dff19c 491 m_oldValue = GetValue();
f1ddb476 492
ea6cbf48 493 m_blockEvent = false;
eeea41ab
VZ
494}
495
678cd6de
VZ
496int wxSpinCtrl::GetValue() const
497{
9e565667 498 const wxString val = wxGetWindowText(m_hwndBuddy);
678cd6de
VZ
499
500 long n;
9e565667 501 if ( !val.ToLong(&n, GetBase()) )
678cd6de 502 n = INT_MIN;
3d104ec3 503
eeea41ab
VZ
504 if ( n < m_min )
505 n = m_min;
506 if ( n > m_max )
507 n = m_max;
678cd6de
VZ
508
509 return n;
510}
511
07901ec9
VZ
512void wxSpinCtrl::SetSelection(long from, long to)
513{
77ffb593 514 // if from and to are both -1, it means (in wxWidgets) that all text should
07901ec9
VZ
515 // be selected - translate into Windows convention
516 if ( (from == -1) && (to == -1) )
517 {
518 from = 0;
519 }
520
7d86a2d4 521 ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to);
07901ec9
VZ
522}
523
345ff9c6
VZ
524// ----------------------------------------------------------------------------
525// wxSpinButton methods
526// ----------------------------------------------------------------------------
527
528void wxSpinCtrl::SetRange(int minVal, int maxVal)
529{
532324df
VZ
530 // Manually adjust the old value to avoid an event being sent from
531 // NormalizeValue() called from inside the base class SetRange() as we're
532 // not supposed to generate any events from here.
533 if ( m_oldValue < minVal )
534 m_oldValue = minVal;
535 else if ( m_oldValue > maxVal )
536 m_oldValue = maxVal;
537
345ff9c6
VZ
538 wxSpinButton::SetRange(minVal, maxVal);
539
9e565667
VZ
540 UpdateBuddyStyle();
541}
542
543void wxSpinCtrl::UpdateBuddyStyle()
544{
345ff9c6
VZ
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
9e565667
VZ
547 // otherwise this would become impossible and also if we don't use
548 // hexadecimal as entering "x" of the "0x" prefix wouldn't be allowed
549 // neither then
345ff9c6
VZ
550 const DWORD styleOld = ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE);
551 DWORD styleNew;
9e565667 552 if ( m_min < 0 || GetBase() != 10 )
345ff9c6
VZ
553 styleNew = styleOld & ~ES_NUMBER;
554 else
555 styleNew = styleOld | ES_NUMBER;
556
557 if ( styleNew != styleOld )
558 ::SetWindowLong(GetBuddyHwnd(), GWL_STYLE, styleNew);
559}
560
baccb514 561// ----------------------------------------------------------------------------
882a8f40 562// forward some methods to subcontrols
baccb514
VZ
563// ----------------------------------------------------------------------------
564
565bool wxSpinCtrl::SetFont(const wxFont& font)
566{
567 if ( !wxWindowBase::SetFont(font) )
568 {
569 // nothing to do
57f4f925 570 return false;
baccb514
VZ
571 }
572
573 WXHANDLE hFont = GetFont().GetResourceHandle();
6fe19057 574 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
b782f2e0 575
57f4f925 576 return true;
b782f2e0
VZ
577}
578
882a8f40
VZ
579bool wxSpinCtrl::Show(bool show)
580{
581 if ( !wxControl::Show(show) )
582 {
57f4f925 583 return false;
882a8f40
VZ
584 }
585
6fe19057 586 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
882a8f40 587
57f4f925 588 return true;
882a8f40
VZ
589}
590
03d4194d
VZ
591bool wxSpinCtrl::Reparent(wxWindowBase *newParent)
592{
593 // Reparenting both the updown control and its buddy does not seem to work:
594 // they continue to be connected somehow, but visually there is no feedback
595 // on the buddy edit control. To avoid this problem, we reparent the buddy
596 // window normally, but we recreate the updown control and reassign its
597 // buddy.
598
03263ff7
VZ
599 // Get the position before changing the parent as it would be offset after
600 // changing it.
601 const wxRect rect = GetRect();
602
03d4194d
VZ
603 if ( !wxWindowBase::Reparent(newParent) )
604 return false;
605
606 newParent->GetChildren().DeleteObject(this);
607
bc73fe96
VZ
608 // destroy the old spin button after detaching it from this wxWindow object
609 // (notice that m_hWnd will be reset by UnsubclassWin() so save it first)
610 const HWND hwndOld = GetHwnd();
03d4194d 611 UnsubclassWin();
bc73fe96 612 if ( !::DestroyWindow(hwndOld) )
43b2d5e7 613 {
03d4194d 614 wxLogLastError(wxT("DestroyWindow"));
43b2d5e7 615 }
03d4194d
VZ
616
617 // create and initialize the new one
618 if ( !wxSpinButton::Create(GetParent(), GetId(),
03263ff7 619 rect.GetPosition(), rect.GetSize(),
03d4194d
VZ
620 GetWindowStyle(), GetName()) )
621 return false;
622
03263ff7
VZ
623 // reapply our values to wxSpinButton
624 wxSpinButton::SetValue(GetValue());
03d4194d 625 SetRange(m_min, m_max);
03263ff7
VZ
626
627 // also set the size again with wxSIZE_ALLOW_MINUS_ONE flag: this is
628 // necessary if our original position used -1 for either x or y
629 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
03d4194d
VZ
630
631 // associate it with the buddy control again
632 ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent()));
633 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)GetBuddyHwnd(), 0);
634
635 return true;
636}
637
882a8f40
VZ
638bool wxSpinCtrl::Enable(bool enable)
639{
640 if ( !wxControl::Enable(enable) )
641 {
57f4f925 642 return false;
882a8f40
VZ
643 }
644
0826c4d3 645 MSWEnableHWND(GetBuddyHwnd(), enable);
882a8f40 646
57f4f925 647 return true;
882a8f40
VZ
648}
649
8bf3196d
GRG
650void wxSpinCtrl::SetFocus()
651{
6fe19057 652 ::SetFocus(GetBuddyHwnd());
8bf3196d
GRG
653}
654
74124ea9
VZ
655#if wxUSE_TOOLTIPS
656
657void wxSpinCtrl::DoSetToolTip(wxToolTip *tip)
658{
659 wxSpinButton::DoSetToolTip(tip);
660
661 if ( tip )
f7dd07f6 662 tip->AddOtherWindow(m_hwndBuddy);
74124ea9
VZ
663}
664
665#endif // wxUSE_TOOLTIPS
666
9750fc42 667// ----------------------------------------------------------------------------
d40e9e06 668// events processing and generation
9750fc42
VZ
669// ----------------------------------------------------------------------------
670
d40e9e06 671void wxSpinCtrl::SendSpinUpdate(int value)
9750fc42 672{
ce7fe42e 673 wxCommandEvent event(wxEVT_SPINCTRL, GetId());
9750fc42 674 event.SetEventObject(this);
d40e9e06 675 event.SetInt(value);
e816f5c7 676
937013e0 677 (void)HandleWindowEvent(event);
d40e9e06
VZ
678
679 m_oldValue = value;
680}
9750fc42 681
177e38e6 682bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
63420bcc 683 WXWORD WXUNUSED(pos), WXHWND control)
d40e9e06 684{
177e38e6
RR
685 wxCHECK_MSG( control, false, wxT("scrolling what?") );
686
687 if ( wParam != SB_THUMBPOSITION )
9750fc42 688 {
177e38e6
RR
689 // probable SB_ENDSCROLL - we don't react to it
690 return false;
9750fc42 691 }
177e38e6 692
63420bcc
VZ
693 // Notice that we can't use "pos" from WM_VSCROLL as it is 16 bit and we
694 // might be using 32 bit range.
695 int new_value = GetValue();
177e38e6
RR
696 if (m_oldValue != new_value)
697 SendSpinUpdate( new_value );
698
63420bcc 699 return true;
177e38e6
RR
700}
701
702bool wxSpinCtrl::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
703{
704 NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam;
705
706 if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control
707 return false;
708
709 *result = 0; // never reject UP and DOWN events
710
711 return TRUE;
9750fc42
VZ
712}
713
177e38e6 714
b782f2e0
VZ
715// ----------------------------------------------------------------------------
716// size calculations
717// ----------------------------------------------------------------------------
718
f68586e5 719wxSize wxSpinCtrl::DoGetBestSize() const
40aa1a7e
VZ
720{
721 return DoGetSizeFromTextSize(DEFAULT_ITEM_WIDTH);
722}
723
724wxSize wxSpinCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const
baccb514
VZ
725{
726 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
baccb514
VZ
727
728 int y;
7a5e53ab 729 wxGetCharSize(GetHWND(), NULL, &y, GetFont());
09f27917
JS
730 // JACS: we should always use the height calculated
731 // from above, because otherwise we'll get a spin control
732 // that's too big. So never use the height calculated
733 // from wxSpinButton::DoGetBestSize().
57f4f925 734
2aa54d47 735 wxSize tsize(xlen + sizeBtn.x + MARGIN_BETWEEN + 3*y/10 + 10,
40aa1a7e
VZ
736 EDIT_HEIGHT_FROM_CHAR_HEIGHT(y));
737
738 // Check if the user requested a non-standard height.
739 if ( ylen > 0 )
740 tsize.IncBy(0, ylen - y);
baccb514 741
40aa1a7e 742 return tsize;
baccb514
VZ
743}
744
b782f2e0
VZ
745void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
746{
baccb514 747 int widthBtn = wxSpinButton::DoGetBestSize().x;
b782f2e0 748 int widthText = width - widthBtn - MARGIN_BETWEEN;
f46e6a1e 749 if ( widthText < 0 )
b782f2e0 750 {
f46e6a1e
VZ
751 // This can happen during the initial window layout when it's total
752 // size is too small to accommodate all the controls and usually is not
753 // a problem because the window will be relaid out with enough space
754 // later. Of course, if it isn't and this is our final size, then we
755 // have a real problem but as we don't know if this is going to be the
756 // case or not, just hope for the best -- we used to give a debug
757 // warning here and this was annoying as it could result in dozens of
758 // perfectly harmless warnings.
759 widthText = 0;
b782f2e0
VZ
760 }
761
8e44f3ca 762 // 1) The buddy window
7d86a2d4 763 DoMoveSibling(m_hwndBuddy, x, y, widthText, height);
b782f2e0 764
8e44f3ca 765 // 2) The button window
f46e6a1e
VZ
766 if ( widthText > 0 )
767 x += widthText + MARGIN_BETWEEN;
7d86a2d4 768 wxSpinButton::DoMoveWindow(x, y, widthBtn, height);
b782f2e0
VZ
769}
770
f6bcfd97
BP
771// get total size of the control
772void wxSpinCtrl::DoGetSize(int *x, int *y) const
773{
774 RECT spinrect, textrect, ctrlrect;
775 GetWindowRect(GetHwnd(), &spinrect);
6fe19057 776 GetWindowRect(GetBuddyHwnd(), &textrect);
f6bcfd97
BP
777 UnionRect(&ctrlrect,&textrect, &spinrect);
778
779 if ( x )
780 *x = ctrlrect.right - ctrlrect.left;
781 if ( y )
782 *y = ctrlrect.bottom - ctrlrect.top;
783}
784
b7527dde
VS
785void wxSpinCtrl::DoGetClientSize(int *x, int *y) const
786{
787 RECT spinrect = wxGetClientRect(GetHwnd());
788 RECT textrect = wxGetClientRect(GetBuddyHwnd());
789 RECT ctrlrect;
790 UnionRect(&ctrlrect,&textrect, &spinrect);
791
792 if ( x )
793 *x = ctrlrect.right - ctrlrect.left;
794 if ( y )
795 *y = ctrlrect.bottom - ctrlrect.top;
796}
797
f6bcfd97
BP
798void wxSpinCtrl::DoGetPosition(int *x, int *y) const
799{
800 // hack: pretend that our HWND is the text control just for a moment
801 WXHWND hWnd = GetHWND();
802 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
803
804 wxSpinButton::DoGetPosition(x, y);
805
806 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
807}
808
74124ea9 809#endif // wxUSE_SPINCTRL