]> git.saurik.com Git - wxWidgets.git/blob - src/msw/spinbutt.cpp
minor changes as per RN comments - add some comments and reorder a switch stmt.
[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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #endif
30
31 #if wxUSE_SPINBTN
32
33 #include "wx/spinbutt.h"
34
35 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
36
37 #include "wx/msw/private.h"
38 #include "wx/msw/wrapcctl.h"
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 // ----------------------------------------------------------------------------
45 // wxWin macros
46 // ----------------------------------------------------------------------------
47
48
49 #if wxUSE_EXTENDED_RTTI
50 WX_DEFINE_FLAGS( wxSpinButtonStyle )
51
52 wxBEGIN_FLAGS( wxSpinButtonStyle )
53 // new style border flags, we put them first to
54 // use them for streaming out
55 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
56 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
57 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
58 wxFLAGS_MEMBER(wxBORDER_RAISED)
59 wxFLAGS_MEMBER(wxBORDER_STATIC)
60 wxFLAGS_MEMBER(wxBORDER_NONE)
61
62 // old style border flags
63 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
64 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
65 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
66 wxFLAGS_MEMBER(wxRAISED_BORDER)
67 wxFLAGS_MEMBER(wxSTATIC_BORDER)
68 wxFLAGS_MEMBER(wxBORDER)
69
70 // standard window styles
71 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
72 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
73 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
74 wxFLAGS_MEMBER(wxWANTS_CHARS)
75 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
76 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
77 wxFLAGS_MEMBER(wxVSCROLL)
78 wxFLAGS_MEMBER(wxHSCROLL)
79
80 wxFLAGS_MEMBER(wxSP_HORIZONTAL)
81 wxFLAGS_MEMBER(wxSP_VERTICAL)
82 wxFLAGS_MEMBER(wxSP_ARROW_KEYS)
83 wxFLAGS_MEMBER(wxSP_WRAP)
84
85 wxEND_FLAGS( wxSpinButtonStyle )
86
87 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinButton, wxControl,"wx/spinbut.h")
88
89 wxBEGIN_PROPERTIES_TABLE(wxSpinButton)
90 wxEVENT_RANGE_PROPERTY( Spin , wxEVT_SCROLL_TOP , wxEVT_SCROLL_ENDSCROLL , wxSpinEvent )
91
92 wxPROPERTY( Value , int , SetValue, GetValue, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
93 wxPROPERTY( Min , int , SetMin, GetMin, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
94 wxPROPERTY( Max , int , SetMax, GetMax, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
95 wxPROPERTY_FLAGS( WindowStyle , wxSpinButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
96 wxEND_PROPERTIES_TABLE()
97
98 wxBEGIN_HANDLERS_TABLE(wxSpinButton)
99 wxEND_HANDLERS_TABLE()
100
101 wxCONSTRUCTOR_5( wxSpinButton , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
102 #else
103 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl)
104 #endif
105
106
107
108 // ----------------------------------------------------------------------------
109 // wxSpinButton
110 // ----------------------------------------------------------------------------
111
112 bool wxSpinButton::Create(wxWindow *parent,
113 wxWindowID id,
114 const wxPoint& pos,
115 const wxSize& size,
116 long style,
117 const wxString& name)
118 {
119 // basic initialization
120 m_windowId = (id == wxID_ANY) ? NewControlId() : id;
121
122 SetName(name);
123
124 int x = pos.x;
125 int y = pos.y;
126 int width = size.x;
127 int height = size.y;
128
129 m_windowStyle = style;
130
131 SetParent(parent);
132
133 // get the right size for the control
134 if ( width <= 0 || height <= 0 )
135 {
136 wxSize size = DoGetBestSize();
137 if ( width <= 0 )
138 width = size.x;
139 if ( height <= 0 )
140 height = size.y;
141 }
142
143 if ( x < 0 )
144 x = 0;
145 if ( y < 0 )
146 y = 0;
147
148 // translate the styles
149 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | /* WS_CLIPSIBLINGS | */
150 UDS_NOTHOUSANDS | // never useful, sometimes harmful
151 UDS_SETBUDDYINT; // it doesn't harm if we don't have buddy
152
153 if ( m_windowStyle & wxCLIP_SIBLINGS )
154 wstyle |= WS_CLIPSIBLINGS;
155 if ( m_windowStyle & wxSP_HORIZONTAL )
156 wstyle |= UDS_HORZ;
157 if ( m_windowStyle & wxSP_ARROW_KEYS )
158 wstyle |= UDS_ARROWKEYS;
159 if ( m_windowStyle & wxSP_WRAP )
160 wstyle |= UDS_WRAP;
161
162 // create the UpDown control.
163 m_hWnd = (WXHWND)CreateUpDownControl
164 (
165 wstyle,
166 x, y, width, height,
167 GetHwndOf(parent),
168 m_windowId,
169 wxGetInstance(),
170 NULL, // no buddy
171 m_max, m_min,
172 m_min // initial position
173 );
174
175 if ( !m_hWnd )
176 {
177 wxLogLastError(wxT("CreateUpDownControl"));
178
179 return false;
180 }
181
182 if ( parent )
183 {
184 parent->AddChild(this);
185 }
186
187 SubclassWin(m_hWnd);
188
189 SetBestSize(size);
190
191 return true;
192 }
193
194 wxSpinButton::~wxSpinButton()
195 {
196 }
197
198 // ----------------------------------------------------------------------------
199 // size calculation
200 // ----------------------------------------------------------------------------
201
202 wxSize wxSpinButton::DoGetBestSize() const
203 {
204 return GetBestSpinnerSize( (GetWindowStyle() & wxSP_VERTICAL) != 0 );
205 }
206
207 // ----------------------------------------------------------------------------
208 // Attributes
209 // ----------------------------------------------------------------------------
210
211 int wxSpinButton::GetValue() const
212 {
213 int n;
214 #ifdef UDM_GETPOS32
215 if ( wxTheApp->GetComCtl32Version() >= 580 )
216 {
217 // use the full 32 bit range if available
218 n = ::SendMessage(GetHwnd(), UDM_GETPOS32, 0, 0);
219 }
220 else
221 #endif // UDM_GETPOS32
222 {
223 // we're limited to 16 bit
224 n = (short)LOWORD(::SendMessage(GetHwnd(), UDM_GETPOS, 0, 0));
225 }
226
227 if (n < m_min) n = m_min;
228 if (n > m_max) n = m_max;
229
230 return n;
231 }
232
233 void wxSpinButton::SetValue(int val)
234 {
235 // wxSpinButtonBase::SetValue(val); -- no, it is pure virtual
236
237 #ifdef UDM_SETPOS32
238 if ( wxTheApp->GetComCtl32Version() >= 580 )
239 {
240 // use the full 32 bit range if available
241 ::SendMessage(GetHwnd(), UDM_SETPOS32, 0, val);
242 }
243 else // we're limited to 16 bit
244 #endif // UDM_SETPOS32
245 {
246 ::SendMessage(GetHwnd(), UDM_SETPOS, 0, MAKELONG((short) val, 0));
247 }
248 }
249
250 void wxSpinButton::SetRange(int minVal, int maxVal)
251 {
252 wxSpinButtonBase::SetRange(minVal, maxVal);
253
254 #ifdef UDM_SETRANGE32
255 if ( wxApp::GetComCtl32Version() >= 471 )
256 {
257 // use the full 32 bit range if available
258 ::SendMessage(GetHwnd(), UDM_SETRANGE32, minVal, maxVal);
259 }
260 else // we're limited to 16 bit
261 #endif // UDM_SETRANGE32
262 {
263 ::SendMessage(GetHwnd(), UDM_SETRANGE, 0,
264 (LPARAM) MAKELONG((short)maxVal, (short)minVal));
265 }
266 }
267
268 bool wxSpinButton::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
269 WXWORD pos, WXHWND control)
270 {
271 wxCHECK_MSG( control, false, wxT("scrolling what?") )
272
273 if ( wParam != SB_THUMBPOSITION )
274 {
275 // probable SB_ENDSCROLL - we don't react to it
276 return false;
277 }
278
279 wxSpinEvent event(wxEVT_SCROLL_THUMBTRACK, m_windowId);
280 event.SetPosition((short)pos); // cast is important for negative values!
281 event.SetEventObject(this);
282
283 return GetEventHandler()->ProcessEvent(event);
284 }
285
286 bool wxSpinButton::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
287 {
288 NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam;
289
290 if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control
291 return false;
292
293 wxSpinEvent event(lpnmud->iDelta > 0 ? wxEVT_SCROLL_LINEUP
294 : wxEVT_SCROLL_LINEDOWN,
295 m_windowId);
296 event.SetPosition(lpnmud->iPos + lpnmud->iDelta);
297 event.SetEventObject(this);
298
299 bool processed = GetEventHandler()->ProcessEvent(event);
300
301 *result = event.IsAllowed() ? 0 : 1;
302
303 return processed;
304 }
305
306 bool wxSpinButton::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD WXUNUSED(id))
307 {
308 // No command messages
309 return false;
310 }
311
312 #endif // wxUSE_SPINBTN