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
))
102 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
103 hwnd
, message
, wParam
, lParam
);
107 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
109 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong((HWND
)hwndBuddy
,
112 int i
= ms_allSpins
.Index(spin
);
114 if ( i
== wxNOT_FOUND
)
118 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
119 _T("wxSpinCtrl has incorrect buddy HWND!") );
124 // process a WM_COMMAND generated by the buddy text control
125 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
131 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
132 event
.SetEventObject(this);
133 wxString val
= wxGetWindowText(m_hwndBuddy
);
134 event
.SetString(val
);
135 event
.SetInt(GetValue());
136 return GetEventHandler()->ProcessEvent(event
);
141 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
144 event
.SetEventObject( this );
145 return GetEventHandler()->ProcessEvent(event
);
155 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
157 switch ( event
.KeyCode() )
161 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
162 InitCommandEvent(event
);
163 wxString val
= wxGetWindowText(m_hwndBuddy
);
164 event
.SetString(val
);
165 event
.SetInt(GetValue());
166 if ( GetEventHandler()->ProcessEvent(event
) )
172 // always produce navigation event - even if we process TAB
173 // ourselves the fact that we got here means that the user code
174 // decided to skip processing of this TAB - probably to let it
175 // do its default job.
177 wxNavigationKeyEvent eventNav
;
178 eventNav
.SetDirection(!event
.ShiftDown());
179 eventNav
.SetWindowChange(event
.ControlDown());
180 eventNav
.SetEventObject(this);
182 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
188 // no, we didn't process it
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 bool wxSpinCtrl::Create(wxWindow
*parent
,
198 const wxString
& value
,
202 int min
, int max
, int initial
,
203 const wxString
& name
)
205 // before using DoGetBestSize(), have to set style to let the base class
206 // know whether this is a horizontal or vertical control (we're always
208 style
|= wxSP_VERTICAL
;
209 SetWindowStyle(style
);
211 // calculate the sizes: the size given is the toal size for both controls
212 // and we need to fit them both in the given width (height is the same)
213 wxSize
sizeText(size
), sizeBtn(size
);
214 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
215 if ( sizeText
.x
<= 0 )
217 // DEFAULT_ITEM_WIDTH is the default width for the text control
218 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
221 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
222 if ( sizeText
.x
<= 0 )
224 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
228 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
230 // create the spin button
231 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
240 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
241 int msStyle
= WS_CHILD
;
243 // Even with extended styles, need to combine with WS_BORDER for them to
245 if ( want3D
|| wxStyleHasBorder(style
) )
246 msStyle
|= WS_BORDER
;
248 // create the text window
249 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
251 exStyle
, // sunken border
252 _T("EDIT"), // window class
253 NULL
, // no window title
254 msStyle
/* | WS_CLIPSIBLINGS */, // style (will be shown later)
255 pos
.x
, pos
.y
, // position
256 0, 0, // size (will be set later)
257 GetHwndOf(parent
), // parent
258 (HMENU
)-1, // control id
259 wxGetInstance(), // app instance
260 NULL
// unused client data
265 wxLogLastError(wxT("CreateWindow(buddy text window)"));
270 // subclass the text ctrl to be able to intercept some events
271 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
272 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
273 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
275 // should have the same font as the other controls
276 SetFont(GetParent()->GetFont());
278 // set the size of the text window - can do it only now, because we
279 // couldn't call DoGetBestSize() before as font wasn't set
280 if ( sizeText
.y
<= 0 )
283 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
285 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
288 DoMoveWindow(pos
.x
, pos
.y
,
289 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
291 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
293 // associate the text window with the spin button
294 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
296 if ( !value
.IsEmpty() )
301 // do it after finishing with m_hwndBuddy creation to avoid generating
302 // initial wxEVT_COMMAND_TEXT_UPDATED message
303 ms_allSpins
.Add(this);
308 wxSpinCtrl::~wxSpinCtrl()
310 ms_allSpins
.Remove(this);
312 // destroy the buddy window because this pointer which wxBuddyTextWndProc
313 // uses will not soon be valid any more
314 ::DestroyWindow(GetBuddyHwnd());
317 // ----------------------------------------------------------------------------
318 // wxTextCtrl-like methods
319 // ----------------------------------------------------------------------------
321 void wxSpinCtrl::SetValue(const wxString
& text
)
323 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
325 wxLogLastError(wxT("SetWindowText(buddy)"));
329 int wxSpinCtrl::GetValue() const
331 wxString val
= wxGetWindowText(m_hwndBuddy
);
334 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
340 // ----------------------------------------------------------------------------
341 // forward some methods to subcontrols
342 // ----------------------------------------------------------------------------
344 bool wxSpinCtrl::SetFont(const wxFont
& font
)
346 if ( !wxWindowBase::SetFont(font
) )
352 WXHANDLE hFont
= GetFont().GetResourceHandle();
353 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
358 bool wxSpinCtrl::Show(bool show
)
360 if ( !wxControl::Show(show
) )
365 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
370 bool wxSpinCtrl::Enable(bool enable
)
372 if ( !wxControl::Enable(enable
) )
377 ::EnableWindow(GetBuddyHwnd(), enable
);
382 void wxSpinCtrl::SetFocus()
384 ::SetFocus(GetBuddyHwnd());
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
391 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
393 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
394 event
.SetEventObject(this);
395 event
.SetInt(eventSpin
.GetPosition());
397 (void)GetEventHandler()->ProcessEvent(event
);
399 if ( eventSpin
.GetSkipped() )
405 // ----------------------------------------------------------------------------
407 // ----------------------------------------------------------------------------
409 wxSize
wxSpinCtrl::DoGetBestSize() const
411 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
412 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
415 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
416 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
420 // make the text tall enough
427 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
429 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
430 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
431 if ( widthText
<= 0 )
433 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
436 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
438 wxLogLastError(wxT("MoveWindow(buddy)"));
441 x
+= widthText
+ MARGIN_BETWEEN
;
442 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
444 wxLogLastError(wxT("MoveWindow"));
448 // get total size of the control
449 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
451 RECT spinrect
, textrect
, ctrlrect
;
452 GetWindowRect(GetHwnd(), &spinrect
);
453 GetWindowRect(GetBuddyHwnd(), &textrect
);
454 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
457 *x
= ctrlrect
.right
- ctrlrect
.left
;
459 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
462 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
464 // hack: pretend that our HWND is the text control just for a moment
465 WXHWND hWnd
= GetHWND();
466 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
468 wxSpinButton::DoGetPosition(x
, y
);
470 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;