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