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