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