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