]>
Commit | Line | Data |
---|---|---|
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 | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | ||
17 | #ifdef __GNUG__ | |
18 | #pragma implementation "spinctrlbase.h" | |
19 | #pragma implementation "spinctrl.h" | |
20 | #endif | |
21 | ||
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 | ||
34 | #if wxUSE_SPINBTN | |
35 | ||
36 | #include "wx/spinctrl.h" | |
37 | #include "wx/os2/private.h" | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // macros | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | extern void wxAssociateWinWithHandle( HWND hWnd | |
44 | ,wxWindowOS2* pWin | |
45 | ); | |
46 | static WXFARPROC fnWndProcSpinCtrl = (WXFARPROC)NULL; | |
47 | wxArraySpins wxSpinCtrl::m_svAllSpins; | |
48 | ||
49 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl) | |
50 | ||
51 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton) | |
52 | EVT_CHAR(wxSpinCtrl::OnChar) | |
53 | EVT_SPIN(-1, wxSpinCtrl::OnSpinChange) | |
54 | EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus) | |
55 | END_EVENT_TABLE() | |
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 | // ============================================================================ | |
66 | MRESULT EXPENTRY wxSpinCtrlWndProc( | |
67 | HWND hWnd | |
68 | , UINT uMessage | |
69 | , MPARAM wParam | |
70 | , MPARAM lParam | |
71 | ) | |
72 | { | |
73 | wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( hWnd | |
74 | ,QWL_USER | |
75 | ); | |
76 | ||
77 | // | |
78 | // Forward some messages (the key ones only so far) to the spin ctrl | |
79 | // | |
80 | switch (uMessage ) | |
81 | { | |
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; | |
97 | ||
98 | } | |
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); | |
110 | ||
111 | // This removes spurious memory leak reporting | |
112 | if (m_svAllSpins.GetCount() == 0) | |
113 | m_svAllSpins.Clear(); | |
114 | } // end of wxSpinCtrl::~wxSpinCtrl | |
115 | ||
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 | { | |
133 | SWP vSwp; | |
134 | ||
135 | if (vId == -1) | |
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) | |
176 | { | |
177 | return FALSE; | |
178 | } | |
179 | m_hWndBuddy = m_hWnd; // One in the same for OS/2 | |
180 | if(pParent) | |
181 | pParent->AddChild((wxSpinButton *)this); | |
182 | wxFont* pTextFont = new wxFont( 10 | |
183 | ,wxMODERN | |
184 | ,wxNORMAL | |
185 | ,wxNORMAL | |
186 | ); | |
187 | SetFont(*pTextFont); | |
188 | ::WinQueryWindowPos(m_hWnd, &vSwp); | |
189 | SetXComp(vSwp.x); | |
190 | SetYComp(vSwp.y); | |
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); | |
209 | delete pTextFont; | |
210 | return TRUE; | |
211 | } // end of wxSpinCtrl::Create | |
212 | ||
213 | wxSize wxSpinCtrl::DoGetBestSize() const | |
214 | { | |
215 | wxSize vSizeBtn = wxSpinButton::DoGetBestSize(); | |
216 | int nHeight; | |
217 | ||
218 | vSizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN; | |
219 | ||
220 | wxGetCharSize( GetHWND() | |
221 | ,NULL | |
222 | ,&nHeight | |
223 | ,(wxFont*)&GetFont() | |
224 | ); | |
225 | nHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight); | |
226 | ||
227 | if (vSizeBtn.y < nHeight) | |
228 | { | |
229 | // | |
230 | // Make the text tall enough | |
231 | // | |
232 | vSizeBtn.y = nHeight; | |
233 | } | |
234 | return vSizeBtn; | |
235 | } // end of wxSpinCtrl::DoGetBestSize | |
236 | ||
237 | void wxSpinCtrl::DoGetPosition( | |
238 | int* pnX | |
239 | , int* pnY | |
240 | ) const | |
241 | { | |
242 | WXHWND hWnd = GetHWND(); | |
243 | ||
244 | wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hWndBuddy; | |
245 | wxSpinButton::DoGetPosition( pnX | |
246 | ,pnY | |
247 | ); | |
248 | wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd; | |
249 | } // end of wxpinCtrl::DoGetPosition | |
250 | ||
251 | void wxSpinCtrl::DoGetSize( | |
252 | int* pnWidth | |
253 | , int* pnHeight | |
254 | ) const | |
255 | { | |
256 | RECTL vSpinrect; | |
257 | ||
258 | ::WinQueryWindowRect(GetHwnd(), &vSpinrect); | |
259 | ||
260 | if (pnWidth) | |
261 | *pnWidth = vSpinrect.xRight - vSpinrect.xLeft; | |
262 | if (pnHeight) | |
263 | *pnHeight = vSpinrect.yTop - vSpinrect.yBottom; | |
264 | } // end of wxSpinCtrl::DoGetSize | |
265 | ||
266 | void wxSpinCtrl::DoMoveWindow( | |
267 | int nX | |
268 | , int nY | |
269 | , int nWidth | |
270 | , int nHeight | |
271 | ) | |
272 | { | |
273 | wxWindowOS2* pParent = (wxWindowOS2*)GetParent(); | |
274 | ||
275 | if (pParent) | |
276 | { | |
277 | int nOS2Height = GetOS2ParentHeight(pParent); | |
278 | ||
279 | nY = nOS2Height - (nY + nHeight); | |
280 | } | |
281 | else | |
282 | { | |
283 | RECTL vRect; | |
284 | ||
285 | ::WinQueryWindowRect(HWND_DESKTOP, &vRect); | |
286 | nY = vRect.yTop - (nY + nHeight); | |
287 | } | |
288 | ::WinSetWindowPos( GetHwnd() | |
289 | ,HWND_TOP | |
290 | ,nX | |
291 | ,nY | |
292 | ,nWidth | |
293 | ,nHeight | |
294 | ,SWP_SIZE | SWP_MOVE | SWP_ZORDER | SWP_SHOW | |
295 | ); | |
296 | } // end of wxSpinCtrl::DoMoveWindow | |
297 | ||
298 | bool wxSpinCtrl::Enable( | |
299 | bool bEnable | |
300 | ) | |
301 | { | |
302 | if (!wxControl::Enable(bEnable)) | |
303 | { | |
304 | return FALSE; | |
305 | } | |
306 | ::WinEnableWindow(GetHwnd(), bEnable); | |
307 | return TRUE; | |
308 | } // end of wxSpinCtrl::Enable | |
309 | ||
310 | wxSpinCtrl* wxSpinCtrl::GetSpinForTextCtrl( | |
311 | WXHWND hWndBuddy | |
312 | ) | |
313 | { | |
314 | wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( (HWND)hWndBuddy | |
315 | ,QWL_USER | |
316 | ); | |
317 | int i = m_svAllSpins.Index(pSpin); | |
318 | ||
319 | if (i == wxNOT_FOUND) | |
320 | return NULL; | |
321 | ||
322 | // sanity check | |
323 | wxASSERT_MSG( pSpin->m_hWndBuddy == hWndBuddy, | |
324 | _T("wxSpinCtrl has incorrect buddy HWND!") ); | |
325 | ||
326 | return pSpin; | |
327 | } // end of wxSpinCtrl::GetSpinForTextCtrl | |
328 | ||
329 | int wxSpinCtrl::GetValue() const | |
330 | { | |
331 | long lVal = 0L; | |
332 | char zVal[10]; | |
333 | ||
334 | ::WinSendMsg( GetHwnd() | |
335 | ,SPBM_QUERYVALUE | |
336 | ,MPFROMP(zVal) | |
337 | ,MPFROM2SHORT( (USHORT)10 | |
338 | ,SPBQ_UPDATEIFVALID | |
339 | ) | |
340 | ); | |
341 | lVal = atol(zVal); | |
342 | return (int)lVal; | |
343 | } // end of wxSpinCtrl::GetValue | |
344 | ||
345 | void wxSpinCtrl::OnChar ( | |
346 | wxKeyEvent& rEvent | |
347 | ) | |
348 | { | |
349 | switch (rEvent.GetKeyCode()) | |
350 | { | |
351 | case WXK_RETURN: | |
352 | { | |
353 | wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_ENTER | |
354 | ,m_windowId | |
355 | ); | |
356 | wxString sVal = wxGetWindowText(m_hWndBuddy); | |
357 | ||
358 | InitCommandEvent(vEvent); | |
359 | vEvent.SetString((char*)sVal.c_str()); | |
360 | vEvent.SetInt(GetValue()); | |
361 | if (GetEventHandler()->ProcessEvent(vEvent)) | |
362 | return; | |
363 | break; | |
364 | } | |
365 | ||
366 | case WXK_TAB: | |
367 | // | |
368 | // Always produce navigation event - even if we process TAB | |
369 | // ourselves the fact that we got here means that the user code | |
370 | // decided to skip processing of this TAB - probably to let it | |
371 | // do its default job. | |
372 | // | |
373 | { | |
374 | wxNavigationKeyEvent vEventNav; | |
375 | ||
376 | vEventNav.SetDirection(!rEvent.ShiftDown()); | |
377 | vEventNav.SetWindowChange(rEvent.ControlDown()); | |
378 | vEventNav.SetEventObject(this); | |
379 | if (GetParent()->GetEventHandler()->ProcessEvent(vEventNav)) | |
380 | return; | |
381 | } | |
382 | break; | |
383 | } | |
384 | ||
385 | // | |
386 | // No, we didn't process it | |
387 | // | |
388 | rEvent.Skip(); | |
389 | } // end of wxSpinCtrl::OnChar | |
390 | ||
391 | void wxSpinCtrl::OnSpinChange( | |
392 | wxSpinEvent& rEventSpin | |
393 | ) | |
394 | { | |
395 | wxCommandEvent vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED | |
396 | ,GetId() | |
397 | ); | |
398 | ||
399 | vEvent.SetEventObject(this); | |
400 | vEvent.SetInt(rEventSpin.GetPosition()); | |
401 | (void)GetEventHandler()->ProcessEvent(vEvent); | |
402 | if (rEventSpin.GetSkipped()) | |
403 | { | |
404 | vEvent.Skip(); | |
405 | } | |
406 | } // end of wxSpinCtrl::OnSpinChange | |
407 | ||
408 | void wxSpinCtrl::OnSetFocus ( | |
409 | wxFocusEvent& rEvent | |
410 | ) | |
411 | { | |
412 | // | |
413 | // When we get focus, give it to our buddy window as it needs it more than | |
414 | // we do | |
415 | // | |
416 | ::WinSetFocus(HWND_DESKTOP, (HWND)m_hWndBuddy); | |
417 | rEvent.Skip(); | |
418 | } // end of wxSpinCtrl::OnSetFocus | |
419 | ||
420 | bool wxSpinCtrl::ProcessTextCommand( | |
421 | WXWORD wCmd | |
422 | , WXWORD wId | |
423 | ) | |
424 | { | |
425 | switch (wCmd) | |
426 | { | |
427 | case SPBN_CHANGE: | |
428 | { | |
429 | wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED | |
430 | ,GetId() | |
431 | ); | |
432 | vEvent.SetEventObject(this); | |
433 | ||
434 | wxString sVal = wxGetWindowText(m_hWndBuddy); | |
435 | ||
436 | vEvent.SetString((char*)sVal.c_str()); | |
437 | vEvent.SetInt(GetValue()); | |
438 | return (GetEventHandler()->ProcessEvent(vEvent)); | |
439 | } | |
440 | ||
441 | case SPBN_SETFOCUS: | |
442 | case SPBN_KILLFOCUS: | |
443 | { | |
444 | wxFocusEvent vEvent( wCmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS : wxEVT_SET_FOCUS | |
445 | ,m_windowId | |
446 | ); | |
447 | ||
448 | vEvent.SetEventObject(this); | |
449 | return(GetEventHandler()->ProcessEvent(vEvent)); | |
450 | } | |
451 | default: | |
452 | break; | |
453 | } | |
454 | ||
455 | // | |
456 | // Not processed | |
457 | // | |
458 | return FALSE; | |
459 | } // end of wxSpinCtrl::ProcessTextCommand | |
460 | ||
461 | void wxSpinCtrl::SetFocus() | |
462 | { | |
463 | ::WinSetFocus(HWND_DESKTOP, GetHwnd()); | |
464 | } // end of wxSpinCtrl::SetFocus | |
465 | ||
466 | bool wxSpinCtrl::SetFont( | |
467 | const wxFont& rFont | |
468 | ) | |
469 | { | |
470 | if (!wxWindowBase::SetFont(rFont)) | |
471 | { | |
472 | // nothing to do | |
473 | return FALSE; | |
474 | } | |
475 | ||
476 | wxOS2SetFont( m_hWnd | |
477 | ,rFont | |
478 | ); | |
479 | return TRUE; | |
480 | } // end of wxSpinCtrl::SetFont | |
481 | ||
482 | void wxSpinCtrl::SetValue( | |
483 | const wxString& rsText | |
484 | ) | |
485 | { | |
486 | long lVal; | |
487 | ||
488 | lVal = atol(rsText.c_str()); | |
489 | wxSpinButton::SetValue(lVal); | |
490 | } // end of wxSpinCtrl::SetValue | |
491 | ||
492 | bool wxSpinCtrl::Show( | |
493 | bool bShow | |
494 | ) | |
495 | { | |
496 | if (!wxControl::Show(bShow)) | |
497 | { | |
498 | return FALSE; | |
499 | } | |
500 | return TRUE; | |
501 | } // end of wxSpinCtrl::Show | |
502 | ||
503 | void wxSpinCtrl::SetSelection ( | |
504 | long lFrom | |
505 | , long lTo | |
506 | ) | |
507 | { | |
508 | // | |
509 | // If from and to are both -1, it means (in wxWindows) that all text should | |
510 | // be selected - translate into Windows convention | |
511 | // | |
512 | if ((lFrom == -1) && (lTo == -1)) | |
513 | { | |
514 | lFrom = 0; | |
515 | } | |
516 | ::WinSendMsg(m_hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), (MPARAM)0); | |
517 | } // end of wxSpinCtrl::SetSelection | |
518 | ||
519 | #endif //wxUSE_SPINBTN |