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