1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for Win32
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
18 #pragma implementation "spinctrlbase.h"
19 #pragma implementation "spinctrl.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 // for compilers that support precompilation, includes "wx.h".
27 #include "wx/wxprec.h"
36 #include "wx/spinctrl.h"
37 #include "wx/os2/private.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 extern void wxAssociateWinWithHandle( HWND hWnd
46 static WXFARPROC fnWndProcSpinCtrl
= (WXFARPROC
)NULL
;
47 wxArraySpins
wxSpinCtrl::m_svAllSpins
;
49 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
51 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
52 EVT_CHAR(wxSpinCtrl::OnChar
)
53 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange
)
54 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // the margin between the up-down control and its buddy
61 static const int MARGIN_BETWEEN
= 5;
63 // ============================================================================
65 // ============================================================================
66 MRESULT EXPENTRY
wxSpinCtrlWndProc(
73 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( hWnd
78 // Forward some messages (the key ones only so far) to the spin ctrl
83 pSpin
->OS2WindowProc( uMessage
89 // The control may have been deleted at this point, so check.
91 if (!(::WinIsWindow(vHabmain
, hWnd
) && ((wxSpinCtrl
*)::WinQueryWindowULong( hWnd
99 return (fnWndProcSpinCtrl( hWnd
105 } // end of wxSpinCtrlWndProc
107 wxSpinCtrl::~wxSpinCtrl()
109 m_svAllSpins
.Remove(this);
111 // This removes spurious memory leak reporting
112 if (m_svAllSpins
.GetCount() == 0)
113 m_svAllSpins
.Clear();
114 } // end of wxSpinCtrl::~wxSpinCtrl
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 bool wxSpinCtrl::Create(
123 , const wxString
& rsValue
124 , const wxPoint
& rPos
125 , const wxSize
& rSize
130 , const wxString
& rsName
136 m_windowId
= NewControlId();
139 m_backgroundColour
= pParent
->GetBackgroundColour();
140 m_foregroundColour
= pParent
->GetForegroundColour();
143 m_windowStyle
= lStyle
;
147 lSstyle
= WS_VISIBLE
|
149 SPBS_MASTER
| // We use only single field spin buttons
150 SPBS_NUMERICONLY
; // We default to numeric data
152 if (m_windowStyle
& wxCLIP_SIBLINGS
)
153 lSstyle
|= WS_CLIPSIBLINGS
;
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
;
164 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
)
179 m_hWndBuddy
= m_hWnd
; // One in the same for OS/2
181 pParent
->AddChild((wxSpinButton
*)this);
182 wxFont
* pTextFont
= new wxFont( 10
188 ::WinQueryWindowPos(m_hWnd
, &vSwp
);
197 SetRange(nMin
, nMax
);
201 // For OS/2 we'll just set our handle into our long data
203 wxAssociateWinWithHandle( m_hWnd
206 ::WinSetWindowULong(GetHwnd(), QWL_USER
, (LONG
)this);
207 fnWndProcSpinCtrl
= (WXFARPROC
)::WinSubclassWindow(m_hWnd
, (PFNWP
)wxSpinCtrlWndProc
);
208 m_svAllSpins
.Add(this);
211 } // end of wxSpinCtrl::Create
213 wxSize
wxSpinCtrl::DoGetBestSize() const
215 wxSize vSizeBtn
= wxSpinButton::DoGetBestSize();
218 vSizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
220 wxGetCharSize( GetHWND()
225 nHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight
);
227 if (vSizeBtn
.y
< nHeight
)
230 // Make the text tall enough
232 vSizeBtn
.y
= nHeight
;
235 } // end of wxSpinCtrl::DoGetBestSize
237 void wxSpinCtrl::DoGetPosition(
242 WXHWND hWnd
= GetHWND();
244 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hWndBuddy
;
245 wxSpinButton::DoGetPosition( pnX
248 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
249 } // end of wxpinCtrl::DoGetPosition
251 void wxSpinCtrl::DoGetSize(
258 ::WinQueryWindowRect(GetHwnd(), &vSpinrect
);
261 *pnWidth
= vSpinrect
.xRight
- vSpinrect
.xLeft
;
263 *pnHeight
= vSpinrect
.yTop
- vSpinrect
.yBottom
;
264 } // end of wxSpinCtrl::DoGetSize
266 void wxSpinCtrl::DoMoveWindow(
273 wxWindowOS2
* pParent
= (wxWindowOS2
*)GetParent();
277 int nOS2Height
= GetOS2ParentHeight(pParent
);
279 nY
= nOS2Height
- (nY
+ nHeight
);
285 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
286 nY
= vRect
.yTop
- (nY
+ nHeight
);
288 ::WinSetWindowPos( GetHwnd()
294 ,SWP_SIZE
| SWP_MOVE
| SWP_ZORDER
| SWP_SHOW
296 } // end of wxSpinCtrl::DoMoveWindow
298 bool wxSpinCtrl::Enable(
302 if (!wxControl::Enable(bEnable
))
306 ::WinEnableWindow(GetHwnd(), bEnable
);
308 } // end of wxSpinCtrl::Enable
310 wxSpinCtrl
* wxSpinCtrl::GetSpinForTextCtrl(
314 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( (HWND
)hWndBuddy
317 int i
= m_svAllSpins
.Index(pSpin
);
319 if (i
== wxNOT_FOUND
)
323 wxASSERT_MSG( pSpin
->m_hWndBuddy
== hWndBuddy
,
324 _T("wxSpinCtrl has incorrect buddy HWND!") );
327 } // end of wxSpinCtrl::GetSpinForTextCtrl
329 int wxSpinCtrl::GetValue() const
334 ::WinSendMsg( GetHwnd()
337 ,MPFROM2SHORT( (USHORT
)10
343 } // end of wxSpinCtrl::GetValue
345 void wxSpinCtrl::OnChar (
349 switch (rEvent
.GetKeyCode())
353 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_ENTER
356 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
358 InitCommandEvent(vEvent
);
359 vEvent
.SetString((char*)sVal
.c_str());
360 vEvent
.SetInt(GetValue());
361 if (GetEventHandler()->ProcessEvent(vEvent
))
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.
374 wxNavigationKeyEvent vEventNav
;
376 vEventNav
.SetDirection(!rEvent
.ShiftDown());
377 vEventNav
.SetWindowChange(rEvent
.ControlDown());
378 vEventNav
.SetEventObject(this);
379 if (GetParent()->GetEventHandler()->ProcessEvent(vEventNav
))
386 // No, we didn't process it
389 } // end of wxSpinCtrl::OnChar
391 void wxSpinCtrl::OnSpinChange(
392 wxSpinEvent
& rEventSpin
395 wxCommandEvent
vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED
399 vEvent
.SetEventObject(this);
400 vEvent
.SetInt(rEventSpin
.GetPosition());
401 (void)GetEventHandler()->ProcessEvent(vEvent
);
402 if (rEventSpin
.GetSkipped())
406 } // end of wxSpinCtrl::OnSpinChange
408 void wxSpinCtrl::OnSetFocus (
413 // When we get focus, give it to our buddy window as it needs it more than
416 ::WinSetFocus(HWND_DESKTOP
, (HWND
)m_hWndBuddy
);
418 } // end of wxSpinCtrl::OnSetFocus
420 bool wxSpinCtrl::ProcessTextCommand(
429 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_UPDATED
432 vEvent
.SetEventObject(this);
434 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
436 vEvent
.SetString((char*)sVal
.c_str());
437 vEvent
.SetInt(GetValue());
438 return (GetEventHandler()->ProcessEvent(vEvent
));
444 wxFocusEvent
vEvent( wCmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
: wxEVT_SET_FOCUS
448 vEvent
.SetEventObject(this);
449 return(GetEventHandler()->ProcessEvent(vEvent
));
459 } // end of wxSpinCtrl::ProcessTextCommand
461 void wxSpinCtrl::SetFocus()
463 ::WinSetFocus(HWND_DESKTOP
, GetHwnd());
464 } // end of wxSpinCtrl::SetFocus
466 bool wxSpinCtrl::SetFont(
470 if (!wxWindowBase::SetFont(rFont
))
480 } // end of wxSpinCtrl::SetFont
482 void wxSpinCtrl::SetValue(
483 const wxString
& rsText
488 lVal
= atol(rsText
.c_str());
489 wxSpinButton::SetValue(lVal
);
490 } // end of wxSpinCtrl::SetValue
492 bool wxSpinCtrl::Show(
496 if (!wxControl::Show(bShow
))
501 } // end of wxSpinCtrl::Show
503 void wxSpinCtrl::SetSelection (
509 // If from and to are both -1, it means (in wxWindows) that all text should
510 // be selected - translate into Windows convention
512 if ((lFrom
== -1) && (lTo
== -1))
516 ::WinSendMsg(m_hWnd
, EM_SETSEL
, MPFROM2SHORT((USHORT
)lFrom
, (USHORT
)lTo
), (MPARAM
)0);
517 } // end of wxSpinCtrl::SetSelection
519 #endif //wxUSE_SPINBTN