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