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 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 bool wxSpinCtrl::Create(wxWindow
*parent
,
78 const wxString
& value
,
82 int min
, int max
, int initial
,
85 // before using DoGetBestSize(), have to set style to let the base class
86 // know whether this is a horizontal or vertical control (we're always
88 style
|= wxSP_VERTICAL
;
89 SetWindowStyle(style
);
91 // calculate the sizes: the size given is the toal size for both controls
92 // and we need to fit them both in the given width (height is the same)
93 wxSize
sizeText(size
), sizeBtn(size
);
94 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
95 if ( sizeText
.x
<= 0 )
97 // DEFAULT_ITEM_WIDTH is the default width for the text control
98 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
101 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
102 if ( sizeText
.x
<= 0 )
104 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
108 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
110 // create the spin button
111 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
119 // create the text window
120 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
122 WS_EX_CLIENTEDGE
, // sunken border
123 _T("EDIT"), // window class
124 NULL
, // no window title
125 WS_CHILD
| WS_BORDER
, // style (will be shown later)
126 pos
.x
, pos
.y
, // position
127 0, 0, // size (will be set later)
128 GetHwndOf(parent
), // parent
129 (HMENU
)-1, // control id
130 wxGetInstance(), // app instance
131 NULL
// unused client data
136 wxLogLastError("CreateWindow(buddy text window)");
141 // should have the same font as the other controls
142 SetFont(GetParent()->GetFont());
144 // set the size of the text window - can do it only now, because we
145 // couldn't call DoGetBestSize() before as font wasn't set
146 if ( sizeText
.y
<= 0 )
149 wxGetCharSize(GetHWND(), &cx
, &cy
, &GetFont());
151 sizeText
.y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
154 DoMoveWindow(pos
.x
, pos
.y
,
155 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
157 (void)::ShowWindow((HWND
)m_hwndBuddy
, SW_SHOW
);
159 // associate the text window with the spin button
160 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
162 if ( !value
.IsEmpty() )
170 // ----------------------------------------------------------------------------
171 // wxTextCtrl-like methods
172 // ----------------------------------------------------------------------------
174 void wxSpinCtrl::SetValue(const wxString
& text
)
176 if ( !::SetWindowText((HWND
)m_hwndBuddy
, text
.c_str()) )
178 wxLogLastError("SetWindowText(buddy)");
182 int wxSpinCtrl::GetValue() const
184 wxString val
= wxGetWindowText(m_hwndBuddy
);
187 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
193 // ----------------------------------------------------------------------------
194 // forward some methods to subcontrols
195 // ----------------------------------------------------------------------------
197 bool wxSpinCtrl::SetFont(const wxFont
& font
)
199 if ( !wxWindowBase::SetFont(font
) )
205 WXHANDLE hFont
= GetFont().GetResourceHandle();
206 (void)::SendMessage((HWND
)m_hwndBuddy
, WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
211 bool wxSpinCtrl::Show(bool show
)
213 if ( !wxControl::Show(show
) )
218 ::ShowWindow((HWND
)m_hwndBuddy
, show
? SW_SHOW
: SW_HIDE
);
223 bool wxSpinCtrl::Enable(bool enable
)
225 if ( !wxControl::Enable(enable
) )
230 ::EnableWindow((HWND
)m_hwndBuddy
, enable
);
235 void wxSpinCtrl::SetFocus()
237 ::SetFocus((HWND
)m_hwndBuddy
);
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
246 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
247 event
.SetEventObject(this);
248 event
.SetInt(eventSpin
.GetPosition());
250 (void)GetEventHandler()->ProcessEvent(event
);
252 if ( eventSpin
.GetSkipped() )
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 wxSize
wxSpinCtrl::DoGetBestSize() const
264 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
265 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
268 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
269 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
273 // make the text tall enough
280 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
282 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
283 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
284 if ( widthText
<= 0 )
286 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
289 if ( !::MoveWindow((HWND
)m_hwndBuddy
, x
, y
, widthText
, height
, TRUE
) )
291 wxLogLastError("MoveWindow(buddy)");
294 x
+= widthText
+ MARGIN_BETWEEN
;
295 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
297 wxLogLastError("MoveWindow");