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