]> git.saurik.com Git - wxWidgets.git/blob - src/os2/spinctrl.cpp
Added forgotten(?) wxMutexGuiEnter.
[wxWidgets.git] / src / os2 / spinctrl.cpp
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 bool bProccesed = FALSE;
77 MRESULT rc = (MRESULT)0;
78 //
79 // Forward some messages (the key ones only so far) to the spin ctrl
80 //
81 switch (uMessage )
82 {
83 case WM_CHAR:
84 pSpin->OS2WindowProc( uMessage
85 ,wParam
86 ,lParam
87 );
88
89 //
90 // The control may have been deleted at this point, so check.
91 //
92 if (!(::WinIsWindow(vHabmain, hWnd) && ((wxSpinCtrl *)::WinQueryWindowULong( hWnd
93 ,QWL_USER
94 )
95 ) == pSpin))
96 return 0;
97 break;
98
99 }
100 return (fnWndProcSpinCtrl( hWnd
101 ,(ULONG)uMessage
102 ,(MPARAM)wParam
103 ,(MPARAM)lParam
104 )
105 );
106 } // end of wxSpinCtrlWndProc
107
108 wxSpinCtrl::~wxSpinCtrl()
109 {
110 m_svAllSpins.Remove(this);
111
112 // This removes spurious memory leak reporting
113 if (m_svAllSpins.GetCount() == 0)
114 m_svAllSpins.Clear();
115 } // end of wxSpinCtrl::~wxSpinCtrl
116
117 // ----------------------------------------------------------------------------
118 // construction
119 // ----------------------------------------------------------------------------
120
121 bool wxSpinCtrl::Create(
122 wxWindow* pParent
123 , wxWindowID vId
124 , const wxString& rsValue
125 , const wxPoint& rPos
126 , const wxSize& rSize
127 , long lStyle
128 , int nMin
129 , int nMax
130 , int nInitial
131 , const wxString& rsName
132 )
133 {
134 SWP vSwp;
135
136 if (vId == -1)
137 m_windowId = NewControlId();
138 else
139 m_windowId = vId;
140 m_backgroundColour = pParent->GetBackgroundColour();
141 m_foregroundColour = pParent->GetForegroundColour();
142 SetName(rsName);
143 SetParent(pParent);
144 m_windowStyle = lStyle;
145
146 int lSstyle = 0L;
147
148 lSstyle = WS_VISIBLE |
149 WS_TABSTOP |
150 SPBS_MASTER | // We use only single field spin buttons
151 SPBS_NUMERICONLY; // We default to numeric data
152
153 if (m_windowStyle & wxCLIP_SIBLINGS )
154 lSstyle |= WS_CLIPSIBLINGS;
155
156 SPBCDATA vCtrlData;
157
158 vCtrlData.cbSize = sizeof(SPBCDATA);
159 vCtrlData.ulTextLimit = 10L;
160 vCtrlData.lLowerLimit = 0L;
161 vCtrlData.lUpperLimit = 100L;
162 vCtrlData.idMasterSpb = vId;
163 vCtrlData.pHWXCtlData = NULL;
164
165 m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent)
166 ,WC_SPINBUTTON
167 ,(PSZ)NULL
168 ,lSstyle
169 ,0L, 0L, 0L, 0L
170 ,GetWinHwnd(pParent)
171 ,HWND_TOP
172 ,(HMENU)vId
173 ,(PVOID)&vCtrlData
174 ,NULL
175 );
176 if (m_hWnd == 0)
177 {
178 return FALSE;
179 }
180 m_hWndBuddy = m_hWnd; // One in the same for OS/2
181 if(pParent)
182 pParent->AddChild((wxSpinButton *)this);
183 wxFont* pTextFont = new wxFont( 10
184 ,wxMODERN
185 ,wxNORMAL
186 ,wxNORMAL
187 );
188 SetFont(*pTextFont);
189 ::WinQueryWindowPos(m_hWnd, &vSwp);
190 SetXComp(vSwp.x);
191 SetYComp(vSwp.y);
192 SetSize( rPos.x
193 ,rPos.y
194 ,rSize.x
195 ,rSize.y
196 );
197
198 SetRange(nMin, nMax);
199 SetValue(nInitial);
200
201 //
202 // For OS/2 we'll just set our handle into our long data
203 //
204 wxAssociateWinWithHandle( m_hWnd
205 ,(wxWindowOS2*)this
206 );
207 ::WinSetWindowULong(GetHwnd(), QWL_USER, (LONG)this);
208 fnWndProcSpinCtrl = (WXFARPROC)::WinSubclassWindow(m_hWnd, (PFNWP)wxSpinCtrlWndProc);
209 m_svAllSpins.Add(this);
210 delete pTextFont;
211 return TRUE;
212 } // end of wxSpinCtrl::Create
213
214 wxSize wxSpinCtrl::DoGetBestSize() const
215 {
216 wxSize vSizeBtn = wxSpinButton::DoGetBestSize();
217 int nHeight;
218
219 vSizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
220
221 wxGetCharSize( GetHWND()
222 ,NULL
223 ,&nHeight
224 ,(wxFont*)&GetFont()
225 );
226 nHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight);
227
228 if (vSizeBtn.y < nHeight)
229 {
230 //
231 // Make the text tall enough
232 //
233 vSizeBtn.y = nHeight;
234 }
235 return vSizeBtn;
236 } // end of wxSpinCtrl::DoGetBestSize
237
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();
275
276 if (pParent)
277 {
278 int nOS2Height = GetOS2ParentHeight(pParent);
279
280 nY = nOS2Height - (nY + nHeight);
281 }
282 else
283 {
284 RECTL vRect;
285
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 )
302 {
303 if (!wxControl::Enable(bEnable))
304 {
305 return FALSE;
306 }
307 ::WinEnableWindow(GetHwnd(), bEnable);
308 return TRUE;
309 } // end of wxSpinCtrl::Enable
310
311 wxSpinCtrl* wxSpinCtrl::GetSpinForTextCtrl(
312 WXHWND hWndBuddy
313 )
314 {
315 wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( (HWND)hWndBuddy
316 ,QWL_USER
317 );
318 int i = m_svAllSpins.Index(pSpin);
319
320 if (i == wxNOT_FOUND)
321 return NULL;
322
323 // sanity check
324 wxASSERT_MSG( pSpin->m_hWndBuddy == hWndBuddy,
325 _T("wxSpinCtrl has incorrect buddy HWND!") );
326
327 return pSpin;
328 } // end of wxSpinCtrl::GetSpinForTextCtrl
329
330 int wxSpinCtrl::GetValue() const
331 {
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 );
342 lVal = atol(zVal);
343 return (int)lVal;
344 } // end of wxSpinCtrl::GetValue
345
346 void wxSpinCtrl::OnChar (
347 wxKeyEvent& rEvent
348 )
349 {
350 switch (rEvent.KeyCode())
351 {
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);
360 vEvent.SetString((char*)sVal.c_str());
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;
384 }
385
386 //
387 // No, we didn't process it
388 //
389 rEvent.Skip();
390 } // end of wxSpinCtrl::OnChar
391
392 void wxSpinCtrl::OnSpinChange(
393 wxSpinEvent& rEventSpin
394 )
395 {
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())
404 {
405 vEvent.Skip();
406 }
407 } // end of wxSpinCtrl::OnSpinChange
408
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
421 bool wxSpinCtrl::ProcessTextCommand(
422 WXWORD wCmd
423 , WXWORD wId
424 )
425 {
426 switch (wCmd)
427 {
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
437 vEvent.SetString((char*)sVal.c_str());
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;
454 }
455
456 //
457 // Not processed
458 //
459 return FALSE;
460 } // end of wxSpinCtrl::ProcessTextCommand
461
462 void wxSpinCtrl::SetFocus()
463 {
464 ::WinSetFocus(HWND_DESKTOP, GetHwnd());
465 } // end of wxSpinCtrl::SetFocus
466
467 bool wxSpinCtrl::SetFont(
468 const wxFont& rFont
469 )
470 {
471 if (!wxWindowBase::SetFont(rFont))
472 {
473 // nothing to do
474 return FALSE;
475 }
476
477 WXHANDLE hFont = GetFont().GetResourceHandle();
478 wxOS2SetFont( m_hWnd
479 ,rFont
480 );
481 return TRUE;
482 } // end of wxSpinCtrl::SetFont
483
484 void wxSpinCtrl::SetValue(
485 const wxString& rsText
486 )
487 {
488 long lVal;
489
490 lVal = atol(rsText.c_str());
491 wxSpinButton::SetValue(lVal);
492 } // end of wxSpinCtrl::SetValue
493
494 bool wxSpinCtrl::Show(
495 bool bShow
496 )
497 {
498 if (!wxControl::Show(bShow))
499 {
500 return FALSE;
501 }
502 return TRUE;
503 } // end of wxSpinCtrl::Show
504
505 void wxSpinCtrl::SetSelection (
506 long lFrom
507 , long lTo
508 )
509 {
510 //
511 // If from and to are both -1, it means (in wxWindows) that all text should
512 // be selected - translate into Windows convention
513 //
514 if ((lFrom == -1) && (lTo == -1))
515 {
516 lFrom = 0;
517 }
518 ::WinSendMsg(m_hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), (MPARAM)0);
519 } // end of wxSpinCtrl::SetSelection
520
521 #endif //wxUSE_SPINBTN