]> git.saurik.com Git - wxWidgets.git/blame - src/msw/spinctrl.cpp
Typo.
[wxWidgets.git] / src / msw / spinctrl.cpp
CommitLineData
b782f2e0
VZ
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
0e528b99
JS
36#if wxUSE_SPINCTRL
37
b782f2e0
VZ
38#if defined(__WIN95__)
39
40#include "wx/spinctrl.h"
41#include "wx/msw/private.h"
42
ae090fdb 43#if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
4c0a2c5c
JS
44 #include <commctrl.h>
45#endif
b782f2e0 46
678cd6de
VZ
47#include <limits.h> // for INT_MIN
48
b782f2e0
VZ
49// ----------------------------------------------------------------------------
50// macros
51// ----------------------------------------------------------------------------
52
9750fc42
VZ
53IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
54
55BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
e3582f7f 56 EVT_CHAR(wxSpinCtrl::OnChar)
9750fc42
VZ
57 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange)
58END_EVENT_TABLE()
b782f2e0 59
6fe19057
VZ
60#define GetBuddyHwnd() (HWND)(m_hwndBuddy)
61
b782f2e0
VZ
62// ----------------------------------------------------------------------------
63// constants
64// ----------------------------------------------------------------------------
65
baccb514
VZ
66// the margin between the up-down control and its buddy (can be arbitrary,
67// choose what you like - or may be decide during run-time depending on the
68// font size?)
69static const int MARGIN_BETWEEN = 1;
b782f2e0
VZ
70
71// ============================================================================
72// implementation
73// ============================================================================
74
6fe19057
VZ
75wxArraySpins wxSpinCtrl::ms_allSpins;
76
f6bcfd97
BP
77// ----------------------------------------------------------------------------
78// wnd proc for the buddy text ctrl
79// ----------------------------------------------------------------------------
80
81LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
82 UINT message,
83 WPARAM wParam,
84 LPARAM lParam)
85{
86 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA);
87
93c4157c
VZ
88 // forward some messages (the key and focus ones only so far) to
89 // the spin ctrl
f6bcfd97
BP
90 switch ( message )
91 {
93c4157c
VZ
92 case WM_SETFOCUS:
93 case WM_KILLFOCUS:
f6bcfd97
BP
94 case WM_CHAR:
95 case WM_DEADCHAR:
96 case WM_KEYUP:
97 case WM_KEYDOWN:
98 spin->MSWWindowProc(message, wParam, lParam);
e3582f7f
JS
99
100 // The control may have been deleted at this point, so check.
101 if (!(::IsWindow(hwnd) && ((wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA)) == spin))
102 return 0;
f6bcfd97 103 break;
350ba193
VZ
104
105 case WM_GETDLGCODE:
106 // we want to get WXK_RETURN in order to generate the event for it
107 return DLGC_WANTCHARS;
f6bcfd97 108 }
350ba193 109
f6bcfd97
BP
110 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
111 hwnd, message, wParam, lParam);
112}
113
6fe19057
VZ
114/* static */
115wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
116{
117 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong((HWND)hwndBuddy,
118 GWL_USERDATA);
119
120 int i = ms_allSpins.Index(spin);
121
122 if ( i == wxNOT_FOUND )
123 return NULL;
124
125 // sanity check
126 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
127 _T("wxSpinCtrl has incorrect buddy HWND!") );
128
129 return spin;
130}
131
132// process a WM_COMMAND generated by the buddy text control
133bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
134{
e3582f7f 135 switch (cmd)
6fe19057 136 {
e3582f7f
JS
137 case EN_CHANGE:
138 {
139 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
140 event.SetEventObject(this);
141 wxString val = wxGetWindowText(m_hwndBuddy);
142 event.SetString(val);
143 event.SetInt(GetValue());
144 return GetEventHandler()->ProcessEvent(event);
145 }
146 case EN_SETFOCUS:
147 case EN_KILLFOCUS:
148 {
149 wxFocusEvent event(cmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
150 : wxEVT_SET_FOCUS,
151 m_windowId);
152 event.SetEventObject( this );
153 return GetEventHandler()->ProcessEvent(event);
154 }
155 default:
156 break;
6fe19057
VZ
157 }
158
159 // not processed
160 return FALSE;
161}
162
e3582f7f
JS
163void wxSpinCtrl::OnChar(wxKeyEvent& event)
164{
77e00fe9 165 switch ( event.GetKeyCode() )
e3582f7f
JS
166 {
167 case WXK_RETURN:
168 {
169 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
170 InitCommandEvent(event);
171 wxString val = wxGetWindowText(m_hwndBuddy);
172 event.SetString(val);
173 event.SetInt(GetValue());
174 if ( GetEventHandler()->ProcessEvent(event) )
175 return;
176 break;
177 }
178
179 case WXK_TAB:
180 // always produce navigation event - even if we process TAB
181 // ourselves the fact that we got here means that the user code
182 // decided to skip processing of this TAB - probably to let it
183 // do its default job.
184 {
185 wxNavigationKeyEvent eventNav;
186 eventNav.SetDirection(!event.ShiftDown());
187 eventNav.SetWindowChange(event.ControlDown());
188 eventNav.SetEventObject(this);
189
190 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
191 return;
192 }
193 break;
194 }
195
196 // no, we didn't process it
197 event.Skip();
198}
199
b782f2e0
VZ
200// ----------------------------------------------------------------------------
201// construction
202// ----------------------------------------------------------------------------
203
204bool wxSpinCtrl::Create(wxWindow *parent,
205 wxWindowID id,
678cd6de 206 const wxString& value,
b782f2e0
VZ
207 const wxPoint& pos,
208 const wxSize& size,
209 long style,
210 int min, int max, int initial,
211 const wxString& name)
212{
213 // before using DoGetBestSize(), have to set style to let the base class
214 // know whether this is a horizontal or vertical control (we're always
215 // vertical)
882a8f40 216 style |= wxSP_VERTICAL;
c76b1a30
JS
217
218 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
219 style |= wxBORDER_SUNKEN;
220
882a8f40 221 SetWindowStyle(style);
b782f2e0
VZ
222
223 // calculate the sizes: the size given is the toal size for both controls
224 // and we need to fit them both in the given width (height is the same)
225 wxSize sizeText(size), sizeBtn(size);
226 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
baccb514
VZ
227 if ( sizeText.x <= 0 )
228 {
229 // DEFAULT_ITEM_WIDTH is the default width for the text control
230 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
231 }
232
b782f2e0
VZ
233 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
234 if ( sizeText.x <= 0 )
235 {
236 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
237 }
238
239 wxPoint posBtn(pos);
240 posBtn.x += sizeText.x + MARGIN_BETWEEN;
241
242 // create the spin button
243 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
244 {
245 return FALSE;
246 }
247
248 SetRange(min, max);
249 SetValue(initial);
250
e3582f7f
JS
251 bool want3D;
252 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
253 int msStyle = WS_CHILD;
254
255 // Even with extended styles, need to combine with WS_BORDER for them to
256 // look right.
257 if ( want3D || wxStyleHasBorder(style) )
258 msStyle |= WS_BORDER;
259
b0766406
JS
260 if ( style & wxCLIP_SIBLINGS )
261 msStyle |= WS_CLIPSIBLINGS;
262
b782f2e0 263 // create the text window
b782f2e0
VZ
264 m_hwndBuddy = (WXHWND)::CreateWindowEx
265 (
e3582f7f 266 exStyle, // sunken border
baccb514
VZ
267 _T("EDIT"), // window class
268 NULL, // no window title
e3582f7f 269 msStyle /* | WS_CLIPSIBLINGS */, // style (will be shown later)
baccb514
VZ
270 pos.x, pos.y, // position
271 0, 0, // size (will be set later)
272 GetHwndOf(parent), // parent
273 (HMENU)-1, // control id
274 wxGetInstance(), // app instance
275 NULL // unused client data
b782f2e0
VZ
276 );
277
278 if ( !m_hwndBuddy )
279 {
f6bcfd97 280 wxLogLastError(wxT("CreateWindow(buddy text window)"));
b782f2e0
VZ
281
282 return FALSE;
283 }
284
f6bcfd97 285 // subclass the text ctrl to be able to intercept some events
6fe19057
VZ
286 m_wndProcBuddy = (WXFARPROC)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC);
287 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA, (LONG)this);
288 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC, (LONG)wxBuddyTextWndProc);
f6bcfd97 289
b782f2e0 290 // should have the same font as the other controls
baccb514
VZ
291 SetFont(GetParent()->GetFont());
292
293 // set the size of the text window - can do it only now, because we
294 // couldn't call DoGetBestSize() before as font wasn't set
295 if ( sizeText.y <= 0 )
296 {
882a8f40
VZ
297 int cx, cy;
298 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
299
300 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
baccb514
VZ
301 }
302
303 DoMoveWindow(pos.x, pos.y,
304 sizeText.x + sizeBtn.x + MARGIN_BETWEEN, sizeText.y);
305
6fe19057 306 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
b782f2e0
VZ
307
308 // associate the text window with the spin button
baccb514
VZ
309 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
310
678cd6de
VZ
311 if ( !value.IsEmpty() )
312 {
313 SetValue(value);
314 }
315
6fe19057
VZ
316 // do it after finishing with m_hwndBuddy creation to avoid generating
317 // initial wxEVT_COMMAND_TEXT_UPDATED message
318 ms_allSpins.Add(this);
319
baccb514
VZ
320 return TRUE;
321}
322
f6bcfd97
BP
323wxSpinCtrl::~wxSpinCtrl()
324{
6fe19057
VZ
325 ms_allSpins.Remove(this);
326
5648c0ad
JS
327 // This removes spurious memory leak reporting
328 if (ms_allSpins.GetCount() == 0)
329 ms_allSpins.Clear();
330
f6bcfd97
BP
331 // destroy the buddy window because this pointer which wxBuddyTextWndProc
332 // uses will not soon be valid any more
6fe19057 333 ::DestroyWindow(GetBuddyHwnd());
f6bcfd97
BP
334}
335
678cd6de
VZ
336// ----------------------------------------------------------------------------
337// wxTextCtrl-like methods
338// ----------------------------------------------------------------------------
339
340void wxSpinCtrl::SetValue(const wxString& text)
341{
6fe19057 342 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
678cd6de 343 {
f6bcfd97 344 wxLogLastError(wxT("SetWindowText(buddy)"));
678cd6de
VZ
345 }
346}
347
348int wxSpinCtrl::GetValue() const
349{
350 wxString val = wxGetWindowText(m_hwndBuddy);
351
352 long n;
353 if ( (wxSscanf(val, wxT("%lu"), &n) != 1) )
354 n = INT_MIN;
355
356 return n;
357}
358
07901ec9
VZ
359void wxSpinCtrl::SetSelection(long from, long to)
360{
361 // if from and to are both -1, it means (in wxWindows) that all text should
362 // be selected - translate into Windows convention
363 if ( (from == -1) && (to == -1) )
364 {
365 from = 0;
366 }
367
368 ::SendMessage((HWND)m_hwndBuddy, EM_SETSEL, (WPARAM)from, (LPARAM)to);
369}
370
baccb514 371// ----------------------------------------------------------------------------
882a8f40 372// forward some methods to subcontrols
baccb514
VZ
373// ----------------------------------------------------------------------------
374
375bool wxSpinCtrl::SetFont(const wxFont& font)
376{
377 if ( !wxWindowBase::SetFont(font) )
378 {
379 // nothing to do
380 return FALSE;
381 }
382
383 WXHANDLE hFont = GetFont().GetResourceHandle();
6fe19057 384 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
b782f2e0
VZ
385
386 return TRUE;
387}
388
882a8f40
VZ
389bool wxSpinCtrl::Show(bool show)
390{
391 if ( !wxControl::Show(show) )
392 {
393 return FALSE;
394 }
395
6fe19057 396 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
882a8f40
VZ
397
398 return TRUE;
399}
400
401bool wxSpinCtrl::Enable(bool enable)
402{
403 if ( !wxControl::Enable(enable) )
404 {
405 return FALSE;
406 }
407
6fe19057 408 ::EnableWindow(GetBuddyHwnd(), enable);
882a8f40
VZ
409
410 return TRUE;
411}
412
8bf3196d
GRG
413void wxSpinCtrl::SetFocus()
414{
6fe19057 415 ::SetFocus(GetBuddyHwnd());
8bf3196d
GRG
416}
417
9750fc42
VZ
418// ----------------------------------------------------------------------------
419// event processing
420// ----------------------------------------------------------------------------
421
422void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin)
423{
424 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
425 event.SetEventObject(this);
426 event.SetInt(eventSpin.GetPosition());
427
428 (void)GetEventHandler()->ProcessEvent(event);
429
430 if ( eventSpin.GetSkipped() )
431 {
432 event.Skip();
433 }
434}
435
b782f2e0
VZ
436// ----------------------------------------------------------------------------
437// size calculations
438// ----------------------------------------------------------------------------
439
f68586e5 440wxSize wxSpinCtrl::DoGetBestSize() const
baccb514
VZ
441{
442 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
443 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
444
445 int y;
446 wxGetCharSize(GetHWND(), NULL, &y, &GetFont());
447 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
448
449 if ( sizeBtn.y < y )
450 {
451 // make the text tall enough
452 sizeBtn.y = y;
453 }
454
455 return sizeBtn;
456}
457
b782f2e0
VZ
458void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
459{
baccb514 460 int widthBtn = wxSpinButton::DoGetBestSize().x;
b782f2e0
VZ
461 int widthText = width - widthBtn - MARGIN_BETWEEN;
462 if ( widthText <= 0 )
463 {
464 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
465 }
466
6fe19057 467 if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) )
b782f2e0 468 {
f6bcfd97 469 wxLogLastError(wxT("MoveWindow(buddy)"));
b782f2e0
VZ
470 }
471
472 x += widthText + MARGIN_BETWEEN;
473 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
474 {
f6bcfd97 475 wxLogLastError(wxT("MoveWindow"));
b782f2e0
VZ
476 }
477}
478
f6bcfd97
BP
479// get total size of the control
480void wxSpinCtrl::DoGetSize(int *x, int *y) const
481{
482 RECT spinrect, textrect, ctrlrect;
483 GetWindowRect(GetHwnd(), &spinrect);
6fe19057 484 GetWindowRect(GetBuddyHwnd(), &textrect);
f6bcfd97
BP
485 UnionRect(&ctrlrect,&textrect, &spinrect);
486
487 if ( x )
488 *x = ctrlrect.right - ctrlrect.left;
489 if ( y )
490 *y = ctrlrect.bottom - ctrlrect.top;
491}
492
493void wxSpinCtrl::DoGetPosition(int *x, int *y) const
494{
495 // hack: pretend that our HWND is the text control just for a moment
496 WXHWND hWnd = GetHWND();
497 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
498
499 wxSpinButton::DoGetPosition(x, y);
500
501 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
502}
503
b782f2e0 504#endif // __WIN95__
0e528b99
JS
505
506#endif
507 // wxUSE_SPINCTRL
508