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_SPIN(-1, wxSpinCtrl::OnSpinChange
)
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // the margin between the up-down control and its buddy
59 static const int MARGIN_BETWEEN
= 5;
61 // ============================================================================
63 // ============================================================================
64 MRESULT EXPENTRY
wxSpinCtrlWndProc(
71 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( hWnd
74 bool bProccesed
= FALSE
;
75 MRESULT rc
= (MRESULT
)0;
77 // Forward some messages (the key ones only so far) to the spin ctrl
82 pSpin
->OS2WindowProc( uMessage
88 // The control may have been deleted at this point, so check.
90 if (!(::WinIsWindow(vHabmain
, hWnd
) && ((wxSpinCtrl
*)::WinQueryWindowULong( hWnd
98 return (fnWndProcSpinCtrl( hWnd
104 } // end of wxSpinCtrlWndProc
106 wxSpinCtrl::~wxSpinCtrl()
108 m_svAllSpins
.Remove(this);
110 // This removes spurious memory leak reporting
111 if (m_svAllSpins
.GetCount() == 0)
112 m_svAllSpins
.Clear();
113 } // end of wxSpinCtrl::~wxSpinCtrl
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 bool wxSpinCtrl::Create(
122 , const wxString
& rsValue
123 , const wxPoint
& rPos
124 , const wxSize
& rSize
129 , const wxString
& rsName
135 m_windowId
= NewControlId();
138 m_backgroundColour
= pParent
->GetBackgroundColour();
139 m_foregroundColour
= pParent
->GetForegroundColour();
142 m_windowStyle
= lStyle
;
146 lSstyle
= WS_VISIBLE
|
148 SPBS_MASTER
| // We use only single field spin buttons
149 SPBS_NUMERICONLY
; // We default to numeric data
151 if (m_windowStyle
& wxCLIP_SIBLINGS
)
152 lSstyle
|= WS_CLIPSIBLINGS
;
156 vCtrlData
.cbSize
= sizeof(SPBCDATA
);
157 vCtrlData
.ulTextLimit
= 10L;
158 vCtrlData
.lLowerLimit
= 0L;
159 vCtrlData
.lUpperLimit
= 100L;
160 vCtrlData
.idMasterSpb
= vId
;
161 vCtrlData
.pHWXCtlData
= NULL
;
163 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
)
178 m_hWndBuddy
= m_hWnd
; // One in the same for OS/2
180 pParent
->AddChild((wxSpinButton
*)this);
181 SetFont(pParent
->GetFont());
182 ::WinQueryWindowPos(m_hWnd
, &vSwp
);
191 SetRange(nMin
, nMax
);
195 // For OS/2 we'll just set our handle into our long data
197 wxAssociateWinWithHandle( m_hWnd
200 ::WinSetWindowULong(GetHwnd(), QWL_USER
, (LONG
)this);
201 fnWndProcSpinCtrl
= (WXFARPROC
)::WinSubclassWindow(m_hWnd
, (PFNWP
)wxSpinCtrlWndProc
);
202 m_svAllSpins
.Add(this);
204 } // end of wxSpinCtrl::Create
206 wxSize
wxSpinCtrl::DoGetBestSize() const
208 wxSize vSizeBtn
= wxSpinButton::DoGetBestSize();
211 vSizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
213 wxGetCharSize( GetHWND()
218 nHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight
);
220 if (vSizeBtn
.y
< nHeight
)
223 // Make the text tall enough
225 vSizeBtn
.y
= nHeight
;
228 } // end of wxSpinCtrl::DoGetBestSize
230 void wxSpinCtrl::DoGetPosition(
235 WXHWND hWnd
= GetHWND();
237 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hWndBuddy
;
238 wxSpinButton::DoGetPosition( pnX
241 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
242 } // end of wxpinCtrl::DoGetPosition
244 void wxSpinCtrl::DoGetSize(
251 ::WinQueryWindowRect(GetHwnd(), &vSpinrect
);
254 *pnWidth
= vSpinrect
.xRight
- vSpinrect
.xLeft
;
256 *pnHeight
= vSpinrect
.yTop
- vSpinrect
.yBottom
;
257 } // end of wxSpinCtrl::DoGetSize
259 void wxSpinCtrl::DoMoveWindow(
266 wxWindowOS2
* pParent
= (wxWindowOS2
*)GetParent();
270 int nOS2Height
= GetOS2ParentHeight(pParent
);
272 nY
= nOS2Height
- (nY
+ nHeight
);
278 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
279 nY
= vRect
.yTop
- (nY
+ nHeight
);
281 ::WinSetWindowPos( GetHwnd()
287 ,SWP_SIZE
| SWP_MOVE
| SWP_ZORDER
| SWP_SHOW
289 } // end of wxSpinCtrl::DoMoveWindow
291 bool wxSpinCtrl::Enable(
295 if (!wxControl::Enable(bEnable
))
299 ::WinEnableWindow(GetHwnd(), bEnable
);
301 } // end of wxSpinCtrl::Enable
303 wxSpinCtrl
* wxSpinCtrl::GetSpinForTextCtrl(
307 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( (HWND
)hWndBuddy
310 int i
= m_svAllSpins
.Index(pSpin
);
312 if (i
== wxNOT_FOUND
)
316 wxASSERT_MSG( pSpin
->m_hWndBuddy
== hWndBuddy
,
317 _T("wxSpinCtrl has incorrect buddy HWND!") );
320 } // end of wxSpinCtrl::GetSpinForTextCtrl
322 int wxSpinCtrl::GetValue() const
327 ::WinSendMsg( GetHwnd()
330 ,MPFROM2SHORT( (USHORT
)10
336 } // end of wxSpinCtrl::GetValue
338 void wxSpinCtrl::OnChar (
342 switch (rEvent
.KeyCode())
346 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_ENTER
349 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
351 InitCommandEvent(vEvent
);
352 vEvent
.SetString((char*)sVal
.c_str());
353 vEvent
.SetInt(GetValue());
354 if (GetEventHandler()->ProcessEvent(vEvent
))
361 // Always produce navigation event - even if we process TAB
362 // ourselves the fact that we got here means that the user code
363 // decided to skip processing of this TAB - probably to let it
364 // do its default job.
367 wxNavigationKeyEvent vEventNav
;
369 vEventNav
.SetDirection(!rEvent
.ShiftDown());
370 vEventNav
.SetWindowChange(rEvent
.ControlDown());
371 vEventNav
.SetEventObject(this);
372 if (GetParent()->GetEventHandler()->ProcessEvent(vEventNav
))
379 // No, we didn't process it
382 } // end of wxSpinCtrl::OnChar
384 void wxSpinCtrl::OnSpinChange(
385 wxSpinEvent
& rEventSpin
388 wxCommandEvent
vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED
392 vEvent
.SetEventObject(this);
393 vEvent
.SetInt(rEventSpin
.GetPosition());
394 (void)GetEventHandler()->ProcessEvent(vEvent
);
395 if (rEventSpin
.GetSkipped())
399 } // end of wxSpinCtrl::OnSpinChange
401 bool wxSpinCtrl::ProcessTextCommand(
410 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_UPDATED
413 vEvent
.SetEventObject(this);
415 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
417 vEvent
.SetString((char*)sVal
.c_str());
418 vEvent
.SetInt(GetValue());
419 return (GetEventHandler()->ProcessEvent(vEvent
));
425 wxFocusEvent
vEvent( wCmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
: wxEVT_SET_FOCUS
429 vEvent
.SetEventObject(this);
430 return(GetEventHandler()->ProcessEvent(vEvent
));
440 } // end of wxSpinCtrl::ProcessTextCommand
442 void wxSpinCtrl::SetFocus()
444 ::WinSetFocus(HWND_DESKTOP
, GetHwnd());
445 } // end of wxSpinCtrl::SetFocus
447 bool wxSpinCtrl::SetFont(
451 if (!wxWindowBase::SetFont(rFont
))
457 WXHANDLE hFont
= GetFont().GetResourceHandle();
462 } // end of wxSpinCtrl::SetFont
464 void wxSpinCtrl::SetValue(
465 const wxString
& rsText
470 lVal
= atol(rsText
.c_str());
471 wxSpinButton::SetValue(lVal
);
472 } // end of wxSpinCtrl::SetValue
474 bool wxSpinCtrl::Show(
478 if (!wxControl::Show(bShow
))
483 } // end of wxSpinCtrl::Show
485 #endif //wxUSE_SPINBTN