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