]>
Commit | Line | Data |
---|---|---|
409c9842 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/spinctrl.cpp | |
3 | // Purpose: wxSpinCtrl class implementation for Win32 | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/15/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
65571936 | 9 | // Licence: wxWindows licence |
409c9842 DW |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | ||
1c85008f SN |
17 | #ifdef __GNUG__ |
18 | #pragma implementation "spinctrlbase.h" | |
19 | #pragma implementation "spinctrl.h" | |
20 | #endif | |
21 | ||
409c9842 DW |
22 | // ---------------------------------------------------------------------------- |
23 | // headers | |
24 | // ---------------------------------------------------------------------------- | |
25 | ||
26 | // for compilers that support precompilation, includes "wx.h". | |
27 | #include "wx/wxprec.h" | |
28 | ||
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/wx.h" | |
32 | #endif | |
33 | ||
312ebad4 | 34 | #if wxUSE_SPINCTRL |
7e99520b | 35 | |
409c9842 DW |
36 | #include "wx/spinctrl.h" |
37 | #include "wx/os2/private.h" | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // macros | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
3c299c3a DW |
43 | extern void wxAssociateWinWithHandle( HWND hWnd |
44 | ,wxWindowOS2* pWin | |
45 | ); | |
46 | static WXFARPROC fnWndProcSpinCtrl = (WXFARPROC)NULL; | |
32334453 | 47 | wxArraySpins wxSpinCtrl::m_svAllSpins; |
3c299c3a | 48 | |
3d62dcb6 | 49 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl) |
409c9842 | 50 | |
3d62dcb6 | 51 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton) |
6e348b12 | 52 | EVT_CHAR(wxSpinCtrl::OnChar) |
312ebad4 | 53 | EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange) |
6e348b12 | 54 | EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus) |
3d62dcb6 | 55 | END_EVENT_TABLE() |
409c9842 DW |
56 | // ---------------------------------------------------------------------------- |
57 | // constants | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | // the margin between the up-down control and its buddy | |
61 | static const int MARGIN_BETWEEN = 5; | |
62 | ||
63 | // ============================================================================ | |
64 | // implementation | |
65 | // ============================================================================ | |
3c299c3a DW |
66 | MRESULT EXPENTRY wxSpinCtrlWndProc( |
67 | HWND hWnd | |
68 | , UINT uMessage | |
69 | , MPARAM wParam | |
70 | , MPARAM lParam | |
71 | ) | |
409c9842 | 72 | { |
3c299c3a DW |
73 | wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( hWnd |
74 | ,QWL_USER | |
75 | ); | |
9923c37d | 76 | |
3c299c3a DW |
77 | // |
78 | // Forward some messages (the key ones only so far) to the spin ctrl | |
79 | // | |
80 | switch (uMessage ) | |
3d62dcb6 | 81 | { |
3c299c3a DW |
82 | case WM_CHAR: |
83 | pSpin->OS2WindowProc( uMessage | |
84 | ,wParam | |
85 | ,lParam | |
86 | ); | |
87 | ||
88 | // | |
89 | // The control may have been deleted at this point, so check. | |
90 | // | |
91 | if (!(::WinIsWindow(vHabmain, hWnd) && ((wxSpinCtrl *)::WinQueryWindowULong( hWnd | |
92 | ,QWL_USER | |
93 | ) | |
94 | ) == pSpin)) | |
95 | return 0; | |
96 | break; | |
3d62dcb6 | 97 | |
409c9842 | 98 | } |
3c299c3a DW |
99 | return (fnWndProcSpinCtrl( hWnd |
100 | ,(ULONG)uMessage | |
101 | ,(MPARAM)wParam | |
102 | ,(MPARAM)lParam | |
103 | ) | |
104 | ); | |
105 | } // end of wxSpinCtrlWndProc | |
106 | ||
107 | wxSpinCtrl::~wxSpinCtrl() | |
108 | { | |
109 | m_svAllSpins.Remove(this); | |
409c9842 | 110 | |
3c299c3a DW |
111 | // This removes spurious memory leak reporting |
112 | if (m_svAllSpins.GetCount() == 0) | |
113 | m_svAllSpins.Clear(); | |
114 | } // end of wxSpinCtrl::~wxSpinCtrl | |
409c9842 | 115 | |
3c299c3a DW |
116 | // ---------------------------------------------------------------------------- |
117 | // construction | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
6670f564 WS |
120 | bool wxSpinCtrl::Create( wxWindow* pParent, |
121 | wxWindowID vId, | |
122 | const wxString& WXUNUSED(rsValue), | |
123 | const wxPoint& rPos, | |
124 | const wxSize& rSize, | |
125 | long lStyle, | |
126 | int nMin, | |
127 | int nMax, | |
128 | int nInitial, | |
129 | const wxString& rsName ) | |
3c299c3a | 130 | { |
d8a3f66c DW |
131 | SWP vSwp; |
132 | ||
312ebad4 | 133 | if (vId == wxID_ANY) |
3c299c3a DW |
134 | m_windowId = NewControlId(); |
135 | else | |
136 | m_windowId = vId; | |
137 | m_backgroundColour = pParent->GetBackgroundColour(); | |
138 | m_foregroundColour = pParent->GetForegroundColour(); | |
139 | SetName(rsName); | |
140 | SetParent(pParent); | |
141 | m_windowStyle = lStyle; | |
142 | ||
143 | int lSstyle = 0L; | |
144 | ||
145 | lSstyle = WS_VISIBLE | | |
146 | WS_TABSTOP | | |
147 | SPBS_MASTER | // We use only single field spin buttons | |
148 | SPBS_NUMERICONLY; // We default to numeric data | |
149 | ||
150 | if (m_windowStyle & wxCLIP_SIBLINGS ) | |
151 | lSstyle |= WS_CLIPSIBLINGS; | |
152 | ||
153 | SPBCDATA vCtrlData; | |
154 | ||
155 | vCtrlData.cbSize = sizeof(SPBCDATA); | |
156 | vCtrlData.ulTextLimit = 10L; | |
157 | vCtrlData.lLowerLimit = 0L; | |
158 | vCtrlData.lUpperLimit = 100L; | |
159 | vCtrlData.idMasterSpb = vId; | |
160 | vCtrlData.pHWXCtlData = NULL; | |
161 | ||
162 | m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent) | |
163 | ,WC_SPINBUTTON | |
164 | ,(PSZ)NULL | |
165 | ,lSstyle | |
166 | ,0L, 0L, 0L, 0L | |
167 | ,GetWinHwnd(pParent) | |
168 | ,HWND_TOP | |
169 | ,(HMENU)vId | |
170 | ,(PVOID)&vCtrlData | |
171 | ,NULL | |
172 | ); | |
173 | if (m_hWnd == 0) | |
409c9842 | 174 | { |
312ebad4 | 175 | return false; |
409c9842 | 176 | } |
3c299c3a DW |
177 | m_hWndBuddy = m_hWnd; // One in the same for OS/2 |
178 | if(pParent) | |
179 | pParent->AddChild((wxSpinButton *)this); | |
b3260bce DW |
180 | wxFont* pTextFont = new wxFont( 10 |
181 | ,wxMODERN | |
182 | ,wxNORMAL | |
183 | ,wxNORMAL | |
184 | ); | |
185 | SetFont(*pTextFont); | |
d8a3f66c DW |
186 | ::WinQueryWindowPos(m_hWnd, &vSwp); |
187 | SetXComp(vSwp.x); | |
188 | SetYComp(vSwp.y); | |
3c299c3a DW |
189 | SetSize( rPos.x |
190 | ,rPos.y | |
191 | ,rSize.x | |
192 | ,rSize.y | |
193 | ); | |
194 | ||
195 | SetRange(nMin, nMax); | |
196 | SetValue(nInitial); | |
197 | ||
198 | // | |
199 | // For OS/2 we'll just set our handle into our long data | |
200 | // | |
201 | wxAssociateWinWithHandle( m_hWnd | |
202 | ,(wxWindowOS2*)this | |
203 | ); | |
204 | ::WinSetWindowULong(GetHwnd(), QWL_USER, (LONG)this); | |
205 | fnWndProcSpinCtrl = (WXFARPROC)::WinSubclassWindow(m_hWnd, (PFNWP)wxSpinCtrlWndProc); | |
206 | m_svAllSpins.Add(this); | |
b3260bce | 207 | delete pTextFont; |
312ebad4 | 208 | return true; |
3c299c3a | 209 | } // end of wxSpinCtrl::Create |
409c9842 | 210 | |
3c299c3a DW |
211 | wxSize wxSpinCtrl::DoGetBestSize() const |
212 | { | |
213 | wxSize vSizeBtn = wxSpinButton::DoGetBestSize(); | |
214 | int nHeight; | |
0d598bae | 215 | wxFont vFont = (wxFont)GetFont(); |
409c9842 | 216 | |
3c299c3a | 217 | vSizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN; |
409c9842 | 218 | |
3c299c3a DW |
219 | wxGetCharSize( GetHWND() |
220 | ,NULL | |
221 | ,&nHeight | |
0d598bae | 222 | ,&vFont |
3c299c3a DW |
223 | ); |
224 | nHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight); | |
3d62dcb6 | 225 | |
3c299c3a | 226 | if (vSizeBtn.y < nHeight) |
3d62dcb6 | 227 | { |
3c299c3a DW |
228 | // |
229 | // Make the text tall enough | |
230 | // | |
231 | vSizeBtn.y = nHeight; | |
3d62dcb6 | 232 | } |
3c299c3a DW |
233 | return vSizeBtn; |
234 | } // end of wxSpinCtrl::DoGetBestSize | |
3d62dcb6 | 235 | |
3c299c3a DW |
236 | void wxSpinCtrl::DoGetPosition( |
237 | int* pnX | |
238 | , int* pnY | |
239 | ) const | |
240 | { | |
241 | WXHWND hWnd = GetHWND(); | |
242 | ||
243 | wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hWndBuddy; | |
244 | wxSpinButton::DoGetPosition( pnX | |
245 | ,pnY | |
246 | ); | |
247 | wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd; | |
248 | } // end of wxpinCtrl::DoGetPosition | |
249 | ||
250 | void wxSpinCtrl::DoGetSize( | |
251 | int* pnWidth | |
252 | , int* pnHeight | |
253 | ) const | |
254 | { | |
255 | RECTL vSpinrect; | |
256 | ||
257 | ::WinQueryWindowRect(GetHwnd(), &vSpinrect); | |
258 | ||
259 | if (pnWidth) | |
260 | *pnWidth = vSpinrect.xRight - vSpinrect.xLeft; | |
261 | if (pnHeight) | |
262 | *pnHeight = vSpinrect.yTop - vSpinrect.yBottom; | |
263 | } // end of wxSpinCtrl::DoGetSize | |
264 | ||
265 | void wxSpinCtrl::DoMoveWindow( | |
266 | int nX | |
267 | , int nY | |
268 | , int nWidth | |
269 | , int nHeight | |
270 | ) | |
271 | { | |
272 | wxWindowOS2* pParent = (wxWindowOS2*)GetParent(); | |
3d62dcb6 | 273 | |
3c299c3a | 274 | if (pParent) |
3d62dcb6 | 275 | { |
d8a3f66c DW |
276 | int nOS2Height = GetOS2ParentHeight(pParent); |
277 | ||
278 | nY = nOS2Height - (nY + nHeight); | |
3d62dcb6 | 279 | } |
3c299c3a DW |
280 | else |
281 | { | |
282 | RECTL vRect; | |
3d62dcb6 | 283 | |
3c299c3a DW |
284 | ::WinQueryWindowRect(HWND_DESKTOP, &vRect); |
285 | nY = vRect.yTop - (nY + nHeight); | |
286 | } | |
287 | ::WinSetWindowPos( GetHwnd() | |
288 | ,HWND_TOP | |
289 | ,nX | |
290 | ,nY | |
291 | ,nWidth | |
292 | ,nHeight | |
293 | ,SWP_SIZE | SWP_MOVE | SWP_ZORDER | SWP_SHOW | |
294 | ); | |
295 | } // end of wxSpinCtrl::DoMoveWindow | |
296 | ||
297 | bool wxSpinCtrl::Enable( | |
298 | bool bEnable | |
299 | ) | |
3d62dcb6 | 300 | { |
3c299c3a | 301 | if (!wxControl::Enable(bEnable)) |
3d62dcb6 | 302 | { |
312ebad4 | 303 | return false; |
3d62dcb6 | 304 | } |
3c299c3a | 305 | ::WinEnableWindow(GetHwnd(), bEnable); |
312ebad4 | 306 | return true; |
3c299c3a | 307 | } // end of wxSpinCtrl::Enable |
3d62dcb6 | 308 | |
3c299c3a DW |
309 | wxSpinCtrl* wxSpinCtrl::GetSpinForTextCtrl( |
310 | WXHWND hWndBuddy | |
311 | ) | |
3d62dcb6 | 312 | { |
3c299c3a DW |
313 | wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( (HWND)hWndBuddy |
314 | ,QWL_USER | |
315 | ); | |
316 | int i = m_svAllSpins.Index(pSpin); | |
3d62dcb6 | 317 | |
3c299c3a DW |
318 | if (i == wxNOT_FOUND) |
319 | return NULL; | |
3d62dcb6 | 320 | |
3c299c3a DW |
321 | // sanity check |
322 | wxASSERT_MSG( pSpin->m_hWndBuddy == hWndBuddy, | |
323 | _T("wxSpinCtrl has incorrect buddy HWND!") ); | |
3d62dcb6 | 324 | |
3c299c3a DW |
325 | return pSpin; |
326 | } // end of wxSpinCtrl::GetSpinForTextCtrl | |
3d62dcb6 | 327 | |
3c299c3a | 328 | int wxSpinCtrl::GetValue() const |
3d62dcb6 | 329 | { |
3c299c3a DW |
330 | long lVal = 0L; |
331 | char zVal[10]; | |
332 | ||
333 | ::WinSendMsg( GetHwnd() | |
334 | ,SPBM_QUERYVALUE | |
335 | ,MPFROMP(zVal) | |
336 | ,MPFROM2SHORT( (USHORT)10 | |
337 | ,SPBQ_UPDATEIFVALID | |
338 | ) | |
339 | ); | |
70a2c656 DW |
340 | lVal = atol(zVal); |
341 | return (int)lVal; | |
3c299c3a DW |
342 | } // end of wxSpinCtrl::GetValue |
343 | ||
344 | void wxSpinCtrl::OnChar ( | |
345 | wxKeyEvent& rEvent | |
346 | ) | |
347 | { | |
9923c37d | 348 | switch (rEvent.GetKeyCode()) |
3d62dcb6 | 349 | { |
3c299c3a DW |
350 | case WXK_RETURN: |
351 | { | |
352 | wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_ENTER | |
353 | ,m_windowId | |
354 | ); | |
355 | wxString sVal = wxGetWindowText(m_hWndBuddy); | |
356 | ||
357 | InitCommandEvent(vEvent); | |
0fba44b4 | 358 | vEvent.SetString(sVal); |
3c299c3a DW |
359 | vEvent.SetInt(GetValue()); |
360 | if (GetEventHandler()->ProcessEvent(vEvent)) | |
361 | return; | |
362 | break; | |
363 | } | |
364 | ||
365 | case WXK_TAB: | |
366 | // | |
367 | // Always produce navigation event - even if we process TAB | |
368 | // ourselves the fact that we got here means that the user code | |
369 | // decided to skip processing of this TAB - probably to let it | |
370 | // do its default job. | |
371 | // | |
372 | { | |
373 | wxNavigationKeyEvent vEventNav; | |
374 | ||
375 | vEventNav.SetDirection(!rEvent.ShiftDown()); | |
376 | vEventNav.SetWindowChange(rEvent.ControlDown()); | |
377 | vEventNav.SetEventObject(this); | |
378 | if (GetParent()->GetEventHandler()->ProcessEvent(vEventNav)) | |
379 | return; | |
380 | } | |
381 | break; | |
3d62dcb6 DW |
382 | } |
383 | ||
3c299c3a DW |
384 | // |
385 | // No, we didn't process it | |
386 | // | |
387 | rEvent.Skip(); | |
388 | } // end of wxSpinCtrl::OnChar | |
3d62dcb6 | 389 | |
3c299c3a DW |
390 | void wxSpinCtrl::OnSpinChange( |
391 | wxSpinEvent& rEventSpin | |
392 | ) | |
3d62dcb6 | 393 | { |
3c299c3a DW |
394 | wxCommandEvent vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED |
395 | ,GetId() | |
396 | ); | |
397 | ||
398 | vEvent.SetEventObject(this); | |
399 | vEvent.SetInt(rEventSpin.GetPosition()); | |
400 | (void)GetEventHandler()->ProcessEvent(vEvent); | |
401 | if (rEventSpin.GetSkipped()) | |
3d62dcb6 | 402 | { |
3c299c3a | 403 | vEvent.Skip(); |
3d62dcb6 | 404 | } |
3c299c3a | 405 | } // end of wxSpinCtrl::OnSpinChange |
3d62dcb6 | 406 | |
6e348b12 DW |
407 | void wxSpinCtrl::OnSetFocus ( |
408 | wxFocusEvent& rEvent | |
409 | ) | |
410 | { | |
411 | // | |
412 | // When we get focus, give it to our buddy window as it needs it more than | |
413 | // we do | |
414 | // | |
415 | ::WinSetFocus(HWND_DESKTOP, (HWND)m_hWndBuddy); | |
416 | rEvent.Skip(); | |
417 | } // end of wxSpinCtrl::OnSetFocus | |
418 | ||
6670f564 WS |
419 | bool wxSpinCtrl::ProcessTextCommand( WXWORD wCmd, |
420 | WXWORD WXUNUSED(wId) ) | |
3d62dcb6 | 421 | { |
3c299c3a | 422 | switch (wCmd) |
3d62dcb6 | 423 | { |
3c299c3a DW |
424 | case SPBN_CHANGE: |
425 | { | |
6670f564 | 426 | wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() ); |
3c299c3a DW |
427 | vEvent.SetEventObject(this); |
428 | ||
6670f564 | 429 | wxString sVal = wxGetWindowText(m_hWndBuddy); |
3c299c3a | 430 | |
0fba44b4 | 431 | vEvent.SetString(sVal); |
3c299c3a DW |
432 | vEvent.SetInt(GetValue()); |
433 | return (GetEventHandler()->ProcessEvent(vEvent)); | |
434 | } | |
435 | ||
436 | case SPBN_SETFOCUS: | |
437 | case SPBN_KILLFOCUS: | |
438 | { | |
6670f564 WS |
439 | wxFocusEvent vEvent( wCmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS : wxEVT_SET_FOCUS |
440 | ,m_windowId | |
441 | ); | |
3c299c3a DW |
442 | |
443 | vEvent.SetEventObject(this); | |
444 | return(GetEventHandler()->ProcessEvent(vEvent)); | |
445 | } | |
446 | default: | |
447 | break; | |
3d62dcb6 DW |
448 | } |
449 | ||
3c299c3a DW |
450 | // |
451 | // Not processed | |
452 | // | |
312ebad4 | 453 | return false; |
3c299c3a | 454 | } // end of wxSpinCtrl::ProcessTextCommand |
3d62dcb6 | 455 | |
3c299c3a | 456 | void wxSpinCtrl::SetFocus() |
3d62dcb6 | 457 | { |
3c299c3a DW |
458 | ::WinSetFocus(HWND_DESKTOP, GetHwnd()); |
459 | } // end of wxSpinCtrl::SetFocus | |
3d62dcb6 | 460 | |
3c299c3a DW |
461 | bool wxSpinCtrl::SetFont( |
462 | const wxFont& rFont | |
463 | ) | |
464 | { | |
465 | if (!wxWindowBase::SetFont(rFont)) | |
3d62dcb6 | 466 | { |
3c299c3a | 467 | // nothing to do |
312ebad4 | 468 | return false; |
3d62dcb6 | 469 | } |
3d62dcb6 | 470 | |
3c299c3a DW |
471 | wxOS2SetFont( m_hWnd |
472 | ,rFont | |
473 | ); | |
312ebad4 | 474 | return true; |
3c299c3a | 475 | } // end of wxSpinCtrl::SetFont |
409c9842 | 476 | |
3c299c3a DW |
477 | void wxSpinCtrl::SetValue( |
478 | const wxString& rsText | |
479 | ) | |
3d62dcb6 | 480 | { |
3c299c3a | 481 | long lVal; |
3d62dcb6 | 482 | |
0fba44b4 | 483 | lVal = atol((char*)rsText.c_str()); |
3c299c3a DW |
484 | wxSpinButton::SetValue(lVal); |
485 | } // end of wxSpinCtrl::SetValue | |
3d62dcb6 | 486 | |
3c299c3a DW |
487 | bool wxSpinCtrl::Show( |
488 | bool bShow | |
489 | ) | |
409c9842 | 490 | { |
3c299c3a | 491 | if (!wxControl::Show(bShow)) |
409c9842 | 492 | { |
312ebad4 | 493 | return false; |
409c9842 | 494 | } |
312ebad4 | 495 | return true; |
3c299c3a | 496 | } // end of wxSpinCtrl::Show |
7e99520b | 497 | |
cfcebdb1 DW |
498 | void wxSpinCtrl::SetSelection ( |
499 | long lFrom | |
500 | , long lTo | |
501 | ) | |
502 | { | |
503 | // | |
77ffb593 | 504 | // If from and to are both -1, it means (in wxWidgets) that all text should |
cfcebdb1 DW |
505 | // be selected - translate into Windows convention |
506 | // | |
507 | if ((lFrom == -1) && (lTo == -1)) | |
508 | { | |
509 | lFrom = 0; | |
510 | } | |
511 | ::WinSendMsg(m_hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), (MPARAM)0); | |
512 | } // end of wxSpinCtrl::SetSelection | |
513 | ||
312ebad4 | 514 | #endif //wxUSE_SPINCTRL |