]>
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 | |
5a0b1008 | 151 | // forward some messages (mostly the key and focus ones) to 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: | |
5a0b1008 VZ |
166 | #ifdef WM_HELP |
167 | // we need to forward WM_HELP too to ensure that the context help | |
168 | // associated with wxSpinCtrl is shown when the text control part of it | |
169 | // is clicked with the "?" cursor | |
170 | case WM_HELP: | |
171 | #endif | |
f6bcfd97 | 172 | spin->MSWWindowProc(message, wParam, lParam); |
e3582f7f JS |
173 | |
174 | // The control may have been deleted at this point, so check. | |
975b6bcf | 175 | if ( !::IsWindow(hwnd) || wxGetWindowUserData(hwnd) != spin ) |
e3582f7f | 176 | return 0; |
f6bcfd97 | 177 | break; |
350ba193 VZ |
178 | |
179 | case WM_GETDLGCODE: | |
180 | // we want to get WXK_RETURN in order to generate the event for it | |
952ff7bc | 181 | return DLGC_WANTALLKEYS; |
f6bcfd97 | 182 | } |
350ba193 | 183 | |
f6bcfd97 BP |
184 | return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(), |
185 | hwnd, message, wParam, lParam); | |
186 | } | |
187 | ||
6fe19057 VZ |
188 | /* static */ |
189 | wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy) | |
190 | { | |
975b6bcf | 191 | wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData((HWND)hwndBuddy); |
6fe19057 VZ |
192 | |
193 | int i = ms_allSpins.Index(spin); | |
194 | ||
195 | if ( i == wxNOT_FOUND ) | |
196 | return NULL; | |
197 | ||
198 | // sanity check | |
199 | wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy, | |
200 | _T("wxSpinCtrl has incorrect buddy HWND!") ); | |
201 | ||
202 | return spin; | |
203 | } | |
204 | ||
205 | // process a WM_COMMAND generated by the buddy text control | |
206 | bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id)) | |
207 | { | |
ea6cbf48 | 208 | if ( (cmd == EN_CHANGE) && (!m_blockEvent )) |
6fe19057 | 209 | { |
0ca3c231 VZ |
210 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); |
211 | event.SetEventObject(this); | |
212 | wxString val = wxGetWindowText(m_hwndBuddy); | |
213 | event.SetString(val); | |
214 | event.SetInt(GetValue()); | |
937013e0 | 215 | return HandleWindowEvent(event); |
6fe19057 VZ |
216 | } |
217 | ||
218 | // not processed | |
57f4f925 | 219 | return false; |
6fe19057 VZ |
220 | } |
221 | ||
e3582f7f JS |
222 | void wxSpinCtrl::OnChar(wxKeyEvent& event) |
223 | { | |
77e00fe9 | 224 | switch ( event.GetKeyCode() ) |
e3582f7f JS |
225 | { |
226 | case WXK_RETURN: | |
227 | { | |
228 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
229 | InitCommandEvent(event); | |
230 | wxString val = wxGetWindowText(m_hwndBuddy); | |
231 | event.SetString(val); | |
232 | event.SetInt(GetValue()); | |
937013e0 | 233 | if ( HandleWindowEvent(event) ) |
e3582f7f JS |
234 | return; |
235 | break; | |
236 | } | |
237 | ||
238 | case WXK_TAB: | |
239 | // always produce navigation event - even if we process TAB | |
240 | // ourselves the fact that we got here means that the user code | |
241 | // decided to skip processing of this TAB - probably to let it | |
242 | // do its default job. | |
243 | { | |
244 | wxNavigationKeyEvent eventNav; | |
245 | eventNav.SetDirection(!event.ShiftDown()); | |
246 | eventNav.SetWindowChange(event.ControlDown()); | |
247 | eventNav.SetEventObject(this); | |
248 | ||
937013e0 | 249 | if ( GetParent()->HandleWindowEvent(eventNav) ) |
e3582f7f JS |
250 | return; |
251 | } | |
252 | break; | |
253 | } | |
254 | ||
255 | // no, we didn't process it | |
256 | event.Skip(); | |
257 | } | |
258 | ||
4a528443 JS |
259 | void wxSpinCtrl::OnKillFocus(wxFocusEvent& event) |
260 | { | |
5ec60151 VZ |
261 | // ensure that a correct value is shown by the control |
262 | NormalizeValue(); | |
4a528443 JS |
263 | event.Skip(); |
264 | } | |
265 | ||
c5c04fab VZ |
266 | void wxSpinCtrl::OnSetFocus(wxFocusEvent& event) |
267 | { | |
268 | // when we get focus, give it to our buddy window as it needs it more than | |
269 | // we do | |
270 | ::SetFocus((HWND)m_hwndBuddy); | |
271 | ||
272 | event.Skip(); | |
273 | } | |
274 | ||
1e8dba5e RR |
275 | void wxSpinCtrl::NormalizeValue() |
276 | { | |
e816f5c7 | 277 | const int value = GetValue(); |
d40e9e06 | 278 | const bool changed = value != m_oldValue; |
e816f5c7 | 279 | |
b6d83018 VZ |
280 | // notice that we have to call SetValue() even if the value didn't change |
281 | // because otherwise we could be left with empty buddy control when value | |
282 | // is 0, see comment in SetValue() | |
e816f5c7 VZ |
283 | SetValue(value); |
284 | ||
b6d83018 VZ |
285 | if ( changed ) |
286 | { | |
d40e9e06 | 287 | SendSpinUpdate(value); |
b6d83018 | 288 | } |
1e8dba5e RR |
289 | } |
290 | ||
b782f2e0 VZ |
291 | // ---------------------------------------------------------------------------- |
292 | // construction | |
293 | // ---------------------------------------------------------------------------- | |
294 | ||
295 | bool wxSpinCtrl::Create(wxWindow *parent, | |
296 | wxWindowID id, | |
678cd6de | 297 | const wxString& value, |
b782f2e0 VZ |
298 | const wxPoint& pos, |
299 | const wxSize& size, | |
300 | long style, | |
301 | int min, int max, int initial, | |
302 | const wxString& name) | |
303 | { | |
ea6cbf48 RR |
304 | m_blockEvent = false; |
305 | ||
d40e9e06 VZ |
306 | // this should be in ctor/init function but I don't want to add one to 2.8 |
307 | // to avoid problems with default ctor which can be inlined in the user | |
308 | // code and so might not get this fix without recompilation | |
309 | m_oldValue = INT_MIN; | |
310 | ||
b782f2e0 VZ |
311 | // before using DoGetBestSize(), have to set style to let the base class |
312 | // know whether this is a horizontal or vertical control (we're always | |
313 | // vertical) | |
882a8f40 | 314 | style |= wxSP_VERTICAL; |
c76b1a30 JS |
315 | |
316 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) | |
5d72f195 RR |
317 | #ifdef __WXWINCE__ |
318 | style |= wxBORDER_SIMPLE; | |
319 | #else | |
c76b1a30 | 320 | style |= wxBORDER_SUNKEN; |
5d72f195 | 321 | #endif |
c76b1a30 | 322 | |
882a8f40 | 323 | SetWindowStyle(style); |
b782f2e0 | 324 | |
fe3d9123 JS |
325 | WXDWORD exStyle = 0; |
326 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ; | |
327 | ||
8e190f0e VZ |
328 | // this control is used for numeric entry so normally using these flags by |
329 | // default shouldn't be a problem, if it is we can always add a style such | |
330 | // as wxSP_NON_NUMERIC later | |
331 | msStyle |= ES_RIGHT | ES_NUMBER; | |
332 | ||
333 | // calculate the sizes: the size given is the total size for both controls | |
b782f2e0 VZ |
334 | // and we need to fit them both in the given width (height is the same) |
335 | wxSize sizeText(size), sizeBtn(size); | |
336 | sizeBtn.x = wxSpinButton::DoGetBestSize().x; | |
baccb514 VZ |
337 | if ( sizeText.x <= 0 ) |
338 | { | |
339 | // DEFAULT_ITEM_WIDTH is the default width for the text control | |
340 | sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x; | |
341 | } | |
342 | ||
b782f2e0 VZ |
343 | sizeText.x -= sizeBtn.x + MARGIN_BETWEEN; |
344 | if ( sizeText.x <= 0 ) | |
345 | { | |
346 | wxLogDebug(_T("not enough space for wxSpinCtrl!")); | |
347 | } | |
348 | ||
349 | wxPoint posBtn(pos); | |
350 | posBtn.x += sizeText.x + MARGIN_BETWEEN; | |
351 | ||
c5c04fab VZ |
352 | // we must create the text control before the spin button for the purpose |
353 | // of the dialog navigation: if there is a static text just before the spin | |
354 | // control, activating it by Alt-letter should give focus to the text | |
355 | // control, not the spin and the dialog navigation code will give focus to | |
356 | // the next control (at Windows level), not the one after it | |
b782f2e0 | 357 | |
c5c04fab | 358 | // create the text window |
b782f2e0 | 359 | |
b782f2e0 VZ |
360 | m_hwndBuddy = (WXHWND)::CreateWindowEx |
361 | ( | |
c5c04fab | 362 | exStyle, // sunken border |
baccb514 VZ |
363 | _T("EDIT"), // window class |
364 | NULL, // no window title | |
c5c04fab | 365 | msStyle, // style (will be shown later) |
baccb514 VZ |
366 | pos.x, pos.y, // position |
367 | 0, 0, // size (will be set later) | |
368 | GetHwndOf(parent), // parent | |
369 | (HMENU)-1, // control id | |
370 | wxGetInstance(), // app instance | |
371 | NULL // unused client data | |
b782f2e0 VZ |
372 | ); |
373 | ||
374 | if ( !m_hwndBuddy ) | |
375 | { | |
f6bcfd97 | 376 | wxLogLastError(wxT("CreateWindow(buddy text window)")); |
b782f2e0 | 377 | |
57f4f925 | 378 | return false; |
b782f2e0 VZ |
379 | } |
380 | ||
c5c04fab VZ |
381 | |
382 | // create the spin button | |
383 | if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) ) | |
384 | { | |
57f4f925 | 385 | return false; |
c5c04fab VZ |
386 | } |
387 | ||
23ec96e3 | 388 | wxSpinButtonBase::SetRange(min, max); |
e816f5c7 | 389 | |
f6bcfd97 | 390 | // subclass the text ctrl to be able to intercept some events |
975b6bcf VZ |
391 | wxSetWindowUserData(GetBuddyHwnd(), this); |
392 | m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(), | |
393 | wxBuddyTextWndProc); | |
f6bcfd97 | 394 | |
8d2e831b RD |
395 | // set up fonts and colours (This is nomally done in MSWCreateControl) |
396 | InheritAttributes(); | |
e0176dd9 VZ |
397 | if (!m_hasFont) |
398 | SetFont(GetDefaultAttributes().font); | |
8d2e831b | 399 | |
baccb514 VZ |
400 | // set the size of the text window - can do it only now, because we |
401 | // couldn't call DoGetBestSize() before as font wasn't set | |
402 | if ( sizeText.y <= 0 ) | |
403 | { | |
882a8f40 | 404 | int cx, cy; |
7a5e53ab | 405 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); |
882a8f40 VZ |
406 | |
407 | sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
baccb514 VZ |
408 | } |
409 | ||
170acdc9 | 410 | SetInitialSize(size); |
baccb514 | 411 | |
6fe19057 | 412 | (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW); |
b782f2e0 VZ |
413 | |
414 | // associate the text window with the spin button | |
baccb514 VZ |
415 | (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0); |
416 | ||
549cc3a8 JS |
417 | SetValue(initial); |
418 | ||
419 | // Set the range in the native control | |
420 | SetRange(min, max); | |
421 | ||
3d104ec3 | 422 | if ( !value.empty() ) |
678cd6de VZ |
423 | { |
424 | SetValue(value); | |
5bcdac45 JS |
425 | m_oldValue = (int) wxAtol(value); |
426 | } | |
427 | else | |
428 | { | |
429 | SetValue(wxString::Format(wxT("%d"), initial)); | |
430 | m_oldValue = initial; | |
678cd6de VZ |
431 | } |
432 | ||
6fe19057 VZ |
433 | // do it after finishing with m_hwndBuddy creation to avoid generating |
434 | // initial wxEVT_COMMAND_TEXT_UPDATED message | |
435 | ms_allSpins.Add(this); | |
436 | ||
57f4f925 | 437 | return true; |
baccb514 VZ |
438 | } |
439 | ||
f6bcfd97 BP |
440 | wxSpinCtrl::~wxSpinCtrl() |
441 | { | |
6fe19057 VZ |
442 | ms_allSpins.Remove(this); |
443 | ||
5648c0ad JS |
444 | // This removes spurious memory leak reporting |
445 | if (ms_allSpins.GetCount() == 0) | |
446 | ms_allSpins.Clear(); | |
447 | ||
f6bcfd97 BP |
448 | // destroy the buddy window because this pointer which wxBuddyTextWndProc |
449 | // uses will not soon be valid any more | |
6fe19057 | 450 | ::DestroyWindow(GetBuddyHwnd()); |
f6bcfd97 BP |
451 | } |
452 | ||
678cd6de VZ |
453 | // ---------------------------------------------------------------------------- |
454 | // wxTextCtrl-like methods | |
455 | // ---------------------------------------------------------------------------- | |
456 | ||
457 | void wxSpinCtrl::SetValue(const wxString& text) | |
458 | { | |
6fe19057 | 459 | if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) ) |
678cd6de | 460 | { |
f6bcfd97 | 461 | wxLogLastError(wxT("SetWindowText(buddy)")); |
678cd6de VZ |
462 | } |
463 | } | |
464 | ||
eeea41ab VZ |
465 | void wxSpinCtrl::SetValue(int val) |
466 | { | |
ea6cbf48 RR |
467 | m_blockEvent = true; |
468 | ||
eeea41ab VZ |
469 | wxSpinButton::SetValue(val); |
470 | ||
471 | // normally setting the value of the spin button is enough as it updates | |
472 | // its buddy control automatically ... | |
473 | if ( wxGetWindowText(m_hwndBuddy).empty() ) | |
474 | { | |
475 | // ... but sometimes it doesn't, notably when the value is 0 and the | |
476 | // text control is currently empty, the spin button seems to be happy | |
477 | // to leave it like this, while we really want to always show the | |
478 | // current value in the control, so do it manually | |
e0a050e3 VS |
479 | ::SetWindowText(GetBuddyHwnd(), |
480 | wxString::Format(_T("%d"), val).wx_str()); | |
eeea41ab | 481 | } |
e816f5c7 | 482 | |
25dff19c | 483 | m_oldValue = GetValue(); |
ea6cbf48 RR |
484 | |
485 | m_blockEvent = false; | |
eeea41ab VZ |
486 | } |
487 | ||
678cd6de VZ |
488 | int wxSpinCtrl::GetValue() const |
489 | { | |
490 | wxString val = wxGetWindowText(m_hwndBuddy); | |
491 | ||
492 | long n; | |
eeea41ab | 493 | if ( (wxSscanf(val, wxT("%ld"), &n) != 1) ) |
678cd6de | 494 | n = INT_MIN; |
3d104ec3 | 495 | |
eeea41ab VZ |
496 | if ( n < m_min ) |
497 | n = m_min; | |
498 | if ( n > m_max ) | |
499 | n = m_max; | |
678cd6de VZ |
500 | |
501 | return n; | |
502 | } | |
503 | ||
07901ec9 VZ |
504 | void wxSpinCtrl::SetSelection(long from, long to) |
505 | { | |
77ffb593 | 506 | // if from and to are both -1, it means (in wxWidgets) that all text should |
07901ec9 VZ |
507 | // be selected - translate into Windows convention |
508 | if ( (from == -1) && (to == -1) ) | |
509 | { | |
510 | from = 0; | |
511 | } | |
512 | ||
7d86a2d4 | 513 | ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to); |
07901ec9 VZ |
514 | } |
515 | ||
baccb514 | 516 | // ---------------------------------------------------------------------------- |
882a8f40 | 517 | // forward some methods to subcontrols |
baccb514 VZ |
518 | // ---------------------------------------------------------------------------- |
519 | ||
520 | bool wxSpinCtrl::SetFont(const wxFont& font) | |
521 | { | |
522 | if ( !wxWindowBase::SetFont(font) ) | |
523 | { | |
524 | // nothing to do | |
57f4f925 | 525 | return false; |
baccb514 VZ |
526 | } |
527 | ||
528 | WXHANDLE hFont = GetFont().GetResourceHandle(); | |
6fe19057 | 529 | (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE); |
b782f2e0 | 530 | |
57f4f925 | 531 | return true; |
b782f2e0 VZ |
532 | } |
533 | ||
882a8f40 VZ |
534 | bool wxSpinCtrl::Show(bool show) |
535 | { | |
536 | if ( !wxControl::Show(show) ) | |
537 | { | |
57f4f925 | 538 | return false; |
882a8f40 VZ |
539 | } |
540 | ||
6fe19057 | 541 | ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE); |
882a8f40 | 542 | |
57f4f925 | 543 | return true; |
882a8f40 VZ |
544 | } |
545 | ||
03d4194d VZ |
546 | bool wxSpinCtrl::Reparent(wxWindowBase *newParent) |
547 | { | |
548 | // Reparenting both the updown control and its buddy does not seem to work: | |
549 | // they continue to be connected somehow, but visually there is no feedback | |
550 | // on the buddy edit control. To avoid this problem, we reparent the buddy | |
551 | // window normally, but we recreate the updown control and reassign its | |
552 | // buddy. | |
553 | ||
554 | if ( !wxWindowBase::Reparent(newParent) ) | |
555 | return false; | |
556 | ||
557 | newParent->GetChildren().DeleteObject(this); | |
558 | ||
559 | // preserve the old values | |
560 | const wxSize size = GetSize(); | |
561 | int value = GetValue(); | |
562 | const wxRect btnRect = wxRectFromRECT(wxGetWindowRect(GetHwnd())); | |
563 | ||
564 | // destroy the old spin button | |
565 | UnsubclassWin(); | |
566 | if ( !::DestroyWindow(GetHwnd()) ) | |
567 | wxLogLastError(wxT("DestroyWindow")); | |
568 | ||
569 | // create and initialize the new one | |
570 | if ( !wxSpinButton::Create(GetParent(), GetId(), | |
571 | btnRect.GetPosition(), btnRect.GetSize(), | |
572 | GetWindowStyle(), GetName()) ) | |
573 | return false; | |
574 | ||
575 | SetValue(value); | |
576 | SetRange(m_min, m_max); | |
577 | SetInitialSize(size); | |
578 | ||
579 | // associate it with the buddy control again | |
580 | ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent())); | |
581 | (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)GetBuddyHwnd(), 0); | |
582 | ||
583 | return true; | |
584 | } | |
585 | ||
882a8f40 VZ |
586 | bool wxSpinCtrl::Enable(bool enable) |
587 | { | |
588 | if ( !wxControl::Enable(enable) ) | |
589 | { | |
57f4f925 | 590 | return false; |
882a8f40 VZ |
591 | } |
592 | ||
6fe19057 | 593 | ::EnableWindow(GetBuddyHwnd(), enable); |
882a8f40 | 594 | |
57f4f925 | 595 | return true; |
882a8f40 VZ |
596 | } |
597 | ||
8bf3196d GRG |
598 | void wxSpinCtrl::SetFocus() |
599 | { | |
6fe19057 | 600 | ::SetFocus(GetBuddyHwnd()); |
8bf3196d GRG |
601 | } |
602 | ||
74124ea9 VZ |
603 | #if wxUSE_TOOLTIPS |
604 | ||
605 | void wxSpinCtrl::DoSetToolTip(wxToolTip *tip) | |
606 | { | |
607 | wxSpinButton::DoSetToolTip(tip); | |
608 | ||
609 | if ( tip ) | |
610 | tip->Add(m_hwndBuddy); | |
611 | } | |
612 | ||
613 | #endif // wxUSE_TOOLTIPS | |
614 | ||
9750fc42 | 615 | // ---------------------------------------------------------------------------- |
d40e9e06 | 616 | // events processing and generation |
9750fc42 VZ |
617 | // ---------------------------------------------------------------------------- |
618 | ||
d40e9e06 | 619 | void wxSpinCtrl::SendSpinUpdate(int value) |
9750fc42 VZ |
620 | { |
621 | wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId()); | |
622 | event.SetEventObject(this); | |
d40e9e06 | 623 | event.SetInt(value); |
e816f5c7 | 624 | |
937013e0 | 625 | (void)HandleWindowEvent(event); |
d40e9e06 VZ |
626 | |
627 | m_oldValue = value; | |
628 | } | |
9750fc42 | 629 | |
d40e9e06 VZ |
630 | void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin) |
631 | { | |
632 | const int value = eventSpin.GetPosition(); | |
633 | if ( value != m_oldValue ) | |
9750fc42 | 634 | { |
d40e9e06 | 635 | SendSpinUpdate(value); |
9750fc42 VZ |
636 | } |
637 | } | |
638 | ||
b782f2e0 VZ |
639 | // ---------------------------------------------------------------------------- |
640 | // size calculations | |
641 | // ---------------------------------------------------------------------------- | |
642 | ||
f68586e5 | 643 | wxSize wxSpinCtrl::DoGetBestSize() const |
baccb514 VZ |
644 | { |
645 | wxSize sizeBtn = wxSpinButton::DoGetBestSize(); | |
646 | sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN; | |
647 | ||
648 | int y; | |
7a5e53ab | 649 | wxGetCharSize(GetHWND(), NULL, &y, GetFont()); |
baccb514 VZ |
650 | y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y); |
651 | ||
09f27917 JS |
652 | // JACS: we should always use the height calculated |
653 | // from above, because otherwise we'll get a spin control | |
654 | // that's too big. So never use the height calculated | |
655 | // from wxSpinButton::DoGetBestSize(). | |
57f4f925 | 656 | |
09f27917 | 657 | // if ( sizeBtn.y < y ) |
baccb514 VZ |
658 | { |
659 | // make the text tall enough | |
660 | sizeBtn.y = y; | |
661 | } | |
662 | ||
663 | return sizeBtn; | |
664 | } | |
665 | ||
b782f2e0 VZ |
666 | void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height) |
667 | { | |
baccb514 | 668 | int widthBtn = wxSpinButton::DoGetBestSize().x; |
b782f2e0 VZ |
669 | int widthText = width - widthBtn - MARGIN_BETWEEN; |
670 | if ( widthText <= 0 ) | |
671 | { | |
672 | wxLogDebug(_T("not enough space for wxSpinCtrl!")); | |
673 | } | |
674 | ||
8e44f3ca | 675 | // 1) The buddy window |
7d86a2d4 | 676 | DoMoveSibling(m_hwndBuddy, x, y, widthText, height); |
b782f2e0 | 677 | |
8e44f3ca | 678 | // 2) The button window |
b782f2e0 | 679 | x += widthText + MARGIN_BETWEEN; |
7d86a2d4 | 680 | wxSpinButton::DoMoveWindow(x, y, widthBtn, height); |
b782f2e0 VZ |
681 | } |
682 | ||
f6bcfd97 BP |
683 | // get total size of the control |
684 | void wxSpinCtrl::DoGetSize(int *x, int *y) const | |
685 | { | |
686 | RECT spinrect, textrect, ctrlrect; | |
687 | GetWindowRect(GetHwnd(), &spinrect); | |
6fe19057 | 688 | GetWindowRect(GetBuddyHwnd(), &textrect); |
f6bcfd97 BP |
689 | UnionRect(&ctrlrect,&textrect, &spinrect); |
690 | ||
691 | if ( x ) | |
692 | *x = ctrlrect.right - ctrlrect.left; | |
693 | if ( y ) | |
694 | *y = ctrlrect.bottom - ctrlrect.top; | |
695 | } | |
696 | ||
b7527dde VS |
697 | void wxSpinCtrl::DoGetClientSize(int *x, int *y) const |
698 | { | |
699 | RECT spinrect = wxGetClientRect(GetHwnd()); | |
700 | RECT textrect = wxGetClientRect(GetBuddyHwnd()); | |
701 | RECT ctrlrect; | |
702 | UnionRect(&ctrlrect,&textrect, &spinrect); | |
703 | ||
704 | if ( x ) | |
705 | *x = ctrlrect.right - ctrlrect.left; | |
706 | if ( y ) | |
707 | *y = ctrlrect.bottom - ctrlrect.top; | |
708 | } | |
709 | ||
f6bcfd97 BP |
710 | void wxSpinCtrl::DoGetPosition(int *x, int *y) const |
711 | { | |
712 | // hack: pretend that our HWND is the text control just for a moment | |
713 | WXHWND hWnd = GetHWND(); | |
714 | wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy; | |
715 | ||
716 | wxSpinButton::DoGetPosition(x, y); | |
717 | ||
718 | wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd; | |
719 | } | |
720 | ||
74124ea9 | 721 | #endif // wxUSE_SPINCTRL |