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