]> git.saurik.com Git - wxWidgets.git/blame - src/msw/spinctrl.cpp
No real changes, just use ProgID term instead of incorrect CLSID.
[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
7ee21e3a 32 #include "wx/hashmap.h"
57bd4c60 33 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
623d5f80
WS
34 #include "wx/event.h"
35 #include "wx/textctrl.h"
193d0c93 36 #include "wx/wxcrtvararg.h"
623d5f80
WS
37#endif
38
b782f2e0
VZ
39#include "wx/msw/private.h"
40
74124ea9
VZ
41#if wxUSE_TOOLTIPS
42 #include "wx/tooltip.h"
43#endif // wxUSE_TOOLTIPS
b782f2e0 44
678cd6de
VZ
45#include <limits.h> // for INT_MIN
46
b782f2e0
VZ
47// ----------------------------------------------------------------------------
48// macros
49// ----------------------------------------------------------------------------
50
f0a126fe 51#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
52WX_DEFINE_FLAGS( wxSpinCtrlStyle )
53
3ff066a4 54wxBEGIN_FLAGS( wxSpinCtrlStyle )
bc9fb572
JS
55 // new style border flags, we put them first to
56 // use them for streaming out
3ff066a4
SC
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)
57f4f925 63
bc9fb572 64 // old style border flags
3ff066a4
SC
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)
cb0afb26 70 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
71
72 // standard window styles
3ff066a4
SC
73 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
74 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
75 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
76 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 77 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
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
87wxEND_FLAGS( wxSpinCtrlStyle )
bc9fb572 88
51741307 89IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl, wxControl,"wx/spinbut.h")
9750fc42 90
3ff066a4 91wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl)
6f45066b 92 wxEVENT_RANGE_PROPERTY( Spin , wxEVT_SCROLL_TOP , wxEVT_SCROLL_CHANGED , wxSpinEvent )
3ff066a4 93 wxEVENT_PROPERTY( Updated , wxEVT_COMMAND_SPINCTRL_UPDATED , wxCommandEvent )
57f4f925 94 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
3ff066a4
SC
95 wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent )
96
af498247 97 wxPROPERTY( ValueString , wxString , SetValue , GetValue , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
57f4f925
WS
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"))
af498247 101 wxPROPERTY_FLAGS( WindowStyle , wxSpinCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
066f1b7a 102/*
57f4f925
WS
103 TODO PROPERTIES
104 style wxSP_ARROW_KEYS
066f1b7a 105*/
3ff066a4 106wxEND_PROPERTIES_TABLE()
51741307 107
3ff066a4
SC
108wxBEGIN_HANDLERS_TABLE(wxSpinCtrl)
109wxEND_HANDLERS_TABLE()
51741307 110
57f4f925 111wxCONSTRUCTOR_6( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , ValueString , wxPoint , Position , wxSize , Size , long , WindowStyle )
51741307
SC
112#else
113IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
114#endif
066f1b7a 115
9750fc42 116BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
e3582f7f 117 EVT_CHAR(wxSpinCtrl::OnChar)
c5c04fab 118 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
4a528443 119 EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus)
9750fc42 120END_EVENT_TABLE()
b782f2e0 121
6fe19057
VZ
122#define GetBuddyHwnd() (HWND)(m_hwndBuddy)
123
b782f2e0
VZ
124// ----------------------------------------------------------------------------
125// constants
126// ----------------------------------------------------------------------------
127
baccb514
VZ
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?)
131static const int MARGIN_BETWEEN = 1;
b782f2e0 132
7ee21e3a
VZ
133
134// ---------------------------------------------------------------------------
135// global vars
136// ---------------------------------------------------------------------------
137
138namespace
139{
140
141// Global hash used to find the spin control corresponding to the given buddy
142// text control HWND.
143WX_DECLARE_HASH_MAP(HWND, wxSpinCtrl *,
144 wxPointerHash, wxPointerEqual,
145 SpinForTextCtrl);
146
147SpinForTextCtrl gs_spinForTextCtrl;
148
149} // anonymous namespace
150
b782f2e0
VZ
151// ============================================================================
152// implementation
153// ============================================================================
154
f6bcfd97
BP
155// ----------------------------------------------------------------------------
156// wnd proc for the buddy text ctrl
157// ----------------------------------------------------------------------------
158
159LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
160 UINT message,
161 WPARAM wParam,
162 LPARAM lParam)
163{
7ee21e3a 164 wxSpinCtrl * const spin = wxSpinCtrl::GetSpinForTextCtrl(hwnd);
f6bcfd97 165
5a0b1008 166 // forward some messages (mostly the key and focus ones) to the spin ctrl
f6bcfd97
BP
167 switch ( message )
168 {
93c4157c 169 case WM_SETFOCUS:
c5c04fab
VZ
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
c140b7e7 172 if ( (WXHWND)wParam == spin->GetHWND() )
c5c04fab
VZ
173 break;
174 //else: fall through
175
93c4157c 176 case WM_KILLFOCUS:
f6bcfd97
BP
177 case WM_CHAR:
178 case WM_DEADCHAR:
179 case WM_KEYUP:
180 case WM_KEYDOWN:
5a0b1008
VZ
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
f6bcfd97 187 spin->MSWWindowProc(message, wParam, lParam);
e3582f7f
JS
188
189 // The control may have been deleted at this point, so check.
7ee21e3a 190 if ( !::IsWindow(hwnd) )
e3582f7f 191 return 0;
f6bcfd97 192 break;
350ba193
VZ
193
194 case WM_GETDLGCODE:
242ec2f7
VZ
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;
f6bcfd97 209 }
350ba193 210
f6bcfd97
BP
211 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
212 hwnd, message, wParam, lParam);
213}
214
6fe19057
VZ
215/* static */
216wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
217{
7ee21e3a
VZ
218 const SpinForTextCtrl::const_iterator
219 it = gs_spinForTextCtrl.find(hwndBuddy);
220 if ( it == gs_spinForTextCtrl.end() )
6fe19057
VZ
221 return NULL;
222
7ee21e3a
VZ
223 wxSpinCtrl * const spin = it->second;
224
6fe19057
VZ
225 // sanity check
226 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
9a83f860 227 wxT("wxSpinCtrl has incorrect buddy HWND!") );
6fe19057
VZ
228
229 return spin;
230}
231
232// process a WM_COMMAND generated by the buddy text control
233bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
234{
ea6cbf48 235 if ( (cmd == EN_CHANGE) && (!m_blockEvent ))
6fe19057 236 {
0ca3c231
VZ
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());
937013e0 242 return HandleWindowEvent(event);
6fe19057
VZ
243 }
244
245 // not processed
57f4f925 246 return false;
6fe19057
VZ
247}
248
e3582f7f
JS
249void wxSpinCtrl::OnChar(wxKeyEvent& event)
250{
77e00fe9 251 switch ( event.GetKeyCode() )
e3582f7f
JS
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());
937013e0 260 if ( HandleWindowEvent(event) )
e3582f7f
JS
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
937013e0 276 if ( GetParent()->HandleWindowEvent(eventNav) )
e3582f7f
JS
277 return;
278 }
279 break;
280 }
281
282 // no, we didn't process it
283 event.Skip();
284}
285
4a528443
JS
286void wxSpinCtrl::OnKillFocus(wxFocusEvent& event)
287{
5ec60151
VZ
288 // ensure that a correct value is shown by the control
289 NormalizeValue();
4a528443
JS
290 event.Skip();
291}
292
c5c04fab
VZ
293void 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
1e8dba5e
RR
302void wxSpinCtrl::NormalizeValue()
303{
e816f5c7 304 const int value = GetValue();
d40e9e06 305 const bool changed = value != m_oldValue;
e816f5c7 306
b6d83018
VZ
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()
e816f5c7
VZ
310 SetValue(value);
311
b6d83018
VZ
312 if ( changed )
313 {
d40e9e06 314 SendSpinUpdate(value);
b6d83018 315 }
1e8dba5e
RR
316}
317
b782f2e0
VZ
318// ----------------------------------------------------------------------------
319// construction
320// ----------------------------------------------------------------------------
321
322bool wxSpinCtrl::Create(wxWindow *parent,
323 wxWindowID id,
678cd6de 324 const wxString& value,
b782f2e0
VZ
325 const wxPoint& pos,
326 const wxSize& size,
327 long style,
328 int min, int max, int initial,
329 const wxString& name)
330{
ea6cbf48
RR
331 m_blockEvent = false;
332
d40e9e06
VZ
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
b782f2e0
VZ
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)
882a8f40 341 style |= wxSP_VERTICAL;
c76b1a30
JS
342
343 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
5d72f195
RR
344#ifdef __WXWINCE__
345 style |= wxBORDER_SIMPLE;
346#else
c76b1a30 347 style |= wxBORDER_SUNKEN;
5d72f195 348#endif
c76b1a30 349
882a8f40 350 SetWindowStyle(style);
b782f2e0 351
fe3d9123
JS
352 WXDWORD exStyle = 0;
353 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
354
7e4952db 355 // propagate text alignment style to text ctrl
f1ddb476 356 if ( style & wxALIGN_RIGHT )
7e4952db 357 msStyle |= ES_RIGHT;
f1ddb476 358 else if ( style & wxALIGN_CENTER )
7e4952db 359 msStyle |= ES_CENTER;
f1ddb476 360
8e190f0e 361 // calculate the sizes: the size given is the total size for both controls
b782f2e0
VZ
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;
baccb514
VZ
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
b782f2e0
VZ
371 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
372 if ( sizeText.x <= 0 )
373 {
9a83f860 374 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
b782f2e0
VZ
375 }
376
377 wxPoint posBtn(pos);
378 posBtn.x += sizeText.x + MARGIN_BETWEEN;
379
c5c04fab
VZ
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
b782f2e0 385
c5c04fab 386 // create the text window
b782f2e0 387
b782f2e0
VZ
388 m_hwndBuddy = (WXHWND)::CreateWindowEx
389 (
c5c04fab 390 exStyle, // sunken border
9a83f860 391 wxT("EDIT"), // window class
baccb514 392 NULL, // no window title
c5c04fab 393 msStyle, // style (will be shown later)
baccb514
VZ
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
b782f2e0
VZ
400 );
401
402 if ( !m_hwndBuddy )
403 {
f6bcfd97 404 wxLogLastError(wxT("CreateWindow(buddy text window)"));
b782f2e0 405
57f4f925 406 return false;
b782f2e0
VZ
407 }
408
c5c04fab
VZ
409
410 // create the spin button
411 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
412 {
57f4f925 413 return false;
c5c04fab
VZ
414 }
415
23ec96e3 416 wxSpinButtonBase::SetRange(min, max);
e816f5c7 417
f6bcfd97 418 // subclass the text ctrl to be able to intercept some events
7ee21e3a
VZ
419 gs_spinForTextCtrl[GetBuddyHwnd()] = this;
420
975b6bcf
VZ
421 m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(),
422 wxBuddyTextWndProc);
f6bcfd97 423
8d2e831b
RD
424 // set up fonts and colours (This is nomally done in MSWCreateControl)
425 InheritAttributes();
e0176dd9
VZ
426 if (!m_hasFont)
427 SetFont(GetDefaultAttributes().font);
8d2e831b 428
baccb514
VZ
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 {
882a8f40 433 int cx, cy;
7a5e53ab 434 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
882a8f40
VZ
435
436 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
baccb514
VZ
437 }
438
170acdc9 439 SetInitialSize(size);
baccb514 440
6fe19057 441 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
b782f2e0
VZ
442
443 // associate the text window with the spin button
baccb514
VZ
444 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
445
549cc3a8
JS
446 SetValue(initial);
447
448 // Set the range in the native control
449 SetRange(min, max);
450
3d104ec3 451 if ( !value.empty() )
678cd6de
VZ
452 {
453 SetValue(value);
5bcdac45
JS
454 m_oldValue = (int) wxAtol(value);
455 }
456 else
457 {
458 SetValue(wxString::Format(wxT("%d"), initial));
459 m_oldValue = initial;
678cd6de
VZ
460 }
461
57f4f925 462 return true;
baccb514
VZ
463}
464
f6bcfd97
BP
465wxSpinCtrl::~wxSpinCtrl()
466{
467 // destroy the buddy window because this pointer which wxBuddyTextWndProc
468 // uses will not soon be valid any more
7ee21e3a
VZ
469 ::DestroyWindow( GetBuddyHwnd() );
470
471 gs_spinForTextCtrl.erase(GetBuddyHwnd());
f6bcfd97
BP
472}
473
678cd6de
VZ
474// ----------------------------------------------------------------------------
475// wxTextCtrl-like methods
476// ----------------------------------------------------------------------------
477
478void wxSpinCtrl::SetValue(const wxString& text)
479{
6fe19057 480 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
678cd6de 481 {
f6bcfd97 482 wxLogLastError(wxT("SetWindowText(buddy)"));
678cd6de
VZ
483 }
484}
485
eeea41ab
VZ
486void wxSpinCtrl::SetValue(int val)
487{
ea6cbf48 488 m_blockEvent = true;
f1ddb476 489
eeea41ab
VZ
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
e0a050e3 500 ::SetWindowText(GetBuddyHwnd(),
9a83f860 501 wxString::Format(wxT("%d"), val).wx_str());
eeea41ab 502 }
e816f5c7 503
25dff19c 504 m_oldValue = GetValue();
f1ddb476 505
ea6cbf48 506 m_blockEvent = false;
eeea41ab
VZ
507}
508
678cd6de
VZ
509int wxSpinCtrl::GetValue() const
510{
511 wxString val = wxGetWindowText(m_hwndBuddy);
512
513 long n;
eeea41ab 514 if ( (wxSscanf(val, wxT("%ld"), &n) != 1) )
678cd6de 515 n = INT_MIN;
3d104ec3 516
eeea41ab
VZ
517 if ( n < m_min )
518 n = m_min;
519 if ( n > m_max )
520 n = m_max;
678cd6de
VZ
521
522 return n;
523}
524
07901ec9
VZ
525void wxSpinCtrl::SetSelection(long from, long to)
526{
77ffb593 527 // if from and to are both -1, it means (in wxWidgets) that all text should
07901ec9
VZ
528 // be selected - translate into Windows convention
529 if ( (from == -1) && (to == -1) )
530 {
531 from = 0;
532 }
533
7d86a2d4 534 ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to);
07901ec9
VZ
535}
536
345ff9c6
VZ
537// ----------------------------------------------------------------------------
538// wxSpinButton methods
539// ----------------------------------------------------------------------------
540
541void 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
baccb514 559// ----------------------------------------------------------------------------
882a8f40 560// forward some methods to subcontrols
baccb514
VZ
561// ----------------------------------------------------------------------------
562
563bool wxSpinCtrl::SetFont(const wxFont& font)
564{
565 if ( !wxWindowBase::SetFont(font) )
566 {
567 // nothing to do
57f4f925 568 return false;
baccb514
VZ
569 }
570
571 WXHANDLE hFont = GetFont().GetResourceHandle();
6fe19057 572 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
b782f2e0 573
57f4f925 574 return true;
b782f2e0
VZ
575}
576
882a8f40
VZ
577bool wxSpinCtrl::Show(bool show)
578{
579 if ( !wxControl::Show(show) )
580 {
57f4f925 581 return false;
882a8f40
VZ
582 }
583
6fe19057 584 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
882a8f40 585
57f4f925 586 return true;
882a8f40
VZ
587}
588
03d4194d
VZ
589bool 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
03263ff7
VZ
597 // Get the position before changing the parent as it would be offset after
598 // changing it.
599 const wxRect rect = GetRect();
600
03d4194d
VZ
601 if ( !wxWindowBase::Reparent(newParent) )
602 return false;
603
604 newParent->GetChildren().DeleteObject(this);
605
bc73fe96
VZ
606 // destroy the old spin button after detaching it from this wxWindow object
607 // (notice that m_hWnd will be reset by UnsubclassWin() so save it first)
608 const HWND hwndOld = GetHwnd();
03d4194d 609 UnsubclassWin();
bc73fe96 610 if ( !::DestroyWindow(hwndOld) )
43b2d5e7 611 {
03d4194d 612 wxLogLastError(wxT("DestroyWindow"));
43b2d5e7 613 }
03d4194d
VZ
614
615 // create and initialize the new one
616 if ( !wxSpinButton::Create(GetParent(), GetId(),
03263ff7 617 rect.GetPosition(), rect.GetSize(),
03d4194d
VZ
618 GetWindowStyle(), GetName()) )
619 return false;
620
03263ff7
VZ
621 // reapply our values to wxSpinButton
622 wxSpinButton::SetValue(GetValue());
03d4194d 623 SetRange(m_min, m_max);
03263ff7
VZ
624
625 // also set the size again with wxSIZE_ALLOW_MINUS_ONE flag: this is
626 // necessary if our original position used -1 for either x or y
627 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
03d4194d
VZ
628
629 // associate it with the buddy control again
630 ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent()));
631 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)GetBuddyHwnd(), 0);
632
633 return true;
634}
635
882a8f40
VZ
636bool wxSpinCtrl::Enable(bool enable)
637{
638 if ( !wxControl::Enable(enable) )
639 {
57f4f925 640 return false;
882a8f40
VZ
641 }
642
0826c4d3 643 MSWEnableHWND(GetBuddyHwnd(), enable);
882a8f40 644
57f4f925 645 return true;
882a8f40
VZ
646}
647
8bf3196d
GRG
648void wxSpinCtrl::SetFocus()
649{
6fe19057 650 ::SetFocus(GetBuddyHwnd());
8bf3196d
GRG
651}
652
74124ea9
VZ
653#if wxUSE_TOOLTIPS
654
655void wxSpinCtrl::DoSetToolTip(wxToolTip *tip)
656{
657 wxSpinButton::DoSetToolTip(tip);
658
659 if ( tip )
660 tip->Add(m_hwndBuddy);
661}
662
663#endif // wxUSE_TOOLTIPS
664
9750fc42 665// ----------------------------------------------------------------------------
d40e9e06 666// events processing and generation
9750fc42
VZ
667// ----------------------------------------------------------------------------
668
d40e9e06 669void wxSpinCtrl::SendSpinUpdate(int value)
9750fc42
VZ
670{
671 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
672 event.SetEventObject(this);
d40e9e06 673 event.SetInt(value);
e816f5c7 674
937013e0 675 (void)HandleWindowEvent(event);
d40e9e06
VZ
676
677 m_oldValue = value;
678}
9750fc42 679
177e38e6
RR
680bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
681 WXWORD pos, WXHWND control)
d40e9e06 682{
177e38e6
RR
683 wxCHECK_MSG( control, false, wxT("scrolling what?") );
684
685 if ( wParam != SB_THUMBPOSITION )
9750fc42 686 {
177e38e6
RR
687 // probable SB_ENDSCROLL - we don't react to it
688 return false;
9750fc42 689 }
177e38e6
RR
690
691 int new_value = (short) pos;
692 if (m_oldValue != new_value)
693 SendSpinUpdate( new_value );
694
695 return TRUE;
696}
697
698bool wxSpinCtrl::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
699{
700 NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam;
701
702 if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control
703 return false;
704
705 *result = 0; // never reject UP and DOWN events
706
707 return TRUE;
9750fc42
VZ
708}
709
177e38e6 710
b782f2e0
VZ
711// ----------------------------------------------------------------------------
712// size calculations
713// ----------------------------------------------------------------------------
714
f68586e5 715wxSize wxSpinCtrl::DoGetBestSize() const
baccb514
VZ
716{
717 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
718 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
719
720 int y;
7a5e53ab 721 wxGetCharSize(GetHWND(), NULL, &y, GetFont());
baccb514
VZ
722 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
723
09f27917
JS
724 // JACS: we should always use the height calculated
725 // from above, because otherwise we'll get a spin control
726 // that's too big. So never use the height calculated
727 // from wxSpinButton::DoGetBestSize().
57f4f925 728
09f27917 729 // if ( sizeBtn.y < y )
baccb514
VZ
730 {
731 // make the text tall enough
732 sizeBtn.y = y;
733 }
734
735 return sizeBtn;
736}
737
b782f2e0
VZ
738void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
739{
baccb514 740 int widthBtn = wxSpinButton::DoGetBestSize().x;
b782f2e0
VZ
741 int widthText = width - widthBtn - MARGIN_BETWEEN;
742 if ( widthText <= 0 )
743 {
9a83f860 744 wxLogDebug(wxT("not enough space for wxSpinCtrl!"));
b782f2e0
VZ
745 }
746
8e44f3ca 747 // 1) The buddy window
7d86a2d4 748 DoMoveSibling(m_hwndBuddy, x, y, widthText, height);
b782f2e0 749
8e44f3ca 750 // 2) The button window
b782f2e0 751 x += widthText + MARGIN_BETWEEN;
7d86a2d4 752 wxSpinButton::DoMoveWindow(x, y, widthBtn, height);
b782f2e0
VZ
753}
754
f6bcfd97
BP
755// get total size of the control
756void wxSpinCtrl::DoGetSize(int *x, int *y) const
757{
758 RECT spinrect, textrect, ctrlrect;
759 GetWindowRect(GetHwnd(), &spinrect);
6fe19057 760 GetWindowRect(GetBuddyHwnd(), &textrect);
f6bcfd97
BP
761 UnionRect(&ctrlrect,&textrect, &spinrect);
762
763 if ( x )
764 *x = ctrlrect.right - ctrlrect.left;
765 if ( y )
766 *y = ctrlrect.bottom - ctrlrect.top;
767}
768
b7527dde
VS
769void wxSpinCtrl::DoGetClientSize(int *x, int *y) const
770{
771 RECT spinrect = wxGetClientRect(GetHwnd());
772 RECT textrect = wxGetClientRect(GetBuddyHwnd());
773 RECT ctrlrect;
774 UnionRect(&ctrlrect,&textrect, &spinrect);
775
776 if ( x )
777 *x = ctrlrect.right - ctrlrect.left;
778 if ( y )
779 *y = ctrlrect.bottom - ctrlrect.top;
780}
781
f6bcfd97
BP
782void wxSpinCtrl::DoGetPosition(int *x, int *y) const
783{
784 // hack: pretend that our HWND is the text control just for a moment
785 WXHWND hWnd = GetHWND();
786 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
787
788 wxSpinButton::DoGetPosition(x, y);
789
790 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
791}
792
74124ea9 793#endif // wxUSE_SPINCTRL