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