]> git.saurik.com Git - wxWidgets.git/blame - src/msw/spinbutt.cpp
Fix pulsing of bitmaps in focused buttons under Windows 7.
[wxWidgets.git] / src / msw / spinbutt.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/msw/spinbutt.cpp
2bda0e17
KB
3// Purpose: wxSpinButton
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
b782f2e0
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
a23fd0e1 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
27#ifndef WX_PRECOMP
57bd4c60 28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
670f9935 29 #include "wx/app.h"
2bda0e17
KB
30#endif
31
0e528b99
JS
32#if wxUSE_SPINBTN
33
c3cb82e1
JS
34#include "wx/spinbutt.h"
35
2bda0e17 36#include "wx/msw/private.h"
2bda0e17 37
0aed4008
VZ
38#ifndef UDM_SETRANGE32
39 #define UDM_SETRANGE32 (WM_USER+111)
40#endif
41
42#ifndef UDM_SETPOS32
43 #define UDM_SETPOS32 (WM_USER+113)
44 #define UDM_GETPOS32 (WM_USER+114)
45#endif
46
b782f2e0
VZ
47// ============================================================================
48// implementation
49// ============================================================================
50
51// ----------------------------------------------------------------------------
52// wxWin macros
53// ----------------------------------------------------------------------------
54
b782f2e0
VZ
55// ----------------------------------------------------------------------------
56// wxSpinButton
57// ----------------------------------------------------------------------------
58
31528cd3
VZ
59bool wxSpinButton::Create(wxWindow *parent,
60 wxWindowID id,
61 const wxPoint& pos,
62 const wxSize& size,
63 long style,
64 const wxString& name)
2bda0e17 65{
b782f2e0 66 // basic initialization
d95de154 67 m_windowId = (id == wxID_ANY) ? NewControlId() : id;
2bda0e17 68
b782f2e0 69 SetName(name);
2bda0e17 70
b782f2e0
VZ
71 int x = pos.x;
72 int y = pos.y;
73 int width = size.x;
74 int height = size.y;
2bda0e17 75
b782f2e0 76 m_windowStyle = style;
2bda0e17 77
b782f2e0 78 SetParent(parent);
2bda0e17 79
b782f2e0
VZ
80 // get the right size for the control
81 if ( width <= 0 || height <= 0 )
82 {
83 wxSize size = DoGetBestSize();
84 if ( width <= 0 )
85 width = size.x;
86 if ( height <= 0 )
87 height = size.y;
88 }
2bda0e17 89
b782f2e0
VZ
90 if ( x < 0 )
91 x = 0;
92 if ( y < 0 )
93 y = 0;
94
95 // translate the styles
f6bcfd97 96 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | /* WS_CLIPSIBLINGS | */
882a8f40
VZ
97 UDS_NOTHOUSANDS | // never useful, sometimes harmful
98 UDS_SETBUDDYINT; // it doesn't harm if we don't have buddy
b782f2e0 99
b0766406
JS
100 if ( m_windowStyle & wxCLIP_SIBLINGS )
101 wstyle |= WS_CLIPSIBLINGS;
b782f2e0
VZ
102 if ( m_windowStyle & wxSP_HORIZONTAL )
103 wstyle |= UDS_HORZ;
104 if ( m_windowStyle & wxSP_ARROW_KEYS )
105 wstyle |= UDS_ARROWKEYS;
106 if ( m_windowStyle & wxSP_WRAP )
107 wstyle |= UDS_WRAP;
108
109 // create the UpDown control.
110 m_hWnd = (WXHWND)CreateUpDownControl
111 (
112 wstyle,
113 x, y, width, height,
114 GetHwndOf(parent),
115 m_windowId,
116 wxGetInstance(),
117 NULL, // no buddy
118 m_max, m_min,
119 m_min // initial position
120 );
121
122 if ( !m_hWnd )
123 {
f6bcfd97 124 wxLogLastError(wxT("CreateUpDownControl"));
2bda0e17 125
d95de154 126 return false;
b782f2e0 127 }
2bda0e17 128
b782f2e0
VZ
129 if ( parent )
130 {
131 parent->AddChild(this);
132 }
31528cd3 133
b782f2e0 134 SubclassWin(m_hWnd);
2bda0e17 135
170acdc9 136 SetInitialSize(size);
8d2e831b 137
d95de154 138 return true;
2bda0e17
KB
139}
140
a23fd0e1 141wxSpinButton::~wxSpinButton()
2bda0e17
KB
142{
143}
144
b782f2e0
VZ
145// ----------------------------------------------------------------------------
146// size calculation
147// ----------------------------------------------------------------------------
148
f68586e5 149wxSize wxSpinButton::DoGetBestSize() const
b782f2e0 150{
5aaabb37 151 return GetBestSpinnerSize( (GetWindowStyle() & wxSP_VERTICAL) != 0 );
b782f2e0
VZ
152}
153
154// ----------------------------------------------------------------------------
2bda0e17 155// Attributes
b782f2e0 156// ----------------------------------------------------------------------------
2bda0e17 157
a23fd0e1 158int wxSpinButton::GetValue() const
2bda0e17 159{
ad8ddd13 160 int n;
3d9fe7b2 161#ifdef UDM_GETPOS32
5625d0d4 162 if ( wxApp::GetComCtl32Version() >= 580 )
3d9fe7b2
VZ
163 {
164 // use the full 32 bit range if available
ad8ddd13 165 n = ::SendMessage(GetHwnd(), UDM_GETPOS32, 0, 0);
3d9fe7b2 166 }
419c6f40 167 else
3d9fe7b2 168#endif // UDM_GETPOS32
419c6f40
WS
169 {
170 // we're limited to 16 bit
171 n = (short)LOWORD(::SendMessage(GetHwnd(), UDM_GETPOS, 0, 0));
172 }
3d9fe7b2 173
ad8ddd13
RR
174 if (n < m_min) n = m_min;
175 if (n > m_max) n = m_max;
419c6f40 176
ad8ddd13 177 return n;
2bda0e17
KB
178}
179
debe6624 180void wxSpinButton::SetValue(int val)
2bda0e17 181{
3d9fe7b2
VZ
182 // wxSpinButtonBase::SetValue(val); -- no, it is pure virtual
183
184#ifdef UDM_SETPOS32
5625d0d4 185 if ( wxApp::GetComCtl32Version() >= 580 )
3d9fe7b2
VZ
186 {
187 // use the full 32 bit range if available
188 ::SendMessage(GetHwnd(), UDM_SETPOS32, 0, val);
189 }
190 else // we're limited to 16 bit
191#endif // UDM_SETPOS32
192 {
193 ::SendMessage(GetHwnd(), UDM_SETPOS, 0, MAKELONG((short) val, 0));
194 }
2bda0e17
KB
195}
196
1e8dba5e 197void wxSpinButton::NormalizeValue()
f4322df6 198{
1e8dba5e
RR
199 SetValue( GetValue() );
200}
201
debe6624 202void wxSpinButton::SetRange(int minVal, int maxVal)
2bda0e17 203{
93232499
VZ
204 const bool hadRange = m_min < m_max;
205
31528cd3 206 wxSpinButtonBase::SetRange(minVal, maxVal);
3d9fe7b2
VZ
207
208#ifdef UDM_SETRANGE32
2a1f999f 209 if ( wxApp::GetComCtl32Version() >= 471 )
3d9fe7b2
VZ
210 {
211 // use the full 32 bit range if available
212 ::SendMessage(GetHwnd(), UDM_SETRANGE32, minVal, maxVal);
213 }
214 else // we're limited to 16 bit
215#endif // UDM_SETRANGE32
216 {
217 ::SendMessage(GetHwnd(), UDM_SETRANGE, 0,
218 (LPARAM) MAKELONG((short)maxVal, (short)minVal));
219 }
93232499
VZ
220
221 // the current value might be out of the new range, force it to be in it
222 NormalizeValue();
223
224 // if range was valid but becomes degenerated (min == max) now or vice
225 // versa then the spin buttons are automatically disabled/enabled back
226 // but don't update themselves for some reason, so do it manually
227 if ( hadRange != (m_min < m_max) )
228 {
229 // update the visual state of the button
230 Refresh();
231 }
2bda0e17
KB
232}
233
33ac7e6f 234bool wxSpinButton::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
63420bcc 235 WXWORD WXUNUSED(pos), WXHWND control)
2bda0e17 236{
637b7e4f 237 wxCHECK_MSG( control, false, wxT("scrolling what?") );
2bda0e17 238
0655ad29 239 if ( wParam != SB_THUMBPOSITION )
a23fd0e1 240 {
0655ad29 241 // probable SB_ENDSCROLL - we don't react to it
d95de154 242 return false;
0655ad29 243 }
2bda0e17 244
0655ad29 245 wxSpinEvent event(wxEVT_SCROLL_THUMBTRACK, m_windowId);
63420bcc
VZ
246 // We can't use 16 bit position provided in this message for spin buttons
247 // using 32 bit range.
248 event.SetPosition(GetValue());
0655ad29 249 event.SetEventObject(this);
2bda0e17 250
937013e0 251 return HandleWindowEvent(event);
0655ad29 252}
2bda0e17 253
33ac7e6f 254bool wxSpinButton::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
0655ad29 255{
be519ffb 256 NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam;
2bda0e17 257
f6bcfd97 258 if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control
d95de154 259 return false;
f6bcfd97 260
0655ad29
VZ
261 wxSpinEvent event(lpnmud->iDelta > 0 ? wxEVT_SCROLL_LINEUP
262 : wxEVT_SCROLL_LINEDOWN,
263 m_windowId);
264 event.SetPosition(lpnmud->iPos + lpnmud->iDelta);
265 event.SetEventObject(this);
2bda0e17 266
937013e0 267 bool processed = HandleWindowEvent(event);
2bda0e17 268
0655ad29 269 *result = event.IsAllowed() ? 0 : 1;
2bda0e17 270
0655ad29 271 return processed;
2bda0e17
KB
272}
273
33ac7e6f 274bool wxSpinButton::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD WXUNUSED(id))
2bda0e17 275{
a23fd0e1 276 // No command messages
d95de154 277 return false;
2bda0e17
KB
278}
279
d95de154 280#endif // wxUSE_SPINBTN