1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for Win32
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 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 #if defined(__WIN95__)
40 #include "wx/spinctrl.h"
41 #include "wx/msw/private.h"
43 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
47 #include <limits.h> // for INT_MIN
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #if wxUSE_EXTENDED_RTTI
54 WX_DEFINE_FLAGS( wxSpinCtrlStyle
)
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
)
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
)
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
)
84 wxFLAGS_MEMBER(wxSP_HORIZONTAL
)
85 wxFLAGS_MEMBER(wxSP_VERTICAL
)
86 wxFLAGS_MEMBER(wxSP_ARROW_KEYS
)
87 wxFLAGS_MEMBER(wxSP_WRAP
)
89 wxEND_FLAGS( wxSpinCtrlStyle
)
91 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
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
)
99 wxPROPERTY( ValueString
, wxString
, SetValue
, GetValue
, EMPTY_MACROVALUE
, 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
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
106 style wxSP_ARROW_KEYS
108 wxEND_PROPERTIES_TABLE()
110 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl
)
111 wxEND_HANDLERS_TABLE()
113 wxCONSTRUCTOR_6( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, ValueString
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
115 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
118 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
119 EVT_CHAR(wxSpinCtrl::OnChar
)
121 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
123 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange
)
126 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
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
135 static const int MARGIN_BETWEEN
= 1;
137 // ============================================================================
139 // ============================================================================
141 wxArraySpins
wxSpinCtrl::ms_allSpins
;
143 // ----------------------------------------------------------------------------
144 // wnd proc for the buddy text ctrl
145 // ----------------------------------------------------------------------------
147 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
152 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData(hwnd
);
154 // forward some messages (the key and focus ones only so far) to
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 ( (WXHWND
)wParam
== spin
->GetHWND() )
170 spin
->MSWWindowProc(message
, wParam
, lParam
);
172 // The control may have been deleted at this point, so check.
173 if ( !::IsWindow(hwnd
) || wxGetWindowUserData(hwnd
) != spin
)
178 // we want to get WXK_RETURN in order to generate the event for it
179 return DLGC_WANTCHARS
;
182 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
183 hwnd
, message
, wParam
, lParam
);
187 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
189 wxSpinCtrl
*spin
= (wxSpinCtrl
*)wxGetWindowUserData((HWND
)hwndBuddy
);
191 int i
= ms_allSpins
.Index(spin
);
193 if ( i
== wxNOT_FOUND
)
197 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
198 _T("wxSpinCtrl has incorrect buddy HWND!") );
203 // process a WM_COMMAND generated by the buddy text control
204 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
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
);
220 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
223 event
.SetEventObject( this );
224 return GetEventHandler()->ProcessEvent(event
);
234 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
236 switch ( event
.GetKeyCode() )
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
) )
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.
256 wxNavigationKeyEvent eventNav
;
257 eventNav
.SetDirection(!event
.ShiftDown());
258 eventNav
.SetWindowChange(event
.ControlDown());
259 eventNav
.SetEventObject(this);
261 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
267 // no, we didn't process it
271 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
273 // when we get focus, give it to our buddy window as it needs it more than
275 ::SetFocus((HWND
)m_hwndBuddy
);
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 bool wxSpinCtrl::Create(wxWindow
*parent
,
286 const wxString
& value
,
290 int min
, int max
, int initial
,
291 const wxString
& name
)
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
296 style
|= wxSP_VERTICAL
;
298 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
300 style
|= wxBORDER_SIMPLE
;
302 style
|= wxBORDER_SUNKEN
;
305 SetWindowStyle(style
);
308 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
310 // calculate the sizes: the size given is the toal size for both controls
311 // and we need to fit them both in the given width (height is the same)
312 wxSize
sizeText(size
), sizeBtn(size
);
313 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
314 if ( sizeText
.x
<= 0 )
316 // DEFAULT_ITEM_WIDTH is the default width for the text control
317 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
320 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
321 if ( sizeText
.x
<= 0 )
323 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
327 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
329 // we must create the text control before the spin button for the purpose
330 // of the dialog navigation: if there is a static text just before the spin
331 // control, activating it by Alt-letter should give focus to the text
332 // control, not the spin and the dialog navigation code will give focus to
333 // the next control (at Windows level), not the one after it
335 // create the text window
337 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
339 exStyle
, // sunken border
340 _T("EDIT"), // window class
341 NULL
, // no window title
342 msStyle
, // style (will be shown later)
343 pos
.x
, pos
.y
, // position
344 0, 0, // size (will be set later)
345 GetHwndOf(parent
), // parent
346 (HMENU
)-1, // control id
347 wxGetInstance(), // app instance
348 NULL
// unused client data
353 wxLogLastError(wxT("CreateWindow(buddy text window)"));
359 // create the spin button
360 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
368 // subclass the text ctrl to be able to intercept some events
369 wxSetWindowUserData(GetBuddyHwnd(), this);
370 m_wndProcBuddy
= (WXFARPROC
)wxSetWindowProc(GetBuddyHwnd(),
373 // should have the same font as the other controls
374 SetFont(GetParent()->GetFont());
376 // set the size of the text window - can do it only now, because we
377 // couldn't call DoGetBestSize() before as font wasn't set
378 if ( sizeText
.y
<= 0 )
381 wxGetCharSize(GetHWND(), &cx
, &cy
, GetFont());
383 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
386 DoMoveWindow(pos
.x
, pos
.y
,
387 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
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) )
442 void wxSpinCtrl::SetSelection(long from
, long to
)
444 // if from and to are both -1, it means (in wxWidgets) that all text should
445 // be selected - translate into Windows convention
446 if ( (from
== -1) && (to
== -1) )
451 ::SendMessage((HWND
)m_hwndBuddy
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
454 // ----------------------------------------------------------------------------
455 // forward some methods to subcontrols
456 // ----------------------------------------------------------------------------
458 bool wxSpinCtrl::SetFont(const wxFont
& font
)
460 if ( !wxWindowBase::SetFont(font
) )
466 WXHANDLE hFont
= GetFont().GetResourceHandle();
467 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
472 bool wxSpinCtrl::Show(bool show
)
474 if ( !wxControl::Show(show
) )
479 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
484 bool wxSpinCtrl::Enable(bool enable
)
486 if ( !wxControl::Enable(enable
) )
491 ::EnableWindow(GetBuddyHwnd(), enable
);
496 void wxSpinCtrl::SetFocus()
498 ::SetFocus(GetBuddyHwnd());
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
505 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
507 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
508 event
.SetEventObject(this);
509 event
.SetInt(eventSpin
.GetPosition());
511 (void)GetEventHandler()->ProcessEvent(event
);
513 if ( eventSpin
.GetSkipped() )
519 // ----------------------------------------------------------------------------
521 // ----------------------------------------------------------------------------
523 wxSize
wxSpinCtrl::DoGetBestSize() const
525 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
526 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
529 wxGetCharSize(GetHWND(), NULL
, &y
, GetFont());
530 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
532 // JACS: we should always use the height calculated
533 // from above, because otherwise we'll get a spin control
534 // that's too big. So never use the height calculated
535 // from wxSpinButton::DoGetBestSize().
537 // if ( sizeBtn.y < y )
539 // make the text tall enough
546 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
548 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
549 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
550 if ( widthText
<= 0 )
552 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
555 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
557 wxLogLastError(wxT("MoveWindow(buddy)"));
560 x
+= widthText
+ MARGIN_BETWEEN
;
561 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
563 wxLogLastError(wxT("MoveWindow"));
567 // get total size of the control
568 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
570 RECT spinrect
, textrect
, ctrlrect
;
571 GetWindowRect(GetHwnd(), &spinrect
);
572 GetWindowRect(GetBuddyHwnd(), &textrect
);
573 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
576 *x
= ctrlrect
.right
- ctrlrect
.left
;
578 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
581 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
583 // hack: pretend that our HWND is the text control just for a moment
584 WXHWND hWnd
= GetHWND();
585 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
587 wxSpinButton::DoGetPosition(x
, y
);
589 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;