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__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
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 SetWindowStyle(style
| wxSP_VERTICAL
);
90 // calculate the sizes: the size given is the toal size for both controls
91 // and we need to fit them both in the given width (height is the same)
92 wxSize
sizeText(size
), sizeBtn(size
);
93 sizeBtn
.x
= wxSpinButton::DoGetBestSize().x
;
94 if ( sizeText
.x
<= 0 )
96 // DEFAULT_ITEM_WIDTH is the default width for the text control
97 sizeText
.x
= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
+ sizeBtn
.x
;
100 sizeText
.x
-= sizeBtn
.x
+ MARGIN_BETWEEN
;
101 if ( sizeText
.x
<= 0 )
103 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
107 posBtn
.x
+= sizeText
.x
+ MARGIN_BETWEEN
;
109 // create the spin button
110 if ( !wxSpinButton::Create(parent
, id
, posBtn
, sizeBtn
, style
, name
) )
118 // create the text window
119 m_hwndBuddy
= (WXHWND
)::CreateWindowEx
121 WS_EX_CLIENTEDGE
, // sunken border
122 _T("EDIT"), // window class
123 NULL
, // no window title
124 WS_CHILD
| WS_BORDER
, // style (will be shown later)
125 pos
.x
, pos
.y
, // position
126 0, 0, // size (will be set later)
127 GetHwndOf(parent
), // parent
128 (HMENU
)-1, // control id
129 wxGetInstance(), // app instance
130 NULL
// unused client data
135 wxLogLastError("CreateWindow(buddy text window)");
140 // should have the same font as the other controls
141 SetFont(GetParent()->GetFont());
143 // set the size of the text window - can do it only now, because we
144 // couldn't call DoGetBestSize() before as font wasn't set
145 if ( sizeText
.y
<= 0 )
147 // make it the same height as the button then
148 sizeText
.y
= DoGetBestSize().y
;
151 DoMoveWindow(pos
.x
, pos
.y
,
152 sizeText
.x
+ sizeBtn
.x
+ MARGIN_BETWEEN
, sizeText
.y
);
154 (void)::ShowWindow((HWND
)m_hwndBuddy
, SW_SHOW
);
156 // associate the text window with the spin button
157 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY
, (WPARAM
)m_hwndBuddy
, 0);
159 if ( !value
.IsEmpty() )
167 // ----------------------------------------------------------------------------
168 // wxTextCtrl-like methods
169 // ----------------------------------------------------------------------------
171 void wxSpinCtrl::SetValue(const wxString
& text
)
173 if ( ::SetWindowText((HWND
)m_hwndBuddy
, text
.c_str()) )
175 wxLogLastError("SetWindowText(buddy)");
179 int wxSpinCtrl::GetValue() const
181 wxString val
= wxGetWindowText(m_hwndBuddy
);
184 if ( (wxSscanf(val
, wxT("%lu"), &n
) != 1) )
190 // ----------------------------------------------------------------------------
191 // when setting font, we need to do it for both controls
192 // ----------------------------------------------------------------------------
194 bool wxSpinCtrl::SetFont(const wxFont
& font
)
196 if ( !wxWindowBase::SetFont(font
) )
202 WXHANDLE hFont
= GetFont().GetResourceHandle();
203 (void)::SendMessage((HWND
)m_hwndBuddy
, WM_SETFONT
, (WPARAM
)hFont
, TRUE
);
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 void wxSpinCtrl::OnSpinChange(wxSpinEvent
& eventSpin
)
214 wxCommandEvent
event(wxEVT_COMMAND_SPINCTRL_UPDATED
, GetId());
215 event
.SetEventObject(this);
216 event
.SetInt(eventSpin
.GetPosition());
218 (void)GetEventHandler()->ProcessEvent(event
);
220 if ( eventSpin
.GetSkipped() )
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
230 wxSize
wxSpinCtrl::DoGetBestSize() const
232 wxSize sizeBtn
= wxSpinButton::DoGetBestSize();
233 sizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
236 wxGetCharSize(GetHWND(), NULL
, &y
, &GetFont());
237 y
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
);
241 // make the text tall enough
248 void wxSpinCtrl::DoMoveWindow(int x
, int y
, int width
, int height
)
250 int widthBtn
= wxSpinButton::DoGetBestSize().x
;
251 int widthText
= width
- widthBtn
- MARGIN_BETWEEN
;
252 if ( widthText
<= 0 )
254 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
257 if ( !::MoveWindow((HWND
)m_hwndBuddy
, x
, y
, widthText
, height
, TRUE
) )
259 wxLogLastError("MoveWindow(buddy)");
262 x
+= widthText
+ MARGIN_BETWEEN
;
263 if ( !::MoveWindow(GetHwnd(), x
, y
, widthBtn
, height
, TRUE
) )
265 wxLogLastError("MoveWindow");