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 // ============================================================================
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
)
58 value wxSP_DEFAULT_VALUE
64 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
65 EVT_CHAR(wxSpinCtrl::OnChar
)
67 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
69 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange
)
72 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 // the margin between the up-down control and its buddy (can be arbitrary,
79 // choose what you like - or may be decide during run-time depending on the
81 static const int MARGIN_BETWEEN
= 1;
83 // ============================================================================
85 // ============================================================================
87 wxArraySpins
wxSpinCtrl::ms_allSpins
;
89 // ----------------------------------------------------------------------------
90 // wnd proc for the buddy text ctrl
91 // ----------------------------------------------------------------------------
93 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
98 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
100 // forward some messages (the key and focus ones only so far) to
105 // if the focus comes from the spin control itself, don't set it
106 // back to it -- we don't want to go into an infinite loop
107 if ( wParam
== spin
->GetHWND() )
116 spin
->MSWWindowProc(message
, wParam
, lParam
);
118 // The control may have been deleted at this point, so check.
119 if (!(::IsWindow(hwnd
) && ((wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
)) == spin
))
124 // we want to get WXK_RETURN in order to generate the event for it
125 return DLGC_WANTCHARS
;
128 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
129 hwnd
, message
, wParam
, lParam
);
133 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
135 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong((HWND
)hwndBuddy
,
138 int i
= ms_allSpins
.Index(spin
);
140 if ( i
== wxNOT_FOUND
)
144 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
145 _T("wxSpinCtrl has incorrect buddy HWND!") );
150 // process a WM_COMMAND generated by the buddy text control
151 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
157 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
158 event
.SetEventObject(this);
159 wxString val
= wxGetWindowText(m_hwndBuddy
);
160 event
.SetString(val
);
161 event
.SetInt(GetValue());
162 return GetEventHandler()->ProcessEvent(event
);
167 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
170 event
.SetEventObject( this );
171 return GetEventHandler()->ProcessEvent(event
);
181 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
183 switch ( event
.GetKeyCode() )
187 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
188 InitCommandEvent(event
);
189 wxString val
= wxGetWindowText(m_hwndBuddy
);
190 event
.SetString(val
);
191 event
.SetInt(GetValue());
192 if ( GetEventHandler()->ProcessEvent(event
) )
198 // always produce navigation event - even if we process TAB
199 // ourselves the fact that we got here means that the user code
200 // decided to skip processing of this TAB - probably to let it
201 // do its default job.
203 wxNavigationKeyEvent eventNav
;
204 eventNav
.SetDirection(!event
.ShiftDown());
205 eventNav
.SetWindowChange(event
.ControlDown());
206 eventNav
.SetEventObject(this);
208 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
214 // no, we didn't process it
218 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
220 // when we get focus, give it to our buddy window as it needs it more than
222 ::SetFocus((HWND
)m_hwndBuddy
);
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 bool wxSpinCtrl::Create(wxWindow
*parent
,
233 const wxString
& value
,
237 int min
, int max
, int initial
,
238 const wxString
& name
)
240 // before using DoGetBestSize(), have to set style to let the base class
241 // know whether this is a horizontal or vertical control (we're always
243 style
|= wxSP_VERTICAL
;
245 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
246 style
|= wxBORDER_SUNKEN
;
248 SetWindowStyle(style
);
251 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
253 // calculate the sizes: the size given is the toal size for both controls
254 // and we need to fit them both in the given width (height is the same)
255 wxSize
sizeText(size
), sizeBtn(size
);
256 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
257 if ( sizeText
.x
<= 0 )
259 // DEFAULT_ITEM_WIDTH is the default width for the text control
260 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
263 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
264 if ( sizeText
.x
<= 0 )
266 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
270 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
272 // we must create the text control before the spin button for the purpose
273 // of the dialog navigation: if there is a static text just before the spin
274 // control, activating it by Alt-letter should give focus to the text
275 // control, not the spin and the dialog navigation code will give focus to
276 // the next control (at Windows level), not the one after it
278 // create the text window
280 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
282 exStyle
, // sunken border
283 _T("EDIT"), // window class
284 NULL
, // no window title
285 msStyle
, // style (will be shown later)
286 pos
.x
, pos
.y
, // position
287 0, 0, // size (will be set later)
288 GetHwndOf(parent
), // parent
289 (HMENU
)-1, // control id
290 wxGetInstance(), // app instance
291 NULL
// unused client data
296 wxLogLastError(wxT("CreateWindow(buddy text window)"));
302 // create the spin button
303 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
311 // subclass the text ctrl to be able to intercept some events
312 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
313 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
314 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
316 // should have the same font as the other controls
317 SetFont(GetParent()->GetFont());
319 // set the size of the text window - can do it only now, because we
320 // couldn't call DoGetBestSize() before as font wasn't set
321 if ( sizeText
.y
<= 0 )
324 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
326 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
329 DoMoveWindow(pos
.x
, pos
.y
,
330 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
332 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
334 // associate the text window with the spin button
335 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
337 if ( !value
.IsEmpty() )
342 // do it after finishing with m_hwndBuddy creation to avoid generating
343 // initial wxEVT_COMMAND_TEXT_UPDATED message
344 ms_allSpins
.Add(this);
349 wxSpinCtrl::~wxSpinCtrl()
351 ms_allSpins
.Remove(this);
353 // This removes spurious memory leak reporting
354 if (ms_allSpins
.GetCount() == 0)
357 // destroy the buddy window because this pointer which wxBuddyTextWndProc
358 // uses will not soon be valid any more
359 ::DestroyWindow(GetBuddyHwnd());
362 // ----------------------------------------------------------------------------
363 // wxTextCtrl-like methods
364 // ----------------------------------------------------------------------------
366 void wxSpinCtrl::SetValue(const wxString
& text
)
368 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
370 wxLogLastError(wxT("SetWindowText(buddy)"));
374 int wxSpinCtrl::GetValue() const
376 wxString val
= wxGetWindowText(m_hwndBuddy
);
379 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
385 void wxSpinCtrl::SetSelection(long from
, long to
)
387 // if from and to are both -1, it means (in wxWindows) that all text should
388 // be selected - translate into Windows convention
389 if ( (from
== -1) && (to
== -1) )
394 ::SendMessage((HWND
)m_hwndBuddy
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
397 // ----------------------------------------------------------------------------
398 // forward some methods to subcontrols
399 // ----------------------------------------------------------------------------
401 bool wxSpinCtrl::SetFont(const wxFont
& font
)
403 if ( !wxWindowBase::SetFont(font
) )
409 WXHANDLE hFont
= GetFont().GetResourceHandle();
410 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
415 bool wxSpinCtrl::Show(bool show
)
417 if ( !wxControl::Show(show
) )
422 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
427 bool wxSpinCtrl::Enable(bool enable
)
429 if ( !wxControl::Enable(enable
) )
434 ::EnableWindow(GetBuddyHwnd(), enable
);
439 void wxSpinCtrl::SetFocus()
441 ::SetFocus(GetBuddyHwnd());
444 // ----------------------------------------------------------------------------
446 // ----------------------------------------------------------------------------
448 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
450 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
451 event
.SetEventObject(this);
452 event
.SetInt(eventSpin
.GetPosition());
454 (void)GetEventHandler()->ProcessEvent(event
);
456 if ( eventSpin
.GetSkipped() )
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
466 wxSize
wxSpinCtrl::DoGetBestSize() const
468 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
469 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
472 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
473 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
477 // make the text tall enough
484 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
486 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
487 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
488 if ( widthText
<= 0 )
490 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
493 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
495 wxLogLastError(wxT("MoveWindow(buddy)"));
498 x
+= widthText
+ MARGIN_BETWEEN
;
499 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
501 wxLogLastError(wxT("MoveWindow"));
505 // get total size of the control
506 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
508 RECT spinrect
, textrect
, ctrlrect
;
509 GetWindowRect(GetHwnd(), &spinrect
);
510 GetWindowRect(GetBuddyHwnd(), &textrect
);
511 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
514 *x
= ctrlrect
.right
- ctrlrect
.left
;
516 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
519 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
521 // hack: pretend that our HWND is the text control just for a moment
522 WXHWND hWnd
= GetHWND();
523 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
525 wxSpinButton::DoGetPosition(x
, y
);
527 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;