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(__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
)
58 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
60 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange
)
63 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // the margin between the up-down control and its buddy (can be arbitrary,
70 // choose what you like - or may be decide during run-time depending on the
72 static const int MARGIN_BETWEEN
= 1;
74 // ============================================================================
76 // ============================================================================
78 wxArraySpins
wxSpinCtrl::ms_allSpins
;
80 // ----------------------------------------------------------------------------
81 // wnd proc for the buddy text ctrl
82 // ----------------------------------------------------------------------------
84 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
89 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
91 // forward some messages (the key and focus ones only so far) to
96 // if the focus comes from the spin control itself, don't set it
97 // back to it -- we don't want to go into an infinite loop
98 if ( wParam
== spin
->GetHWND() )
107 spin
->MSWWindowProc(message
, wParam
, lParam
);
109 // The control may have been deleted at this point, so check.
110 if (!(::IsWindow(hwnd
) && ((wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
)) == spin
))
115 // we want to get WXK_RETURN in order to generate the event for it
116 return DLGC_WANTCHARS
;
119 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
120 hwnd
, message
, wParam
, lParam
);
124 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
126 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong((HWND
)hwndBuddy
,
129 int i
= ms_allSpins
.Index(spin
);
131 if ( i
== wxNOT_FOUND
)
135 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
136 _T("wxSpinCtrl has incorrect buddy HWND!") );
141 // process a WM_COMMAND generated by the buddy text control
142 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
148 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
149 event
.SetEventObject(this);
150 wxString val
= wxGetWindowText(m_hwndBuddy
);
151 event
.SetString(val
);
152 event
.SetInt(GetValue());
153 return GetEventHandler()->ProcessEvent(event
);
158 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
161 event
.SetEventObject( this );
162 return GetEventHandler()->ProcessEvent(event
);
172 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
174 switch ( event
.GetKeyCode() )
178 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
179 InitCommandEvent(event
);
180 wxString val
= wxGetWindowText(m_hwndBuddy
);
181 event
.SetString(val
);
182 event
.SetInt(GetValue());
183 if ( GetEventHandler()->ProcessEvent(event
) )
189 // always produce navigation event - even if we process TAB
190 // ourselves the fact that we got here means that the user code
191 // decided to skip processing of this TAB - probably to let it
192 // do its default job.
194 wxNavigationKeyEvent eventNav
;
195 eventNav
.SetDirection(!event
.ShiftDown());
196 eventNav
.SetWindowChange(event
.ControlDown());
197 eventNav
.SetEventObject(this);
199 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
205 // no, we didn't process it
209 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
211 // when we get focus, give it to our buddy window as it needs it more than
213 ::SetFocus((HWND
)m_hwndBuddy
);
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 bool wxSpinCtrl::Create(wxWindow
*parent
,
224 const wxString
& value
,
228 int min
, int max
, int initial
,
229 const wxString
& name
)
231 // before using DoGetBestSize(), have to set style to let the base class
232 // know whether this is a horizontal or vertical control (we're always
234 style
|= wxSP_VERTICAL
;
236 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
237 style
|= wxBORDER_SUNKEN
;
239 SetWindowStyle(style
);
242 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
244 // calculate the sizes: the size given is the toal size for both controls
245 // and we need to fit them both in the given width (height is the same)
246 wxSize
sizeText(size
), sizeBtn(size
);
247 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
248 if ( sizeText
.x
<= 0 )
250 // DEFAULT_ITEM_WIDTH is the default width for the text control
251 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
254 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
255 if ( sizeText
.x
<= 0 )
257 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
261 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
263 // we must create the text control before the spin button for the purpose
264 // of the dialog navigation: if there is a static text just before the spin
265 // control, activating it by Alt-letter should give focus to the text
266 // control, not the spin and the dialog navigation code will give focus to
267 // the next control (at Windows level), not the one after it
269 // create the text window
271 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
273 exStyle
, // sunken border
274 _T("EDIT"), // window class
275 NULL
, // no window title
276 msStyle
, // style (will be shown later)
277 pos
.x
, pos
.y
, // position
278 0, 0, // size (will be set later)
279 GetHwndOf(parent
), // parent
280 (HMENU
)-1, // control id
281 wxGetInstance(), // app instance
282 NULL
// unused client data
287 wxLogLastError(wxT("CreateWindow(buddy text window)"));
293 // create the spin button
294 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
302 // subclass the text ctrl to be able to intercept some events
303 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
304 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
305 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
307 // should have the same font as the other controls
308 SetFont(GetParent()->GetFont());
310 // set the size of the text window - can do it only now, because we
311 // couldn't call DoGetBestSize() before as font wasn't set
312 if ( sizeText
.y
<= 0 )
315 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
317 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
320 DoMoveWindow(pos
.x
, pos
.y
,
321 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
323 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
325 // associate the text window with the spin button
326 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
328 if ( !value
.IsEmpty() )
333 // do it after finishing with m_hwndBuddy creation to avoid generating
334 // initial wxEVT_COMMAND_TEXT_UPDATED message
335 ms_allSpins
.Add(this);
340 wxSpinCtrl::~wxSpinCtrl()
342 ms_allSpins
.Remove(this);
344 // This removes spurious memory leak reporting
345 if (ms_allSpins
.GetCount() == 0)
348 // destroy the buddy window because this pointer which wxBuddyTextWndProc
349 // uses will not soon be valid any more
350 ::DestroyWindow(GetBuddyHwnd());
353 // ----------------------------------------------------------------------------
354 // wxTextCtrl-like methods
355 // ----------------------------------------------------------------------------
357 void wxSpinCtrl::SetValue(const wxString
& text
)
359 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
361 wxLogLastError(wxT("SetWindowText(buddy)"));
365 int wxSpinCtrl::GetValue() const
367 wxString val
= wxGetWindowText(m_hwndBuddy
);
370 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
376 void wxSpinCtrl::SetSelection(long from
, long to
)
378 // if from and to are both -1, it means (in wxWindows) that all text should
379 // be selected - translate into Windows convention
380 if ( (from
== -1) && (to
== -1) )
385 ::SendMessage((HWND
)m_hwndBuddy
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
388 // ----------------------------------------------------------------------------
389 // forward some methods to subcontrols
390 // ----------------------------------------------------------------------------
392 bool wxSpinCtrl::SetFont(const wxFont
& font
)
394 if ( !wxWindowBase::SetFont(font
) )
400 WXHANDLE hFont
= GetFont().GetResourceHandle();
401 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
406 bool wxSpinCtrl::Show(bool show
)
408 if ( !wxControl::Show(show
) )
413 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
418 bool wxSpinCtrl::Enable(bool enable
)
420 if ( !wxControl::Enable(enable
) )
425 ::EnableWindow(GetBuddyHwnd(), enable
);
430 void wxSpinCtrl::SetFocus()
432 ::SetFocus(GetBuddyHwnd());
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
439 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
441 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
442 event
.SetEventObject(this);
443 event
.SetInt(eventSpin
.GetPosition());
445 (void)GetEventHandler()->ProcessEvent(event
);
447 if ( eventSpin
.GetSkipped() )
453 // ----------------------------------------------------------------------------
455 // ----------------------------------------------------------------------------
457 wxSize
wxSpinCtrl::DoGetBestSize() const
459 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
460 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
463 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
464 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
468 // make the text tall enough
475 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
477 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
478 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
479 if ( widthText
<= 0 )
481 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
484 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
486 wxLogLastError(wxT("MoveWindow(buddy)"));
489 x
+= widthText
+ MARGIN_BETWEEN
;
490 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
492 wxLogLastError(wxT("MoveWindow"));
496 // get total size of the control
497 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
499 RECT spinrect
, textrect
, ctrlrect
;
500 GetWindowRect(GetHwnd(), &spinrect
);
501 GetWindowRect(GetBuddyHwnd(), &textrect
);
502 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
505 *x
= ctrlrect
.right
- ctrlrect
.left
;
507 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
510 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
512 // hack: pretend that our HWND is the text control just for a moment
513 WXHWND hWnd
= GetHWND();
514 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
516 wxSpinButton::DoGetPosition(x
, y
);
518 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;