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