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