1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for Win32
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999-2005 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "spinctrlbase.h"
18 #pragma implementation "spinctrl.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // for compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
38 #include "wx/spinctrl.h"
39 #include "wx/msw/private.h"
40 #include "wx/msw/wrapcctl.h"
43 #include "wx/tooltip.h"
44 #endif // wxUSE_TOOLTIPS
46 #include <limits.h> // for INT_MIN
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #if wxUSE_EXTENDED_RTTI
53 WX_DEFINE_FLAGS( wxSpinCtrlStyle
)
55 wxBEGIN_FLAGS( wxSpinCtrlStyle
)
56 // new style border flags, we put them first to
57 // use them for streaming out
58 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
59 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
60 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
61 wxFLAGS_MEMBER(wxBORDER_RAISED
)
62 wxFLAGS_MEMBER(wxBORDER_STATIC
)
63 wxFLAGS_MEMBER(wxBORDER_NONE
)
65 // old style border flags
66 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
67 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
68 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
69 wxFLAGS_MEMBER(wxRAISED_BORDER
)
70 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
71 wxFLAGS_MEMBER(wxBORDER
)
73 // standard window styles
74 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
75 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
76 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
77 wxFLAGS_MEMBER(wxWANTS_CHARS
)
78 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
79 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
80 wxFLAGS_MEMBER(wxVSCROLL
)
81 wxFLAGS_MEMBER(wxHSCROLL
)
83 wxFLAGS_MEMBER(wxSP_HORIZONTAL
)
84 wxFLAGS_MEMBER(wxSP_VERTICAL
)
85 wxFLAGS_MEMBER(wxSP_ARROW_KEYS
)
86 wxFLAGS_MEMBER(wxSP_WRAP
)
88 wxEND_FLAGS( wxSpinCtrlStyle
)
90 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
92 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl
)
93 wxEVENT_RANGE_PROPERTY( Spin
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_ENDSCROLL
, wxSpinEvent
)
94 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SPINCTRL_UPDATED
, wxCommandEvent
)
95 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
96 wxEVENT_PROPERTY( TextEnter
, wxEVT_COMMAND_TEXT_ENTER
, wxCommandEvent
)
98 wxPROPERTY( ValueString
, wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
99 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
100 wxPROPERTY( Min
, int , SetMin
, GetMin
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
101 wxPROPERTY( Max
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
102 wxPROPERTY_FLAGS( WindowStyle
, wxSpinCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
105 style wxSP_ARROW_KEYS
107 wxEND_PROPERTIES_TABLE()
109 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl
)
110 wxEND_HANDLERS_TABLE()
112 wxCONSTRUCTOR_6( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, ValueString
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
114 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
117 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
118 EVT_CHAR(wxSpinCtrl::OnChar
)
120 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
122 EVT_SPIN(wxID_ANY
, wxSpinCtrl::OnSpinChange
)
125 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 // the margin between the up-down control and its buddy (can be arbitrary,
132 // choose what you like - or may be decide during run-time depending on the
134 static const int MARGIN_BETWEEN
= 1;
136 // ============================================================================
138 // ============================================================================
140 wxArraySpins
wxSpinCtrl::ms_allSpins
;
142 // ----------------------------------------------------------------------------
143 // wnd proc for the buddy text ctrl
144 // ----------------------------------------------------------------------------
146 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
151 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData(hwnd
);
153 // forward some messages (the key and focus ones only so far) to
158 // if the focus comes from the spin control itself, don't set it
159 // back to it -- we don't want to go into an infinite loop
160 if ( (WXHWND
)wParam
== spin
->GetHWND() )
169 spin
->MSWWindowProc(message
, wParam
, lParam
);
171 // The control may have been deleted at this point, so check.
172 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
177 // we want to get WXK_RETURN in order to generate the event for it
178 return DLGC_WANTCHARS
;
181 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
182 hwnd
, message
, wParam
, lParam
);
186 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
188 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData((HWND
)hwndBuddy
);
190 int i
= ms_allSpins
.Index(spin
);
192 if ( i
== wxNOT_FOUND
)
196 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
197 _T("wxSpinCtrl has incorrect buddy HWND!") );
202 // process a WM_COMMAND generated by the buddy text control
203 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
209 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
210 event
.SetEventObject(this);
211 wxString val
= wxGetWindowText(m_hwndBuddy
);
212 event
.SetString(val
);
213 event
.SetInt(GetValue());
214 return GetEventHandler()->ProcessEvent(event
);
219 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
222 event
.SetEventObject( this );
223 return GetEventHandler()->ProcessEvent(event
);
233 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
235 switch ( event
.GetKeyCode() )
239 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
240 InitCommandEvent(event
);
241 wxString val
= wxGetWindowText(m_hwndBuddy
);
242 event
.SetString(val
);
243 event
.SetInt(GetValue());
244 if ( GetEventHandler()->ProcessEvent(event
) )
250 // always produce navigation event - even if we process TAB
251 // ourselves the fact that we got here means that the user code
252 // decided to skip processing of this TAB - probably to let it
253 // do its default job.
255 wxNavigationKeyEvent eventNav
;
256 eventNav
.SetDirection(!event
.ShiftDown());
257 eventNav
.SetWindowChange(event
.ControlDown());
258 eventNav
.SetEventObject(this);
260 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
266 // no, we didn't process it
270 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
272 // when we get focus, give it to our buddy window as it needs it more than
274 ::SetFocus((HWND
)m_hwndBuddy
);
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 bool wxSpinCtrl::Create(wxWindow
*parent
,
285 const wxString
& value
,
289 int min
, int max
, int initial
,
290 const wxString
& name
)
292 // before using DoGetBestSize(), have to set style to let the base class
293 // know whether this is a horizontal or vertical control (we're always
295 style
|= wxSP_VERTICAL
;
297 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
299 style
|= wxBORDER_SIMPLE
;
301 style
|= wxBORDER_SUNKEN
;
304 SetWindowStyle(style
);
307 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
309 // calculate the sizes: the size given is the toal size for both controls
310 // and we need to fit them both in the given width (height is the same)
311 wxSize
sizeText(size
), sizeBtn(size
);
312 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
313 if ( sizeText
.x
<= 0 )
315 // DEFAULT_ITEM_WIDTH is the default width for the text control
316 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
319 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
320 if ( sizeText
.x
<= 0 )
322 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
326 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
328 // we must create the text control before the spin button for the purpose
329 // of the dialog navigation: if there is a static text just before the spin
330 // control, activating it by Alt-letter should give focus to the text
331 // control, not the spin and the dialog navigation code will give focus to
332 // the next control (at Windows level), not the one after it
334 // create the text window
336 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
338 exStyle
, // sunken border
339 _T("EDIT"), // window class
340 NULL
, // no window title
341 msStyle
, // style (will be shown later)
342 pos
.x
, pos
.y
, // position
343 0, 0, // size (will be set later)
344 GetHwndOf(parent
), // parent
345 (HMENU
)-1, // control id
346 wxGetInstance(), // app instance
347 NULL
// unused client data
352 wxLogLastError(wxT("CreateWindow(buddy text window)"));
358 // create the spin button
359 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
367 // subclass the text ctrl to be able to intercept some events
368 wxSetWindowUserData(GetBuddyHwnd(), this);
369 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
372 // set up fonts and colours (This is nomally done in MSWCreateControl)
375 SetFont(GetDefaultAttributes().font
);
377 // set the size of the text window - can do it only now, because we
378 // couldn't call DoGetBestSize() before as font wasn't set
379 if ( sizeText
.y
<= 0 )
382 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
384 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
389 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
391 // associate the text window with the spin button
392 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
394 if ( !value
.IsEmpty() )
399 // do it after finishing with m_hwndBuddy creation to avoid generating
400 // initial wxEVT_COMMAND_TEXT_UPDATED message
401 ms_allSpins
.Add(this);
406 wxSpinCtrl::~wxSpinCtrl()
408 ms_allSpins
.Remove(this);
410 // This removes spurious memory leak reporting
411 if (ms_allSpins
.GetCount() == 0)
414 // destroy the buddy window because this pointer which wxBuddyTextWndProc
415 // uses will not soon be valid any more
416 ::DestroyWindow(GetBuddyHwnd());
419 // ----------------------------------------------------------------------------
420 // wxTextCtrl-like methods
421 // ----------------------------------------------------------------------------
423 void wxSpinCtrl::SetValue(const wxString
& text
)
425 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
427 wxLogLastError(wxT("SetWindowText(buddy)"));
431 int wxSpinCtrl::GetValue() const
433 wxString val
= wxGetWindowText(m_hwndBuddy
);
436 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
439 if (n
< m_min
) n
= m_min
;
440 if (n
> m_max
) n
= m_max
;
445 void wxSpinCtrl::SetSelection(long from
, long to
)
447 // if from and to are both -1, it means (in wxWidgets) that all text should
448 // be selected - translate into Windows convention
449 if ( (from
== -1) && (to
== -1) )
454 ::SendMessage((HWND
)m_hwndBuddy
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
457 // ----------------------------------------------------------------------------
458 // forward some methods to subcontrols
459 // ----------------------------------------------------------------------------
461 bool wxSpinCtrl::SetFont(const wxFont
& font
)
463 if ( !wxWindowBase::SetFont(font
) )
469 WXHANDLE hFont
= GetFont().GetResourceHandle();
470 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
475 bool wxSpinCtrl::Show(bool show
)
477 if ( !wxControl::Show(show
) )
482 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
487 bool wxSpinCtrl::Enable(bool enable
)
489 if ( !wxControl::Enable(enable
) )
494 ::EnableWindow(GetBuddyHwnd(), enable
);
499 void wxSpinCtrl::SetFocus()
501 ::SetFocus(GetBuddyHwnd());
506 void wxSpinCtrl::DoSetToolTip(wxToolTip
*tip
)
508 wxSpinButton::DoSetToolTip(tip
);
511 tip
->Add(m_hwndBuddy
);
514 #endif // wxUSE_TOOLTIPS
516 // ----------------------------------------------------------------------------
518 // ----------------------------------------------------------------------------
520 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
522 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
523 event
.SetEventObject(this);
524 event
.SetInt(eventSpin
.GetPosition());
526 (void)GetEventHandler()->ProcessEvent(event
);
528 if ( eventSpin
.GetSkipped() )
534 // ----------------------------------------------------------------------------
536 // ----------------------------------------------------------------------------
538 wxSize
wxSpinCtrl::DoGetBestSize() const
540 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
541 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
544 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
545 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
547 // JACS: we should always use the height calculated
548 // from above, because otherwise we'll get a spin control
549 // that's too big. So never use the height calculated
550 // from wxSpinButton::DoGetBestSize().
552 // if ( sizeBtn.y < y )
554 // make the text tall enough
561 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
563 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
564 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
565 if ( widthText
<= 0 )
567 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
570 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
572 wxLogLastError(wxT("MoveWindow(buddy)"));
575 x
+= widthText
+ MARGIN_BETWEEN
;
576 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
578 wxLogLastError(wxT("MoveWindow"));
582 // get total size of the control
583 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
585 RECT spinrect
, textrect
, ctrlrect
;
586 GetWindowRect(GetHwnd(), &spinrect
);
587 GetWindowRect(GetBuddyHwnd(), &textrect
);
588 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
591 *x
= ctrlrect
.right
- ctrlrect
.left
;
593 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
596 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
598 // hack: pretend that our HWND is the text control just for a moment
599 WXHWND hWnd
= GetHWND();
600 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
602 wxSpinButton::DoGetPosition(x
, y
);
604 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
607 #endif // wxUSE_SPINCTRL