]>
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 | ||
120 | bool wxSpinCtrl::Create( | |
121 | wxWindow* pParent | |
122 | , wxWindowID vId | |
123 | , const wxString& rsValue | |
124 | , const wxPoint& rPos | |
125 | , const wxSize& rSize | |
126 | , long lStyle | |
127 | , int nMin | |
128 | , int nMax | |
129 | , int nInitial | |
130 | , const wxString& rsName | |
131 | ) | |
132 | { | |
d8a3f66c DW |
133 | SWP vSwp; |
134 | ||
312ebad4 | 135 | if (vId == wxID_ANY) |
3c299c3a DW |
136 | m_windowId = NewControlId(); |
137 | else | |
138 | m_windowId = vId; | |
139 | m_backgroundColour = pParent->GetBackgroundColour(); | |
140 | m_foregroundColour = pParent->GetForegroundColour(); | |
141 | SetName(rsName); | |
142 | SetParent(pParent); | |
143 | m_windowStyle = lStyle; | |
144 | ||
145 | int lSstyle = 0L; | |
146 | ||
147 | lSstyle = WS_VISIBLE | | |
148 | WS_TABSTOP | | |
149 | SPBS_MASTER | // We use only single field spin buttons | |
150 | SPBS_NUMERICONLY; // We default to numeric data | |
151 | ||
152 | if (m_windowStyle & wxCLIP_SIBLINGS ) | |
153 | lSstyle |= WS_CLIPSIBLINGS; | |
154 | ||
155 | SPBCDATA vCtrlData; | |
156 | ||
157 | vCtrlData.cbSize = sizeof(SPBCDATA); | |
158 | vCtrlData.ulTextLimit = 10L; | |
159 | vCtrlData.lLowerLimit = 0L; | |
160 | vCtrlData.lUpperLimit = 100L; | |
161 | vCtrlData.idMasterSpb = vId; | |
162 | vCtrlData.pHWXCtlData = NULL; | |
163 | ||
164 | m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent) | |
165 | ,WC_SPINBUTTON | |
166 | ,(PSZ)NULL | |
167 | ,lSstyle | |
168 | ,0L, 0L, 0L, 0L | |
169 | ,GetWinHwnd(pParent) | |
170 | ,HWND_TOP | |
171 | ,(HMENU)vId | |
172 | ,(PVOID)&vCtrlData | |
173 | ,NULL | |
174 | ); | |
175 | if (m_hWnd == 0) | |
409c9842 | 176 | { |
312ebad4 | 177 | return false; |
409c9842 | 178 | } |
3c299c3a DW |
179 | m_hWndBuddy = m_hWnd; // One in the same for OS/2 |
180 | if(pParent) | |
181 | pParent->AddChild((wxSpinButton *)this); | |
b3260bce DW |
182 | wxFont* pTextFont = new wxFont( 10 |
183 | ,wxMODERN | |
184 | ,wxNORMAL | |
185 | ,wxNORMAL | |
186 | ); | |
187 | SetFont(*pTextFont); | |
d8a3f66c DW |
188 | ::WinQueryWindowPos(m_hWnd, &vSwp); |
189 | SetXComp(vSwp.x); | |
190 | SetYComp(vSwp.y); | |
3c299c3a DW |
191 | SetSize( rPos.x |
192 | ,rPos.y | |
193 | ,rSize.x | |
194 | ,rSize.y | |
195 | ); | |
196 | ||
197 | SetRange(nMin, nMax); | |
198 | SetValue(nInitial); | |
199 | ||
200 | // | |
201 | // For OS/2 we'll just set our handle into our long data | |
202 | // | |
203 | wxAssociateWinWithHandle( m_hWnd | |
204 | ,(wxWindowOS2*)this | |
205 | ); | |
206 | ::WinSetWindowULong(GetHwnd(), QWL_USER, (LONG)this); | |
207 | fnWndProcSpinCtrl = (WXFARPROC)::WinSubclassWindow(m_hWnd, (PFNWP)wxSpinCtrlWndProc); | |
208 | m_svAllSpins.Add(this); | |
b3260bce | 209 | delete pTextFont; |
312ebad4 | 210 | return true; |
3c299c3a | 211 | } // end of wxSpinCtrl::Create |
409c9842 | 212 | |
3c299c3a DW |
213 | wxSize wxSpinCtrl::DoGetBestSize() const |
214 | { | |
215 | wxSize vSizeBtn = wxSpinButton::DoGetBestSize(); | |
216 | int nHeight; | |
0d598bae | 217 | wxFont vFont = (wxFont)GetFont(); |
409c9842 | 218 | |
3c299c3a | 219 | vSizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN; |
409c9842 | 220 | |
3c299c3a DW |
221 | wxGetCharSize( GetHWND() |
222 | ,NULL | |
223 | ,&nHeight | |
0d598bae | 224 | ,&vFont |
3c299c3a DW |
225 | ); |
226 | nHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight); | |
3d62dcb6 | 227 | |
3c299c3a | 228 | if (vSizeBtn.y < nHeight) |
3d62dcb6 | 229 | { |
3c299c3a DW |
230 | // |
231 | // Make the text tall enough | |
232 | // | |
233 | vSizeBtn.y = nHeight; | |
3d62dcb6 | 234 | } |
3c299c3a DW |
235 | return vSizeBtn; |
236 | } // end of wxSpinCtrl::DoGetBestSize | |
3d62dcb6 | 237 | |
3c299c3a DW |
238 | void wxSpinCtrl::DoGetPosition( |
239 | int* pnX | |
240 | , int* pnY | |
241 | ) const | |
242 | { | |
243 | WXHWND hWnd = GetHWND(); | |
244 | ||
245 | wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hWndBuddy; | |
246 | wxSpinButton::DoGetPosition( pnX | |
247 | ,pnY | |
248 | ); | |
249 | wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd; | |
250 | } // end of wxpinCtrl::DoGetPosition | |
251 | ||
252 | void wxSpinCtrl::DoGetSize( | |
253 | int* pnWidth | |
254 | , int* pnHeight | |
255 | ) const | |
256 | { | |
257 | RECTL vSpinrect; | |
258 | ||
259 | ::WinQueryWindowRect(GetHwnd(), &vSpinrect); | |
260 | ||
261 | if (pnWidth) | |
262 | *pnWidth = vSpinrect.xRight - vSpinrect.xLeft; | |
263 | if (pnHeight) | |
264 | *pnHeight = vSpinrect.yTop - vSpinrect.yBottom; | |
265 | } // end of wxSpinCtrl::DoGetSize | |
266 | ||
267 | void wxSpinCtrl::DoMoveWindow( | |
268 | int nX | |
269 | , int nY | |
270 | , int nWidth | |
271 | , int nHeight | |
272 | ) | |
273 | { | |
274 | wxWindowOS2* pParent = (wxWindowOS2*)GetParent(); | |
3d62dcb6 | 275 | |
3c299c3a | 276 | if (pParent) |
3d62dcb6 | 277 | { |
d8a3f66c DW |
278 | int nOS2Height = GetOS2ParentHeight(pParent); |
279 | ||
280 | nY = nOS2Height - (nY + nHeight); | |
3d62dcb6 | 281 | } |
3c299c3a DW |
282 | else |
283 | { | |
284 | RECTL vRect; | |
3d62dcb6 | 285 | |
3c299c3a DW |
286 | ::WinQueryWindowRect(HWND_DESKTOP, &vRect); |
287 | nY = vRect.yTop - (nY + nHeight); | |
288 | } | |
289 | ::WinSetWindowPos( GetHwnd() | |
290 | ,HWND_TOP | |
291 | ,nX | |
292 | ,nY | |
293 | ,nWidth | |
294 | ,nHeight | |
295 | ,SWP_SIZE | SWP_MOVE | SWP_ZORDER | SWP_SHOW | |
296 | ); | |
297 | } // end of wxSpinCtrl::DoMoveWindow | |
298 | ||
299 | bool wxSpinCtrl::Enable( | |
300 | bool bEnable | |
301 | ) | |
3d62dcb6 | 302 | { |
3c299c3a | 303 | if (!wxControl::Enable(bEnable)) |
3d62dcb6 | 304 | { |
312ebad4 | 305 | return false; |
3d62dcb6 | 306 | } |
3c299c3a | 307 | ::WinEnableWindow(GetHwnd(), bEnable); |
312ebad4 | 308 | return true; |
3c299c3a | 309 | } // end of wxSpinCtrl::Enable |
3d62dcb6 | 310 | |
3c299c3a DW |
311 | wxSpinCtrl* wxSpinCtrl::GetSpinForTextCtrl( |
312 | WXHWND hWndBuddy | |
313 | ) | |
3d62dcb6 | 314 | { |
3c299c3a DW |
315 | wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( (HWND)hWndBuddy |
316 | ,QWL_USER | |
317 | ); | |
318 | int i = m_svAllSpins.Index(pSpin); | |
3d62dcb6 | 319 | |
3c299c3a DW |
320 | if (i == wxNOT_FOUND) |
321 | return NULL; | |
3d62dcb6 | 322 | |
3c299c3a DW |
323 | // sanity check |
324 | wxASSERT_MSG( pSpin->m_hWndBuddy == hWndBuddy, | |
325 | _T("wxSpinCtrl has incorrect buddy HWND!") ); | |
3d62dcb6 | 326 | |
3c299c3a DW |
327 | return pSpin; |
328 | } // end of wxSpinCtrl::GetSpinForTextCtrl | |
3d62dcb6 | 329 | |
3c299c3a | 330 | int wxSpinCtrl::GetValue() const |
3d62dcb6 | 331 | { |
3c299c3a DW |
332 | long lVal = 0L; |
333 | char zVal[10]; | |
334 | ||
335 | ::WinSendMsg( GetHwnd() | |
336 | ,SPBM_QUERYVALUE | |
337 | ,MPFROMP(zVal) | |
338 | ,MPFROM2SHORT( (USHORT)10 | |
339 | ,SPBQ_UPDATEIFVALID | |
340 | ) | |
341 | ); | |
70a2c656 DW |
342 | lVal = atol(zVal); |
343 | return (int)lVal; | |
3c299c3a DW |
344 | } // end of wxSpinCtrl::GetValue |
345 | ||
346 | void wxSpinCtrl::OnChar ( | |
347 | wxKeyEvent& rEvent | |
348 | ) | |
349 | { | |
9923c37d | 350 | switch (rEvent.GetKeyCode()) |
3d62dcb6 | 351 | { |
3c299c3a DW |
352 | case WXK_RETURN: |
353 | { | |
354 | wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_ENTER | |
355 | ,m_windowId | |
356 | ); | |
357 | wxString sVal = wxGetWindowText(m_hWndBuddy); | |
358 | ||
359 | InitCommandEvent(vEvent); | |
0fba44b4 | 360 | vEvent.SetString(sVal); |
3c299c3a DW |
361 | vEvent.SetInt(GetValue()); |
362 | if (GetEventHandler()->ProcessEvent(vEvent)) | |
363 | return; | |
364 | break; | |
365 | } | |
366 | ||
367 | case WXK_TAB: | |
368 | // | |
369 | // Always produce navigation event - even if we process TAB | |
370 | // ourselves the fact that we got here means that the user code | |
371 | // decided to skip processing of this TAB - probably to let it | |
372 | // do its default job. | |
373 | // | |
374 | { | |
375 | wxNavigationKeyEvent vEventNav; | |
376 | ||
377 | vEventNav.SetDirection(!rEvent.ShiftDown()); | |
378 | vEventNav.SetWindowChange(rEvent.ControlDown()); | |
379 | vEventNav.SetEventObject(this); | |
380 | if (GetParent()->GetEventHandler()->ProcessEvent(vEventNav)) | |
381 | return; | |
382 | } | |
383 | break; | |
3d62dcb6 DW |
384 | } |
385 | ||
3c299c3a DW |
386 | // |
387 | // No, we didn't process it | |
388 | // | |
389 | rEvent.Skip(); | |
390 | } // end of wxSpinCtrl::OnChar | |
3d62dcb6 | 391 | |
3c299c3a DW |
392 | void wxSpinCtrl::OnSpinChange( |
393 | wxSpinEvent& rEventSpin | |
394 | ) | |
3d62dcb6 | 395 | { |
3c299c3a DW |
396 | wxCommandEvent vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED |
397 | ,GetId() | |
398 | ); | |
399 | ||
400 | vEvent.SetEventObject(this); | |
401 | vEvent.SetInt(rEventSpin.GetPosition()); | |
402 | (void)GetEventHandler()->ProcessEvent(vEvent); | |
403 | if (rEventSpin.GetSkipped()) | |
3d62dcb6 | 404 | { |
3c299c3a | 405 | vEvent.Skip(); |
3d62dcb6 | 406 | } |
3c299c3a | 407 | } // end of wxSpinCtrl::OnSpinChange |
3d62dcb6 | 408 | |
6e348b12 DW |
409 | void wxSpinCtrl::OnSetFocus ( |
410 | wxFocusEvent& rEvent | |
411 | ) | |
412 | { | |
413 | // | |
414 | // When we get focus, give it to our buddy window as it needs it more than | |
415 | // we do | |
416 | // | |
417 | ::WinSetFocus(HWND_DESKTOP, (HWND)m_hWndBuddy); | |
418 | rEvent.Skip(); | |
419 | } // end of wxSpinCtrl::OnSetFocus | |
420 | ||
3c299c3a DW |
421 | bool wxSpinCtrl::ProcessTextCommand( |
422 | WXWORD wCmd | |
423 | , WXWORD wId | |
424 | ) | |
3d62dcb6 | 425 | { |
3c299c3a | 426 | switch (wCmd) |
3d62dcb6 | 427 | { |
3c299c3a DW |
428 | case SPBN_CHANGE: |
429 | { | |
430 | wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED | |
431 | ,GetId() | |
432 | ); | |
433 | vEvent.SetEventObject(this); | |
434 | ||
435 | wxString sVal = wxGetWindowText(m_hWndBuddy); | |
436 | ||
0fba44b4 | 437 | vEvent.SetString(sVal); |
3c299c3a DW |
438 | vEvent.SetInt(GetValue()); |
439 | return (GetEventHandler()->ProcessEvent(vEvent)); | |
440 | } | |
441 | ||
442 | case SPBN_SETFOCUS: | |
443 | case SPBN_KILLFOCUS: | |
444 | { | |
445 | wxFocusEvent vEvent( wCmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS : wxEVT_SET_FOCUS | |
446 | ,m_windowId | |
447 | ); | |
448 | ||
449 | vEvent.SetEventObject(this); | |
450 | return(GetEventHandler()->ProcessEvent(vEvent)); | |
451 | } | |
452 | default: | |
453 | break; | |
3d62dcb6 DW |
454 | } |
455 | ||
3c299c3a DW |
456 | // |
457 | // Not processed | |
458 | // | |
312ebad4 | 459 | return false; |
3c299c3a | 460 | } // end of wxSpinCtrl::ProcessTextCommand |
3d62dcb6 | 461 | |
3c299c3a | 462 | void wxSpinCtrl::SetFocus() |
3d62dcb6 | 463 | { |
3c299c3a DW |
464 | ::WinSetFocus(HWND_DESKTOP, GetHwnd()); |
465 | } // end of wxSpinCtrl::SetFocus | |
3d62dcb6 | 466 | |
3c299c3a DW |
467 | bool wxSpinCtrl::SetFont( |
468 | const wxFont& rFont | |
469 | ) | |
470 | { | |
471 | if (!wxWindowBase::SetFont(rFont)) | |
3d62dcb6 | 472 | { |
3c299c3a | 473 | // nothing to do |
312ebad4 | 474 | return false; |
3d62dcb6 | 475 | } |
3d62dcb6 | 476 | |
3c299c3a DW |
477 | wxOS2SetFont( m_hWnd |
478 | ,rFont | |
479 | ); | |
312ebad4 | 480 | return true; |
3c299c3a | 481 | } // end of wxSpinCtrl::SetFont |
409c9842 | 482 | |
3c299c3a DW |
483 | void wxSpinCtrl::SetValue( |
484 | const wxString& rsText | |
485 | ) | |
3d62dcb6 | 486 | { |
3c299c3a | 487 | long lVal; |
3d62dcb6 | 488 | |
0fba44b4 | 489 | lVal = atol((char*)rsText.c_str()); |
3c299c3a DW |
490 | wxSpinButton::SetValue(lVal); |
491 | } // end of wxSpinCtrl::SetValue | |
3d62dcb6 | 492 | |
3c299c3a DW |
493 | bool wxSpinCtrl::Show( |
494 | bool bShow | |
495 | ) | |
409c9842 | 496 | { |
3c299c3a | 497 | if (!wxControl::Show(bShow)) |
409c9842 | 498 | { |
312ebad4 | 499 | return false; |
409c9842 | 500 | } |
312ebad4 | 501 | return true; |
3c299c3a | 502 | } // end of wxSpinCtrl::Show |
7e99520b | 503 | |
cfcebdb1 DW |
504 | void wxSpinCtrl::SetSelection ( |
505 | long lFrom | |
506 | , long lTo | |
507 | ) | |
508 | { | |
509 | // | |
77ffb593 | 510 | // If from and to are both -1, it means (in wxWidgets) that all text should |
cfcebdb1 DW |
511 | // be selected - translate into Windows convention |
512 | // | |
513 | if ((lFrom == -1) && (lTo == -1)) | |
514 | { | |
515 | lFrom = 0; | |
516 | } | |
517 | ::WinSendMsg(m_hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), (MPARAM)0); | |
518 | } // end of wxSpinCtrl::SetSelection | |
519 | ||
312ebad4 | 520 | #endif //wxUSE_SPINCTRL |