]>
Commit | Line | Data |
---|---|---|
b782f2e0 | 1 | ///////////////////////////////////////////////////////////////////////////// |
623d5f80 | 2 | // Name: src/msw/spinctrl.cpp |
b782f2e0 VZ |
3 | // Purpose: wxSpinCtrl class implementation for Win32 |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 22.07.99 | |
7 | // RCS-ID: $Id$ | |
74124ea9 | 8 | // Copyright: (c) 1999-2005 Vadim Zeitlin |
65571936 | 9 | // Licence: wxWindows licence |
b782f2e0 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
b782f2e0 VZ |
16 | // ---------------------------------------------------------------------------- |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
0e528b99 JS |
27 | #if wxUSE_SPINCTRL |
28 | ||
b782f2e0 | 29 | #include "wx/spinctrl.h" |
623d5f80 WS |
30 | |
31 | #ifndef WX_PRECOMP | |
57bd4c60 | 32 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
623d5f80 WS |
33 | #include "wx/event.h" |
34 | #include "wx/textctrl.h" | |
193d0c93 | 35 | #include "wx/wxcrtvararg.h" |
623d5f80 WS |
36 | #endif |
37 | ||
b782f2e0 VZ |
38 | #include "wx/msw/private.h" |
39 | ||
74124ea9 VZ |
40 | #if wxUSE_TOOLTIPS |
41 | #include "wx/tooltip.h" | |
42 | #endif // wxUSE_TOOLTIPS | |
b782f2e0 | 43 | |
678cd6de VZ |
44 | #include <limits.h> // for INT_MIN |
45 | ||
b782f2e0 VZ |
46 | // ---------------------------------------------------------------------------- |
47 | // macros | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
f0a126fe | 50 | #if wxUSE_EXTENDED_RTTI |
bc9fb572 JS |
51 | WX_DEFINE_FLAGS( wxSpinCtrlStyle ) |
52 | ||
3ff066a4 | 53 | wxBEGIN_FLAGS( wxSpinCtrlStyle ) |
bc9fb572 JS |
54 | // new style border flags, we put them first to |
55 | // use them for streaming out | |
3ff066a4 SC |
56 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
57 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
58 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
59 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
60 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
61 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
57f4f925 | 62 | |
bc9fb572 | 63 | // old style border flags |
3ff066a4 SC |
64 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
65 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
66 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
67 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
68 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 69 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
70 | |
71 | // standard window styles | |
3ff066a4 SC |
72 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
73 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
74 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
75 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 76 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
77 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
78 | wxFLAGS_MEMBER(wxVSCROLL) | |
79 | wxFLAGS_MEMBER(wxHSCROLL) | |
80 | ||
81 | wxFLAGS_MEMBER(wxSP_HORIZONTAL) | |
82 | wxFLAGS_MEMBER(wxSP_VERTICAL) | |
83 | wxFLAGS_MEMBER(wxSP_ARROW_KEYS) | |
84 | wxFLAGS_MEMBER(wxSP_WRAP) | |
85 | ||
86 | wxEND_FLAGS( wxSpinCtrlStyle ) | |
bc9fb572 | 87 | |
51741307 | 88 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl, wxControl,"wx/spinbut.h") |
9750fc42 | 89 | |
3ff066a4 | 90 | wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl) |
6f45066b | 91 | wxEVENT_RANGE_PROPERTY( Spin , wxEVT_SCROLL_TOP , wxEVT_SCROLL_CHANGED , wxSpinEvent ) |
3ff066a4 | 92 | wxEVENT_PROPERTY( Updated , wxEVT_COMMAND_SPINCTRL_UPDATED , wxCommandEvent ) |
57f4f925 | 93 | wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent ) |
3ff066a4 SC |
94 | wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent ) |
95 | ||
af498247 | 96 | wxPROPERTY( ValueString , wxString , SetValue , GetValue , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ; |
57f4f925 WS |
97 | wxPROPERTY( Value , int , SetValue, GetValue, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
98 | wxPROPERTY( Min , int , SetMin, GetMin, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
99 | wxPROPERTY( Max , int , SetMax, GetMax, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
af498247 | 100 | wxPROPERTY_FLAGS( WindowStyle , wxSpinCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
066f1b7a | 101 | /* |
57f4f925 WS |
102 | TODO PROPERTIES |
103 | style wxSP_ARROW_KEYS | |
066f1b7a | 104 | */ |
3ff066a4 | 105 | wxEND_PROPERTIES_TABLE() |
51741307 | 106 | |
3ff066a4 SC |
107 | wxBEGIN_HANDLERS_TABLE(wxSpinCtrl) |
108 | wxEND_HANDLERS_TABLE() | |
51741307 | 109 | |
57f4f925 | 110 | wxCONSTRUCTOR_6( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , ValueString , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
51741307 SC |
111 | #else |
112 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl) | |
113 | #endif | |
066f1b7a | 114 | |
9750fc42 | 115 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton) |
e3582f7f | 116 | EVT_CHAR(wxSpinCtrl::OnChar) |
c5c04fab VZ |
117 | |
118 | EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus) | |
4a528443 | 119 | EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus) |
57f4f925 | 120 | EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange) |
9750fc42 | 121 | END_EVENT_TABLE() |
b782f2e0 | 122 | |
6fe19057 VZ |
123 | #define GetBuddyHwnd() (HWND)(m_hwndBuddy) |
124 | ||
b782f2e0 VZ |
125 | // ---------------------------------------------------------------------------- |
126 | // constants | |
127 | // ---------------------------------------------------------------------------- | |
128 | ||
baccb514 VZ |
129 | // the margin between the up-down control and its buddy (can be arbitrary, |
130 | // choose what you like - or may be decide during run-time depending on the | |
131 | // font size?) | |
132 | static const int MARGIN_BETWEEN = 1; | |
b782f2e0 VZ |
133 | |
134 | // ============================================================================ | |
135 | // implementation | |
136 | // ============================================================================ | |
137 | ||
6fe19057 VZ |
138 | wxArraySpins wxSpinCtrl::ms_allSpins; |
139 | ||
f6bcfd97 BP |
140 | // ---------------------------------------------------------------------------- |
141 | // wnd proc for the buddy text ctrl | |
142 | // ---------------------------------------------------------------------------- | |
143 | ||
144 | LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd, | |
145 | UINT message, | |
146 | WPARAM wParam, | |
147 | LPARAM lParam) | |
148 | { | |
975b6bcf | 149 | wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData(hwnd); |
f6bcfd97 | 150 | |
93c4157c VZ |
151 | // forward some messages (the key and focus ones only so far) to |
152 | // the spin ctrl | |
f6bcfd97 BP |
153 | switch ( message ) |
154 | { | |
93c4157c | 155 | case WM_SETFOCUS: |
c5c04fab VZ |
156 | // if the focus comes from the spin control itself, don't set it |
157 | // back to it -- we don't want to go into an infinite loop | |
c140b7e7 | 158 | if ( (WXHWND)wParam == spin->GetHWND() ) |
c5c04fab VZ |
159 | break; |
160 | //else: fall through | |
161 | ||
93c4157c | 162 | case WM_KILLFOCUS: |
f6bcfd97 BP |
163 | case WM_CHAR: |
164 | case WM_DEADCHAR: | |
165 | case WM_KEYUP: | |
166 | case WM_KEYDOWN: | |
167 | spin->MSWWindowProc(message, wParam, lParam); | |
e3582f7f JS |
168 | |
169 | // The control may have been deleted at this point, so check. | |
975b6bcf | 170 | if ( !::IsWindow(hwnd) || wxGetWindowUserData(hwnd) != spin ) |
e3582f7f | 171 | return 0; |
f6bcfd97 | 172 | break; |
350ba193 VZ |
173 | |
174 | case WM_GETDLGCODE: | |
175 | // we want to get WXK_RETURN in order to generate the event for it | |
176 | return DLGC_WANTCHARS; | |
f6bcfd97 | 177 | } |
350ba193 | 178 | |
f6bcfd97 BP |
179 | return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(), |
180 | hwnd, message, wParam, lParam); | |
181 | } | |
182 | ||
6fe19057 VZ |
183 | /* static */ |
184 | wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy) | |
185 | { | |
975b6bcf | 186 | wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData((HWND)hwndBuddy); |
6fe19057 VZ |
187 | |
188 | int i = ms_allSpins.Index(spin); | |
189 | ||
190 | if ( i == wxNOT_FOUND ) | |
191 | return NULL; | |
192 | ||
193 | // sanity check | |
194 | wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy, | |
195 | _T("wxSpinCtrl has incorrect buddy HWND!") ); | |
196 | ||
197 | return spin; | |
198 | } | |
199 | ||
200 | // process a WM_COMMAND generated by the buddy text control | |
201 | bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id)) | |
202 | { | |
0ca3c231 | 203 | if ( cmd == EN_CHANGE ) |
6fe19057 | 204 | { |
0ca3c231 VZ |
205 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); |
206 | event.SetEventObject(this); | |
207 | wxString val = wxGetWindowText(m_hwndBuddy); | |
208 | event.SetString(val); | |
209 | event.SetInt(GetValue()); | |
937013e0 | 210 | return HandleWindowEvent(event); |
6fe19057 VZ |
211 | } |
212 | ||
213 | // not processed | |
57f4f925 | 214 | return false; |
6fe19057 VZ |
215 | } |
216 | ||
e3582f7f JS |
217 | void wxSpinCtrl::OnChar(wxKeyEvent& event) |
218 | { | |
77e00fe9 | 219 | switch ( event.GetKeyCode() ) |
e3582f7f JS |
220 | { |
221 | case WXK_RETURN: | |
222 | { | |
223 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
224 | InitCommandEvent(event); | |
225 | wxString val = wxGetWindowText(m_hwndBuddy); | |
226 | event.SetString(val); | |
227 | event.SetInt(GetValue()); | |
937013e0 | 228 | if ( HandleWindowEvent(event) ) |
e3582f7f JS |
229 | return; |
230 | break; | |
231 | } | |
232 | ||
233 | case WXK_TAB: | |
234 | // always produce navigation event - even if we process TAB | |
235 | // ourselves the fact that we got here means that the user code | |
236 | // decided to skip processing of this TAB - probably to let it | |
237 | // do its default job. | |
238 | { | |
239 | wxNavigationKeyEvent eventNav; | |
240 | eventNav.SetDirection(!event.ShiftDown()); | |
241 | eventNav.SetWindowChange(event.ControlDown()); | |
242 | eventNav.SetEventObject(this); | |
243 | ||
937013e0 | 244 | if ( GetParent()->HandleWindowEvent(eventNav) ) |
e3582f7f JS |
245 | return; |
246 | } | |
247 | break; | |
248 | } | |
249 | ||
250 | // no, we didn't process it | |
251 | event.Skip(); | |
252 | } | |
253 | ||
4a528443 JS |
254 | void wxSpinCtrl::OnKillFocus(wxFocusEvent& event) |
255 | { | |
5ec60151 VZ |
256 | // ensure that a correct value is shown by the control |
257 | NormalizeValue(); | |
4a528443 JS |
258 | event.Skip(); |
259 | } | |
260 | ||
c5c04fab VZ |
261 | void wxSpinCtrl::OnSetFocus(wxFocusEvent& event) |
262 | { | |
263 | // when we get focus, give it to our buddy window as it needs it more than | |
264 | // we do | |
265 | ::SetFocus((HWND)m_hwndBuddy); | |
266 | ||
267 | event.Skip(); | |
268 | } | |
269 | ||
1e8dba5e RR |
270 | void wxSpinCtrl::NormalizeValue() |
271 | { | |
e816f5c7 | 272 | const int value = GetValue(); |
d40e9e06 | 273 | const bool changed = value != m_oldValue; |
e816f5c7 | 274 | |
b6d83018 VZ |
275 | // notice that we have to call SetValue() even if the value didn't change |
276 | // because otherwise we could be left with empty buddy control when value | |
277 | // is 0, see comment in SetValue() | |
e816f5c7 VZ |
278 | SetValue(value); |
279 | ||
b6d83018 VZ |
280 | if ( changed ) |
281 | { | |
d40e9e06 | 282 | SendSpinUpdate(value); |
b6d83018 | 283 | } |
1e8dba5e RR |
284 | } |
285 | ||
b782f2e0 VZ |
286 | // ---------------------------------------------------------------------------- |
287 | // construction | |
288 | // ---------------------------------------------------------------------------- | |
289 | ||
290 | bool wxSpinCtrl::Create(wxWindow *parent, | |
291 | wxWindowID id, | |
678cd6de | 292 | const wxString& value, |
b782f2e0 VZ |
293 | const wxPoint& pos, |
294 | const wxSize& size, | |
295 | long style, | |
296 | int min, int max, int initial, | |
297 | const wxString& name) | |
298 | { | |
d40e9e06 VZ |
299 | // this should be in ctor/init function but I don't want to add one to 2.8 |
300 | // to avoid problems with default ctor which can be inlined in the user | |
301 | // code and so might not get this fix without recompilation | |
302 | m_oldValue = INT_MIN; | |
303 | ||
b782f2e0 VZ |
304 | // before using DoGetBestSize(), have to set style to let the base class |
305 | // know whether this is a horizontal or vertical control (we're always | |
306 | // vertical) | |
882a8f40 | 307 | style |= wxSP_VERTICAL; |
c76b1a30 JS |
308 | |
309 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) | |
5d72f195 RR |
310 | #ifdef __WXWINCE__ |
311 | style |= wxBORDER_SIMPLE; | |
312 | #else | |
c76b1a30 | 313 | style |= wxBORDER_SUNKEN; |
5d72f195 | 314 | #endif |
c76b1a30 | 315 | |
882a8f40 | 316 | SetWindowStyle(style); |
b782f2e0 | 317 | |
fe3d9123 JS |
318 | WXDWORD exStyle = 0; |
319 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ; | |
320 | ||
8e190f0e VZ |
321 | // this control is used for numeric entry so normally using these flags by |
322 | // default shouldn't be a problem, if it is we can always add a style such | |
323 | // as wxSP_NON_NUMERIC later | |
324 | msStyle |= ES_RIGHT | ES_NUMBER; | |
325 | ||
326 | // calculate the sizes: the size given is the total size for both controls | |
b782f2e0 VZ |
327 | // and we need to fit them both in the given width (height is the same) |
328 | wxSize sizeText(size), sizeBtn(size); | |
329 | sizeBtn.x = wxSpinButton::DoGetBestSize().x; | |
baccb514 VZ |
330 | if ( sizeText.x <= 0 ) |
331 | { | |
332 | // DEFAULT_ITEM_WIDTH is the default width for the text control | |
333 | sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x; | |
334 | } | |
335 | ||
b782f2e0 VZ |
336 | sizeText.x -= sizeBtn.x + MARGIN_BETWEEN; |
337 | if ( sizeText.x <= 0 ) | |
338 | { | |
339 | wxLogDebug(_T("not enough space for wxSpinCtrl!")); | |
340 | } | |
341 | ||
342 | wxPoint posBtn(pos); | |
343 | posBtn.x += sizeText.x + MARGIN_BETWEEN; | |
344 | ||
c5c04fab VZ |
345 | // we must create the text control before the spin button for the purpose |
346 | // of the dialog navigation: if there is a static text just before the spin | |
347 | // control, activating it by Alt-letter should give focus to the text | |
348 | // control, not the spin and the dialog navigation code will give focus to | |
349 | // the next control (at Windows level), not the one after it | |
b782f2e0 | 350 | |
c5c04fab | 351 | // create the text window |
b782f2e0 | 352 | |
b782f2e0 VZ |
353 | m_hwndBuddy = (WXHWND)::CreateWindowEx |
354 | ( | |
c5c04fab | 355 | exStyle, // sunken border |
baccb514 VZ |
356 | _T("EDIT"), // window class |
357 | NULL, // no window title | |
c5c04fab | 358 | msStyle, // style (will be shown later) |
baccb514 VZ |
359 | pos.x, pos.y, // position |
360 | 0, 0, // size (will be set later) | |
361 | GetHwndOf(parent), // parent | |
362 | (HMENU)-1, // control id | |
363 | wxGetInstance(), // app instance | |
364 | NULL // unused client data | |
b782f2e0 VZ |
365 | ); |
366 | ||
367 | if ( !m_hwndBuddy ) | |
368 | { | |
f6bcfd97 | 369 | wxLogLastError(wxT("CreateWindow(buddy text window)")); |
b782f2e0 | 370 | |
57f4f925 | 371 | return false; |
b782f2e0 VZ |
372 | } |
373 | ||
c5c04fab VZ |
374 | |
375 | // create the spin button | |
376 | if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) ) | |
377 | { | |
57f4f925 | 378 | return false; |
c5c04fab VZ |
379 | } |
380 | ||
23ec96e3 | 381 | wxSpinButtonBase::SetRange(min, max); |
e816f5c7 | 382 | |
f6bcfd97 | 383 | // subclass the text ctrl to be able to intercept some events |
975b6bcf VZ |
384 | wxSetWindowUserData(GetBuddyHwnd(), this); |
385 | m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(), | |
386 | wxBuddyTextWndProc); | |
f6bcfd97 | 387 | |
8d2e831b RD |
388 | // set up fonts and colours (This is nomally done in MSWCreateControl) |
389 | InheritAttributes(); | |
e0176dd9 VZ |
390 | if (!m_hasFont) |
391 | SetFont(GetDefaultAttributes().font); | |
8d2e831b | 392 | |
baccb514 VZ |
393 | // set the size of the text window - can do it only now, because we |
394 | // couldn't call DoGetBestSize() before as font wasn't set | |
395 | if ( sizeText.y <= 0 ) | |
396 | { | |
882a8f40 | 397 | int cx, cy; |
7a5e53ab | 398 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); |
882a8f40 VZ |
399 | |
400 | sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
baccb514 VZ |
401 | } |
402 | ||
170acdc9 | 403 | SetInitialSize(size); |
baccb514 | 404 | |
6fe19057 | 405 | (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW); |
b782f2e0 VZ |
406 | |
407 | // associate the text window with the spin button | |
baccb514 VZ |
408 | (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0); |
409 | ||
549cc3a8 JS |
410 | SetValue(initial); |
411 | ||
412 | // Set the range in the native control | |
413 | SetRange(min, max); | |
414 | ||
3d104ec3 | 415 | if ( !value.empty() ) |
678cd6de VZ |
416 | { |
417 | SetValue(value); | |
5bcdac45 JS |
418 | m_oldValue = (int) wxAtol(value); |
419 | } | |
420 | else | |
421 | { | |
422 | SetValue(wxString::Format(wxT("%d"), initial)); | |
423 | m_oldValue = initial; | |
678cd6de VZ |
424 | } |
425 | ||
6fe19057 VZ |
426 | // do it after finishing with m_hwndBuddy creation to avoid generating |
427 | // initial wxEVT_COMMAND_TEXT_UPDATED message | |
428 | ms_allSpins.Add(this); | |
429 | ||
57f4f925 | 430 | return true; |
baccb514 VZ |
431 | } |
432 | ||
f6bcfd97 BP |
433 | wxSpinCtrl::~wxSpinCtrl() |
434 | { | |
6fe19057 VZ |
435 | ms_allSpins.Remove(this); |
436 | ||
5648c0ad JS |
437 | // This removes spurious memory leak reporting |
438 | if (ms_allSpins.GetCount() == 0) | |
439 | ms_allSpins.Clear(); | |
440 | ||
f6bcfd97 BP |
441 | // destroy the buddy window because this pointer which wxBuddyTextWndProc |
442 | // uses will not soon be valid any more | |
6fe19057 | 443 | ::DestroyWindow(GetBuddyHwnd()); |
f6bcfd97 BP |
444 | } |
445 | ||
678cd6de VZ |
446 | // ---------------------------------------------------------------------------- |
447 | // wxTextCtrl-like methods | |
448 | // ---------------------------------------------------------------------------- | |
449 | ||
450 | void wxSpinCtrl::SetValue(const wxString& text) | |
451 | { | |
6fe19057 | 452 | if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) ) |
678cd6de | 453 | { |
f6bcfd97 | 454 | wxLogLastError(wxT("SetWindowText(buddy)")); |
678cd6de VZ |
455 | } |
456 | } | |
457 | ||
eeea41ab VZ |
458 | void wxSpinCtrl::SetValue(int val) |
459 | { | |
460 | wxSpinButton::SetValue(val); | |
461 | ||
462 | // normally setting the value of the spin button is enough as it updates | |
463 | // its buddy control automatically ... | |
464 | if ( wxGetWindowText(m_hwndBuddy).empty() ) | |
465 | { | |
466 | // ... but sometimes it doesn't, notably when the value is 0 and the | |
467 | // text control is currently empty, the spin button seems to be happy | |
468 | // to leave it like this, while we really want to always show the | |
469 | // current value in the control, so do it manually | |
e0a050e3 VS |
470 | ::SetWindowText(GetBuddyHwnd(), |
471 | wxString::Format(_T("%d"), val).wx_str()); | |
eeea41ab | 472 | } |
e816f5c7 | 473 | |
25dff19c | 474 | m_oldValue = GetValue(); |
eeea41ab VZ |
475 | } |
476 | ||
678cd6de VZ |
477 | int wxSpinCtrl::GetValue() const |
478 | { | |
479 | wxString val = wxGetWindowText(m_hwndBuddy); | |
480 | ||
481 | long n; | |
eeea41ab | 482 | if ( (wxSscanf(val, wxT("%ld"), &n) != 1) ) |
678cd6de | 483 | n = INT_MIN; |
3d104ec3 | 484 | |
eeea41ab VZ |
485 | if ( n < m_min ) |
486 | n = m_min; | |
487 | if ( n > m_max ) | |
488 | n = m_max; | |
678cd6de VZ |
489 | |
490 | return n; | |
491 | } | |
492 | ||
07901ec9 VZ |
493 | void wxSpinCtrl::SetSelection(long from, long to) |
494 | { | |
77ffb593 | 495 | // if from and to are both -1, it means (in wxWidgets) that all text should |
07901ec9 VZ |
496 | // be selected - translate into Windows convention |
497 | if ( (from == -1) && (to == -1) ) | |
498 | { | |
499 | from = 0; | |
500 | } | |
501 | ||
7d86a2d4 | 502 | ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to); |
07901ec9 VZ |
503 | } |
504 | ||
baccb514 | 505 | // ---------------------------------------------------------------------------- |
882a8f40 | 506 | // forward some methods to subcontrols |
baccb514 VZ |
507 | // ---------------------------------------------------------------------------- |
508 | ||
509 | bool wxSpinCtrl::SetFont(const wxFont& font) | |
510 | { | |
511 | if ( !wxWindowBase::SetFont(font) ) | |
512 | { | |
513 | // nothing to do | |
57f4f925 | 514 | return false; |
baccb514 VZ |
515 | } |
516 | ||
517 | WXHANDLE hFont = GetFont().GetResourceHandle(); | |
6fe19057 | 518 | (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE); |
b782f2e0 | 519 | |
57f4f925 | 520 | return true; |
b782f2e0 VZ |
521 | } |
522 | ||
882a8f40 VZ |
523 | bool wxSpinCtrl::Show(bool show) |
524 | { | |
525 | if ( !wxControl::Show(show) ) | |
526 | { | |
57f4f925 | 527 | return false; |
882a8f40 VZ |
528 | } |
529 | ||
6fe19057 | 530 | ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE); |
882a8f40 | 531 | |
57f4f925 | 532 | return true; |
882a8f40 VZ |
533 | } |
534 | ||
535 | bool wxSpinCtrl::Enable(bool enable) | |
536 | { | |
537 | if ( !wxControl::Enable(enable) ) | |
538 | { | |
57f4f925 | 539 | return false; |
882a8f40 VZ |
540 | } |
541 | ||
6fe19057 | 542 | ::EnableWindow(GetBuddyHwnd(), enable); |
882a8f40 | 543 | |
57f4f925 | 544 | return true; |
882a8f40 VZ |
545 | } |
546 | ||
8bf3196d GRG |
547 | void wxSpinCtrl::SetFocus() |
548 | { | |
6fe19057 | 549 | ::SetFocus(GetBuddyHwnd()); |
8bf3196d GRG |
550 | } |
551 | ||
74124ea9 VZ |
552 | #if wxUSE_TOOLTIPS |
553 | ||
554 | void wxSpinCtrl::DoSetToolTip(wxToolTip *tip) | |
555 | { | |
556 | wxSpinButton::DoSetToolTip(tip); | |
557 | ||
558 | if ( tip ) | |
559 | tip->Add(m_hwndBuddy); | |
560 | } | |
561 | ||
562 | #endif // wxUSE_TOOLTIPS | |
563 | ||
9750fc42 | 564 | // ---------------------------------------------------------------------------- |
d40e9e06 | 565 | // events processing and generation |
9750fc42 VZ |
566 | // ---------------------------------------------------------------------------- |
567 | ||
d40e9e06 | 568 | void wxSpinCtrl::SendSpinUpdate(int value) |
9750fc42 VZ |
569 | { |
570 | wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId()); | |
571 | event.SetEventObject(this); | |
d40e9e06 | 572 | event.SetInt(value); |
e816f5c7 | 573 | |
937013e0 | 574 | (void)HandleWindowEvent(event); |
d40e9e06 VZ |
575 | |
576 | m_oldValue = value; | |
577 | } | |
9750fc42 | 578 | |
d40e9e06 VZ |
579 | void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin) |
580 | { | |
581 | const int value = eventSpin.GetPosition(); | |
582 | if ( value != m_oldValue ) | |
9750fc42 | 583 | { |
d40e9e06 | 584 | SendSpinUpdate(value); |
9750fc42 VZ |
585 | } |
586 | } | |
587 | ||
b782f2e0 VZ |
588 | // ---------------------------------------------------------------------------- |
589 | // size calculations | |
590 | // ---------------------------------------------------------------------------- | |
591 | ||
f68586e5 | 592 | wxSize wxSpinCtrl::DoGetBestSize() const |
baccb514 VZ |
593 | { |
594 | wxSize sizeBtn = wxSpinButton::DoGetBestSize(); | |
595 | sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN; | |
596 | ||
597 | int y; | |
7a5e53ab | 598 | wxGetCharSize(GetHWND(), NULL, &y, GetFont()); |
baccb514 VZ |
599 | y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y); |
600 | ||
09f27917 JS |
601 | // JACS: we should always use the height calculated |
602 | // from above, because otherwise we'll get a spin control | |
603 | // that's too big. So never use the height calculated | |
604 | // from wxSpinButton::DoGetBestSize(). | |
57f4f925 | 605 | |
09f27917 | 606 | // if ( sizeBtn.y < y ) |
baccb514 VZ |
607 | { |
608 | // make the text tall enough | |
609 | sizeBtn.y = y; | |
610 | } | |
611 | ||
612 | return sizeBtn; | |
613 | } | |
614 | ||
b782f2e0 VZ |
615 | void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height) |
616 | { | |
baccb514 | 617 | int widthBtn = wxSpinButton::DoGetBestSize().x; |
b782f2e0 VZ |
618 | int widthText = width - widthBtn - MARGIN_BETWEEN; |
619 | if ( widthText <= 0 ) | |
620 | { | |
621 | wxLogDebug(_T("not enough space for wxSpinCtrl!")); | |
622 | } | |
623 | ||
8e44f3ca | 624 | // 1) The buddy window |
7d86a2d4 | 625 | DoMoveSibling(m_hwndBuddy, x, y, widthText, height); |
b782f2e0 | 626 | |
8e44f3ca | 627 | // 2) The button window |
b782f2e0 | 628 | x += widthText + MARGIN_BETWEEN; |
7d86a2d4 | 629 | wxSpinButton::DoMoveWindow(x, y, widthBtn, height); |
b782f2e0 VZ |
630 | } |
631 | ||
f6bcfd97 BP |
632 | // get total size of the control |
633 | void wxSpinCtrl::DoGetSize(int *x, int *y) const | |
634 | { | |
635 | RECT spinrect, textrect, ctrlrect; | |
636 | GetWindowRect(GetHwnd(), &spinrect); | |
6fe19057 | 637 | GetWindowRect(GetBuddyHwnd(), &textrect); |
f6bcfd97 BP |
638 | UnionRect(&ctrlrect,&textrect, &spinrect); |
639 | ||
640 | if ( x ) | |
641 | *x = ctrlrect.right - ctrlrect.left; | |
642 | if ( y ) | |
643 | *y = ctrlrect.bottom - ctrlrect.top; | |
644 | } | |
645 | ||
b7527dde VS |
646 | void wxSpinCtrl::DoGetClientSize(int *x, int *y) const |
647 | { | |
648 | RECT spinrect = wxGetClientRect(GetHwnd()); | |
649 | RECT textrect = wxGetClientRect(GetBuddyHwnd()); | |
650 | RECT ctrlrect; | |
651 | UnionRect(&ctrlrect,&textrect, &spinrect); | |
652 | ||
653 | if ( x ) | |
654 | *x = ctrlrect.right - ctrlrect.left; | |
655 | if ( y ) | |
656 | *y = ctrlrect.bottom - ctrlrect.top; | |
657 | } | |
658 | ||
f6bcfd97 BP |
659 | void wxSpinCtrl::DoGetPosition(int *x, int *y) const |
660 | { | |
661 | // hack: pretend that our HWND is the text control just for a moment | |
662 | WXHWND hWnd = GetHWND(); | |
663 | wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy; | |
664 | ||
665 | wxSpinButton::DoGetPosition(x, y); | |
666 | ||
667 | wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd; | |
668 | } | |
669 | ||
74124ea9 | 670 | #endif // wxUSE_SPINCTRL |