]> git.saurik.com Git - wxWidgets.git/blame - src/msw/spinbutt.cpp
generate key events for Space/Enter in addition to the activate events, as wxMSW...
[wxWidgets.git] / src / msw / spinbutt.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
2ab30ebf 2// Name: msw/spinbutt.cpp
2bda0e17
KB
3// Purpose: wxSpinButton
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
a23fd0e1 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
b782f2e0
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
a23fd0e1 21 #pragma implementation "spinbutt.h"
3c1a88d8 22 #pragma implementation "spinbutbase.h"
2bda0e17
KB
23#endif
24
25// For compilers that support precompilation, includes "wx.h".
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
a23fd0e1 29 #pragma hdrstop
2bda0e17
KB
30#endif
31
32#ifndef WX_PRECOMP
3181cff4 33#include "wx/app.h"
2bda0e17
KB
34#endif
35
0e528b99
JS
36#if wxUSE_SPINBTN
37
c3cb82e1
JS
38#include "wx/spinbutt.h"
39
2fa7c206
JS
40IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
41
2ab30ebf
VZ
42// Can't resolve reference to CreateUpDownControl in
43// TWIN32, but could probably use normal CreateWindow instead.
57c208c5 44#if defined(__WIN95__) && !defined(__TWIN32__)
2bda0e17 45
2bda0e17
KB
46#include "wx/msw/private.h"
47
ae090fdb 48#if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
a23fd0e1 49 #include <commctrl.h>
2bda0e17
KB
50#endif
51
b782f2e0
VZ
52// ============================================================================
53// implementation
54// ============================================================================
55
56// ----------------------------------------------------------------------------
57// wxWin macros
58// ----------------------------------------------------------------------------
59
9750fc42 60IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl)
2bda0e17 61
b782f2e0
VZ
62// ----------------------------------------------------------------------------
63// wxSpinButton
64// ----------------------------------------------------------------------------
65
31528cd3
VZ
66bool wxSpinButton::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxString& name)
2bda0e17 72{
b782f2e0
VZ
73 // basic initialization
74 InitBase();
2bda0e17 75
b782f2e0 76 m_windowId = (id == -1) ? NewControlId() : id;
2bda0e17 77
b782f2e0
VZ
78 m_backgroundColour = parent->GetBackgroundColour() ;
79 m_foregroundColour = parent->GetForegroundColour() ;
2bda0e17 80
b782f2e0 81 SetName(name);
2bda0e17 82
b782f2e0
VZ
83 int x = pos.x;
84 int y = pos.y;
85 int width = size.x;
86 int height = size.y;
2bda0e17 87
b782f2e0 88 m_windowStyle = style;
2bda0e17 89
b782f2e0 90 SetParent(parent);
2bda0e17 91
b782f2e0
VZ
92 // get the right size for the control
93 if ( width <= 0 || height <= 0 )
94 {
95 wxSize size = DoGetBestSize();
96 if ( width <= 0 )
97 width = size.x;
98 if ( height <= 0 )
99 height = size.y;
100 }
2bda0e17 101
b782f2e0
VZ
102 if ( x < 0 )
103 x = 0;
104 if ( y < 0 )
105 y = 0;
106
107 // translate the styles
f6bcfd97 108 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | /* WS_CLIPSIBLINGS | */
882a8f40
VZ
109 UDS_NOTHOUSANDS | // never useful, sometimes harmful
110 UDS_SETBUDDYINT; // it doesn't harm if we don't have buddy
b782f2e0 111
b0766406
JS
112 if ( m_windowStyle & wxCLIP_SIBLINGS )
113 wstyle |= WS_CLIPSIBLINGS;
b782f2e0
VZ
114 if ( m_windowStyle & wxSP_HORIZONTAL )
115 wstyle |= UDS_HORZ;
116 if ( m_windowStyle & wxSP_ARROW_KEYS )
117 wstyle |= UDS_ARROWKEYS;
118 if ( m_windowStyle & wxSP_WRAP )
119 wstyle |= UDS_WRAP;
120
121 // create the UpDown control.
122 m_hWnd = (WXHWND)CreateUpDownControl
123 (
124 wstyle,
125 x, y, width, height,
126 GetHwndOf(parent),
127 m_windowId,
128 wxGetInstance(),
129 NULL, // no buddy
130 m_max, m_min,
131 m_min // initial position
132 );
133
134 if ( !m_hWnd )
135 {
f6bcfd97 136 wxLogLastError(wxT("CreateUpDownControl"));
2bda0e17 137
b782f2e0
VZ
138 return FALSE;
139 }
2bda0e17 140
b782f2e0
VZ
141 if ( parent )
142 {
143 parent->AddChild(this);
144 }
31528cd3 145
b782f2e0 146 SubclassWin(m_hWnd);
2bda0e17 147
b782f2e0 148 return TRUE;
2bda0e17
KB
149}
150
a23fd0e1 151wxSpinButton::~wxSpinButton()
2bda0e17
KB
152{
153}
154
b782f2e0
VZ
155// ----------------------------------------------------------------------------
156// size calculation
157// ----------------------------------------------------------------------------
158
f68586e5 159wxSize wxSpinButton::DoGetBestSize() const
b782f2e0
VZ
160{
161 if ( (GetWindowStyle() & wxSP_VERTICAL) != 0 )
162 {
163 // vertical control
164 return wxSize(GetSystemMetrics(SM_CXVSCROLL),
165 2*GetSystemMetrics(SM_CYVSCROLL));
166 }
167 else
168 {
169 // horizontal control
170 return wxSize(2*GetSystemMetrics(SM_CXHSCROLL),
171 GetSystemMetrics(SM_CYHSCROLL));
172 }
173}
174
175// ----------------------------------------------------------------------------
2bda0e17 176// Attributes
b782f2e0 177// ----------------------------------------------------------------------------
2bda0e17 178
a23fd0e1 179int wxSpinButton::GetValue() const
2bda0e17 180{
3d9fe7b2
VZ
181#ifdef UDM_GETPOS32
182 if ( wxTheApp->GetComCtl32Version() >= 580 )
183 {
184 // use the full 32 bit range if available
185 return ::SendMessage(GetHwnd(), UDM_GETPOS32, 0, 0);
186 }
187#endif // UDM_GETPOS32
188
189 // we're limited to 16 bit
0655ad29 190 return (short)LOWORD(::SendMessage(GetHwnd(), UDM_GETPOS, 0, 0));
2bda0e17
KB
191}
192
debe6624 193void wxSpinButton::SetValue(int val)
2bda0e17 194{
3d9fe7b2
VZ
195 // wxSpinButtonBase::SetValue(val); -- no, it is pure virtual
196
197#ifdef UDM_SETPOS32
198 if ( wxTheApp->GetComCtl32Version() >= 580 )
199 {
200 // use the full 32 bit range if available
201 ::SendMessage(GetHwnd(), UDM_SETPOS32, 0, val);
202 }
203 else // we're limited to 16 bit
204#endif // UDM_SETPOS32
205 {
206 ::SendMessage(GetHwnd(), UDM_SETPOS, 0, MAKELONG((short) val, 0));
207 }
2bda0e17
KB
208}
209
debe6624 210void wxSpinButton::SetRange(int minVal, int maxVal)
2bda0e17 211{
31528cd3 212 wxSpinButtonBase::SetRange(minVal, maxVal);
3d9fe7b2
VZ
213
214#ifdef UDM_SETRANGE32
215 if ( wxTheApp->GetComCtl32Version() >= 471 )
216 {
217 // use the full 32 bit range if available
218 ::SendMessage(GetHwnd(), UDM_SETRANGE32, minVal, maxVal);
219 }
220 else // we're limited to 16 bit
221#endif // UDM_SETRANGE32
222 {
223 ::SendMessage(GetHwnd(), UDM_SETRANGE, 0,
224 (LPARAM) MAKELONG((short)maxVal, (short)minVal));
225 }
2bda0e17
KB
226}
227
33ac7e6f 228bool wxSpinButton::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
a23fd0e1 229 WXWORD pos, WXHWND control)
2bda0e17 230{
223d09f6 231 wxCHECK_MSG( control, FALSE, wxT("scrolling what?") )
2bda0e17 232
0655ad29 233 if ( wParam != SB_THUMBPOSITION )
a23fd0e1 234 {
0655ad29
VZ
235 // probable SB_ENDSCROLL - we don't react to it
236 return FALSE;
237 }
2bda0e17 238
0655ad29
VZ
239 wxSpinEvent event(wxEVT_SCROLL_THUMBTRACK, m_windowId);
240 event.SetPosition((short)pos); // cast is important for negative values!
241 event.SetEventObject(this);
2bda0e17 242
0655ad29
VZ
243 return GetEventHandler()->ProcessEvent(event);
244}
2bda0e17 245
33ac7e6f 246bool wxSpinButton::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
0655ad29 247{
be519ffb 248 NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam;
2bda0e17 249
f6bcfd97
BP
250 if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control
251 return FALSE;
252
0655ad29
VZ
253 wxSpinEvent event(lpnmud->iDelta > 0 ? wxEVT_SCROLL_LINEUP
254 : wxEVT_SCROLL_LINEDOWN,
255 m_windowId);
256 event.SetPosition(lpnmud->iPos + lpnmud->iDelta);
257 event.SetEventObject(this);
2bda0e17 258
0655ad29 259 bool processed = GetEventHandler()->ProcessEvent(event);
2bda0e17 260
0655ad29 261 *result = event.IsAllowed() ? 0 : 1;
2bda0e17 262
0655ad29 263 return processed;
2bda0e17
KB
264}
265
33ac7e6f 266bool wxSpinButton::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD WXUNUSED(id))
2bda0e17 267{
a23fd0e1
VZ
268 // No command messages
269 return FALSE;
2bda0e17
KB
270}
271
a23fd0e1 272#endif // __WIN95__
0e528b99
JS
273
274#endif
275 // wxUSE_SPINCTN
276