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 // ============================================================================
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(__TWIN32__)) && !defined(__CYGWIN10__))
47 #include <limits.h> // for INT_MIN
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
55 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
56 EVT_CHAR(wxSpinCtrl::OnChar
)
57 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange
)
60 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // the margin between the up-down control and its buddy (can be arbitrary,
67 // choose what you like - or may be decide during run-time depending on the
69 static const int MARGIN_BETWEEN
= 1;
71 // ============================================================================
73 // ============================================================================
75 wxArraySpins
wxSpinCtrl::ms_allSpins
;
77 // ----------------------------------------------------------------------------
78 // wnd proc for the buddy text ctrl
79 // ----------------------------------------------------------------------------
81 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
86 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
88 // forward some messages (the key and focus ones only so far) to
98 spin
->MSWWindowProc(message
, wParam
, lParam
);
100 // The control may have been deleted at this point, so check.
101 if (!(::IsWindow(hwnd
) && ((wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
)) == spin
))
106 // we want to get WXK_RETURN in order to generate the event for it
107 return DLGC_WANTCHARS
;
110 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
111 hwnd
, message
, wParam
, lParam
);
115 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
117 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong((HWND
)hwndBuddy
,
120 int i
= ms_allSpins
.Index(spin
);
122 if ( i
== wxNOT_FOUND
)
126 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
127 _T("wxSpinCtrl has incorrect buddy HWND!") );
132 // process a WM_COMMAND generated by the buddy text control
133 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
139 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
140 event
.SetEventObject(this);
141 wxString val
= wxGetWindowText(m_hwndBuddy
);
142 event
.SetString(val
);
143 event
.SetInt(GetValue());
144 return GetEventHandler()->ProcessEvent(event
);
149 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
152 event
.SetEventObject( this );
153 return GetEventHandler()->ProcessEvent(event
);
163 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
165 switch ( event
.GetKeyCode() )
169 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
170 InitCommandEvent(event
);
171 wxString val
= wxGetWindowText(m_hwndBuddy
);
172 event
.SetString(val
);
173 event
.SetInt(GetValue());
174 if ( GetEventHandler()->ProcessEvent(event
) )
180 // always produce navigation event - even if we process TAB
181 // ourselves the fact that we got here means that the user code
182 // decided to skip processing of this TAB - probably to let it
183 // do its default job.
185 wxNavigationKeyEvent eventNav
;
186 eventNav
.SetDirection(!event
.ShiftDown());
187 eventNav
.SetWindowChange(event
.ControlDown());
188 eventNav
.SetEventObject(this);
190 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
196 // no, we didn't process it
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 bool wxSpinCtrl::Create(wxWindow
*parent
,
206 const wxString
& value
,
210 int min
, int max
, int initial
,
211 const wxString
& name
)
213 // before using DoGetBestSize(), have to set style to let the base class
214 // know whether this is a horizontal or vertical control (we're always
216 style
|= wxSP_VERTICAL
;
218 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
219 style
|= wxBORDER_SUNKEN
;
221 SetWindowStyle(style
);
223 // calculate the sizes: the size given is the toal size for both controls
224 // and we need to fit them both in the given width (height is the same)
225 wxSize
sizeText(size
), sizeBtn(size
);
226 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
227 if ( sizeText
.x
<= 0 )
229 // DEFAULT_ITEM_WIDTH is the default width for the text control
230 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
233 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
234 if ( sizeText
.x
<= 0 )
236 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
240 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
242 // create the spin button
243 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
252 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
253 int msStyle
= WS_CHILD
;
255 // Even with extended styles, need to combine with WS_BORDER for them to
257 if ( want3D
|| wxStyleHasBorder(style
) )
258 msStyle
|= WS_BORDER
;
260 if ( style
& wxCLIP_SIBLINGS
)
261 msStyle
|= WS_CLIPSIBLINGS
;
263 // create the text window
264 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
266 exStyle
, // sunken border
267 _T("EDIT"), // window class
268 NULL
, // no window title
269 msStyle
/* | WS_CLIPSIBLINGS */, // style (will be shown later)
270 pos
.x
, pos
.y
, // position
271 0, 0, // size (will be set later)
272 GetHwndOf(parent
), // parent
273 (HMENU
)-1, // control id
274 wxGetInstance(), // app instance
275 NULL
// unused client data
280 wxLogLastError(wxT("CreateWindow(buddy text window)"));
285 // subclass the text ctrl to be able to intercept some events
286 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
287 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
288 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
290 // should have the same font as the other controls
291 SetFont(GetParent()->GetFont());
293 // set the size of the text window - can do it only now, because we
294 // couldn't call DoGetBestSize() before as font wasn't set
295 if ( sizeText
.y
<= 0 )
298 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
300 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
303 DoMoveWindow(pos
.x
, pos
.y
,
304 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
306 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
308 // associate the text window with the spin button
309 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
311 if ( !value
.IsEmpty() )
316 // do it after finishing with m_hwndBuddy creation to avoid generating
317 // initial wxEVT_COMMAND_TEXT_UPDATED message
318 ms_allSpins
.Add(this);
323 wxSpinCtrl::~wxSpinCtrl()
325 ms_allSpins
.Remove(this);
327 // This removes spurious memory leak reporting
328 if (ms_allSpins
.GetCount() == 0)
331 // destroy the buddy window because this pointer which wxBuddyTextWndProc
332 // uses will not soon be valid any more
333 ::DestroyWindow(GetBuddyHwnd());
336 // ----------------------------------------------------------------------------
337 // wxTextCtrl-like methods
338 // ----------------------------------------------------------------------------
340 void wxSpinCtrl::SetValue(const wxString
& text
)
342 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
344 wxLogLastError(wxT("SetWindowText(buddy)"));
348 int wxSpinCtrl::GetValue() const
350 wxString val
= wxGetWindowText(m_hwndBuddy
);
353 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
359 void wxSpinCtrl::SetSelection(long from
, long to
)
361 // if from and to are both -1, it means (in wxWindows) that all text should
362 // be selected - translate into Windows convention
363 if ( (from
== -1) && (to
== -1) )
368 ::SendMessage((HWND
)m_hwndBuddy
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
371 // ----------------------------------------------------------------------------
372 // forward some methods to subcontrols
373 // ----------------------------------------------------------------------------
375 bool wxSpinCtrl::SetFont(const wxFont
& font
)
377 if ( !wxWindowBase::SetFont(font
) )
383 WXHANDLE hFont
= GetFont().GetResourceHandle();
384 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
389 bool wxSpinCtrl::Show(bool show
)
391 if ( !wxControl::Show(show
) )
396 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
401 bool wxSpinCtrl::Enable(bool enable
)
403 if ( !wxControl::Enable(enable
) )
408 ::EnableWindow(GetBuddyHwnd(), enable
);
413 void wxSpinCtrl::SetFocus()
415 ::SetFocus(GetBuddyHwnd());
418 // ----------------------------------------------------------------------------
420 // ----------------------------------------------------------------------------
422 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
424 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
425 event
.SetEventObject(this);
426 event
.SetInt(eventSpin
.GetPosition());
428 (void)GetEventHandler()->ProcessEvent(event
);
430 if ( eventSpin
.GetSkipped() )
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 wxSize
wxSpinCtrl::DoGetBestSize() const
442 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
443 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
446 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
447 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
451 // make the text tall enough
458 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
460 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
461 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
462 if ( widthText
<= 0 )
464 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
467 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
469 wxLogLastError(wxT("MoveWindow(buddy)"));
472 x
+= widthText
+ MARGIN_BETWEEN
;
473 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
475 wxLogLastError(wxT("MoveWindow"));
479 // get total size of the control
480 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
482 RECT spinrect
, textrect
, ctrlrect
;
483 GetWindowRect(GetHwnd(), &spinrect
);
484 GetWindowRect(GetBuddyHwnd(), &textrect
);
485 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
488 *x
= ctrlrect
.right
- ctrlrect
.left
;
490 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
493 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
495 // hack: pretend that our HWND is the text control just for a moment
496 WXHWND hWnd
= GetHWND();
497 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
499 wxSpinButton::DoGetPosition(x
, y
);
501 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;