]> git.saurik.com Git - wxWidgets.git/blob - src/msw/spinbutt.cpp
assert in GetNextItem() fixed (?)
[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 #if defined(__WIN95__) && !defined(__TWIN32__)
42
43 #include "wx/spinbutt.h"
44 #include "wx/msw/private.h"
45
46 #if !defined(__GNUWIN32__) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
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 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent);
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 |
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 & wxSP_HORIZONTAL )
112 wstyle |= UDS_HORZ;
113 if ( m_windowStyle & wxSP_ARROW_KEYS )
114 wstyle |= UDS_ARROWKEYS;
115 if ( m_windowStyle & wxSP_WRAP )
116 wstyle |= UDS_WRAP;
117
118 // create the UpDown control.
119 m_hWnd = (WXHWND)CreateUpDownControl
120 (
121 wstyle,
122 x, y, width, height,
123 GetHwndOf(parent),
124 m_windowId,
125 wxGetInstance(),
126 NULL, // no buddy
127 m_max, m_min,
128 m_min // initial position
129 );
130
131 if ( !m_hWnd )
132 {
133 wxLogLastError("CreateUpDownControl");
134
135 return FALSE;
136 }
137
138 if ( parent )
139 {
140 parent->AddChild(this);
141 }
142
143 SubclassWin(m_hWnd);
144
145 return TRUE;
146 }
147
148 wxSpinButton::~wxSpinButton()
149 {
150 }
151
152 // ----------------------------------------------------------------------------
153 // size calculation
154 // ----------------------------------------------------------------------------
155
156 wxSize wxSpinButton::DoGetBestSize() const
157 {
158 if ( (GetWindowStyle() & wxSP_VERTICAL) != 0 )
159 {
160 // vertical control
161 return wxSize(GetSystemMetrics(SM_CXVSCROLL),
162 2*GetSystemMetrics(SM_CYVSCROLL));
163 }
164 else
165 {
166 // horizontal control
167 return wxSize(2*GetSystemMetrics(SM_CXHSCROLL),
168 GetSystemMetrics(SM_CYHSCROLL));
169 }
170 }
171
172 // ----------------------------------------------------------------------------
173 // Attributes
174 // ----------------------------------------------------------------------------
175
176 int wxSpinButton::GetValue() const
177 {
178 return (short)LOWORD(::SendMessage(GetHwnd(), UDM_GETPOS, 0, 0));
179 }
180
181 void wxSpinButton::SetValue(int val)
182 {
183 ::SendMessage(GetHwnd(), UDM_SETPOS, 0, (LPARAM) MAKELONG((short) val, 0));
184 }
185
186 void wxSpinButton::SetRange(int minVal, int maxVal)
187 {
188 wxSpinButtonBase::SetRange(minVal, maxVal);
189 ::SendMessage(GetHwnd(), UDM_SETRANGE, 0,
190 (LPARAM) MAKELONG((short)maxVal, (short)minVal));
191 }
192
193 bool wxSpinButton::MSWOnScroll(int orientation, WXWORD wParam,
194 WXWORD pos, WXHWND control)
195 {
196 wxCHECK_MSG( control, FALSE, wxT("scrolling what?") )
197
198 if ( wParam != SB_THUMBPOSITION )
199 {
200 // probable SB_ENDSCROLL - we don't react to it
201 return FALSE;
202 }
203
204 wxSpinEvent event(wxEVT_SCROLL_THUMBTRACK, m_windowId);
205 event.SetPosition((short)pos); // cast is important for negative values!
206 event.SetEventObject(this);
207
208 return GetEventHandler()->ProcessEvent(event);
209 }
210
211 bool wxSpinButton::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
212 {
213 #ifndef __GNUWIN32__
214 #if defined(__BORLANDC__) || defined(__WATCOMC__)
215 LPNM_UPDOWN lpnmud = (LPNM_UPDOWN)lParam;
216 #elif defined(__VISUALC__) && (__VISUALC__ >= 1000) && (__VISUALC__ < 1020)
217 LPNM_UPDOWN lpnmud = (LPNM_UPDOWN)lParam;
218 #else
219 LPNMUPDOWN lpnmud = (LPNMUPDOWN)lParam;
220 #endif
221
222 wxSpinEvent event(lpnmud->iDelta > 0 ? wxEVT_SCROLL_LINEUP
223 : wxEVT_SCROLL_LINEDOWN,
224 m_windowId);
225 event.SetPosition(lpnmud->iPos + lpnmud->iDelta);
226 event.SetEventObject(this);
227
228 bool processed = GetEventHandler()->ProcessEvent(event);
229
230 *result = event.IsAllowed() ? 0 : 1;
231
232 return processed;
233 #else // GnuWin32
234 return FALSE;
235 #endif
236 }
237
238 bool wxSpinButton::MSWCommand(WXUINT cmd, WXWORD id)
239 {
240 // No command messages
241 return FALSE;
242 }
243
244 #endif // __WIN95__
245
246 #endif
247 // wxUSE_SPINCTN
248