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