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 0 // wxUSE_EXTENDED_RTTI
54 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
56 WX_BEGIN_PROPERTIES_TABLE(wxSpinCtrl
)
57 WX_PROPERTY( Value
, int , SetValue
, GetValue
, 0 )
58 WX_PROPERTY( Min
, int , SetMin
, GetMin
, 0 )
59 WX_PROPERTY( Max
, int , SetMax
, GetMax
, 0 )
64 WX_END_PROPERTIES_TABLE()
66 WX_BEGIN_HANDLERS_TABLE(wxSpinCtrl
)
67 WX_END_HANDLERS_TABLE()
69 WX_CONSTRUCTOR_5( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
71 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
74 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
75 EVT_CHAR(wxSpinCtrl::OnChar
)
77 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
79 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange
)
82 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // the margin between the up-down control and its buddy (can be arbitrary,
89 // choose what you like - or may be decide during run-time depending on the
91 static const int MARGIN_BETWEEN
= 1;
93 // ============================================================================
95 // ============================================================================
97 wxArraySpins
wxSpinCtrl::ms_allSpins
;
99 // ----------------------------------------------------------------------------
100 // wnd proc for the buddy text ctrl
101 // ----------------------------------------------------------------------------
103 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
108 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
110 // forward some messages (the key and focus ones only so far) to
115 // if the focus comes from the spin control itself, don't set it
116 // back to it -- we don't want to go into an infinite loop
117 if ( wParam
== spin
->GetHWND() )
126 spin
->MSWWindowProc(message
, wParam
, lParam
);
128 // The control may have been deleted at this point, so check.
129 if (!(::IsWindow(hwnd
) && ((wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
)) == spin
))
134 // we want to get WXK_RETURN in order to generate the event for it
135 return DLGC_WANTCHARS
;
138 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
139 hwnd
, message
, wParam
, lParam
);
143 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
145 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong((HWND
)hwndBuddy
,
148 int i
= ms_allSpins
.Index(spin
);
150 if ( i
== wxNOT_FOUND
)
154 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
155 _T("wxSpinCtrl has incorrect buddy HWND!") );
160 // process a WM_COMMAND generated by the buddy text control
161 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
167 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
168 event
.SetEventObject(this);
169 wxString val
= wxGetWindowText(m_hwndBuddy
);
170 event
.SetString(val
);
171 event
.SetInt(GetValue());
172 return GetEventHandler()->ProcessEvent(event
);
177 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
180 event
.SetEventObject( this );
181 return GetEventHandler()->ProcessEvent(event
);
191 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
193 switch ( event
.GetKeyCode() )
197 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
198 InitCommandEvent(event
);
199 wxString val
= wxGetWindowText(m_hwndBuddy
);
200 event
.SetString(val
);
201 event
.SetInt(GetValue());
202 if ( GetEventHandler()->ProcessEvent(event
) )
208 // always produce navigation event - even if we process TAB
209 // ourselves the fact that we got here means that the user code
210 // decided to skip processing of this TAB - probably to let it
211 // do its default job.
213 wxNavigationKeyEvent eventNav
;
214 eventNav
.SetDirection(!event
.ShiftDown());
215 eventNav
.SetWindowChange(event
.ControlDown());
216 eventNav
.SetEventObject(this);
218 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
224 // no, we didn't process it
228 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
230 // when we get focus, give it to our buddy window as it needs it more than
232 ::SetFocus((HWND
)m_hwndBuddy
);
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 bool wxSpinCtrl::Create(wxWindow
*parent
,
243 const wxString
& value
,
247 int min
, int max
, int initial
,
248 const wxString
& name
)
250 // before using DoGetBestSize(), have to set style to let the base class
251 // know whether this is a horizontal or vertical control (we're always
253 style
|= wxSP_VERTICAL
;
255 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
256 style
|= wxBORDER_SUNKEN
;
258 SetWindowStyle(style
);
261 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
263 // calculate the sizes: the size given is the toal size for both controls
264 // and we need to fit them both in the given width (height is the same)
265 wxSize
sizeText(size
), sizeBtn(size
);
266 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
267 if ( sizeText
.x
<= 0 )
269 // DEFAULT_ITEM_WIDTH is the default width for the text control
270 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
273 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
274 if ( sizeText
.x
<= 0 )
276 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
280 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
282 // we must create the text control before the spin button for the purpose
283 // of the dialog navigation: if there is a static text just before the spin
284 // control, activating it by Alt-letter should give focus to the text
285 // control, not the spin and the dialog navigation code will give focus to
286 // the next control (at Windows level), not the one after it
288 // create the text window
290 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
292 exStyle
, // sunken border
293 _T("EDIT"), // window class
294 NULL
, // no window title
295 msStyle
, // style (will be shown later)
296 pos
.x
, pos
.y
, // position
297 0, 0, // size (will be set later)
298 GetHwndOf(parent
), // parent
299 (HMENU
)-1, // control id
300 wxGetInstance(), // app instance
301 NULL
// unused client data
306 wxLogLastError(wxT("CreateWindow(buddy text window)"));
312 // create the spin button
313 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
321 // subclass the text ctrl to be able to intercept some events
322 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
323 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
324 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
326 // should have the same font as the other controls
327 SetFont(GetParent()->GetFont());
329 // set the size of the text window - can do it only now, because we
330 // couldn't call DoGetBestSize() before as font wasn't set
331 if ( sizeText
.y
<= 0 )
334 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
336 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
339 DoMoveWindow(pos
.x
, pos
.y
,
340 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
342 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
344 // associate the text window with the spin button
345 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
347 if ( !value
.IsEmpty() )
352 // do it after finishing with m_hwndBuddy creation to avoid generating
353 // initial wxEVT_COMMAND_TEXT_UPDATED message
354 ms_allSpins
.Add(this);
359 wxSpinCtrl::~wxSpinCtrl()
361 ms_allSpins
.Remove(this);
363 // This removes spurious memory leak reporting
364 if (ms_allSpins
.GetCount() == 0)
367 // destroy the buddy window because this pointer which wxBuddyTextWndProc
368 // uses will not soon be valid any more
369 ::DestroyWindow(GetBuddyHwnd());
372 // ----------------------------------------------------------------------------
373 // wxTextCtrl-like methods
374 // ----------------------------------------------------------------------------
376 void wxSpinCtrl::SetValue(const wxString
& text
)
378 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
380 wxLogLastError(wxT("SetWindowText(buddy)"));
384 int wxSpinCtrl::GetValue() const
386 wxString val
= wxGetWindowText(m_hwndBuddy
);
389 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
395 void wxSpinCtrl::SetSelection(long from
, long to
)
397 // if from and to are both -1, it means (in wxWindows) that all text should
398 // be selected - translate into Windows convention
399 if ( (from
== -1) && (to
== -1) )
404 ::SendMessage((HWND
)m_hwndBuddy
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
407 // ----------------------------------------------------------------------------
408 // forward some methods to subcontrols
409 // ----------------------------------------------------------------------------
411 bool wxSpinCtrl::SetFont(const wxFont
& font
)
413 if ( !wxWindowBase::SetFont(font
) )
419 WXHANDLE hFont
= GetFont().GetResourceHandle();
420 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
425 bool wxSpinCtrl::Show(bool show
)
427 if ( !wxControl::Show(show
) )
432 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
437 bool wxSpinCtrl::Enable(bool enable
)
439 if ( !wxControl::Enable(enable
) )
444 ::EnableWindow(GetBuddyHwnd(), enable
);
449 void wxSpinCtrl::SetFocus()
451 ::SetFocus(GetBuddyHwnd());
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
460 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
461 event
.SetEventObject(this);
462 event
.SetInt(eventSpin
.GetPosition());
464 (void)GetEventHandler()->ProcessEvent(event
);
466 if ( eventSpin
.GetSkipped() )
472 // ----------------------------------------------------------------------------
474 // ----------------------------------------------------------------------------
476 wxSize
wxSpinCtrl::DoGetBestSize() const
478 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
479 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
482 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
483 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
487 // make the text tall enough
494 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
496 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
497 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
498 if ( widthText
<= 0 )
500 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
503 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
505 wxLogLastError(wxT("MoveWindow(buddy)"));
508 x
+= widthText
+ MARGIN_BETWEEN
;
509 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
511 wxLogLastError(wxT("MoveWindow"));
515 // get total size of the control
516 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
518 RECT spinrect
, textrect
, ctrlrect
;
519 GetWindowRect(GetHwnd(), &spinrect
);
520 GetWindowRect(GetBuddyHwnd(), &textrect
);
521 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
524 *x
= ctrlrect
.right
- ctrlrect
.left
;
526 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
529 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
531 // hack: pretend that our HWND is the text control just for a moment
532 WXHWND hWnd
= GetHWND();
533 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
535 wxSpinButton::DoGetPosition(x
, y
);
537 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;