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
133 m_windowId
= NewControlId();
136 m_backgroundColour
= pParent
->GetBackgroundColour();
137 m_foregroundColour
= pParent
->GetForegroundColour();
140 m_windowStyle
= lStyle
;
144 lSstyle
= WS_VISIBLE
|
146 SPBS_MASTER
| // We use only single field spin buttons
147 SPBS_NUMERICONLY
; // We default to numeric data
149 if (m_windowStyle
& wxCLIP_SIBLINGS
)
150 lSstyle
|= WS_CLIPSIBLINGS
;
154 vCtrlData
.cbSize
= sizeof(SPBCDATA
);
155 vCtrlData
.ulTextLimit
= 10L;
156 vCtrlData
.lLowerLimit
= 0L;
157 vCtrlData
.lUpperLimit
= 100L;
158 vCtrlData
.idMasterSpb
= vId
;
159 vCtrlData
.pHWXCtlData
= NULL
;
161 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
)
176 m_hWndBuddy
= m_hWnd
; // One in the same for OS/2
178 pParent
->AddChild((wxSpinButton
*)this);
179 SetFont(pParent
->GetFont());
186 SetRange(nMin
, nMax
);
190 // For OS/2 we'll just set our handle into our long data
192 wxAssociateWinWithHandle( m_hWnd
195 ::WinSetWindowULong(GetHwnd(), QWL_USER
, (LONG
)this);
196 fnWndProcSpinCtrl
= (WXFARPROC
)::WinSubclassWindow(m_hWnd
, (PFNWP
)wxSpinCtrlWndProc
);
197 m_svAllSpins
.Add(this);
199 } // end of wxSpinCtrl::Create
201 wxSize
wxSpinCtrl::DoGetBestSize() const
203 wxSize vSizeBtn
= wxSpinButton::DoGetBestSize();
206 vSizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
208 wxGetCharSize( GetHWND()
213 nHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight
);
215 if (vSizeBtn
.y
< nHeight
)
218 // Make the text tall enough
220 vSizeBtn
.y
= nHeight
;
223 } // end of wxSpinCtrl::DoGetBestSize
225 void wxSpinCtrl::DoGetPosition(
230 WXHWND hWnd
= GetHWND();
232 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hWndBuddy
;
233 wxSpinButton::DoGetPosition( pnX
236 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
237 } // end of wxpinCtrl::DoGetPosition
239 void wxSpinCtrl::DoGetSize(
246 ::WinQueryWindowRect(GetHwnd(), &vSpinrect
);
249 *pnWidth
= vSpinrect
.xRight
- vSpinrect
.xLeft
;
251 *pnHeight
= vSpinrect
.yTop
- vSpinrect
.yBottom
;
252 } // end of wxSpinCtrl::DoGetSize
254 void wxSpinCtrl::DoMoveWindow(
261 wxWindowOS2
* pParent
= (wxWindowOS2
*)GetParent();
265 if (pParent
->IsKindOf(CLASSINFO(wxFrame
)))
267 nY
= pParent
->GetClientSize().y
- (nY
+ nHeight
);
270 nY
= pParent
->GetSize().y
- (nY
+ nHeight
);
276 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
277 nY
= vRect
.yTop
- (nY
+ nHeight
);
279 ::WinSetWindowPos( GetHwnd()
285 ,SWP_SIZE
| SWP_MOVE
| SWP_ZORDER
| SWP_SHOW
287 } // end of wxSpinCtrl::DoMoveWindow
289 bool wxSpinCtrl::Enable(
293 if (!wxControl::Enable(bEnable
))
297 ::WinEnableWindow(GetHwnd(), bEnable
);
299 } // end of wxSpinCtrl::Enable
301 wxSpinCtrl
* wxSpinCtrl::GetSpinForTextCtrl(
305 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( (HWND
)hWndBuddy
308 int i
= m_svAllSpins
.Index(pSpin
);
310 if (i
== wxNOT_FOUND
)
314 wxASSERT_MSG( pSpin
->m_hWndBuddy
== hWndBuddy
,
315 _T("wxSpinCtrl has incorrect buddy HWND!") );
318 } // end of wxSpinCtrl::GetSpinForTextCtrl
320 int wxSpinCtrl::GetValue() const
325 ::WinSendMsg( GetHwnd()
328 ,MPFROM2SHORT( (USHORT
)10
334 } // end of wxSpinCtrl::GetValue
336 void wxSpinCtrl::OnChar (
340 switch (rEvent
.KeyCode())
344 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_ENTER
347 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
349 InitCommandEvent(vEvent
);
350 vEvent
.SetString((char*)sVal
.c_str());
351 vEvent
.SetInt(GetValue());
352 if (GetEventHandler()->ProcessEvent(vEvent
))
359 // Always produce navigation event - even if we process TAB
360 // ourselves the fact that we got here means that the user code
361 // decided to skip processing of this TAB - probably to let it
362 // do its default job.
365 wxNavigationKeyEvent vEventNav
;
367 vEventNav
.SetDirection(!rEvent
.ShiftDown());
368 vEventNav
.SetWindowChange(rEvent
.ControlDown());
369 vEventNav
.SetEventObject(this);
370 if (GetParent()->GetEventHandler()->ProcessEvent(vEventNav
))
377 // No, we didn't process it
380 } // end of wxSpinCtrl::OnChar
382 void wxSpinCtrl::OnSpinChange(
383 wxSpinEvent
& rEventSpin
386 wxCommandEvent
vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED
390 vEvent
.SetEventObject(this);
391 vEvent
.SetInt(rEventSpin
.GetPosition());
392 (void)GetEventHandler()->ProcessEvent(vEvent
);
393 if (rEventSpin
.GetSkipped())
397 } // end of wxSpinCtrl::OnSpinChange
399 bool wxSpinCtrl::ProcessTextCommand(
408 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_UPDATED
411 vEvent
.SetEventObject(this);
413 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
415 vEvent
.SetString((char*)sVal
.c_str());
416 vEvent
.SetInt(GetValue());
417 return (GetEventHandler()->ProcessEvent(vEvent
));
423 wxFocusEvent
vEvent( wCmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
: wxEVT_SET_FOCUS
427 vEvent
.SetEventObject(this);
428 return(GetEventHandler()->ProcessEvent(vEvent
));
438 } // end of wxSpinCtrl::ProcessTextCommand
440 void wxSpinCtrl::SetFocus()
442 ::WinSetFocus(HWND_DESKTOP
, GetHwnd());
443 } // end of wxSpinCtrl::SetFocus
445 bool wxSpinCtrl::SetFont(
449 if (!wxWindowBase::SetFont(rFont
))
455 WXHANDLE hFont
= GetFont().GetResourceHandle();
460 } // end of wxSpinCtrl::SetFont
462 void wxSpinCtrl::SetValue(
463 const wxString
& rsText
468 lVal
= atol(rsText
.c_str());
469 wxSpinButton::SetValue(lVal
);
470 } // end of wxSpinCtrl::SetValue
472 bool wxSpinCtrl::Show(
476 if (!wxControl::Show(bShow
))
481 } // end of wxSpinCtrl::Show
483 #endif //wxUSE_SPINBTN