]> git.saurik.com Git - wxWidgets.git/blob - src/msw/spinctrl.cpp
unicode compilation fix
[wxWidgets.git] / src / msw / spinctrl.cpp
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
36 #if wxUSE_SPINCTRL
37
38 #if defined(__WIN95__)
39
40 #include "wx/spinctrl.h"
41 #include "wx/msw/private.h"
42
43 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
44 #include <commctrl.h>
45 #endif
46
47 #include <limits.h> // for INT_MIN
48
49 // ----------------------------------------------------------------------------
50 // macros
51 // ----------------------------------------------------------------------------
52
53 #if 0 // wxUSE_EXTENDED_RTTI
54 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl, wxControl,"wx/spinbut.h")
55
56 WX_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 )
60 /*
61 TODO PROPERTIES
62 style wxSP_ARROW_KEYS
63 */
64 WX_END_PROPERTIES_TABLE()
65
66 WX_BEGIN_HANDLERS_TABLE(wxSpinCtrl)
67 WX_END_HANDLERS_TABLE()
68
69 WX_CONSTRUCTOR_5( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
70 #else
71 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
72 #endif
73
74 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
75 EVT_CHAR(wxSpinCtrl::OnChar)
76
77 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
78
79 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange)
80 END_EVENT_TABLE()
81
82 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
83
84 // ----------------------------------------------------------------------------
85 // constants
86 // ----------------------------------------------------------------------------
87
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?)
91 static const int MARGIN_BETWEEN = 1;
92
93 // ============================================================================
94 // implementation
95 // ============================================================================
96
97 wxArraySpins wxSpinCtrl::ms_allSpins;
98
99 // ----------------------------------------------------------------------------
100 // wnd proc for the buddy text ctrl
101 // ----------------------------------------------------------------------------
102
103 LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
104 UINT message,
105 WPARAM wParam,
106 LPARAM lParam)
107 {
108 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA);
109
110 // forward some messages (the key and focus ones only so far) to
111 // the spin ctrl
112 switch ( message )
113 {
114 case WM_SETFOCUS:
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
121 case WM_KILLFOCUS:
122 case WM_CHAR:
123 case WM_DEADCHAR:
124 case WM_KEYUP:
125 case WM_KEYDOWN:
126 spin->MSWWindowProc(message, wParam, lParam);
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;
131 break;
132
133 case WM_GETDLGCODE:
134 // we want to get WXK_RETURN in order to generate the event for it
135 return DLGC_WANTCHARS;
136 }
137
138 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
139 hwnd, message, wParam, lParam);
140 }
141
142 /* static */
143 wxSpinCtrl *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
161 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
162 {
163 switch (cmd)
164 {
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;
185 }
186
187 // not processed
188 return FALSE;
189 }
190
191 void wxSpinCtrl::OnChar(wxKeyEvent& event)
192 {
193 switch ( event.GetKeyCode() )
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
228 void 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
237 // ----------------------------------------------------------------------------
238 // construction
239 // ----------------------------------------------------------------------------
240
241 bool wxSpinCtrl::Create(wxWindow *parent,
242 wxWindowID id,
243 const wxString& value,
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)
253 style |= wxSP_VERTICAL;
254
255 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
256 style |= wxBORDER_SUNKEN;
257
258 SetWindowStyle(style);
259
260 WXDWORD exStyle = 0;
261 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
262
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;
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
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
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
287
288 // create the text window
289
290 m_hwndBuddy = (WXHWND)::CreateWindowEx
291 (
292 exStyle, // sunken border
293 _T("EDIT"), // window class
294 NULL, // no window title
295 msStyle, // style (will be shown later)
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
302 );
303
304 if ( !m_hwndBuddy )
305 {
306 wxLogLastError(wxT("CreateWindow(buddy text window)"));
307
308 return FALSE;
309 }
310
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
321 // subclass the text ctrl to be able to intercept some events
322 m_wndProcBuddy = (WXFARPROC)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC);
323 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA, (LONG)this);
324 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC, (LONG)wxBuddyTextWndProc);
325
326 // should have the same font as the other controls
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 {
333 int cx, cy;
334 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
335
336 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
337 }
338
339 DoMoveWindow(pos.x, pos.y,
340 sizeText.x + sizeBtn.x + MARGIN_BETWEEN, sizeText.y);
341
342 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
343
344 // associate the text window with the spin button
345 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
346
347 if ( !value.IsEmpty() )
348 {
349 SetValue(value);
350 }
351
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
356 return TRUE;
357 }
358
359 wxSpinCtrl::~wxSpinCtrl()
360 {
361 ms_allSpins.Remove(this);
362
363 // This removes spurious memory leak reporting
364 if (ms_allSpins.GetCount() == 0)
365 ms_allSpins.Clear();
366
367 // destroy the buddy window because this pointer which wxBuddyTextWndProc
368 // uses will not soon be valid any more
369 ::DestroyWindow(GetBuddyHwnd());
370 }
371
372 // ----------------------------------------------------------------------------
373 // wxTextCtrl-like methods
374 // ----------------------------------------------------------------------------
375
376 void wxSpinCtrl::SetValue(const wxString& text)
377 {
378 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
379 {
380 wxLogLastError(wxT("SetWindowText(buddy)"));
381 }
382 }
383
384 int 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
395 void 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
407 // ----------------------------------------------------------------------------
408 // forward some methods to subcontrols
409 // ----------------------------------------------------------------------------
410
411 bool 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();
420 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
421
422 return TRUE;
423 }
424
425 bool wxSpinCtrl::Show(bool show)
426 {
427 if ( !wxControl::Show(show) )
428 {
429 return FALSE;
430 }
431
432 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
433
434 return TRUE;
435 }
436
437 bool wxSpinCtrl::Enable(bool enable)
438 {
439 if ( !wxControl::Enable(enable) )
440 {
441 return FALSE;
442 }
443
444 ::EnableWindow(GetBuddyHwnd(), enable);
445
446 return TRUE;
447 }
448
449 void wxSpinCtrl::SetFocus()
450 {
451 ::SetFocus(GetBuddyHwnd());
452 }
453
454 // ----------------------------------------------------------------------------
455 // event processing
456 // ----------------------------------------------------------------------------
457
458 void 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
472 // ----------------------------------------------------------------------------
473 // size calculations
474 // ----------------------------------------------------------------------------
475
476 wxSize wxSpinCtrl::DoGetBestSize() const
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
494 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
495 {
496 int widthBtn = wxSpinButton::DoGetBestSize().x;
497 int widthText = width - widthBtn - MARGIN_BETWEEN;
498 if ( widthText <= 0 )
499 {
500 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
501 }
502
503 if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) )
504 {
505 wxLogLastError(wxT("MoveWindow(buddy)"));
506 }
507
508 x += widthText + MARGIN_BETWEEN;
509 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
510 {
511 wxLogLastError(wxT("MoveWindow"));
512 }
513 }
514
515 // get total size of the control
516 void wxSpinCtrl::DoGetSize(int *x, int *y) const
517 {
518 RECT spinrect, textrect, ctrlrect;
519 GetWindowRect(GetHwnd(), &spinrect);
520 GetWindowRect(GetBuddyHwnd(), &textrect);
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
529 void 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
540 #endif // __WIN95__
541
542 #endif
543 // wxUSE_SPINCTRL
544