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
;
251 // we want to get WXK_RETURN in order to generate the event for it
252 m_lDlgCode
= DLGC_WANTCHARS
;
254 // create the text window
255 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
257 exStyle
, // sunken border
258 _T("EDIT"), // window class
259 NULL
, // no window title
260 msStyle
/* | WS_CLIPSIBLINGS */, // style (will be shown later)
261 pos
.x
, pos
.y
, // position
262 0, 0, // size (will be set later)
263 GetHwndOf(parent
), // parent
264 (HMENU
)-1, // control id
265 wxGetInstance(), // app instance
266 NULL
// unused client data
271 wxLogLastError(wxT("CreateWindow(buddy text window)"));
276 // subclass the text ctrl to be able to intercept some events
277 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
278 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
279 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
281 // should have the same font as the other controls
282 SetFont(GetParent()->GetFont());
284 // set the size of the text window - can do it only now, because we
285 // couldn't call DoGetBestSize() before as font wasn't set
286 if ( sizeText
.y
<= 0 )
289 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
291 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
294 DoMoveWindow(pos
.x
, pos
.y
,
295 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
297 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
299 // associate the text window with the spin button
300 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
302 if ( !value
.IsEmpty() )
307 // do it after finishing with m_hwndBuddy creation to avoid generating
308 // initial wxEVT_COMMAND_TEXT_UPDATED message
309 ms_allSpins
.Add(this);
314 wxSpinCtrl
::~wxSpinCtrl()
316 ms_allSpins
.Remove(this);
318 // destroy the buddy window because this pointer which wxBuddyTextWndProc
319 // uses will not soon be valid any more
320 ::DestroyWindow(GetBuddyHwnd());
323 // ----------------------------------------------------------------------------
324 // wxTextCtrl-like methods
325 // ----------------------------------------------------------------------------
327 void wxSpinCtrl
::SetValue(const wxString
& text
)
329 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
331 wxLogLastError(wxT("SetWindowText(buddy)"));
335 int wxSpinCtrl
::GetValue() const
337 wxString val
= wxGetWindowText(m_hwndBuddy
);
340 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
346 // ----------------------------------------------------------------------------
347 // forward some methods to subcontrols
348 // ----------------------------------------------------------------------------
350 bool wxSpinCtrl
::SetFont(const wxFont
& font
)
352 if ( !wxWindowBase
::SetFont(font
) )
358 WXHANDLE hFont
= GetFont().GetResourceHandle();
359 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
364 bool wxSpinCtrl
::Show(bool show
)
366 if ( !wxControl
::Show(show
) )
371 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW
: SW_HIDE
);
376 bool wxSpinCtrl
::Enable(bool enable
)
378 if ( !wxControl
::Enable(enable
) )
383 ::EnableWindow(GetBuddyHwnd(), enable
);
388 void wxSpinCtrl
::SetFocus()
390 ::SetFocus(GetBuddyHwnd());
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 void wxSpinCtrl
::OnSpinChange(wxSpinEvent
& eventSpin
)
399 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
400 event
.SetEventObject(this);
401 event
.SetInt(eventSpin
.GetPosition());
403 (void)GetEventHandler()->ProcessEvent(event
);
405 if ( eventSpin
.GetSkipped() )
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 wxSize wxSpinCtrl
::DoGetBestSize() const
417 wxSize sizeBtn
= wxSpinButton
::DoGetBestSize();
418 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
421 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
422 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
426 // make the text tall enough
433 void wxSpinCtrl
::DoMoveWindow(int x
, int y
, int width
, int height
)
435 int widthBtn
= wxSpinButton
::DoGetBestSize().x
;
436 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
437 if ( widthText
<= 0 )
439 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
442 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
444 wxLogLastError(wxT("MoveWindow(buddy)"));
447 x
+= widthText
+ MARGIN_BETWEEN
;
448 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
450 wxLogLastError(wxT("MoveWindow"));
454 // get total size of the control
455 void wxSpinCtrl
::DoGetSize(int *x
, int *y
) const
457 RECT spinrect
, textrect
, ctrlrect
;
458 GetWindowRect(GetHwnd(), &spinrect
);
459 GetWindowRect(GetBuddyHwnd(), &textrect
);
460 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
463 *x
= ctrlrect
.right
- ctrlrect
.left
;
465 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
468 void wxSpinCtrl
::DoGetPosition(int *x
, int *y
) const
470 // hack: pretend that our HWND is the text control just for a moment
471 WXHWND hWnd
= GetHWND();
472 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
474 wxSpinButton
::DoGetPosition(x
, y
);
476 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;