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 #if wxUSE_EXTENDED_RTTI
54 WX_DEFINE_FLAGS( wxSpinCtrlStyle
)
56 WX_BEGIN_FLAGS( wxSpinCtrlStyle
)
57 // new style border flags, we put them first to
58 // use them for streaming out
59 WX_FLAGS_MEMBER(wxBORDER_SIMPLE
)
60 WX_FLAGS_MEMBER(wxBORDER_SUNKEN
)
61 WX_FLAGS_MEMBER(wxBORDER_DOUBLE
)
62 WX_FLAGS_MEMBER(wxBORDER_RAISED
)
63 WX_FLAGS_MEMBER(wxBORDER_STATIC
)
64 WX_FLAGS_MEMBER(wxBORDER_NONE
)
66 // old style border flags
67 WX_FLAGS_MEMBER(wxSIMPLE_BORDER
)
68 WX_FLAGS_MEMBER(wxSUNKEN_BORDER
)
69 WX_FLAGS_MEMBER(wxDOUBLE_BORDER
)
70 WX_FLAGS_MEMBER(wxRAISED_BORDER
)
71 WX_FLAGS_MEMBER(wxSTATIC_BORDER
)
72 WX_FLAGS_MEMBER(wxNO_BORDER
)
74 // standard window styles
75 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL
)
76 WX_FLAGS_MEMBER(wxCLIP_CHILDREN
)
77 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
78 WX_FLAGS_MEMBER(wxWANTS_CHARS
)
79 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
80 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB
)
81 WX_FLAGS_MEMBER(wxVSCROLL
)
82 WX_FLAGS_MEMBER(wxHSCROLL
)
84 WX_FLAGS_MEMBER(wxSP_HORIZONTAL
)
85 WX_FLAGS_MEMBER(wxSP_VERTICAL
)
86 WX_FLAGS_MEMBER(wxSP_ARROW_KEYS
)
87 WX_FLAGS_MEMBER(wxSP_WRAP
)
89 WX_END_FLAGS( wxSpinCtrlStyle
)
91 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl
, wxControl
,"wx/spinbut.h")
93 WX_BEGIN_PROPERTIES_TABLE(wxSpinCtrl
)
94 WX_PROPERTY( ValueString
, wxString
, SetValue
, GetValue
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
95 WX_PROPERTY( Value
, int , SetValue
, GetValue
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
96 WX_PROPERTY( Min
, int , SetMin
, GetMin
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
97 WX_PROPERTY( Max
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
98 WX_PROPERTY_FLAGS( WindowStyle
, wxSpinCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
101 style wxSP_ARROW_KEYS
103 WX_END_PROPERTIES_TABLE()
105 WX_BEGIN_HANDLERS_TABLE(wxSpinCtrl
)
106 WX_END_HANDLERS_TABLE()
108 WX_CONSTRUCTOR_6( wxSpinCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, ValueString
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
110 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
113 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
114 EVT_CHAR(wxSpinCtrl::OnChar
)
116 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
118 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange
)
121 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 // the margin between the up-down control and its buddy (can be arbitrary,
128 // choose what you like - or may be decide during run-time depending on the
130 static const int MARGIN_BETWEEN
= 1;
132 // ============================================================================
134 // ============================================================================
136 wxArraySpins
wxSpinCtrl::ms_allSpins
;
138 // ----------------------------------------------------------------------------
139 // wnd proc for the buddy text ctrl
140 // ----------------------------------------------------------------------------
142 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
147 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
149 // forward some messages (the key and focus ones only so far) to
154 // if the focus comes from the spin control itself, don't set it
155 // back to it -- we don't want to go into an infinite loop
156 if ( wParam
== spin
->GetHWND() )
165 spin
->MSWWindowProc(message
, wParam
, lParam
);
167 // The control may have been deleted at this point, so check.
168 if (!(::IsWindow(hwnd
) && ((wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
)) == spin
))
173 // we want to get WXK_RETURN in order to generate the event for it
174 return DLGC_WANTCHARS
;
177 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
178 hwnd
, message
, wParam
, lParam
);
182 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
184 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong((HWND
)hwndBuddy
,
187 int i
= ms_allSpins
.Index(spin
);
189 if ( i
== wxNOT_FOUND
)
193 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
194 _T("wxSpinCtrl has incorrect buddy HWND!") );
199 // process a WM_COMMAND generated by the buddy text control
200 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
206 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
207 event
.SetEventObject(this);
208 wxString val
= wxGetWindowText(m_hwndBuddy
);
209 event
.SetString(val
);
210 event
.SetInt(GetValue());
211 return GetEventHandler()->ProcessEvent(event
);
216 wxFocusEvent
event(cmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
219 event
.SetEventObject( this );
220 return GetEventHandler()->ProcessEvent(event
);
230 void wxSpinCtrl::OnChar(wxKeyEvent
& event
)
232 switch ( event
.GetKeyCode() )
236 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
237 InitCommandEvent(event
);
238 wxString val
= wxGetWindowText(m_hwndBuddy
);
239 event
.SetString(val
);
240 event
.SetInt(GetValue());
241 if ( GetEventHandler()->ProcessEvent(event
) )
247 // always produce navigation event - even if we process TAB
248 // ourselves the fact that we got here means that the user code
249 // decided to skip processing of this TAB - probably to let it
250 // do its default job.
252 wxNavigationKeyEvent eventNav
;
253 eventNav
.SetDirection(!event
.ShiftDown());
254 eventNav
.SetWindowChange(event
.ControlDown());
255 eventNav
.SetEventObject(this);
257 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
263 // no, we didn't process it
267 void wxSpinCtrl::OnSetFocus(wxFocusEvent
& event
)
269 // when we get focus, give it to our buddy window as it needs it more than
271 ::SetFocus((HWND
)m_hwndBuddy
);
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
280 bool wxSpinCtrl::Create(wxWindow
*parent
,
282 const wxString
& value
,
286 int min
, int max
, int initial
,
287 const wxString
& name
)
289 // before using DoGetBestSize(), have to set style to let the base class
290 // know whether this is a horizontal or vertical control (we're always
292 style
|= wxSP_VERTICAL
;
294 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
295 style
|= wxBORDER_SUNKEN
;
297 SetWindowStyle(style
);
300 WXDWORD msStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
302 // calculate the sizes: the size given is the toal size for both controls
303 // and we need to fit them both in the given width (height is the same)
304 wxSize
sizeText(size
), sizeBtn(size
);
305 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
306 if ( sizeText
.x
<= 0 )
308 // DEFAULT_ITEM_WIDTH is the default width for the text control
309 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
312 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
313 if ( sizeText
.x
<= 0 )
315 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
319 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
321 // we must create the text control before the spin button for the purpose
322 // of the dialog navigation: if there is a static text just before the spin
323 // control, activating it by Alt-letter should give focus to the text
324 // control, not the spin and the dialog navigation code will give focus to
325 // the next control (at Windows level), not the one after it
327 // create the text window
329 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
331 exStyle
, // sunken border
332 _T("EDIT"), // window class
333 NULL
, // no window title
334 msStyle
, // style (will be shown later)
335 pos
.x
, pos
.y
, // position
336 0, 0, // size (will be set later)
337 GetHwndOf(parent
), // parent
338 (HMENU
)-1, // control id
339 wxGetInstance(), // app instance
340 NULL
// unused client data
345 wxLogLastError(wxT("CreateWindow(buddy text window)"));
351 // create the spin button
352 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
360 // subclass the text ctrl to be able to intercept some events
361 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
362 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
363 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
365 // should have the same font as the other controls
366 SetFont(GetParent()->GetFont());
368 // set the size of the text window - can do it only now, because we
369 // couldn't call DoGetBestSize() before as font wasn't set
370 if ( sizeText
.y
<= 0 )
373 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
375 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
378 DoMoveWindow(pos
.x
, pos
.y
,
379 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
381 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
383 // associate the text window with the spin button
384 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
386 if ( !value
.IsEmpty() )
391 // do it after finishing with m_hwndBuddy creation to avoid generating
392 // initial wxEVT_COMMAND_TEXT_UPDATED message
393 ms_allSpins
.Add(this);
398 wxSpinCtrl::~wxSpinCtrl()
400 ms_allSpins
.Remove(this);
402 // This removes spurious memory leak reporting
403 if (ms_allSpins
.GetCount() == 0)
406 // destroy the buddy window because this pointer which wxBuddyTextWndProc
407 // uses will not soon be valid any more
408 ::DestroyWindow(GetBuddyHwnd());
411 // ----------------------------------------------------------------------------
412 // wxTextCtrl-like methods
413 // ----------------------------------------------------------------------------
415 void wxSpinCtrl::SetValue(const wxString
& text
)
417 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
419 wxLogLastError(wxT("SetWindowText(buddy)"));
423 int wxSpinCtrl::GetValue() const
425 wxString val
= wxGetWindowText(m_hwndBuddy
);
428 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
434 void wxSpinCtrl::SetSelection(long from
, long to
)
436 // if from and to are both -1, it means (in wxWindows) that all text should
437 // be selected - translate into Windows convention
438 if ( (from
== -1) && (to
== -1) )
443 ::SendMessage((HWND
)m_hwndBuddy
, EM_SETSEL
, (WPARAM
)from
, (LPARAM
)to
);
446 // ----------------------------------------------------------------------------
447 // forward some methods to subcontrols
448 // ----------------------------------------------------------------------------
450 bool wxSpinCtrl::SetFont(const wxFont
& font
)
452 if ( !wxWindowBase::SetFont(font
) )
458 WXHANDLE hFont
= GetFont().GetResourceHandle();
459 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
464 bool wxSpinCtrl::Show(bool show
)
466 if ( !wxControl::Show(show
) )
471 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
476 bool wxSpinCtrl::Enable(bool enable
)
478 if ( !wxControl::Enable(enable
) )
483 ::EnableWindow(GetBuddyHwnd(), enable
);
488 void wxSpinCtrl::SetFocus()
490 ::SetFocus(GetBuddyHwnd());
493 // ----------------------------------------------------------------------------
495 // ----------------------------------------------------------------------------
497 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
499 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
500 event
.SetEventObject(this);
501 event
.SetInt(eventSpin
.GetPosition());
503 (void)GetEventHandler()->ProcessEvent(event
);
505 if ( eventSpin
.GetSkipped() )
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 wxSize
wxSpinCtrl::DoGetBestSize() const
517 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
518 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
521 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
522 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
526 // make the text tall enough
533 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
535 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
536 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
537 if ( widthText
<= 0 )
539 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
542 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
544 wxLogLastError(wxT("MoveWindow(buddy)"));
547 x
+= widthText
+ MARGIN_BETWEEN
;
548 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
550 wxLogLastError(wxT("MoveWindow"));
554 // get total size of the control
555 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
557 RECT spinrect
, textrect
, ctrlrect
;
558 GetWindowRect(GetHwnd(), &spinrect
);
559 GetWindowRect(GetBuddyHwnd(), &textrect
);
560 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
563 *x
= ctrlrect
.right
- ctrlrect
.left
;
565 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
568 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
570 // hack: pretend that our HWND is the text control just for a moment
571 WXHWND hWnd
= GetHWND();
572 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
574 wxSpinButton::DoGetPosition(x
, y
);
576 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;