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 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
56 WX_BEGIN_PROPERTIES_TABLE(wxSpinCtrl
)
57 WX_PROPERTY( ValueString
, wxString
, SetValue
, GetValue
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
58 WX_PROPERTY( Value
, int , SetValue
, GetValue
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
59 WX_PROPERTY( Min
, int , SetMin
, GetMin
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
60 WX_PROPERTY( Max
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
65 WX_END_PROPERTIES_TABLE()
67 WX_BEGIN_HANDLERS_TABLE(wxSpinCtrl
)
68 WX_END_HANDLERS_TABLE()
70 WX_CONSTRUCTOR_6( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, ValueString
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
72 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
75 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
76 EVT_CHAR(wxSpinCtrl::OnChar
)
78 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
80 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange
)
83 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // the margin between the up-down control and its buddy (can be arbitrary,
90 // choose what you like - or may be decide during run-time depending on the
92 static const int MARGIN_BETWEEN
= 1;
94 // ============================================================================
96 // ============================================================================
98 wxArraySpins
wxSpinCtrl::ms_allSpins
;
100 // ----------------------------------------------------------------------------
101 // wnd proc for the buddy text ctrl
102 // ----------------------------------------------------------------------------
104 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
109 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
111 // forward some messages (the key and focus ones only so far) to
116 // if the focus comes from the spin control itself, don't set it
117 // back to it -- we don't want to go into an infinite loop
118 if ( wParam
== spin
->GetHWND() )
127 spin
->MSWWindowProc(message
, wParam
, lParam
);
129 // The control may have been deleted at this point, so check.
130 if (!(::IsWindow(hwnd
) && ((wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
)) == spin
))
135 // we want to get WXK_RETURN in order to generate the event for it
136 return DLGC_WANTCHARS
;
139 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
140 hwnd
, message
, wParam
, lParam
);
144 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
146 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong((HWND
)hwndBuddy
,
149 int i
= ms_allSpins
.Index(spin
);
151 if ( i
== wxNOT_FOUND
)
155 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
156 _T("wxSpinCtrl has incorrect buddy HWND!") );
161 // process a WM_COMMAND generated by the buddy text control
162 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
168 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
169 event
.SetEventObject(this);
170 wxString val
= wxGetWindowText(m_hwndBuddy
);
171 event
.SetString(val
);
172 event
.SetInt(GetValue());
173 return GetEventHandler()->ProcessEvent(event
);
178 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
181 event
.SetEventObject( this );
182 return GetEventHandler()->ProcessEvent(event
);
192 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
194 switch ( event
.GetKeyCode() )
198 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
199 InitCommandEvent(event
);
200 wxString val
= wxGetWindowText(m_hwndBuddy
);
201 event
.SetString(val
);
202 event
.SetInt(GetValue());
203 if ( GetEventHandler()->ProcessEvent(event
) )
209 // always produce navigation event - even if we process TAB
210 // ourselves the fact that we got here means that the user code
211 // decided to skip processing of this TAB - probably to let it
212 // do its default job.
214 wxNavigationKeyEvent eventNav
;
215 eventNav
.SetDirection(!event
.ShiftDown());
216 eventNav
.SetWindowChange(event
.ControlDown());
217 eventNav
.SetEventObject(this);
219 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
225 // no, we didn't process it
229 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
231 // when we get focus, give it to our buddy window as it needs it more than
233 ::SetFocus((HWND
)m_hwndBuddy
);
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 bool wxSpinCtrl::Create(wxWindow
*parent
,
244 const wxString
& value
,
248 int min
, int max
, int initial
,
249 const wxString
& name
)
251 // before using DoGetBestSize(), have to set style to let the base class
252 // know whether this is a horizontal or vertical control (we're always
254 style
|= wxSP_VERTICAL
;
256 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
257 style
|= wxBORDER_SUNKEN
;
259 SetWindowStyle(style
);
262 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
264 // calculate the sizes: the size given is the toal size for both controls
265 // and we need to fit them both in the given width (height is the same)
266 wxSize
sizeText(size
), sizeBtn(size
);
267 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
268 if ( sizeText
.x
<= 0 )
270 // DEFAULT_ITEM_WIDTH is the default width for the text control
271 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
274 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
275 if ( sizeText
.x
<= 0 )
277 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
281 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
283 // we must create the text control before the spin button for the purpose
284 // of the dialog navigation: if there is a static text just before the spin
285 // control, activating it by Alt-letter should give focus to the text
286 // control, not the spin and the dialog navigation code will give focus to
287 // the next control (at Windows level), not the one after it
289 // create the text window
291 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
293 exStyle
, // sunken border
294 _T("EDIT"), // window class
295 NULL
, // no window title
296 msStyle
, // style (will be shown later)
297 pos
.x
, pos
.y
, // position
298 0, 0, // size (will be set later)
299 GetHwndOf(parent
), // parent
300 (HMENU
)-1, // control id
301 wxGetInstance(), // app instance
302 NULL
// unused client data
307 wxLogLastError(wxT("CreateWindow(buddy text window)"));
313 // create the spin button
314 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
322 // subclass the text ctrl to be able to intercept some events
323 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
324 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
325 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
327 // should have the same font as the other controls
328 SetFont(GetParent()->GetFont());
330 // set the size of the text window - can do it only now, because we
331 // couldn't call DoGetBestSize() before as font wasn't set
332 if ( sizeText
.y
<= 0 )
335 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
337 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
340 DoMoveWindow(pos
.x
, pos
.y
,
341 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
343 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
345 // associate the text window with the spin button
346 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
348 if ( !value
.IsEmpty() )
353 // do it after finishing with m_hwndBuddy creation to avoid generating
354 // initial wxEVT_COMMAND_TEXT_UPDATED message
355 ms_allSpins
.Add(this);
360 wxSpinCtrl::~wxSpinCtrl()
362 ms_allSpins
.Remove(this);
364 // This removes spurious memory leak reporting
365 if (ms_allSpins
.GetCount() == 0)
368 // destroy the buddy window because this pointer which wxBuddyTextWndProc
369 // uses will not soon be valid any more
370 ::DestroyWindow(GetBuddyHwnd());
373 // ----------------------------------------------------------------------------
374 // wxTextCtrl-like methods
375 // ----------------------------------------------------------------------------
377 void wxSpinCtrl::SetValue(const wxString
& text
)
379 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
381 wxLogLastError(wxT("SetWindowText(buddy)"));
385 int wxSpinCtrl::GetValue() const
387 wxString val
= wxGetWindowText(m_hwndBuddy
);
390 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
396 void wxSpinCtrl::SetSelection(long from
, long to
)
398 // if from and to are both -1, it means (in wxWindows) that all text should
399 // be selected - translate into Windows convention
400 if ( (from
== -1) && (to
== -1) )
405 ::SendMessage((HWND
)m_hwndBuddy
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
408 // ----------------------------------------------------------------------------
409 // forward some methods to subcontrols
410 // ----------------------------------------------------------------------------
412 bool wxSpinCtrl::SetFont(const wxFont
& font
)
414 if ( !wxWindowBase::SetFont(font
) )
420 WXHANDLE hFont
= GetFont().GetResourceHandle();
421 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
426 bool wxSpinCtrl::Show(bool show
)
428 if ( !wxControl::Show(show
) )
433 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
438 bool wxSpinCtrl::Enable(bool enable
)
440 if ( !wxControl::Enable(enable
) )
445 ::EnableWindow(GetBuddyHwnd(), enable
);
450 void wxSpinCtrl::SetFocus()
452 ::SetFocus(GetBuddyHwnd());
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
459 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
461 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
462 event
.SetEventObject(this);
463 event
.SetInt(eventSpin
.GetPosition());
465 (void)GetEventHandler()->ProcessEvent(event
);
467 if ( eventSpin
.GetSkipped() )
473 // ----------------------------------------------------------------------------
475 // ----------------------------------------------------------------------------
477 wxSize
wxSpinCtrl::DoGetBestSize() const
479 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
480 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
483 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
484 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
488 // make the text tall enough
495 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
497 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
498 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
499 if ( widthText
<= 0 )
501 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
504 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
506 wxLogLastError(wxT("MoveWindow(buddy)"));
509 x
+= widthText
+ MARGIN_BETWEEN
;
510 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
512 wxLogLastError(wxT("MoveWindow"));
516 // get total size of the control
517 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
519 RECT spinrect
, textrect
, ctrlrect
;
520 GetWindowRect(GetHwnd(), &spinrect
);
521 GetWindowRect(GetBuddyHwnd(), &textrect
);
522 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
525 *x
= ctrlrect
.right
- ctrlrect
.left
;
527 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
530 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
532 // hack: pretend that our HWND is the text control just for a moment
533 WXHWND hWnd
= GetHWND();
534 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
536 wxSpinButton::DoGetPosition(x
, y
);
538 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;