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