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