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_SPIN(-1, wxSpinCtrl::OnSpinChange
)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // the margin between the up-down control and its buddy (can be arbitrary,
64 // choose what you like - or may be decide during run-time depending on the
66 static const int MARGIN_BETWEEN
= 1;
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
73 // wnd proc for the buddy text ctrl
74 // ----------------------------------------------------------------------------
76 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
81 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
83 // forward some messages (the key ones only so far) to the spin ctrl
90 spin
->MSWWindowProc(message
, wParam
, lParam
);
94 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
95 hwnd
, message
, wParam
, lParam
);
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 bool wxSpinCtrl::Create(wxWindow
*parent
,
104 const wxString
& value
,
108 int min
, int max
, int initial
,
109 const wxString
& name
)
111 // before using DoGetBestSize(), have to set style to let the base class
112 // know whether this is a horizontal or vertical control (we're always
114 style
|= wxSP_VERTICAL
;
115 SetWindowStyle(style
);
117 // calculate the sizes: the size given is the toal size for both controls
118 // and we need to fit them both in the given width (height is the same)
119 wxSize
sizeText(size
), sizeBtn(size
);
120 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
121 if ( sizeText
.x
<= 0 )
123 // DEFAULT_ITEM_WIDTH is the default width for the text control
124 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
127 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
128 if ( sizeText
.x
<= 0 )
130 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
134 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
136 // create the spin button
137 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
145 // create the text window
146 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
148 WS_EX_CLIENTEDGE
, // sunken border
149 _T("EDIT"), // window class
150 NULL
, // no window title
151 WS_CHILD
| WS_BORDER
/* | WS_CLIPSIBLINGS */, // style (will be shown later)
152 pos
.x
, pos
.y
, // position
153 0, 0, // size (will be set later)
154 GetHwndOf(parent
), // parent
155 (HMENU
)-1, // control id
156 wxGetInstance(), // app instance
157 NULL
// unused client data
162 wxLogLastError(wxT("CreateWindow(buddy text window)"));
167 // subclass the text ctrl to be able to intercept some events
168 m_oldBuddyWndProc
= (WXFARPROC
)::GetWindowLong((HWND
)m_hwndBuddy
, GWL_WNDPROC
);
169 ::SetWindowLong((HWND
)m_hwndBuddy
, GWL_USERDATA
, (LONG
)this);
170 ::SetWindowLong((HWND
)m_hwndBuddy
, GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
172 // should have the same font as the other controls
173 SetFont(GetParent()->GetFont());
175 // set the size of the text window - can do it only now, because we
176 // couldn't call DoGetBestSize() before as font wasn't set
177 if ( sizeText
.y
<= 0 )
180 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
182 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
185 DoMoveWindow(pos
.x
, pos
.y
,
186 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
188 (void)::ShowWindow((HWND
)m_hwndBuddy
, SW_SHOW
);
190 // associate the text window with the spin button
191 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
193 if ( !value
.IsEmpty() )
201 wxSpinCtrl::~wxSpinCtrl()
203 // destroy the buddy window because this pointer which wxBuddyTextWndProc
204 // uses will not soon be valid any more
205 ::DestroyWindow((HWND
)m_hwndBuddy
);
208 // ----------------------------------------------------------------------------
209 // wxTextCtrl-like methods
210 // ----------------------------------------------------------------------------
212 void wxSpinCtrl::SetValue(const wxString
& text
)
214 if ( !::SetWindowText((HWND
)m_hwndBuddy
, text
.c_str()) )
216 wxLogLastError(wxT("SetWindowText(buddy)"));
220 int wxSpinCtrl::GetValue() const
222 wxString val
= wxGetWindowText(m_hwndBuddy
);
225 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
231 // ----------------------------------------------------------------------------
232 // forward some methods to subcontrols
233 // ----------------------------------------------------------------------------
235 bool wxSpinCtrl::SetFont(const wxFont
& font
)
237 if ( !wxWindowBase::SetFont(font
) )
243 WXHANDLE hFont
= GetFont().GetResourceHandle();
244 (void)::SendMessage((HWND
)m_hwndBuddy
, WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
249 bool wxSpinCtrl::Show(bool show
)
251 if ( !wxControl::Show(show
) )
256 ::ShowWindow((HWND
)m_hwndBuddy
, show
? SW_SHOW
: SW_HIDE
);
261 bool wxSpinCtrl::Enable(bool enable
)
263 if ( !wxControl::Enable(enable
) )
268 ::EnableWindow((HWND
)m_hwndBuddy
, enable
);
273 void wxSpinCtrl::SetFocus()
275 ::SetFocus((HWND
)m_hwndBuddy
);
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
284 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
285 event
.SetEventObject(this);
286 event
.SetInt(eventSpin
.GetPosition());
288 (void)GetEventHandler()->ProcessEvent(event
);
290 if ( eventSpin
.GetSkipped() )
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 wxSize
wxSpinCtrl::DoGetBestSize() const
302 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
303 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
306 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
307 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
311 // make the text tall enough
318 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
320 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
321 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
322 if ( widthText
<= 0 )
324 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
327 if ( !::MoveWindow((HWND
)m_hwndBuddy
, x
, y
, widthText
, height
, TRUE
) )
329 wxLogLastError(wxT("MoveWindow(buddy)"));
332 x
+= widthText
+ MARGIN_BETWEEN
;
333 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
335 wxLogLastError(wxT("MoveWindow"));
339 // get total size of the control
340 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
342 RECT spinrect
, textrect
, ctrlrect
;
343 GetWindowRect(GetHwnd(), &spinrect
);
344 GetWindowRect((HWND
)m_hwndBuddy
, &textrect
);
345 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
348 *x
= ctrlrect
.right
- ctrlrect
.left
;
350 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
353 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
355 // hack: pretend that our HWND is the text control just for a moment
356 WXHWND hWnd
= GetHWND();
357 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
359 wxSpinButton::DoGetPosition(x
, y
);
361 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;