]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/spinctrl.cpp | |
3 | // Purpose: wxSpinCtrl class implementation for Win32 | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 22.07.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999-2005 Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
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 | ||
27 | #if wxUSE_SPINCTRL | |
28 | ||
29 | #include "wx/spinctrl.h" | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" | |
33 | #include "wx/event.h" | |
34 | #include "wx/textctrl.h" | |
35 | #include "wx/wxcrtvararg.h" | |
36 | #endif | |
37 | ||
38 | #include "wx/msw/private.h" | |
39 | ||
40 | #if wxUSE_TOOLTIPS | |
41 | #include "wx/tooltip.h" | |
42 | #endif // wxUSE_TOOLTIPS | |
43 | ||
44 | #include <limits.h> // for INT_MIN | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // macros | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | #if wxUSE_EXTENDED_RTTI | |
51 | WX_DEFINE_FLAGS( wxSpinCtrlStyle ) | |
52 | ||
53 | wxBEGIN_FLAGS( wxSpinCtrlStyle ) | |
54 | // new style border flags, we put them first to | |
55 | // use them for streaming out | |
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) | |
62 | ||
63 | // old style border flags | |
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) | |
69 | wxFLAGS_MEMBER(wxBORDER) | |
70 | ||
71 | // standard window styles | |
72 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
73 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
74 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
75 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
76 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
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 ) | |
87 | ||
88 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl, wxControl,"wx/spinbut.h") | |
89 | ||
90 | wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl) | |
91 | wxEVENT_RANGE_PROPERTY( Spin , wxEVT_SCROLL_TOP , wxEVT_SCROLL_CHANGED , wxSpinEvent ) | |
92 | wxEVENT_PROPERTY( Updated , wxEVT_COMMAND_SPINCTRL_UPDATED , wxCommandEvent ) | |
93 | wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent ) | |
94 | wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent ) | |
95 | ||
96 | wxPROPERTY( ValueString , wxString , SetValue , GetValue , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ; | |
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")) | |
100 | wxPROPERTY_FLAGS( WindowStyle , wxSpinCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
101 | /* | |
102 | TODO PROPERTIES | |
103 | style wxSP_ARROW_KEYS | |
104 | */ | |
105 | wxEND_PROPERTIES_TABLE() | |
106 | ||
107 | wxBEGIN_HANDLERS_TABLE(wxSpinCtrl) | |
108 | wxEND_HANDLERS_TABLE() | |
109 | ||
110 | wxCONSTRUCTOR_6( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , ValueString , wxPoint , Position , wxSize , Size , long , WindowStyle ) | |
111 | #else | |
112 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl) | |
113 | #endif | |
114 | ||
115 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton) | |
116 | EVT_CHAR(wxSpinCtrl::OnChar) | |
117 | EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus) | |
118 | EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus) | |
119 | END_EVENT_TABLE() | |
120 | ||
121 | #define GetBuddyHwnd() (HWND)(m_hwndBuddy) | |
122 | ||
123 | // ---------------------------------------------------------------------------- | |
124 | // constants | |
125 | // ---------------------------------------------------------------------------- | |
126 | ||
127 | // the margin between the up-down control and its buddy (can be arbitrary, | |
128 | // choose what you like - or may be decide during run-time depending on the | |
129 | // font size?) | |
130 | static const int MARGIN_BETWEEN = 1; | |
131 | ||
132 | // ============================================================================ | |
133 | // implementation | |
134 | // ============================================================================ | |
135 | ||
136 | wxArraySpins wxSpinCtrl::ms_allSpins; | |
137 | ||
138 | // ---------------------------------------------------------------------------- | |
139 | // wnd proc for the buddy text ctrl | |
140 | // ---------------------------------------------------------------------------- | |
141 | ||
142 | LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd, | |
143 | UINT message, | |
144 | WPARAM wParam, | |
145 | LPARAM lParam) | |
146 | { | |
147 | wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData(hwnd); | |
148 | ||
149 | // forward some messages (mostly the key and focus ones) to the spin ctrl | |
150 | switch ( message ) | |
151 | { | |
152 | case WM_SETFOCUS: | |
153 | // if the focus comes from the spin control itself, don't set it | |
154 | // back to it -- we don't want to go into an infinite loop | |
155 | if ( (WXHWND)wParam == spin->GetHWND() ) | |
156 | break; | |
157 | //else: fall through | |
158 | ||
159 | case WM_KILLFOCUS: | |
160 | case WM_CHAR: | |
161 | case WM_DEADCHAR: | |
162 | case WM_KEYUP: | |
163 | case WM_KEYDOWN: | |
164 | #ifdef WM_HELP | |
165 | // we need to forward WM_HELP too to ensure that the context help | |
166 | // associated with wxSpinCtrl is shown when the text control part of it | |
167 | // is clicked with the "?" cursor | |
168 | case WM_HELP: | |
169 | #endif | |
170 | spin->MSWWindowProc(message, wParam, lParam); | |
171 | ||
172 | // The control may have been deleted at this point, so check. | |
173 | if ( !::IsWindow(hwnd) || wxGetWindowUserData(hwnd) != spin ) | |
174 | return 0; | |
175 | break; | |
176 | ||
177 | case WM_GETDLGCODE: | |
178 | if ( spin->HasFlag(wxTE_PROCESS_ENTER) ) | |
179 | { | |
180 | long dlgCode = ::CallWindowProc | |
181 | ( | |
182 | CASTWNDPROC spin->GetBuddyWndProc(), | |
183 | hwnd, | |
184 | message, | |
185 | wParam, | |
186 | lParam | |
187 | ); | |
188 | dlgCode |= DLGC_WANTMESSAGE; | |
189 | return dlgCode; | |
190 | } | |
191 | break; | |
192 | } | |
193 | ||
194 | return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(), | |
195 | hwnd, message, wParam, lParam); | |
196 | } | |
197 | ||
198 | /* static */ | |
199 | wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy) | |
200 | { | |
201 | wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData((HWND)hwndBuddy); | |
202 | ||
203 | int i = ms_allSpins.Index(spin); | |
204 | ||
205 | if ( i == wxNOT_FOUND ) | |
206 | return NULL; | |
207 | ||
208 | // sanity check | |
209 | wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy, | |
210 | wxT("wxSpinCtrl has incorrect buddy HWND!") ); | |
211 | ||
212 | return spin; | |
213 | } | |
214 | ||
215 | // process a WM_COMMAND generated by the buddy text control | |
216 | bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id)) | |
217 | { | |
218 | if ( (cmd == EN_CHANGE) && (!m_blockEvent )) | |
219 | { | |
220 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
221 | event.SetEventObject(this); | |
222 | wxString val = wxGetWindowText(m_hwndBuddy); | |
223 | event.SetString(val); | |
224 | event.SetInt(GetValue()); | |
225 | return HandleWindowEvent(event); | |
226 | } | |
227 | ||
228 | // not processed | |
229 | return false; | |
230 | } | |
231 | ||
232 | void wxSpinCtrl::OnChar(wxKeyEvent& event) | |
233 | { | |
234 | switch ( event.GetKeyCode() ) | |
235 | { | |
236 | case WXK_RETURN: | |
237 | { | |
238 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
239 | InitCommandEvent(event); | |
240 | wxString val = wxGetWindowText(m_hwndBuddy); | |
241 | event.SetString(val); | |
242 | event.SetInt(GetValue()); | |
243 | if ( HandleWindowEvent(event) ) | |
244 | return; | |
245 | break; | |
246 | } | |
247 | ||
248 | case WXK_TAB: | |
249 | // always produce navigation event - even if we process TAB | |
250 | // ourselves the fact that we got here means that the user code | |
251 | // decided to skip processing of this TAB - probably to let it | |
252 | // do its default job. | |
253 | { | |
254 | wxNavigationKeyEvent eventNav; | |
255 | eventNav.SetDirection(!event.ShiftDown()); | |
256 | eventNav.SetWindowChange(event.ControlDown()); | |
257 | eventNav.SetEventObject(this); | |
258 | ||
259 | if ( GetParent()->HandleWindowEvent(eventNav) ) | |
260 | return; | |
261 | } | |
262 | break; | |
263 | } | |
264 | ||
265 | // no, we didn't process it | |
266 | event.Skip(); | |
267 | } | |
268 | ||
269 | void wxSpinCtrl::OnKillFocus(wxFocusEvent& event) | |
270 | { | |
271 | // ensure that a correct value is shown by the control | |
272 | NormalizeValue(); | |
273 | event.Skip(); | |
274 | } | |
275 | ||
276 | void wxSpinCtrl::OnSetFocus(wxFocusEvent& event) | |
277 | { | |
278 | // when we get focus, give it to our buddy window as it needs it more than | |
279 | // we do | |
280 | ::SetFocus((HWND)m_hwndBuddy); | |
281 | ||
282 | event.Skip(); | |
283 | } | |
284 | ||
285 | void wxSpinCtrl::NormalizeValue() | |
286 | { | |
287 | const int value = GetValue(); | |
288 | const bool changed = value != m_oldValue; | |
289 | ||
290 | // notice that we have to call SetValue() even if the value didn't change | |
291 | // because otherwise we could be left with empty buddy control when value | |
292 | // is 0, see comment in SetValue() | |
293 | SetValue(value); | |
294 | ||
295 | if ( changed ) | |
296 | { | |
297 | SendSpinUpdate(value); | |
298 | } | |
299 | } | |
300 | ||
301 | // ---------------------------------------------------------------------------- | |
302 | // construction | |
303 | // ---------------------------------------------------------------------------- | |
304 | ||
305 | bool wxSpinCtrl::Create(wxWindow *parent, | |
306 | wxWindowID id, | |
307 | const wxString& value, | |
308 | const wxPoint& pos, | |
309 | const wxSize& size, | |
310 | long style, | |
311 | int min, int max, int initial, | |
312 | const wxString& name) | |
313 | { | |
314 | m_blockEvent = false; | |
315 | ||
316 | // this should be in ctor/init function but I don't want to add one to 2.8 | |
317 | // to avoid problems with default ctor which can be inlined in the user | |
318 | // code and so might not get this fix without recompilation | |
319 | m_oldValue = INT_MIN; | |
320 | ||
321 | // before using DoGetBestSize(), have to set style to let the base class | |
322 | // know whether this is a horizontal or vertical control (we're always | |
323 | // vertical) | |
324 | style |= wxSP_VERTICAL; | |
325 | ||
326 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) | |
327 | #ifdef __WXWINCE__ | |
328 | style |= wxBORDER_SIMPLE; | |
329 | #else | |
330 | style |= wxBORDER_SUNKEN; | |
331 | #endif | |
332 | ||
333 | SetWindowStyle(style); | |
334 | ||
335 | WXDWORD exStyle = 0; | |
336 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ; | |
337 | ||
338 | // propagate text alignment style to text ctrl | |
339 | if ( style & wxALIGN_RIGHT ) | |
340 | msStyle |= ES_RIGHT; | |
341 | else if ( style & wxALIGN_CENTER ) | |
342 | msStyle |= ES_CENTER; | |
343 | ||
344 | // calculate the sizes: the size given is the total size for both controls | |
345 | // and we need to fit them both in the given width (height is the same) | |
346 | wxSize sizeText(size), sizeBtn(size); | |
347 | sizeBtn.x = wxSpinButton::DoGetBestSize().x; | |
348 | if ( sizeText.x <= 0 ) | |
349 | { | |
350 | // DEFAULT_ITEM_WIDTH is the default width for the text control | |
351 | sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x; | |
352 | } | |
353 | ||
354 | sizeText.x -= sizeBtn.x + MARGIN_BETWEEN; | |
355 | if ( sizeText.x <= 0 ) | |
356 | { | |
357 | wxLogDebug(wxT("not enough space for wxSpinCtrl!")); | |
358 | } | |
359 | ||
360 | wxPoint posBtn(pos); | |
361 | posBtn.x += sizeText.x + MARGIN_BETWEEN; | |
362 | ||
363 | // we must create the text control before the spin button for the purpose | |
364 | // of the dialog navigation: if there is a static text just before the spin | |
365 | // control, activating it by Alt-letter should give focus to the text | |
366 | // control, not the spin and the dialog navigation code will give focus to | |
367 | // the next control (at Windows level), not the one after it | |
368 | ||
369 | // create the text window | |
370 | ||
371 | m_hwndBuddy = (WXHWND)::CreateWindowEx | |
372 | ( | |
373 | exStyle, // sunken border | |
374 | wxT("EDIT"), // window class | |
375 | NULL, // no window title | |
376 | msStyle, // style (will be shown later) | |
377 | pos.x, pos.y, // position | |
378 | 0, 0, // size (will be set later) | |
379 | GetHwndOf(parent), // parent | |
380 | (HMENU)-1, // control id | |
381 | wxGetInstance(), // app instance | |
382 | NULL // unused client data | |
383 | ); | |
384 | ||
385 | if ( !m_hwndBuddy ) | |
386 | { | |
387 | wxLogLastError(wxT("CreateWindow(buddy text window)")); | |
388 | ||
389 | return false; | |
390 | } | |
391 | ||
392 | ||
393 | // create the spin button | |
394 | if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) ) | |
395 | { | |
396 | return false; | |
397 | } | |
398 | ||
399 | wxSpinButtonBase::SetRange(min, max); | |
400 | ||
401 | // subclass the text ctrl to be able to intercept some events | |
402 | wxSetWindowUserData(GetBuddyHwnd(), this); | |
403 | m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(), | |
404 | wxBuddyTextWndProc); | |
405 | ||
406 | // set up fonts and colours (This is nomally done in MSWCreateControl) | |
407 | InheritAttributes(); | |
408 | if (!m_hasFont) | |
409 | SetFont(GetDefaultAttributes().font); | |
410 | ||
411 | // set the size of the text window - can do it only now, because we | |
412 | // couldn't call DoGetBestSize() before as font wasn't set | |
413 | if ( sizeText.y <= 0 ) | |
414 | { | |
415 | int cx, cy; | |
416 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); | |
417 | ||
418 | sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
419 | } | |
420 | ||
421 | SetInitialSize(size); | |
422 | ||
423 | (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW); | |
424 | ||
425 | // associate the text window with the spin button | |
426 | (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0); | |
427 | ||
428 | SetValue(initial); | |
429 | ||
430 | // Set the range in the native control | |
431 | SetRange(min, max); | |
432 | ||
433 | if ( !value.empty() ) | |
434 | { | |
435 | SetValue(value); | |
436 | m_oldValue = (int) wxAtol(value); | |
437 | } | |
438 | else | |
439 | { | |
440 | SetValue(wxString::Format(wxT("%d"), initial)); | |
441 | m_oldValue = initial; | |
442 | } | |
443 | ||
444 | // do it after finishing with m_hwndBuddy creation to avoid generating | |
445 | // initial wxEVT_COMMAND_TEXT_UPDATED message | |
446 | ms_allSpins.Add(this); | |
447 | ||
448 | return true; | |
449 | } | |
450 | ||
451 | wxSpinCtrl::~wxSpinCtrl() | |
452 | { | |
453 | ms_allSpins.Remove(this); | |
454 | ||
455 | // This removes spurious memory leak reporting | |
456 | if (ms_allSpins.GetCount() == 0) | |
457 | ms_allSpins.Clear(); | |
458 | ||
459 | // destroy the buddy window because this pointer which wxBuddyTextWndProc | |
460 | // uses will not soon be valid any more | |
461 | ::DestroyWindow(GetBuddyHwnd()); | |
462 | } | |
463 | ||
464 | // ---------------------------------------------------------------------------- | |
465 | // wxTextCtrl-like methods | |
466 | // ---------------------------------------------------------------------------- | |
467 | ||
468 | void wxSpinCtrl::SetValue(const wxString& text) | |
469 | { | |
470 | if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) ) | |
471 | { | |
472 | wxLogLastError(wxT("SetWindowText(buddy)")); | |
473 | } | |
474 | } | |
475 | ||
476 | void wxSpinCtrl::SetValue(int val) | |
477 | { | |
478 | m_blockEvent = true; | |
479 | ||
480 | wxSpinButton::SetValue(val); | |
481 | ||
482 | // normally setting the value of the spin button is enough as it updates | |
483 | // its buddy control automatically ... | |
484 | if ( wxGetWindowText(m_hwndBuddy).empty() ) | |
485 | { | |
486 | // ... but sometimes it doesn't, notably when the value is 0 and the | |
487 | // text control is currently empty, the spin button seems to be happy | |
488 | // to leave it like this, while we really want to always show the | |
489 | // current value in the control, so do it manually | |
490 | ::SetWindowText(GetBuddyHwnd(), | |
491 | wxString::Format(wxT("%d"), val).wx_str()); | |
492 | } | |
493 | ||
494 | m_oldValue = GetValue(); | |
495 | ||
496 | m_blockEvent = false; | |
497 | } | |
498 | ||
499 | int wxSpinCtrl::GetValue() const | |
500 | { | |
501 | wxString val = wxGetWindowText(m_hwndBuddy); | |
502 | ||
503 | long n; | |
504 | if ( (wxSscanf(val, wxT("%ld"), &n) != 1) ) | |
505 | n = INT_MIN; | |
506 | ||
507 | if ( n < m_min ) | |
508 | n = m_min; | |
509 | if ( n > m_max ) | |
510 | n = m_max; | |
511 | ||
512 | return n; | |
513 | } | |
514 | ||
515 | void wxSpinCtrl::SetSelection(long from, long to) | |
516 | { | |
517 | // if from and to are both -1, it means (in wxWidgets) that all text should | |
518 | // be selected - translate into Windows convention | |
519 | if ( (from == -1) && (to == -1) ) | |
520 | { | |
521 | from = 0; | |
522 | } | |
523 | ||
524 | ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to); | |
525 | } | |
526 | ||
527 | // ---------------------------------------------------------------------------- | |
528 | // wxSpinButton methods | |
529 | // ---------------------------------------------------------------------------- | |
530 | ||
531 | void wxSpinCtrl::SetRange(int minVal, int maxVal) | |
532 | { | |
533 | wxSpinButton::SetRange(minVal, maxVal); | |
534 | ||
535 | // this control is used for numeric entry so restrict the input to numeric | |
536 | // keys only -- but only if we don't need to be able to enter "-" in it as | |
537 | // otherwise this would become impossible | |
538 | const DWORD styleOld = ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE); | |
539 | DWORD styleNew; | |
540 | if ( minVal < 0 ) | |
541 | styleNew = styleOld & ~ES_NUMBER; | |
542 | else | |
543 | styleNew = styleOld | ES_NUMBER; | |
544 | ||
545 | if ( styleNew != styleOld ) | |
546 | ::SetWindowLong(GetBuddyHwnd(), GWL_STYLE, styleNew); | |
547 | } | |
548 | ||
549 | // ---------------------------------------------------------------------------- | |
550 | // forward some methods to subcontrols | |
551 | // ---------------------------------------------------------------------------- | |
552 | ||
553 | bool wxSpinCtrl::SetFont(const wxFont& font) | |
554 | { | |
555 | if ( !wxWindowBase::SetFont(font) ) | |
556 | { | |
557 | // nothing to do | |
558 | return false; | |
559 | } | |
560 | ||
561 | WXHANDLE hFont = GetFont().GetResourceHandle(); | |
562 | (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE); | |
563 | ||
564 | return true; | |
565 | } | |
566 | ||
567 | bool wxSpinCtrl::Show(bool show) | |
568 | { | |
569 | if ( !wxControl::Show(show) ) | |
570 | { | |
571 | return false; | |
572 | } | |
573 | ||
574 | ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE); | |
575 | ||
576 | return true; | |
577 | } | |
578 | ||
579 | bool wxSpinCtrl::Reparent(wxWindowBase *newParent) | |
580 | { | |
581 | // Reparenting both the updown control and its buddy does not seem to work: | |
582 | // they continue to be connected somehow, but visually there is no feedback | |
583 | // on the buddy edit control. To avoid this problem, we reparent the buddy | |
584 | // window normally, but we recreate the updown control and reassign its | |
585 | // buddy. | |
586 | ||
587 | if ( !wxWindowBase::Reparent(newParent) ) | |
588 | return false; | |
589 | ||
590 | newParent->GetChildren().DeleteObject(this); | |
591 | ||
592 | // preserve the old values | |
593 | const wxSize size = GetSize(); | |
594 | int value = GetValue(); | |
595 | const wxRect btnRect = wxRectFromRECT(wxGetWindowRect(GetHwnd())); | |
596 | ||
597 | // destroy the old spin button | |
598 | UnsubclassWin(); | |
599 | if ( !::DestroyWindow(GetHwnd()) ) | |
600 | { | |
601 | wxLogLastError(wxT("DestroyWindow")); | |
602 | } | |
603 | ||
604 | // create and initialize the new one | |
605 | if ( !wxSpinButton::Create(GetParent(), GetId(), | |
606 | btnRect.GetPosition(), btnRect.GetSize(), | |
607 | GetWindowStyle(), GetName()) ) | |
608 | return false; | |
609 | ||
610 | SetValue(value); | |
611 | SetRange(m_min, m_max); | |
612 | SetInitialSize(size); | |
613 | ||
614 | // associate it with the buddy control again | |
615 | ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent())); | |
616 | (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)GetBuddyHwnd(), 0); | |
617 | ||
618 | return true; | |
619 | } | |
620 | ||
621 | bool wxSpinCtrl::Enable(bool enable) | |
622 | { | |
623 | if ( !wxControl::Enable(enable) ) | |
624 | { | |
625 | return false; | |
626 | } | |
627 | ||
628 | ::EnableWindow(GetBuddyHwnd(), enable); | |
629 | ||
630 | return true; | |
631 | } | |
632 | ||
633 | void wxSpinCtrl::SetFocus() | |
634 | { | |
635 | ::SetFocus(GetBuddyHwnd()); | |
636 | } | |
637 | ||
638 | #if wxUSE_TOOLTIPS | |
639 | ||
640 | void wxSpinCtrl::DoSetToolTip(wxToolTip *tip) | |
641 | { | |
642 | wxSpinButton::DoSetToolTip(tip); | |
643 | ||
644 | if ( tip ) | |
645 | tip->Add(m_hwndBuddy); | |
646 | } | |
647 | ||
648 | #endif // wxUSE_TOOLTIPS | |
649 | ||
650 | // ---------------------------------------------------------------------------- | |
651 | // events processing and generation | |
652 | // ---------------------------------------------------------------------------- | |
653 | ||
654 | void wxSpinCtrl::SendSpinUpdate(int value) | |
655 | { | |
656 | wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId()); | |
657 | event.SetEventObject(this); | |
658 | event.SetInt(value); | |
659 | ||
660 | (void)HandleWindowEvent(event); | |
661 | ||
662 | m_oldValue = value; | |
663 | } | |
664 | ||
665 | bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam, | |
666 | WXWORD pos, WXHWND control) | |
667 | { | |
668 | wxCHECK_MSG( control, false, wxT("scrolling what?") ); | |
669 | ||
670 | if ( wParam != SB_THUMBPOSITION ) | |
671 | { | |
672 | // probable SB_ENDSCROLL - we don't react to it | |
673 | return false; | |
674 | } | |
675 | ||
676 | int new_value = (short) pos; | |
677 | if (m_oldValue != new_value) | |
678 | SendSpinUpdate( new_value ); | |
679 | ||
680 | return TRUE; | |
681 | } | |
682 | ||
683 | bool wxSpinCtrl::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result) | |
684 | { | |
685 | NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam; | |
686 | ||
687 | if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control | |
688 | return false; | |
689 | ||
690 | *result = 0; // never reject UP and DOWN events | |
691 | ||
692 | return TRUE; | |
693 | } | |
694 | ||
695 | ||
696 | // ---------------------------------------------------------------------------- | |
697 | // size calculations | |
698 | // ---------------------------------------------------------------------------- | |
699 | ||
700 | wxSize wxSpinCtrl::DoGetBestSize() const | |
701 | { | |
702 | wxSize sizeBtn = wxSpinButton::DoGetBestSize(); | |
703 | sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN; | |
704 | ||
705 | int y; | |
706 | wxGetCharSize(GetHWND(), NULL, &y, GetFont()); | |
707 | y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y); | |
708 | ||
709 | // JACS: we should always use the height calculated | |
710 | // from above, because otherwise we'll get a spin control | |
711 | // that's too big. So never use the height calculated | |
712 | // from wxSpinButton::DoGetBestSize(). | |
713 | ||
714 | // if ( sizeBtn.y < y ) | |
715 | { | |
716 | // make the text tall enough | |
717 | sizeBtn.y = y; | |
718 | } | |
719 | ||
720 | return sizeBtn; | |
721 | } | |
722 | ||
723 | void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height) | |
724 | { | |
725 | int widthBtn = wxSpinButton::DoGetBestSize().x; | |
726 | int widthText = width - widthBtn - MARGIN_BETWEEN; | |
727 | if ( widthText <= 0 ) | |
728 | { | |
729 | wxLogDebug(wxT("not enough space for wxSpinCtrl!")); | |
730 | } | |
731 | ||
732 | // 1) The buddy window | |
733 | DoMoveSibling(m_hwndBuddy, x, y, widthText, height); | |
734 | ||
735 | // 2) The button window | |
736 | x += widthText + MARGIN_BETWEEN; | |
737 | wxSpinButton::DoMoveWindow(x, y, widthBtn, height); | |
738 | } | |
739 | ||
740 | // get total size of the control | |
741 | void wxSpinCtrl::DoGetSize(int *x, int *y) const | |
742 | { | |
743 | RECT spinrect, textrect, ctrlrect; | |
744 | GetWindowRect(GetHwnd(), &spinrect); | |
745 | GetWindowRect(GetBuddyHwnd(), &textrect); | |
746 | UnionRect(&ctrlrect,&textrect, &spinrect); | |
747 | ||
748 | if ( x ) | |
749 | *x = ctrlrect.right - ctrlrect.left; | |
750 | if ( y ) | |
751 | *y = ctrlrect.bottom - ctrlrect.top; | |
752 | } | |
753 | ||
754 | void wxSpinCtrl::DoGetClientSize(int *x, int *y) const | |
755 | { | |
756 | RECT spinrect = wxGetClientRect(GetHwnd()); | |
757 | RECT textrect = wxGetClientRect(GetBuddyHwnd()); | |
758 | RECT ctrlrect; | |
759 | UnionRect(&ctrlrect,&textrect, &spinrect); | |
760 | ||
761 | if ( x ) | |
762 | *x = ctrlrect.right - ctrlrect.left; | |
763 | if ( y ) | |
764 | *y = ctrlrect.bottom - ctrlrect.top; | |
765 | } | |
766 | ||
767 | void wxSpinCtrl::DoGetPosition(int *x, int *y) const | |
768 | { | |
769 | // hack: pretend that our HWND is the text control just for a moment | |
770 | WXHWND hWnd = GetHWND(); | |
771 | wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy; | |
772 | ||
773 | wxSpinButton::DoGetPosition(x, y); | |
774 | ||
775 | wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd; | |
776 | } | |
777 | ||
778 | #endif // wxUSE_SPINCTRL |