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 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // the margin between the up-down control and its buddy (can be arbitrary,
66 // choose what you like - or may be decide during run-time depending on the
68 static const int MARGIN_BETWEEN
= 1;
70 // ============================================================================
72 // ============================================================================
74 wxArraySpins
wxSpinCtrl::ms_allSpins
;
76 // ----------------------------------------------------------------------------
77 // wnd proc for the buddy text ctrl
78 // ----------------------------------------------------------------------------
80 LRESULT APIENTRY _EXPORT
wxBuddyTextWndProc(HWND hwnd
,
85 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong(hwnd
, GWL_USERDATA
);
87 // forward some messages (the key ones only so far) to the spin ctrl
94 spin
->MSWWindowProc(message
, wParam
, lParam
);
98 return ::CallWindowProc(CASTWNDPROC spin
->GetBuddyWndProc(),
99 hwnd
, message
, wParam
, lParam
);
103 wxSpinCtrl
*wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy
)
105 wxSpinCtrl
*spin
= (wxSpinCtrl
*)::GetWindowLong((HWND
)hwndBuddy
,
108 int i
= ms_allSpins
.Index(spin
);
110 if ( i
== wxNOT_FOUND
)
114 wxASSERT_MSG( spin
->m_hwndBuddy
== hwndBuddy
,
115 _T("wxSpinCtrl has incorrect buddy HWND!") );
120 // process a WM_COMMAND generated by the buddy text control
121 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd
, WXWORD
WXUNUSED(id
))
123 if ( cmd
== EN_CHANGE
)
125 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
126 event
.SetEventObject(this);
127 event
.SetInt(GetValue());
128 GetEventHandler()->ProcessEvent(event
);
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 bool wxSpinCtrl::Create(wxWindow
*parent
,
143 const wxString
& value
,
147 int min
, int max
, int initial
,
148 const wxString
& name
)
150 // before using DoGetBestSize(), have to set style to let the base class
151 // know whether this is a horizontal or vertical control (we're always
153 style
|= wxSP_VERTICAL
;
154 SetWindowStyle(style
);
156 // calculate the sizes: the size given is the toal size for both controls
157 // and we need to fit them both in the given width (height is the same)
158 wxSize
sizeText(size
), sizeBtn(size
);
159 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
160 if ( sizeText
.x
<= 0 )
162 // DEFAULT_ITEM_WIDTH is the default width for the text control
163 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
166 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
167 if ( sizeText
.x
<= 0 )
169 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
173 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
175 // create the spin button
176 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
184 // create the text window
185 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
187 WS_EX_CLIENTEDGE
, // sunken border
188 _T("EDIT"), // window class
189 NULL
, // no window title
190 WS_CHILD
| WS_BORDER
/* | WS_CLIPSIBLINGS */, // style (will be shown later)
191 pos
.x
, pos
.y
, // position
192 0, 0, // size (will be set later)
193 GetHwndOf(parent
), // parent
194 (HMENU
)-1, // control id
195 wxGetInstance(), // app instance
196 NULL
// unused client data
201 wxLogLastError(wxT("CreateWindow(buddy text window)"));
206 // subclass the text ctrl to be able to intercept some events
207 m_wndProcBuddy
= (WXFARPROC
)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
);
208 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA
, (LONG
)this);
209 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC
, (LONG
)wxBuddyTextWndProc
);
211 // should have the same font as the other controls
212 SetFont(GetParent()->GetFont());
214 // set the size of the text window - can do it only now, because we
215 // couldn't call DoGetBestSize() before as font wasn't set
216 if ( sizeText
.y
<= 0 )
219 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
221 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
224 DoMoveWindow(pos
.x
, pos
.y
,
225 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
227 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW
);
229 // associate the text window with the spin button
230 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
232 if ( !value
.IsEmpty() )
237 // do it after finishing with m_hwndBuddy creation to avoid generating
238 // initial wxEVT_COMMAND_TEXT_UPDATED message
239 ms_allSpins
.Add(this);
244 wxSpinCtrl::~wxSpinCtrl()
246 ms_allSpins
.Remove(this);
248 // destroy the buddy window because this pointer which wxBuddyTextWndProc
249 // uses will not soon be valid any more
250 ::DestroyWindow(GetBuddyHwnd());
253 // ----------------------------------------------------------------------------
254 // wxTextCtrl-like methods
255 // ----------------------------------------------------------------------------
257 void wxSpinCtrl::SetValue(const wxString
& text
)
259 if ( !::SetWindowText(GetBuddyHwnd(), text
.c_str()) )
261 wxLogLastError(wxT("SetWindowText(buddy)"));
265 int wxSpinCtrl::GetValue() const
267 wxString val
= wxGetWindowText(m_hwndBuddy
);
270 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
276 // ----------------------------------------------------------------------------
277 // forward some methods to subcontrols
278 // ----------------------------------------------------------------------------
280 bool wxSpinCtrl::SetFont(const wxFont
& font
)
282 if ( !wxWindowBase::SetFont(font
) )
288 WXHANDLE hFont
= GetFont().GetResourceHandle();
289 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
294 bool wxSpinCtrl::Show(bool show
)
296 if ( !wxControl::Show(show
) )
301 ::ShowWindow(GetBuddyHwnd(), show
? SW_SHOW
: SW_HIDE
);
306 bool wxSpinCtrl::Enable(bool enable
)
308 if ( !wxControl::Enable(enable
) )
313 ::EnableWindow(GetBuddyHwnd(), enable
);
318 void wxSpinCtrl::SetFocus()
320 ::SetFocus(GetBuddyHwnd());
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
327 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
329 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
330 event
.SetEventObject(this);
331 event
.SetInt(eventSpin
.GetPosition());
333 (void)GetEventHandler()->ProcessEvent(event
);
335 if ( eventSpin
.GetSkipped() )
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 wxSize
wxSpinCtrl::DoGetBestSize() const
347 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
348 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
351 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
352 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
356 // make the text tall enough
363 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
365 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
366 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
367 if ( widthText
<= 0 )
369 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
372 if ( !::MoveWindow(GetBuddyHwnd(), x
, y
, widthText
, height
, TRUE
) )
374 wxLogLastError(wxT("MoveWindow(buddy)"));
377 x
+= widthText
+ MARGIN_BETWEEN
;
378 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
380 wxLogLastError(wxT("MoveWindow"));
384 // get total size of the control
385 void wxSpinCtrl::DoGetSize(int *x
, int *y
) const
387 RECT spinrect
, textrect
, ctrlrect
;
388 GetWindowRect(GetHwnd(), &spinrect
);
389 GetWindowRect(GetBuddyHwnd(), &textrect
);
390 UnionRect(&ctrlrect
,&textrect
, &spinrect
);
393 *x
= ctrlrect
.right
- ctrlrect
.left
;
395 *y
= ctrlrect
.bottom
- ctrlrect
.top
;
398 void wxSpinCtrl::DoGetPosition(int *x
, int *y
) const
400 // hack: pretend that our HWND is the text control just for a moment
401 WXHWND hWnd
= GetHWND();
402 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hwndBuddy
;
404 wxSpinButton::DoGetPosition(x
, y
);
406 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;