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