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