]> git.saurik.com Git - wxWidgets.git/blob - src/msw/spinctrl.cpp
ANSI 'C' fix (decs in "for" clauses no longer have any scope outside clause itself)
[wxWidgets.git] / src / msw / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for Win32
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 22.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "spinctrlbase.h"
18 #pragma implementation "spinctrl.h"
19 #endif
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
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 #if wxUSE_SPINCTRL
37
38 #if defined(__WIN95__)
39
40 #include "wx/spinctrl.h"
41 #include "wx/msw/private.h"
42
43 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
44 #include <commctrl.h>
45 #endif
46
47 #include <limits.h> // for INT_MIN
48
49 // ----------------------------------------------------------------------------
50 // macros
51 // ----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
54
55 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
56 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange)
57 END_EVENT_TABLE()
58
59 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
60
61 // ----------------------------------------------------------------------------
62 // constants
63 // ----------------------------------------------------------------------------
64
65 // the margin between the up-down control and its buddy (can be arbitrary,
66 // choose what you like - or may be decide during run-time depending on the
67 // font size?)
68 static const int MARGIN_BETWEEN = 1;
69
70 // ============================================================================
71 // implementation
72 // ============================================================================
73
74 wxArraySpins wxSpinCtrl::ms_allSpins;
75
76 // ----------------------------------------------------------------------------
77 // wnd proc for the buddy text ctrl
78 // ----------------------------------------------------------------------------
79
80 LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
81 UINT message,
82 WPARAM wParam,
83 LPARAM lParam)
84 {
85 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA);
86
87 // forward some messages (the key ones only so far) to the spin ctrl
88 switch ( message )
89 {
90 case WM_CHAR:
91 case WM_DEADCHAR:
92 case WM_KEYUP:
93 case WM_KEYDOWN:
94 spin->MSWWindowProc(message, wParam, lParam);
95 break;
96 }
97
98 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
99 hwnd, message, wParam, lParam);
100 }
101
102 /* static */
103 wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
104 {
105 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong((HWND)hwndBuddy,
106 GWL_USERDATA);
107
108 int i = ms_allSpins.Index(spin);
109
110 if ( i == wxNOT_FOUND )
111 return NULL;
112
113 // sanity check
114 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
115 _T("wxSpinCtrl has incorrect buddy HWND!") );
116
117 return spin;
118 }
119
120 // process a WM_COMMAND generated by the buddy text control
121 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
122 {
123 if ( cmd == EN_CHANGE )
124 {
125 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
126 event.SetEventObject(this);
127 event.SetInt(GetValue());
128 GetEventHandler()->ProcessEvent(event);
129
130 return TRUE;
131 }
132
133 // not processed
134 return FALSE;
135 }
136
137 // ----------------------------------------------------------------------------
138 // construction
139 // ----------------------------------------------------------------------------
140
141 bool wxSpinCtrl::Create(wxWindow *parent,
142 wxWindowID id,
143 const wxString& value,
144 const wxPoint& pos,
145 const wxSize& size,
146 long style,
147 int min, int max, int initial,
148 const wxString& name)
149 {
150 // before using DoGetBestSize(), have to set style to let the base class
151 // know whether this is a horizontal or vertical control (we're always
152 // vertical)
153 style |= wxSP_VERTICAL;
154 SetWindowStyle(style);
155
156 // calculate the sizes: the size given is the toal size for both controls
157 // and we need to fit them both in the given width (height is the same)
158 wxSize sizeText(size), sizeBtn(size);
159 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
160 if ( sizeText.x <= 0 )
161 {
162 // DEFAULT_ITEM_WIDTH is the default width for the text control
163 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
164 }
165
166 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
167 if ( sizeText.x <= 0 )
168 {
169 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
170 }
171
172 wxPoint posBtn(pos);
173 posBtn.x += sizeText.x + MARGIN_BETWEEN;
174
175 // create the spin button
176 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
177 {
178 return FALSE;
179 }
180
181 SetRange(min, max);
182 SetValue(initial);
183
184 // create the text window
185 m_hwndBuddy = (WXHWND)::CreateWindowEx
186 (
187 WS_EX_CLIENTEDGE, // sunken border
188 _T("EDIT"), // window class
189 NULL, // no window title
190 WS_CHILD | WS_BORDER /* | WS_CLIPSIBLINGS */, // style (will be shown later)
191 pos.x, pos.y, // position
192 0, 0, // size (will be set later)
193 GetHwndOf(parent), // parent
194 (HMENU)-1, // control id
195 wxGetInstance(), // app instance
196 NULL // unused client data
197 );
198
199 if ( !m_hwndBuddy )
200 {
201 wxLogLastError(wxT("CreateWindow(buddy text window)"));
202
203 return FALSE;
204 }
205
206 // subclass the text ctrl to be able to intercept some events
207 m_wndProcBuddy = (WXFARPROC)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC);
208 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA, (LONG)this);
209 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC, (LONG)wxBuddyTextWndProc);
210
211 // should have the same font as the other controls
212 SetFont(GetParent()->GetFont());
213
214 // set the size of the text window - can do it only now, because we
215 // couldn't call DoGetBestSize() before as font wasn't set
216 if ( sizeText.y <= 0 )
217 {
218 int cx, cy;
219 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
220
221 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
222 }
223
224 DoMoveWindow(pos.x, pos.y,
225 sizeText.x + sizeBtn.x + MARGIN_BETWEEN, sizeText.y);
226
227 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
228
229 // associate the text window with the spin button
230 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
231
232 if ( !value.IsEmpty() )
233 {
234 SetValue(value);
235 }
236
237 // do it after finishing with m_hwndBuddy creation to avoid generating
238 // initial wxEVT_COMMAND_TEXT_UPDATED message
239 ms_allSpins.Add(this);
240
241 return TRUE;
242 }
243
244 wxSpinCtrl::~wxSpinCtrl()
245 {
246 ms_allSpins.Remove(this);
247
248 // destroy the buddy window because this pointer which wxBuddyTextWndProc
249 // uses will not soon be valid any more
250 ::DestroyWindow(GetBuddyHwnd());
251 }
252
253 // ----------------------------------------------------------------------------
254 // wxTextCtrl-like methods
255 // ----------------------------------------------------------------------------
256
257 void wxSpinCtrl::SetValue(const wxString& text)
258 {
259 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
260 {
261 wxLogLastError(wxT("SetWindowText(buddy)"));
262 }
263 }
264
265 int wxSpinCtrl::GetValue() const
266 {
267 wxString val = wxGetWindowText(m_hwndBuddy);
268
269 long n;
270 if ( (wxSscanf(val, wxT("%lu"), &n) != 1) )
271 n = INT_MIN;
272
273 return n;
274 }
275
276 // ----------------------------------------------------------------------------
277 // forward some methods to subcontrols
278 // ----------------------------------------------------------------------------
279
280 bool wxSpinCtrl::SetFont(const wxFont& font)
281 {
282 if ( !wxWindowBase::SetFont(font) )
283 {
284 // nothing to do
285 return FALSE;
286 }
287
288 WXHANDLE hFont = GetFont().GetResourceHandle();
289 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
290
291 return TRUE;
292 }
293
294 bool wxSpinCtrl::Show(bool show)
295 {
296 if ( !wxControl::Show(show) )
297 {
298 return FALSE;
299 }
300
301 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
302
303 return TRUE;
304 }
305
306 bool wxSpinCtrl::Enable(bool enable)
307 {
308 if ( !wxControl::Enable(enable) )
309 {
310 return FALSE;
311 }
312
313 ::EnableWindow(GetBuddyHwnd(), enable);
314
315 return TRUE;
316 }
317
318 void wxSpinCtrl::SetFocus()
319 {
320 ::SetFocus(GetBuddyHwnd());
321 }
322
323 // ----------------------------------------------------------------------------
324 // event processing
325 // ----------------------------------------------------------------------------
326
327 void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin)
328 {
329 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
330 event.SetEventObject(this);
331 event.SetInt(eventSpin.GetPosition());
332
333 (void)GetEventHandler()->ProcessEvent(event);
334
335 if ( eventSpin.GetSkipped() )
336 {
337 event.Skip();
338 }
339 }
340
341 // ----------------------------------------------------------------------------
342 // size calculations
343 // ----------------------------------------------------------------------------
344
345 wxSize wxSpinCtrl::DoGetBestSize() const
346 {
347 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
348 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
349
350 int y;
351 wxGetCharSize(GetHWND(), NULL, &y, &GetFont());
352 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
353
354 if ( sizeBtn.y < y )
355 {
356 // make the text tall enough
357 sizeBtn.y = y;
358 }
359
360 return sizeBtn;
361 }
362
363 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
364 {
365 int widthBtn = wxSpinButton::DoGetBestSize().x;
366 int widthText = width - widthBtn - MARGIN_BETWEEN;
367 if ( widthText <= 0 )
368 {
369 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
370 }
371
372 if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) )
373 {
374 wxLogLastError(wxT("MoveWindow(buddy)"));
375 }
376
377 x += widthText + MARGIN_BETWEEN;
378 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
379 {
380 wxLogLastError(wxT("MoveWindow"));
381 }
382 }
383
384 // get total size of the control
385 void wxSpinCtrl::DoGetSize(int *x, int *y) const
386 {
387 RECT spinrect, textrect, ctrlrect;
388 GetWindowRect(GetHwnd(), &spinrect);
389 GetWindowRect(GetBuddyHwnd(), &textrect);
390 UnionRect(&ctrlrect,&textrect, &spinrect);
391
392 if ( x )
393 *x = ctrlrect.right - ctrlrect.left;
394 if ( y )
395 *y = ctrlrect.bottom - ctrlrect.top;
396 }
397
398 void wxSpinCtrl::DoGetPosition(int *x, int *y) const
399 {
400 // hack: pretend that our HWND is the text control just for a moment
401 WXHWND hWnd = GetHWND();
402 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
403
404 wxSpinButton::DoGetPosition(x, y);
405
406 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
407 }
408
409 #endif // __WIN95__
410
411 #endif
412 // wxUSE_SPINCTRL
413