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__))
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 ones only so far) to the spin ctrl
95 spin
->MSWWindowProc(message
, wParam
, lParam
);
97 // The control may have been deleted at this point, so check.
98 if (!(::IsWindow(hwnd
) && ((wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
)) == spin
))
103 // we want to get WXK_RETURN in order to generate the event for it
104 return DLGC_WANTCHARS
;
107 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
108 hwnd
, message
, wParam
, lParam
);
112 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
114 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong((HWND
)hwndBuddy
,
117 int i
= ms_allSpins
.Index(spin
);
119 if ( i
== wxNOT_FOUND
)
123 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
124 _T("wxSpinCtrl has incorrect buddy HWND!") );
129 // process a WM_COMMAND generated by the buddy text control
130 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
136 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
137 event
.SetEventObject(this);
138 wxString val
= wxGetWindowText(m_hwndBuddy
);
139 event
.SetString(val
);
140 event
.SetInt(GetValue());
141 return GetEventHandler()->ProcessEvent(event
);
146 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
149 event
.SetEventObject( this );
150 return GetEventHandler()->ProcessEvent(event
);
160 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
162 switch ( event
.KeyCode() )
166 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
167 InitCommandEvent(event
);
168 wxString val
= wxGetWindowText(m_hwndBuddy
);
169 event
.SetString(val
);
170 event
.SetInt(GetValue());
171 if ( GetEventHandler()->ProcessEvent(event
) )
177 // always produce navigation event - even if we process TAB
178 // ourselves the fact that we got here means that the user code
179 // decided to skip processing of this TAB - probably to let it
180 // do its default job.
182 wxNavigationKeyEvent eventNav
;
183 eventNav
.SetDirection(!event
.ShiftDown());
184 eventNav
.SetWindowChange(event
.ControlDown());
185 eventNav
.SetEventObject(this);
187 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
193 // no, we didn't process it
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 bool wxSpinCtrl::Create(wxWindow
*parent
,
203 const wxString
& value
,
207 int min
, int max
, int initial
,
208 const wxString
& name
)
210 // before using DoGetBestSize(), have to set style to let the base class
211 // know whether this is a horizontal or vertical control (we're always
213 style
|= wxSP_VERTICAL
;
214 SetWindowStyle(style
);
216 // calculate the sizes: the size given is the toal size for both controls
217 // and we need to fit them both in the given width (height is the same)
218 wxSize
sizeText(size
), sizeBtn(size
);
219 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
220 if ( sizeText
.x
<= 0 )
222 // DEFAULT_ITEM_WIDTH is the default width for the text control
223 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
226 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
227 if ( sizeText
.x
<= 0 )
229 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
233 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
235 // create the spin button
236 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
245 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
246 int msStyle
= WS_CHILD
;
248 // Even with extended styles, need to combine with WS_BORDER for them to
250 if ( want3D
|| wxStyleHasBorder(style
) )
251 msStyle
|= WS_BORDER
;
253 if ( style
& wxCLIP_SIBLINGS
)
254 msStyle
|= WS_CLIPSIBLINGS
;
256 // create the text window
257 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
259 exStyle
, // sunken border
260 _T("EDIT"), // window class
261 NULL
, // no window title
262 msStyle
/* | WS_CLIPSIBLINGS */, // style (will be shown later)
263 pos
.x
, pos
.y
, // position
264 0, 0, // size (will be set later)
265 GetHwndOf(parent
), // parent
266 (HMENU
)-1, // control id
267 wxGetInstance(), // app instance
268 NULL
// unused client data
273 wxLogLastError(wxT("CreateWindow(buddy text window)"));
278 // subclass the text ctrl to be able to intercept some events
279 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
280 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
281 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
283 // should have the same font as the other controls
284 SetFont(GetParent()->GetFont());
286 // set the size of the text window - can do it only now, because we
287 // couldn't call DoGetBestSize() before as font wasn't set
288 if ( sizeText
.y
<= 0 )
291 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
293 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
296 DoMoveWindow(pos
.x
, pos
.y
,
297 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
299 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
301 // associate the text window with the spin button
302 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
304 if ( !value
.IsEmpty() )
309 // do it after finishing with m_hwndBuddy creation to avoid generating
310 // initial wxEVT_COMMAND_TEXT_UPDATED message
311 ms_allSpins
.Add(this);
316 wxSpinCtrl::~wxSpinCtrl()
318 ms_allSpins
.Remove(this);
320 // destroy the buddy window because this pointer which wxBuddyTextWndProc
321 // uses will not soon be valid any more
322 ::DestroyWindow(GetBuddyHwnd());
325 // ----------------------------------------------------------------------------
326 // wxTextCtrl-like methods
327 // ----------------------------------------------------------------------------
329 void wxSpinCtrl::SetValue(const wxString
& text
)
331 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
333 wxLogLastError(wxT("SetWindowText(buddy)"));
337 int wxSpinCtrl::GetValue() const
339 wxString val
= wxGetWindowText(m_hwndBuddy
);
342 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
348 // ----------------------------------------------------------------------------
349 // forward some methods to subcontrols
350 // ----------------------------------------------------------------------------
352 bool wxSpinCtrl::SetFont(const wxFont
& font
)
354 if ( !wxWindowBase::SetFont(font
) )
360 WXHANDLE hFont
= GetFont().GetResourceHandle();
361 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
366 bool wxSpinCtrl::Show(bool show
)
368 if ( !wxControl::Show(show
) )
373 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
378 bool wxSpinCtrl::Enable(bool enable
)
380 if ( !wxControl::Enable(enable
) )
385 ::EnableWindow(GetBuddyHwnd(), enable
);
390 void wxSpinCtrl::SetFocus()
392 ::SetFocus(GetBuddyHwnd());
395 // ----------------------------------------------------------------------------
397 // ----------------------------------------------------------------------------
399 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
401 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
402 event
.SetEventObject(this);
403 event
.SetInt(eventSpin
.GetPosition());
405 (void)GetEventHandler()->ProcessEvent(event
);
407 if ( eventSpin
.GetSkipped() )
413 // ----------------------------------------------------------------------------
415 // ----------------------------------------------------------------------------
417 wxSize
wxSpinCtrl::DoGetBestSize() const
419 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
420 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
423 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
424 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
428 // make the text tall enough
435 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
437 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
438 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
439 if ( widthText
<= 0 )
441 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
444 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
446 wxLogLastError(wxT("MoveWindow(buddy)"));
449 x
+= widthText
+ MARGIN_BETWEEN
;
450 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
452 wxLogLastError(wxT("MoveWindow"));
456 // get total size of the control
457 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
459 RECT spinrect
, textrect
, ctrlrect
;
460 GetWindowRect(GetHwnd(), &spinrect
);
461 GetWindowRect(GetBuddyHwnd(), &textrect
);
462 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
465 *x
= ctrlrect
.right
- ctrlrect
.left
;
467 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
470 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
472 // hack: pretend that our HWND is the text control just for a moment
473 WXHWND hWnd
= GetHWND();
474 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
476 wxSpinButton::DoGetPosition(x
, y
);
478 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;