]>
Commit | Line | Data |
---|---|---|
b782f2e0 | 1 | ///////////////////////////////////////////////////////////////////////////// |
623d5f80 | 2 | // Name: src/msw/spinctrl.cpp |
b782f2e0 VZ |
3 | // Purpose: wxSpinCtrl class implementation for Win32 |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 22.07.99 | |
7 | // RCS-ID: $Id$ | |
74124ea9 | 8 | // Copyright: (c) 1999-2005 Vadim Zeitlin |
65571936 | 9 | // Licence: wxWindows licence |
b782f2e0 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
b782f2e0 VZ |
16 | // ---------------------------------------------------------------------------- |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
0e528b99 JS |
27 | #if wxUSE_SPINCTRL |
28 | ||
b782f2e0 | 29 | #include "wx/spinctrl.h" |
623d5f80 WS |
30 | |
31 | #ifndef WX_PRECOMP | |
7ee21e3a | 32 | #include "wx/hashmap.h" |
57bd4c60 | 33 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
623d5f80 WS |
34 | #include "wx/event.h" |
35 | #include "wx/textctrl.h" | |
193d0c93 | 36 | #include "wx/wxcrtvararg.h" |
623d5f80 WS |
37 | #endif |
38 | ||
b782f2e0 VZ |
39 | #include "wx/msw/private.h" |
40 | ||
74124ea9 VZ |
41 | #if wxUSE_TOOLTIPS |
42 | #include "wx/tooltip.h" | |
43 | #endif // wxUSE_TOOLTIPS | |
b782f2e0 | 44 | |
678cd6de VZ |
45 | #include <limits.h> // for INT_MIN |
46 | ||
b782f2e0 VZ |
47 | // ---------------------------------------------------------------------------- |
48 | // macros | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
9750fc42 | 51 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton) |
e3582f7f | 52 | EVT_CHAR(wxSpinCtrl::OnChar) |
c5c04fab | 53 | EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus) |
4a528443 | 54 | EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus) |
9750fc42 | 55 | END_EVENT_TABLE() |
b782f2e0 | 56 | |
6fe19057 VZ |
57 | #define GetBuddyHwnd() (HWND)(m_hwndBuddy) |
58 | ||
b782f2e0 VZ |
59 | // ---------------------------------------------------------------------------- |
60 | // constants | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
baccb514 VZ |
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; | |
b782f2e0 | 67 | |
7ee21e3a VZ |
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 | ||
b782f2e0 VZ |
86 | // ============================================================================ |
87 | // implementation | |
88 | // ============================================================================ | |
89 | ||
f6bcfd97 BP |
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 | { | |
7ee21e3a | 99 | wxSpinCtrl * const spin = wxSpinCtrl::GetSpinForTextCtrl(hwnd); |
f6bcfd97 | 100 | |
5a0b1008 | 101 | // forward some messages (mostly the key and focus ones) to the spin ctrl |
f6bcfd97 BP |
102 | switch ( message ) |
103 | { | |
93c4157c | 104 | case WM_SETFOCUS: |
c5c04fab VZ |
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 | |
c140b7e7 | 107 | if ( (WXHWND)wParam == spin->GetHWND() ) |
c5c04fab VZ |
108 | break; |
109 | //else: fall through | |
110 | ||
93c4157c | 111 | case WM_KILLFOCUS: |
f6bcfd97 BP |
112 | case WM_CHAR: |
113 | case WM_DEADCHAR: | |
114 | case WM_KEYUP: | |
115 | case WM_KEYDOWN: | |
5a0b1008 VZ |
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 | |
9f6e407c VZ |
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 | } | |
f6bcfd97 | 137 | break; |
350ba193 VZ |
138 | |
139 | case WM_GETDLGCODE: | |
242ec2f7 VZ |
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; | |
f6bcfd97 | 154 | } |
350ba193 | 155 | |
f6bcfd97 BP |
156 | return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(), |
157 | hwnd, message, wParam, lParam); | |
158 | } | |
159 | ||
6fe19057 VZ |
160 | /* static */ |
161 | wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy) | |
162 | { | |
7ee21e3a VZ |
163 | const SpinForTextCtrl::const_iterator |
164 | it = gs_spinForTextCtrl.find(hwndBuddy); | |
165 | if ( it == gs_spinForTextCtrl.end() ) | |
6fe19057 VZ |
166 | return NULL; |
167 | ||
7ee21e3a VZ |
168 | wxSpinCtrl * const spin = it->second; |
169 | ||
6fe19057 VZ |
170 | // sanity check |
171 | wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy, | |
9a83f860 | 172 | wxT("wxSpinCtrl has incorrect buddy HWND!") ); |
6fe19057 VZ |
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 | { | |
ea6cbf48 | 180 | if ( (cmd == EN_CHANGE) && (!m_blockEvent )) |
6fe19057 | 181 | { |
0ca3c231 VZ |
182 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); |
183 | event.SetEventObject(this); | |
184 | wxString val = wxGetWindowText(m_hwndBuddy); | |
185 | event.SetString(val); | |
186 | event.SetInt(GetValue()); | |
937013e0 | 187 | return HandleWindowEvent(event); |
6fe19057 VZ |
188 | } |
189 | ||
190 | // not processed | |
57f4f925 | 191 | return false; |
6fe19057 VZ |
192 | } |
193 | ||
e3582f7f JS |
194 | void wxSpinCtrl::OnChar(wxKeyEvent& event) |
195 | { | |
77e00fe9 | 196 | switch ( event.GetKeyCode() ) |
e3582f7f JS |
197 | { |
198 | case WXK_RETURN: | |
199 | { | |
200 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
201 | InitCommandEvent(event); | |
202 | wxString val = wxGetWindowText(m_hwndBuddy); | |
203 | event.SetString(val); | |
204 | event.SetInt(GetValue()); | |
937013e0 | 205 | if ( HandleWindowEvent(event) ) |
e3582f7f JS |
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 | ||
937013e0 | 221 | if ( GetParent()->HandleWindowEvent(eventNav) ) |
e3582f7f JS |
222 | return; |
223 | } | |
224 | break; | |
225 | } | |
226 | ||
227 | // no, we didn't process it | |
228 | event.Skip(); | |
229 | } | |
230 | ||
4a528443 JS |
231 | void wxSpinCtrl::OnKillFocus(wxFocusEvent& event) |
232 | { | |
5ec60151 VZ |
233 | // ensure that a correct value is shown by the control |
234 | NormalizeValue(); | |
4a528443 JS |
235 | event.Skip(); |
236 | } | |
237 | ||
c5c04fab VZ |
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 | ||
1e8dba5e RR |
247 | void wxSpinCtrl::NormalizeValue() |
248 | { | |
e816f5c7 | 249 | const int value = GetValue(); |
d40e9e06 | 250 | const bool changed = value != m_oldValue; |
e816f5c7 | 251 | |
b6d83018 VZ |
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() | |
e816f5c7 VZ |
255 | SetValue(value); |
256 | ||
b6d83018 VZ |
257 | if ( changed ) |
258 | { | |
d40e9e06 | 259 | SendSpinUpdate(value); |
b6d83018 | 260 | } |
1e8dba5e RR |
261 | } |
262 | ||
b782f2e0 VZ |
263 | // ---------------------------------------------------------------------------- |
264 | // construction | |
265 | // ---------------------------------------------------------------------------- | |
266 | ||
267 | bool wxSpinCtrl::Create(wxWindow *parent, | |
268 | wxWindowID id, | |
678cd6de | 269 | const wxString& value, |
b782f2e0 VZ |
270 | const wxPoint& pos, |
271 | const wxSize& size, | |
272 | long style, | |
273 | int min, int max, int initial, | |
274 | const wxString& name) | |
275 | { | |
ea6cbf48 RR |
276 | m_blockEvent = false; |
277 | ||
d40e9e06 VZ |
278 | // this should be in ctor/init function but I don't want to add one to 2.8 |
279 | // to avoid problems with default ctor which can be inlined in the user | |
280 | // code and so might not get this fix without recompilation | |
281 | m_oldValue = INT_MIN; | |
282 | ||
b782f2e0 VZ |
283 | // before using DoGetBestSize(), have to set style to let the base class |
284 | // know whether this is a horizontal or vertical control (we're always | |
285 | // vertical) | |
882a8f40 | 286 | style |= wxSP_VERTICAL; |
c76b1a30 JS |
287 | |
288 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) | |
5d72f195 RR |
289 | #ifdef __WXWINCE__ |
290 | style |= wxBORDER_SIMPLE; | |
291 | #else | |
c76b1a30 | 292 | style |= wxBORDER_SUNKEN; |
5d72f195 | 293 | #endif |
c76b1a30 | 294 | |
882a8f40 | 295 | SetWindowStyle(style); |
b782f2e0 | 296 | |
fe3d9123 JS |
297 | WXDWORD exStyle = 0; |
298 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ; | |
299 | ||
7e4952db | 300 | // propagate text alignment style to text ctrl |
f1ddb476 | 301 | if ( style & wxALIGN_RIGHT ) |
7e4952db | 302 | msStyle |= ES_RIGHT; |
f1ddb476 | 303 | else if ( style & wxALIGN_CENTER ) |
7e4952db | 304 | msStyle |= ES_CENTER; |
f1ddb476 | 305 | |
8e190f0e | 306 | // calculate the sizes: the size given is the total size for both controls |
b782f2e0 VZ |
307 | // and we need to fit them both in the given width (height is the same) |
308 | wxSize sizeText(size), sizeBtn(size); | |
309 | sizeBtn.x = wxSpinButton::DoGetBestSize().x; | |
baccb514 VZ |
310 | if ( sizeText.x <= 0 ) |
311 | { | |
312 | // DEFAULT_ITEM_WIDTH is the default width for the text control | |
313 | sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x; | |
314 | } | |
315 | ||
b782f2e0 VZ |
316 | sizeText.x -= sizeBtn.x + MARGIN_BETWEEN; |
317 | if ( sizeText.x <= 0 ) | |
318 | { | |
9a83f860 | 319 | wxLogDebug(wxT("not enough space for wxSpinCtrl!")); |
b782f2e0 VZ |
320 | } |
321 | ||
322 | wxPoint posBtn(pos); | |
323 | posBtn.x += sizeText.x + MARGIN_BETWEEN; | |
324 | ||
c5c04fab VZ |
325 | // we must create the text control before the spin button for the purpose |
326 | // of the dialog navigation: if there is a static text just before the spin | |
327 | // control, activating it by Alt-letter should give focus to the text | |
328 | // control, not the spin and the dialog navigation code will give focus to | |
329 | // the next control (at Windows level), not the one after it | |
b782f2e0 | 330 | |
c5c04fab | 331 | // create the text window |
b782f2e0 | 332 | |
b782f2e0 VZ |
333 | m_hwndBuddy = (WXHWND)::CreateWindowEx |
334 | ( | |
c5c04fab | 335 | exStyle, // sunken border |
9a83f860 | 336 | wxT("EDIT"), // window class |
baccb514 | 337 | NULL, // no window title |
c5c04fab | 338 | msStyle, // style (will be shown later) |
baccb514 VZ |
339 | pos.x, pos.y, // position |
340 | 0, 0, // size (will be set later) | |
341 | GetHwndOf(parent), // parent | |
342 | (HMENU)-1, // control id | |
343 | wxGetInstance(), // app instance | |
344 | NULL // unused client data | |
b782f2e0 VZ |
345 | ); |
346 | ||
347 | if ( !m_hwndBuddy ) | |
348 | { | |
f6bcfd97 | 349 | wxLogLastError(wxT("CreateWindow(buddy text window)")); |
b782f2e0 | 350 | |
57f4f925 | 351 | return false; |
b782f2e0 VZ |
352 | } |
353 | ||
c5c04fab VZ |
354 | |
355 | // create the spin button | |
356 | if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) ) | |
357 | { | |
57f4f925 | 358 | return false; |
c5c04fab VZ |
359 | } |
360 | ||
23ec96e3 | 361 | wxSpinButtonBase::SetRange(min, max); |
e816f5c7 | 362 | |
f6bcfd97 | 363 | // subclass the text ctrl to be able to intercept some events |
7ee21e3a VZ |
364 | gs_spinForTextCtrl[GetBuddyHwnd()] = this; |
365 | ||
975b6bcf VZ |
366 | m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(), |
367 | wxBuddyTextWndProc); | |
f6bcfd97 | 368 | |
8d2e831b RD |
369 | // set up fonts and colours (This is nomally done in MSWCreateControl) |
370 | InheritAttributes(); | |
e0176dd9 VZ |
371 | if (!m_hasFont) |
372 | SetFont(GetDefaultAttributes().font); | |
8d2e831b | 373 | |
baccb514 VZ |
374 | // set the size of the text window - can do it only now, because we |
375 | // couldn't call DoGetBestSize() before as font wasn't set | |
376 | if ( sizeText.y <= 0 ) | |
377 | { | |
882a8f40 | 378 | int cx, cy; |
7a5e53ab | 379 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); |
882a8f40 VZ |
380 | |
381 | sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
baccb514 VZ |
382 | } |
383 | ||
170acdc9 | 384 | SetInitialSize(size); |
baccb514 | 385 | |
6fe19057 | 386 | (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW); |
b782f2e0 VZ |
387 | |
388 | // associate the text window with the spin button | |
baccb514 VZ |
389 | (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0); |
390 | ||
549cc3a8 JS |
391 | SetValue(initial); |
392 | ||
393 | // Set the range in the native control | |
394 | SetRange(min, max); | |
395 | ||
3d104ec3 | 396 | if ( !value.empty() ) |
678cd6de VZ |
397 | { |
398 | SetValue(value); | |
5bcdac45 JS |
399 | m_oldValue = (int) wxAtol(value); |
400 | } | |
401 | else | |
402 | { | |
403 | SetValue(wxString::Format(wxT("%d"), initial)); | |
404 | m_oldValue = initial; | |
678cd6de VZ |
405 | } |
406 | ||
57f4f925 | 407 | return true; |
baccb514 VZ |
408 | } |
409 | ||
f6bcfd97 BP |
410 | wxSpinCtrl::~wxSpinCtrl() |
411 | { | |
412 | // destroy the buddy window because this pointer which wxBuddyTextWndProc | |
413 | // uses will not soon be valid any more | |
7ee21e3a VZ |
414 | ::DestroyWindow( GetBuddyHwnd() ); |
415 | ||
416 | gs_spinForTextCtrl.erase(GetBuddyHwnd()); | |
f6bcfd97 BP |
417 | } |
418 | ||
678cd6de VZ |
419 | // ---------------------------------------------------------------------------- |
420 | // wxTextCtrl-like methods | |
421 | // ---------------------------------------------------------------------------- | |
422 | ||
423 | void wxSpinCtrl::SetValue(const wxString& text) | |
424 | { | |
6fe19057 | 425 | if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) ) |
678cd6de | 426 | { |
f6bcfd97 | 427 | wxLogLastError(wxT("SetWindowText(buddy)")); |
678cd6de VZ |
428 | } |
429 | } | |
430 | ||
eeea41ab VZ |
431 | void wxSpinCtrl::SetValue(int val) |
432 | { | |
ea6cbf48 | 433 | m_blockEvent = true; |
f1ddb476 | 434 | |
eeea41ab VZ |
435 | wxSpinButton::SetValue(val); |
436 | ||
437 | // normally setting the value of the spin button is enough as it updates | |
438 | // its buddy control automatically ... | |
439 | if ( wxGetWindowText(m_hwndBuddy).empty() ) | |
440 | { | |
441 | // ... but sometimes it doesn't, notably when the value is 0 and the | |
442 | // text control is currently empty, the spin button seems to be happy | |
443 | // to leave it like this, while we really want to always show the | |
444 | // current value in the control, so do it manually | |
e0a050e3 | 445 | ::SetWindowText(GetBuddyHwnd(), |
9a83f860 | 446 | wxString::Format(wxT("%d"), val).wx_str()); |
eeea41ab | 447 | } |
e816f5c7 | 448 | |
25dff19c | 449 | m_oldValue = GetValue(); |
f1ddb476 | 450 | |
ea6cbf48 | 451 | m_blockEvent = false; |
eeea41ab VZ |
452 | } |
453 | ||
678cd6de VZ |
454 | int wxSpinCtrl::GetValue() const |
455 | { | |
456 | wxString val = wxGetWindowText(m_hwndBuddy); | |
457 | ||
458 | long n; | |
eeea41ab | 459 | if ( (wxSscanf(val, wxT("%ld"), &n) != 1) ) |
678cd6de | 460 | n = INT_MIN; |
3d104ec3 | 461 | |
eeea41ab VZ |
462 | if ( n < m_min ) |
463 | n = m_min; | |
464 | if ( n > m_max ) | |
465 | n = m_max; | |
678cd6de VZ |
466 | |
467 | return n; | |
468 | } | |
469 | ||
07901ec9 VZ |
470 | void wxSpinCtrl::SetSelection(long from, long to) |
471 | { | |
77ffb593 | 472 | // if from and to are both -1, it means (in wxWidgets) that all text should |
07901ec9 VZ |
473 | // be selected - translate into Windows convention |
474 | if ( (from == -1) && (to == -1) ) | |
475 | { | |
476 | from = 0; | |
477 | } | |
478 | ||
7d86a2d4 | 479 | ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to); |
07901ec9 VZ |
480 | } |
481 | ||
345ff9c6 VZ |
482 | // ---------------------------------------------------------------------------- |
483 | // wxSpinButton methods | |
484 | // ---------------------------------------------------------------------------- | |
485 | ||
486 | void wxSpinCtrl::SetRange(int minVal, int maxVal) | |
487 | { | |
488 | wxSpinButton::SetRange(minVal, maxVal); | |
489 | ||
490 | // this control is used for numeric entry so restrict the input to numeric | |
491 | // keys only -- but only if we don't need to be able to enter "-" in it as | |
492 | // otherwise this would become impossible | |
493 | const DWORD styleOld = ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE); | |
494 | DWORD styleNew; | |
495 | if ( minVal < 0 ) | |
496 | styleNew = styleOld & ~ES_NUMBER; | |
497 | else | |
498 | styleNew = styleOld | ES_NUMBER; | |
499 | ||
500 | if ( styleNew != styleOld ) | |
501 | ::SetWindowLong(GetBuddyHwnd(), GWL_STYLE, styleNew); | |
502 | } | |
503 | ||
baccb514 | 504 | // ---------------------------------------------------------------------------- |
882a8f40 | 505 | // forward some methods to subcontrols |
baccb514 VZ |
506 | // ---------------------------------------------------------------------------- |
507 | ||
508 | bool wxSpinCtrl::SetFont(const wxFont& font) | |
509 | { | |
510 | if ( !wxWindowBase::SetFont(font) ) | |
511 | { | |
512 | // nothing to do | |
57f4f925 | 513 | return false; |
baccb514 VZ |
514 | } |
515 | ||
516 | WXHANDLE hFont = GetFont().GetResourceHandle(); | |
6fe19057 | 517 | (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE); |
b782f2e0 | 518 | |
57f4f925 | 519 | return true; |
b782f2e0 VZ |
520 | } |
521 | ||
882a8f40 VZ |
522 | bool wxSpinCtrl::Show(bool show) |
523 | { | |
524 | if ( !wxControl::Show(show) ) | |
525 | { | |
57f4f925 | 526 | return false; |
882a8f40 VZ |
527 | } |
528 | ||
6fe19057 | 529 | ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE); |
882a8f40 | 530 | |
57f4f925 | 531 | return true; |
882a8f40 VZ |
532 | } |
533 | ||
03d4194d VZ |
534 | bool wxSpinCtrl::Reparent(wxWindowBase *newParent) |
535 | { | |
536 | // Reparenting both the updown control and its buddy does not seem to work: | |
537 | // they continue to be connected somehow, but visually there is no feedback | |
538 | // on the buddy edit control. To avoid this problem, we reparent the buddy | |
539 | // window normally, but we recreate the updown control and reassign its | |
540 | // buddy. | |
541 | ||
03263ff7 VZ |
542 | // Get the position before changing the parent as it would be offset after |
543 | // changing it. | |
544 | const wxRect rect = GetRect(); | |
545 | ||
03d4194d VZ |
546 | if ( !wxWindowBase::Reparent(newParent) ) |
547 | return false; | |
548 | ||
549 | newParent->GetChildren().DeleteObject(this); | |
550 | ||
bc73fe96 VZ |
551 | // destroy the old spin button after detaching it from this wxWindow object |
552 | // (notice that m_hWnd will be reset by UnsubclassWin() so save it first) | |
553 | const HWND hwndOld = GetHwnd(); | |
03d4194d | 554 | UnsubclassWin(); |
bc73fe96 | 555 | if ( !::DestroyWindow(hwndOld) ) |
43b2d5e7 | 556 | { |
03d4194d | 557 | wxLogLastError(wxT("DestroyWindow")); |
43b2d5e7 | 558 | } |
03d4194d VZ |
559 | |
560 | // create and initialize the new one | |
561 | if ( !wxSpinButton::Create(GetParent(), GetId(), | |
03263ff7 | 562 | rect.GetPosition(), rect.GetSize(), |
03d4194d VZ |
563 | GetWindowStyle(), GetName()) ) |
564 | return false; | |
565 | ||
03263ff7 VZ |
566 | // reapply our values to wxSpinButton |
567 | wxSpinButton::SetValue(GetValue()); | |
03d4194d | 568 | SetRange(m_min, m_max); |
03263ff7 VZ |
569 | |
570 | // also set the size again with wxSIZE_ALLOW_MINUS_ONE flag: this is | |
571 | // necessary if our original position used -1 for either x or y | |
572 | SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); | |
03d4194d VZ |
573 | |
574 | // associate it with the buddy control again | |
575 | ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent())); | |
576 | (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)GetBuddyHwnd(), 0); | |
577 | ||
578 | return true; | |
579 | } | |
580 | ||
882a8f40 VZ |
581 | bool wxSpinCtrl::Enable(bool enable) |
582 | { | |
583 | if ( !wxControl::Enable(enable) ) | |
584 | { | |
57f4f925 | 585 | return false; |
882a8f40 VZ |
586 | } |
587 | ||
0826c4d3 | 588 | MSWEnableHWND(GetBuddyHwnd(), enable); |
882a8f40 | 589 | |
57f4f925 | 590 | return true; |
882a8f40 VZ |
591 | } |
592 | ||
8bf3196d GRG |
593 | void wxSpinCtrl::SetFocus() |
594 | { | |
6fe19057 | 595 | ::SetFocus(GetBuddyHwnd()); |
8bf3196d GRG |
596 | } |
597 | ||
74124ea9 VZ |
598 | #if wxUSE_TOOLTIPS |
599 | ||
600 | void wxSpinCtrl::DoSetToolTip(wxToolTip *tip) | |
601 | { | |
602 | wxSpinButton::DoSetToolTip(tip); | |
603 | ||
604 | if ( tip ) | |
605 | tip->Add(m_hwndBuddy); | |
606 | } | |
607 | ||
608 | #endif // wxUSE_TOOLTIPS | |
609 | ||
9750fc42 | 610 | // ---------------------------------------------------------------------------- |
d40e9e06 | 611 | // events processing and generation |
9750fc42 VZ |
612 | // ---------------------------------------------------------------------------- |
613 | ||
d40e9e06 | 614 | void wxSpinCtrl::SendSpinUpdate(int value) |
9750fc42 VZ |
615 | { |
616 | wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId()); | |
617 | event.SetEventObject(this); | |
d40e9e06 | 618 | event.SetInt(value); |
e816f5c7 | 619 | |
937013e0 | 620 | (void)HandleWindowEvent(event); |
d40e9e06 VZ |
621 | |
622 | m_oldValue = value; | |
623 | } | |
9750fc42 | 624 | |
177e38e6 RR |
625 | bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam, |
626 | WXWORD pos, WXHWND control) | |
d40e9e06 | 627 | { |
177e38e6 RR |
628 | wxCHECK_MSG( control, false, wxT("scrolling what?") ); |
629 | ||
630 | if ( wParam != SB_THUMBPOSITION ) | |
9750fc42 | 631 | { |
177e38e6 RR |
632 | // probable SB_ENDSCROLL - we don't react to it |
633 | return false; | |
9750fc42 | 634 | } |
177e38e6 RR |
635 | |
636 | int new_value = (short) pos; | |
637 | if (m_oldValue != new_value) | |
638 | SendSpinUpdate( new_value ); | |
639 | ||
640 | return TRUE; | |
641 | } | |
642 | ||
643 | bool wxSpinCtrl::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result) | |
644 | { | |
645 | NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam; | |
646 | ||
647 | if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control | |
648 | return false; | |
649 | ||
650 | *result = 0; // never reject UP and DOWN events | |
651 | ||
652 | return TRUE; | |
9750fc42 VZ |
653 | } |
654 | ||
177e38e6 | 655 | |
b782f2e0 VZ |
656 | // ---------------------------------------------------------------------------- |
657 | // size calculations | |
658 | // ---------------------------------------------------------------------------- | |
659 | ||
f68586e5 | 660 | wxSize wxSpinCtrl::DoGetBestSize() const |
baccb514 VZ |
661 | { |
662 | wxSize sizeBtn = wxSpinButton::DoGetBestSize(); | |
663 | sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN; | |
664 | ||
665 | int y; | |
7a5e53ab | 666 | wxGetCharSize(GetHWND(), NULL, &y, GetFont()); |
baccb514 VZ |
667 | y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y); |
668 | ||
09f27917 JS |
669 | // JACS: we should always use the height calculated |
670 | // from above, because otherwise we'll get a spin control | |
671 | // that's too big. So never use the height calculated | |
672 | // from wxSpinButton::DoGetBestSize(). | |
57f4f925 | 673 | |
09f27917 | 674 | // if ( sizeBtn.y < y ) |
baccb514 VZ |
675 | { |
676 | // make the text tall enough | |
677 | sizeBtn.y = y; | |
678 | } | |
679 | ||
680 | return sizeBtn; | |
681 | } | |
682 | ||
b782f2e0 VZ |
683 | void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height) |
684 | { | |
baccb514 | 685 | int widthBtn = wxSpinButton::DoGetBestSize().x; |
b782f2e0 VZ |
686 | int widthText = width - widthBtn - MARGIN_BETWEEN; |
687 | if ( widthText <= 0 ) | |
688 | { | |
9a83f860 | 689 | wxLogDebug(wxT("not enough space for wxSpinCtrl!")); |
b782f2e0 VZ |
690 | } |
691 | ||
8e44f3ca | 692 | // 1) The buddy window |
7d86a2d4 | 693 | DoMoveSibling(m_hwndBuddy, x, y, widthText, height); |
b782f2e0 | 694 | |
8e44f3ca | 695 | // 2) The button window |
b782f2e0 | 696 | x += widthText + MARGIN_BETWEEN; |
7d86a2d4 | 697 | wxSpinButton::DoMoveWindow(x, y, widthBtn, height); |
b782f2e0 VZ |
698 | } |
699 | ||
f6bcfd97 BP |
700 | // get total size of the control |
701 | void wxSpinCtrl::DoGetSize(int *x, int *y) const | |
702 | { | |
703 | RECT spinrect, textrect, ctrlrect; | |
704 | GetWindowRect(GetHwnd(), &spinrect); | |
6fe19057 | 705 | GetWindowRect(GetBuddyHwnd(), &textrect); |
f6bcfd97 BP |
706 | UnionRect(&ctrlrect,&textrect, &spinrect); |
707 | ||
708 | if ( x ) | |
709 | *x = ctrlrect.right - ctrlrect.left; | |
710 | if ( y ) | |
711 | *y = ctrlrect.bottom - ctrlrect.top; | |
712 | } | |
713 | ||
b7527dde VS |
714 | void wxSpinCtrl::DoGetClientSize(int *x, int *y) const |
715 | { | |
716 | RECT spinrect = wxGetClientRect(GetHwnd()); | |
717 | RECT textrect = wxGetClientRect(GetBuddyHwnd()); | |
718 | RECT ctrlrect; | |
719 | UnionRect(&ctrlrect,&textrect, &spinrect); | |
720 | ||
721 | if ( x ) | |
722 | *x = ctrlrect.right - ctrlrect.left; | |
723 | if ( y ) | |
724 | *y = ctrlrect.bottom - ctrlrect.top; | |
725 | } | |
726 | ||
f6bcfd97 BP |
727 | void wxSpinCtrl::DoGetPosition(int *x, int *y) const |
728 | { | |
729 | // hack: pretend that our HWND is the text control just for a moment | |
730 | WXHWND hWnd = GetHWND(); | |
731 | wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy; | |
732 | ||
733 | wxSpinButton::DoGetPosition(x, y); | |
734 | ||
735 | wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd; | |
736 | } | |
737 | ||
74124ea9 | 738 | #endif // wxUSE_SPINCTRL |