]>
Commit | Line | Data |
---|---|---|
b782f2e0 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: 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) Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | #ifdef __GNUG__ | |
17 | #pragma implementation "spinctrlbase.h" | |
18 | #pragma implementation "spinctrl.h" | |
19 | #endif | |
20 | ||
21 | // ---------------------------------------------------------------------------- | |
22 | // headers | |
23 | // ---------------------------------------------------------------------------- | |
24 | ||
25 | // for compilers that support precompilation, includes "wx.h". | |
26 | #include "wx/wxprec.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
32 | #ifndef WX_PRECOMP | |
33 | #include "wx/wx.h" | |
34 | #endif | |
35 | ||
0e528b99 JS |
36 | #if wxUSE_SPINCTRL |
37 | ||
b782f2e0 VZ |
38 | #if defined(__WIN95__) |
39 | ||
40 | #include "wx/spinctrl.h" | |
41 | #include "wx/msw/private.h" | |
42 | ||
b39dbf34 | 43 | #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)) |
4c0a2c5c JS |
44 | #include <commctrl.h> |
45 | #endif | |
b782f2e0 | 46 | |
678cd6de VZ |
47 | #include <limits.h> // for INT_MIN |
48 | ||
b782f2e0 VZ |
49 | // ---------------------------------------------------------------------------- |
50 | // macros | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
9750fc42 VZ |
53 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl) |
54 | ||
55 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton) | |
e3582f7f | 56 | EVT_CHAR(wxSpinCtrl::OnChar) |
c5c04fab VZ |
57 | |
58 | EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus) | |
59 | ||
9750fc42 VZ |
60 | EVT_SPIN(-1, wxSpinCtrl::OnSpinChange) |
61 | END_EVENT_TABLE() | |
b782f2e0 | 62 | |
6fe19057 VZ |
63 | #define GetBuddyHwnd() (HWND)(m_hwndBuddy) |
64 | ||
b782f2e0 VZ |
65 | // ---------------------------------------------------------------------------- |
66 | // constants | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
baccb514 VZ |
69 | // the margin between the up-down control and its buddy (can be arbitrary, |
70 | // choose what you like - or may be decide during run-time depending on the | |
71 | // font size?) | |
72 | static const int MARGIN_BETWEEN = 1; | |
b782f2e0 VZ |
73 | |
74 | // ============================================================================ | |
75 | // implementation | |
76 | // ============================================================================ | |
77 | ||
6fe19057 VZ |
78 | wxArraySpins wxSpinCtrl::ms_allSpins; |
79 | ||
f6bcfd97 BP |
80 | // ---------------------------------------------------------------------------- |
81 | // wnd proc for the buddy text ctrl | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
84 | LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd, | |
85 | UINT message, | |
86 | WPARAM wParam, | |
87 | LPARAM lParam) | |
88 | { | |
89 | wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA); | |
90 | ||
93c4157c VZ |
91 | // forward some messages (the key and focus ones only so far) to |
92 | // the spin ctrl | |
f6bcfd97 BP |
93 | switch ( message ) |
94 | { | |
93c4157c | 95 | case WM_SETFOCUS: |
c5c04fab VZ |
96 | // if the focus comes from the spin control itself, don't set it |
97 | // back to it -- we don't want to go into an infinite loop | |
98 | if ( wParam == spin->GetHWND() ) | |
99 | break; | |
100 | //else: fall through | |
101 | ||
93c4157c | 102 | case WM_KILLFOCUS: |
f6bcfd97 BP |
103 | case WM_CHAR: |
104 | case WM_DEADCHAR: | |
105 | case WM_KEYUP: | |
106 | case WM_KEYDOWN: | |
107 | spin->MSWWindowProc(message, wParam, lParam); | |
e3582f7f JS |
108 | |
109 | // The control may have been deleted at this point, so check. | |
110 | if (!(::IsWindow(hwnd) && ((wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA)) == spin)) | |
111 | return 0; | |
f6bcfd97 | 112 | break; |
350ba193 VZ |
113 | |
114 | case WM_GETDLGCODE: | |
115 | // we want to get WXK_RETURN in order to generate the event for it | |
116 | return DLGC_WANTCHARS; | |
f6bcfd97 | 117 | } |
350ba193 | 118 | |
f6bcfd97 BP |
119 | return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(), |
120 | hwnd, message, wParam, lParam); | |
121 | } | |
122 | ||
6fe19057 VZ |
123 | /* static */ |
124 | wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy) | |
125 | { | |
126 | wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong((HWND)hwndBuddy, | |
127 | GWL_USERDATA); | |
128 | ||
129 | int i = ms_allSpins.Index(spin); | |
130 | ||
131 | if ( i == wxNOT_FOUND ) | |
132 | return NULL; | |
133 | ||
134 | // sanity check | |
135 | wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy, | |
136 | _T("wxSpinCtrl has incorrect buddy HWND!") ); | |
137 | ||
138 | return spin; | |
139 | } | |
140 | ||
141 | // process a WM_COMMAND generated by the buddy text control | |
142 | bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id)) | |
143 | { | |
e3582f7f | 144 | switch (cmd) |
6fe19057 | 145 | { |
e3582f7f JS |
146 | case EN_CHANGE: |
147 | { | |
148 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
149 | event.SetEventObject(this); | |
150 | wxString val = wxGetWindowText(m_hwndBuddy); | |
151 | event.SetString(val); | |
152 | event.SetInt(GetValue()); | |
153 | return GetEventHandler()->ProcessEvent(event); | |
154 | } | |
155 | case EN_SETFOCUS: | |
156 | case EN_KILLFOCUS: | |
157 | { | |
158 | wxFocusEvent event(cmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS | |
159 | : wxEVT_SET_FOCUS, | |
160 | m_windowId); | |
161 | event.SetEventObject( this ); | |
162 | return GetEventHandler()->ProcessEvent(event); | |
163 | } | |
164 | default: | |
165 | break; | |
6fe19057 VZ |
166 | } |
167 | ||
168 | // not processed | |
169 | return FALSE; | |
170 | } | |
171 | ||
e3582f7f JS |
172 | void wxSpinCtrl::OnChar(wxKeyEvent& event) |
173 | { | |
77e00fe9 | 174 | switch ( event.GetKeyCode() ) |
e3582f7f JS |
175 | { |
176 | case WXK_RETURN: | |
177 | { | |
178 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
179 | InitCommandEvent(event); | |
180 | wxString val = wxGetWindowText(m_hwndBuddy); | |
181 | event.SetString(val); | |
182 | event.SetInt(GetValue()); | |
183 | if ( GetEventHandler()->ProcessEvent(event) ) | |
184 | return; | |
185 | break; | |
186 | } | |
187 | ||
188 | case WXK_TAB: | |
189 | // always produce navigation event - even if we process TAB | |
190 | // ourselves the fact that we got here means that the user code | |
191 | // decided to skip processing of this TAB - probably to let it | |
192 | // do its default job. | |
193 | { | |
194 | wxNavigationKeyEvent eventNav; | |
195 | eventNav.SetDirection(!event.ShiftDown()); | |
196 | eventNav.SetWindowChange(event.ControlDown()); | |
197 | eventNav.SetEventObject(this); | |
198 | ||
199 | if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) ) | |
200 | return; | |
201 | } | |
202 | break; | |
203 | } | |
204 | ||
205 | // no, we didn't process it | |
206 | event.Skip(); | |
207 | } | |
208 | ||
c5c04fab VZ |
209 | void wxSpinCtrl::OnSetFocus(wxFocusEvent& event) |
210 | { | |
211 | // when we get focus, give it to our buddy window as it needs it more than | |
212 | // we do | |
213 | ::SetFocus((HWND)m_hwndBuddy); | |
214 | ||
215 | event.Skip(); | |
216 | } | |
217 | ||
b782f2e0 VZ |
218 | // ---------------------------------------------------------------------------- |
219 | // construction | |
220 | // ---------------------------------------------------------------------------- | |
221 | ||
222 | bool wxSpinCtrl::Create(wxWindow *parent, | |
223 | wxWindowID id, | |
678cd6de | 224 | const wxString& value, |
b782f2e0 VZ |
225 | const wxPoint& pos, |
226 | const wxSize& size, | |
227 | long style, | |
228 | int min, int max, int initial, | |
229 | const wxString& name) | |
230 | { | |
231 | // before using DoGetBestSize(), have to set style to let the base class | |
232 | // know whether this is a horizontal or vertical control (we're always | |
233 | // vertical) | |
882a8f40 | 234 | style |= wxSP_VERTICAL; |
c76b1a30 JS |
235 | |
236 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) | |
237 | style |= wxBORDER_SUNKEN; | |
238 | ||
882a8f40 | 239 | SetWindowStyle(style); |
b782f2e0 | 240 | |
fe3d9123 JS |
241 | WXDWORD exStyle = 0; |
242 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ; | |
243 | ||
b782f2e0 VZ |
244 | // calculate the sizes: the size given is the toal size for both controls |
245 | // and we need to fit them both in the given width (height is the same) | |
246 | wxSize sizeText(size), sizeBtn(size); | |
247 | sizeBtn.x = wxSpinButton::DoGetBestSize().x; | |
baccb514 VZ |
248 | if ( sizeText.x <= 0 ) |
249 | { | |
250 | // DEFAULT_ITEM_WIDTH is the default width for the text control | |
251 | sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x; | |
252 | } | |
253 | ||
b782f2e0 VZ |
254 | sizeText.x -= sizeBtn.x + MARGIN_BETWEEN; |
255 | if ( sizeText.x <= 0 ) | |
256 | { | |
257 | wxLogDebug(_T("not enough space for wxSpinCtrl!")); | |
258 | } | |
259 | ||
260 | wxPoint posBtn(pos); | |
261 | posBtn.x += sizeText.x + MARGIN_BETWEEN; | |
262 | ||
c5c04fab VZ |
263 | // we must create the text control before the spin button for the purpose |
264 | // of the dialog navigation: if there is a static text just before the spin | |
265 | // control, activating it by Alt-letter should give focus to the text | |
266 | // control, not the spin and the dialog navigation code will give focus to | |
267 | // the next control (at Windows level), not the one after it | |
b782f2e0 | 268 | |
c5c04fab | 269 | // create the text window |
b782f2e0 | 270 | |
b782f2e0 VZ |
271 | m_hwndBuddy = (WXHWND)::CreateWindowEx |
272 | ( | |
c5c04fab | 273 | exStyle, // sunken border |
baccb514 VZ |
274 | _T("EDIT"), // window class |
275 | NULL, // no window title | |
c5c04fab | 276 | msStyle, // style (will be shown later) |
baccb514 VZ |
277 | pos.x, pos.y, // position |
278 | 0, 0, // size (will be set later) | |
279 | GetHwndOf(parent), // parent | |
280 | (HMENU)-1, // control id | |
281 | wxGetInstance(), // app instance | |
282 | NULL // unused client data | |
b782f2e0 VZ |
283 | ); |
284 | ||
285 | if ( !m_hwndBuddy ) | |
286 | { | |
f6bcfd97 | 287 | wxLogLastError(wxT("CreateWindow(buddy text window)")); |
b782f2e0 VZ |
288 | |
289 | return FALSE; | |
290 | } | |
291 | ||
c5c04fab VZ |
292 | |
293 | // create the spin button | |
294 | if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) ) | |
295 | { | |
296 | return FALSE; | |
297 | } | |
298 | ||
299 | SetRange(min, max); | |
300 | SetValue(initial); | |
301 | ||
f6bcfd97 | 302 | // subclass the text ctrl to be able to intercept some events |
6fe19057 VZ |
303 | m_wndProcBuddy = (WXFARPROC)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC); |
304 | ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA, (LONG)this); | |
305 | ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC, (LONG)wxBuddyTextWndProc); | |
f6bcfd97 | 306 | |
b782f2e0 | 307 | // should have the same font as the other controls |
baccb514 VZ |
308 | SetFont(GetParent()->GetFont()); |
309 | ||
310 | // set the size of the text window - can do it only now, because we | |
311 | // couldn't call DoGetBestSize() before as font wasn't set | |
312 | if ( sizeText.y <= 0 ) | |
313 | { | |
882a8f40 VZ |
314 | int cx, cy; |
315 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
316 | ||
317 | sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
baccb514 VZ |
318 | } |
319 | ||
320 | DoMoveWindow(pos.x, pos.y, | |
321 | sizeText.x + sizeBtn.x + MARGIN_BETWEEN, sizeText.y); | |
322 | ||
6fe19057 | 323 | (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW); |
b782f2e0 VZ |
324 | |
325 | // associate the text window with the spin button | |
baccb514 VZ |
326 | (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0); |
327 | ||
678cd6de VZ |
328 | if ( !value.IsEmpty() ) |
329 | { | |
330 | SetValue(value); | |
331 | } | |
332 | ||
6fe19057 VZ |
333 | // do it after finishing with m_hwndBuddy creation to avoid generating |
334 | // initial wxEVT_COMMAND_TEXT_UPDATED message | |
335 | ms_allSpins.Add(this); | |
336 | ||
baccb514 VZ |
337 | return TRUE; |
338 | } | |
339 | ||
f6bcfd97 BP |
340 | wxSpinCtrl::~wxSpinCtrl() |
341 | { | |
6fe19057 VZ |
342 | ms_allSpins.Remove(this); |
343 | ||
5648c0ad JS |
344 | // This removes spurious memory leak reporting |
345 | if (ms_allSpins.GetCount() == 0) | |
346 | ms_allSpins.Clear(); | |
347 | ||
f6bcfd97 BP |
348 | // destroy the buddy window because this pointer which wxBuddyTextWndProc |
349 | // uses will not soon be valid any more | |
6fe19057 | 350 | ::DestroyWindow(GetBuddyHwnd()); |
f6bcfd97 BP |
351 | } |
352 | ||
678cd6de VZ |
353 | // ---------------------------------------------------------------------------- |
354 | // wxTextCtrl-like methods | |
355 | // ---------------------------------------------------------------------------- | |
356 | ||
357 | void wxSpinCtrl::SetValue(const wxString& text) | |
358 | { | |
6fe19057 | 359 | if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) ) |
678cd6de | 360 | { |
f6bcfd97 | 361 | wxLogLastError(wxT("SetWindowText(buddy)")); |
678cd6de VZ |
362 | } |
363 | } | |
364 | ||
365 | int wxSpinCtrl::GetValue() const | |
366 | { | |
367 | wxString val = wxGetWindowText(m_hwndBuddy); | |
368 | ||
369 | long n; | |
370 | if ( (wxSscanf(val, wxT("%lu"), &n) != 1) ) | |
371 | n = INT_MIN; | |
372 | ||
373 | return n; | |
374 | } | |
375 | ||
07901ec9 VZ |
376 | void wxSpinCtrl::SetSelection(long from, long to) |
377 | { | |
378 | // if from and to are both -1, it means (in wxWindows) that all text should | |
379 | // be selected - translate into Windows convention | |
380 | if ( (from == -1) && (to == -1) ) | |
381 | { | |
382 | from = 0; | |
383 | } | |
384 | ||
385 | ::SendMessage((HWND)m_hwndBuddy, EM_SETSEL, (WPARAM)from, (LPARAM)to); | |
386 | } | |
387 | ||
baccb514 | 388 | // ---------------------------------------------------------------------------- |
882a8f40 | 389 | // forward some methods to subcontrols |
baccb514 VZ |
390 | // ---------------------------------------------------------------------------- |
391 | ||
392 | bool wxSpinCtrl::SetFont(const wxFont& font) | |
393 | { | |
394 | if ( !wxWindowBase::SetFont(font) ) | |
395 | { | |
396 | // nothing to do | |
397 | return FALSE; | |
398 | } | |
399 | ||
400 | WXHANDLE hFont = GetFont().GetResourceHandle(); | |
6fe19057 | 401 | (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE); |
b782f2e0 VZ |
402 | |
403 | return TRUE; | |
404 | } | |
405 | ||
882a8f40 VZ |
406 | bool wxSpinCtrl::Show(bool show) |
407 | { | |
408 | if ( !wxControl::Show(show) ) | |
409 | { | |
410 | return FALSE; | |
411 | } | |
412 | ||
6fe19057 | 413 | ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE); |
882a8f40 VZ |
414 | |
415 | return TRUE; | |
416 | } | |
417 | ||
418 | bool wxSpinCtrl::Enable(bool enable) | |
419 | { | |
420 | if ( !wxControl::Enable(enable) ) | |
421 | { | |
422 | return FALSE; | |
423 | } | |
424 | ||
6fe19057 | 425 | ::EnableWindow(GetBuddyHwnd(), enable); |
882a8f40 VZ |
426 | |
427 | return TRUE; | |
428 | } | |
429 | ||
8bf3196d GRG |
430 | void wxSpinCtrl::SetFocus() |
431 | { | |
6fe19057 | 432 | ::SetFocus(GetBuddyHwnd()); |
8bf3196d GRG |
433 | } |
434 | ||
9750fc42 VZ |
435 | // ---------------------------------------------------------------------------- |
436 | // event processing | |
437 | // ---------------------------------------------------------------------------- | |
438 | ||
439 | void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin) | |
440 | { | |
441 | wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId()); | |
442 | event.SetEventObject(this); | |
443 | event.SetInt(eventSpin.GetPosition()); | |
444 | ||
445 | (void)GetEventHandler()->ProcessEvent(event); | |
446 | ||
447 | if ( eventSpin.GetSkipped() ) | |
448 | { | |
449 | event.Skip(); | |
450 | } | |
451 | } | |
452 | ||
b782f2e0 VZ |
453 | // ---------------------------------------------------------------------------- |
454 | // size calculations | |
455 | // ---------------------------------------------------------------------------- | |
456 | ||
f68586e5 | 457 | wxSize wxSpinCtrl::DoGetBestSize() const |
baccb514 VZ |
458 | { |
459 | wxSize sizeBtn = wxSpinButton::DoGetBestSize(); | |
460 | sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN; | |
461 | ||
462 | int y; | |
463 | wxGetCharSize(GetHWND(), NULL, &y, &GetFont()); | |
464 | y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y); | |
465 | ||
466 | if ( sizeBtn.y < y ) | |
467 | { | |
468 | // make the text tall enough | |
469 | sizeBtn.y = y; | |
470 | } | |
471 | ||
472 | return sizeBtn; | |
473 | } | |
474 | ||
b782f2e0 VZ |
475 | void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height) |
476 | { | |
baccb514 | 477 | int widthBtn = wxSpinButton::DoGetBestSize().x; |
b782f2e0 VZ |
478 | int widthText = width - widthBtn - MARGIN_BETWEEN; |
479 | if ( widthText <= 0 ) | |
480 | { | |
481 | wxLogDebug(_T("not enough space for wxSpinCtrl!")); | |
482 | } | |
483 | ||
6fe19057 | 484 | if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) ) |
b782f2e0 | 485 | { |
f6bcfd97 | 486 | wxLogLastError(wxT("MoveWindow(buddy)")); |
b782f2e0 VZ |
487 | } |
488 | ||
489 | x += widthText + MARGIN_BETWEEN; | |
490 | if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) ) | |
491 | { | |
f6bcfd97 | 492 | wxLogLastError(wxT("MoveWindow")); |
b782f2e0 VZ |
493 | } |
494 | } | |
495 | ||
f6bcfd97 BP |
496 | // get total size of the control |
497 | void wxSpinCtrl::DoGetSize(int *x, int *y) const | |
498 | { | |
499 | RECT spinrect, textrect, ctrlrect; | |
500 | GetWindowRect(GetHwnd(), &spinrect); | |
6fe19057 | 501 | GetWindowRect(GetBuddyHwnd(), &textrect); |
f6bcfd97 BP |
502 | UnionRect(&ctrlrect,&textrect, &spinrect); |
503 | ||
504 | if ( x ) | |
505 | *x = ctrlrect.right - ctrlrect.left; | |
506 | if ( y ) | |
507 | *y = ctrlrect.bottom - ctrlrect.top; | |
508 | } | |
509 | ||
510 | void wxSpinCtrl::DoGetPosition(int *x, int *y) const | |
511 | { | |
512 | // hack: pretend that our HWND is the text control just for a moment | |
513 | WXHWND hWnd = GetHWND(); | |
514 | wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy; | |
515 | ||
516 | wxSpinButton::DoGetPosition(x, y); | |
517 | ||
518 | wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd; | |
519 | } | |
520 | ||
b782f2e0 | 521 | #endif // __WIN95__ |
0e528b99 JS |
522 | |
523 | #endif | |
524 | // wxUSE_SPINCTRL | |
525 |