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 if ( style
& wxCLIP_SIBLINGS
)
249 msStyle
|= WS_CLIPSIBLINGS
;
252 // create the text window
253 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
255 exStyle
, // sunken border
256 _T("EDIT"), // window class
257 NULL
, // no window title
258 msStyle
/* | WS_CLIPSIBLINGS */, // style (will be shown later)
259 pos
.x
, pos
.y
, // position
260 0, 0, // size (will be set later)
261 GetHwndOf(parent
), // parent
262 (HMENU
)-1, // control id
263 wxGetInstance(), // app instance
264 NULL
// unused client data
269 wxLogLastError(wxT("CreateWindow(buddy text window)"));
274 // subclass the text ctrl to be able to intercept some events
275 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
276 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
277 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
279 // should have the same font as the other controls
280 SetFont(GetParent()->GetFont());
282 // set the size of the text window - can do it only now, because we
283 // couldn't call DoGetBestSize() before as font wasn't set
284 if ( sizeText
.y
<= 0 )
287 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
289 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
292 DoMoveWindow(pos
.x
, pos
.y
,
293 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
295 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
297 // associate the text window with the spin button
298 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
300 if ( !value
.IsEmpty() )
305 // do it after finishing with m_hwndBuddy creation to avoid generating
306 // initial wxEVT_COMMAND_TEXT_UPDATED message
307 ms_allSpins
.Add(this);
312 wxSpinCtrl::~wxSpinCtrl()
314 ms_allSpins
.Remove(this);
316 // destroy the buddy window because this pointer which wxBuddyTextWndProc
317 // uses will not soon be valid any more
318 ::DestroyWindow(GetBuddyHwnd());
321 // ----------------------------------------------------------------------------
322 // wxTextCtrl-like methods
323 // ----------------------------------------------------------------------------
325 void wxSpinCtrl::SetValue(const wxString
& text
)
327 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
329 wxLogLastError(wxT("SetWindowText(buddy)"));
333 int wxSpinCtrl::GetValue() const
335 wxString val
= wxGetWindowText(m_hwndBuddy
);
338 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
344 // ----------------------------------------------------------------------------
345 // forward some methods to subcontrols
346 // ----------------------------------------------------------------------------
348 bool wxSpinCtrl::SetFont(const wxFont
& font
)
350 if ( !wxWindowBase::SetFont(font
) )
356 WXHANDLE hFont
= GetFont().GetResourceHandle();
357 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
362 bool wxSpinCtrl::Show(bool show
)
364 if ( !wxControl::Show(show
) )
369 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
374 bool wxSpinCtrl::Enable(bool enable
)
376 if ( !wxControl::Enable(enable
) )
381 ::EnableWindow(GetBuddyHwnd(), enable
);
386 void wxSpinCtrl::SetFocus()
388 ::SetFocus(GetBuddyHwnd());
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
397 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
398 event
.SetEventObject(this);
399 event
.SetInt(eventSpin
.GetPosition());
401 (void)GetEventHandler()->ProcessEvent(event
);
403 if ( eventSpin
.GetSkipped() )
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
413 wxSize
wxSpinCtrl::DoGetBestSize() const
415 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
416 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
419 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
420 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
424 // make the text tall enough
431 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
433 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
434 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
435 if ( widthText
<= 0 )
437 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
440 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
442 wxLogLastError(wxT("MoveWindow(buddy)"));
445 x
+= widthText
+ MARGIN_BETWEEN
;
446 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
448 wxLogLastError(wxT("MoveWindow"));
452 // get total size of the control
453 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
455 RECT spinrect
, textrect
, ctrlrect
;
456 GetWindowRect(GetHwnd(), &spinrect
);
457 GetWindowRect(GetBuddyHwnd(), &textrect
);
458 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
461 *x
= ctrlrect
.right
- ctrlrect
.left
;
463 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
466 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
468 // hack: pretend that our HWND is the text control just for a moment
469 WXHWND hWnd
= GetHWND();
470 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
472 wxSpinButton::DoGetPosition(x
, y
);
474 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;