]> git.saurik.com Git - wxWidgets.git/blob - src/msw/spinbutt.cpp
826c4a5e1b8bf8e520cb951c38e6aac4eb2dc560
[wxWidgets.git] / src / msw / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/wx.h"
34 #endif
35
36 // Can't resolve reference to CreateUpDownControl in
37 // TWIN32, but could probably use normal CreateWindow instead.
38
39 #if wxUSE_SPINBTN
40
41 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
42
43 #if defined(__WIN95__) && !defined(__TWIN32__)
44
45 #include "wx/spinbutt.h"
46 #include "wx/msw/private.h"
47
48 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
49 #include <commctrl.h>
50 #endif
51
52 // ============================================================================
53 // implementation
54 // ============================================================================
55
56 // ----------------------------------------------------------------------------
57 // wxWin macros
58 // ----------------------------------------------------------------------------
59
60 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl)
61
62 // ----------------------------------------------------------------------------
63 // wxSpinButton
64 // ----------------------------------------------------------------------------
65
66 bool wxSpinButton::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxString& name)
72 {
73 // basic initialization
74 InitBase();
75
76 m_windowId = (id == -1) ? NewControlId() : id;
77
78 m_backgroundColour = parent->GetBackgroundColour() ;
79 m_foregroundColour = parent->GetForegroundColour() ;
80
81 SetName(name);
82
83 int x = pos.x;
84 int y = pos.y;
85 int width = size.x;
86 int height = size.y;
87
88 m_windowStyle = style;
89
90 SetParent(parent);
91
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 }
101
102 if ( x < 0 )
103 x = 0;
104 if ( y < 0 )
105 y = 0;
106
107 // translate the styles
108 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
109 UDS_NOTHOUSANDS | // never useful, sometimes harmful
110 UDS_SETBUDDYINT; // it doesn't harm if we don't have buddy
111
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("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 return (short)LOWORD(::SendMessage(GetHwnd(), UDM_GETPOS, 0, 0));
180 }
181
182 void wxSpinButton::SetValue(int val)
183 {
184 ::SendMessage(GetHwnd(), UDM_SETPOS, 0, (LPARAM) MAKELONG((short) val, 0));
185 }
186
187 void wxSpinButton::SetRange(int minVal, int maxVal)
188 {
189 wxSpinButtonBase::SetRange(minVal, maxVal);
190 ::SendMessage(GetHwnd(), UDM_SETRANGE, 0,
191 (LPARAM) MAKELONG((short)maxVal, (short)minVal));
192 }
193
194 bool wxSpinButton::MSWOnScroll(int orientation, WXWORD wParam,
195 WXWORD pos, WXHWND control)
196 {
197 wxCHECK_MSG( control, FALSE, wxT("scrolling what?") )
198
199 if ( wParam != SB_THUMBPOSITION )
200 {
201 // probable SB_ENDSCROLL - we don't react to it
202 return FALSE;
203 }
204
205 wxSpinEvent event(wxEVT_SCROLL_THUMBTRACK, m_windowId);
206 event.SetPosition((short)pos); // cast is important for negative values!
207 event.SetEventObject(this);
208
209 return GetEventHandler()->ProcessEvent(event);
210 }
211
212 bool wxSpinButton::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
213 {
214 #ifndef __GNUWIN32__
215 #if defined(__BORLANDC__) || defined(__WATCOMC__)
216 LPNM_UPDOWN lpnmud = (LPNM_UPDOWN)lParam;
217 #elif defined(__VISUALC__) && (__VISUALC__ >= 1000) && (__VISUALC__ < 1020)
218 LPNM_UPDOWN lpnmud = (LPNM_UPDOWN)lParam;
219 #else
220 LPNMUPDOWN lpnmud = (LPNMUPDOWN)lParam;
221 #endif
222
223 wxSpinEvent event(lpnmud->iDelta > 0 ? wxEVT_SCROLL_LINEUP
224 : wxEVT_SCROLL_LINEDOWN,
225 m_windowId);
226 event.SetPosition(lpnmud->iPos + lpnmud->iDelta);
227 event.SetEventObject(this);
228
229 bool processed = GetEventHandler()->ProcessEvent(event);
230
231 *result = event.IsAllowed() ? 0 : 1;
232
233 return processed;
234 #else // GnuWin32
235 return FALSE;
236 #endif
237 }
238
239 bool wxSpinButton::MSWCommand(WXUINT cmd, WXWORD id)
240 {
241 // No command messages
242 return FALSE;
243 }
244
245 #endif // __WIN95__
246
247 #endif
248 // wxUSE_SPINCTN
249