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