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(wxID_ANY
, 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( wxWindow
* pParent
,
122 const wxString
& WXUNUSED(rsValue
),
129 const wxString
& rsName
)
134 m_windowId
= NewControlId();
137 m_backgroundColour
= pParent
->GetBackgroundColour();
138 m_foregroundColour
= pParent
->GetForegroundColour();
141 m_windowStyle
= lStyle
;
145 lSstyle
= WS_VISIBLE
|
147 SPBS_MASTER
| // We use only single field spin buttons
148 SPBS_NUMERICONLY
; // We default to numeric data
150 if (m_windowStyle
& wxCLIP_SIBLINGS
)
151 lSstyle
|= WS_CLIPSIBLINGS
;
155 vCtrlData
.cbSize
= sizeof(SPBCDATA
);
156 vCtrlData
.ulTextLimit
= 10L;
157 vCtrlData
.lLowerLimit
= 0L;
158 vCtrlData
.lUpperLimit
= 100L;
159 vCtrlData
.idMasterSpb
= vId
;
160 vCtrlData
.pHWXCtlData
= NULL
;
162 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
)
177 m_hWndBuddy
= m_hWnd
; // One in the same for OS/2
179 pParent
->AddChild((wxSpinButton
*)this);
180 wxFont
* pTextFont
= new wxFont( 10
186 ::WinQueryWindowPos(m_hWnd
, &vSwp
);
195 SetRange(nMin
, nMax
);
199 // For OS/2 we'll just set our handle into our long data
201 wxAssociateWinWithHandle( m_hWnd
204 ::WinSetWindowULong(GetHwnd(), QWL_USER
, (LONG
)this);
205 fnWndProcSpinCtrl
= (WXFARPROC
)::WinSubclassWindow(m_hWnd
, (PFNWP
)wxSpinCtrlWndProc
);
206 m_svAllSpins
.Add(this);
209 } // end of wxSpinCtrl::Create
211 wxSize
wxSpinCtrl::DoGetBestSize() const
213 wxSize vSizeBtn
= wxSpinButton::DoGetBestSize();
215 wxFont vFont
= (wxFont
)GetFont();
217 vSizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
219 wxGetCharSize( GetHWND()
224 nHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight
);
226 if (vSizeBtn
.y
< nHeight
)
229 // Make the text tall enough
231 vSizeBtn
.y
= nHeight
;
234 } // end of wxSpinCtrl::DoGetBestSize
236 void wxSpinCtrl::DoGetPosition(
241 WXHWND hWnd
= GetHWND();
243 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hWndBuddy
;
244 wxSpinButton::DoGetPosition( pnX
247 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
248 } // end of wxpinCtrl::DoGetPosition
250 void wxSpinCtrl::DoGetSize(
257 ::WinQueryWindowRect(GetHwnd(), &vSpinrect
);
260 *pnWidth
= vSpinrect
.xRight
- vSpinrect
.xLeft
;
262 *pnHeight
= vSpinrect
.yTop
- vSpinrect
.yBottom
;
263 } // end of wxSpinCtrl::DoGetSize
265 void wxSpinCtrl::DoMoveWindow(
272 wxWindowOS2
* pParent
= (wxWindowOS2
*)GetParent();
276 int nOS2Height
= GetOS2ParentHeight(pParent
);
278 nY
= nOS2Height
- (nY
+ nHeight
);
284 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
285 nY
= vRect
.yTop
- (nY
+ nHeight
);
287 ::WinSetWindowPos( GetHwnd()
293 ,SWP_SIZE
| SWP_MOVE
| SWP_ZORDER
| SWP_SHOW
295 } // end of wxSpinCtrl::DoMoveWindow
297 bool wxSpinCtrl::Enable(
301 if (!wxControl::Enable(bEnable
))
305 ::WinEnableWindow(GetHwnd(), bEnable
);
307 } // end of wxSpinCtrl::Enable
309 wxSpinCtrl
* wxSpinCtrl::GetSpinForTextCtrl(
313 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( (HWND
)hWndBuddy
316 int i
= m_svAllSpins
.Index(pSpin
);
318 if (i
== wxNOT_FOUND
)
322 wxASSERT_MSG( pSpin
->m_hWndBuddy
== hWndBuddy
,
323 _T("wxSpinCtrl has incorrect buddy HWND!") );
326 } // end of wxSpinCtrl::GetSpinForTextCtrl
328 int wxSpinCtrl::GetValue() const
333 ::WinSendMsg( GetHwnd()
336 ,MPFROM2SHORT( (USHORT
)10
342 } // end of wxSpinCtrl::GetValue
344 void wxSpinCtrl::OnChar (
348 switch (rEvent
.GetKeyCode())
352 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_ENTER
355 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
357 InitCommandEvent(vEvent
);
358 vEvent
.SetString(sVal
);
359 vEvent
.SetInt(GetValue());
360 if (GetEventHandler()->ProcessEvent(vEvent
))
367 // Always produce navigation event - even if we process TAB
368 // ourselves the fact that we got here means that the user code
369 // decided to skip processing of this TAB - probably to let it
370 // do its default job.
373 wxNavigationKeyEvent vEventNav
;
375 vEventNav
.SetDirection(!rEvent
.ShiftDown());
376 vEventNav
.SetWindowChange(rEvent
.ControlDown());
377 vEventNav
.SetEventObject(this);
378 if (GetParent()->GetEventHandler()->ProcessEvent(vEventNav
))
385 // No, we didn't process it
388 } // end of wxSpinCtrl::OnChar
390 void wxSpinCtrl::OnSpinChange(
391 wxSpinEvent
& rEventSpin
394 wxCommandEvent
vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED
398 vEvent
.SetEventObject(this);
399 vEvent
.SetInt(rEventSpin
.GetPosition());
400 (void)GetEventHandler()->ProcessEvent(vEvent
);
401 if (rEventSpin
.GetSkipped())
405 } // end of wxSpinCtrl::OnSpinChange
407 void wxSpinCtrl::OnSetFocus (
412 // When we get focus, give it to our buddy window as it needs it more than
415 ::WinSetFocus(HWND_DESKTOP
, (HWND
)m_hWndBuddy
);
417 } // end of wxSpinCtrl::OnSetFocus
419 bool wxSpinCtrl::ProcessTextCommand( WXWORD wCmd
,
420 WXWORD
WXUNUSED(wId
) )
426 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_UPDATED
, GetId() );
427 vEvent
.SetEventObject(this);
429 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
431 vEvent
.SetString(sVal
);
432 vEvent
.SetInt(GetValue());
433 return (GetEventHandler()->ProcessEvent(vEvent
));
439 wxFocusEvent
vEvent( wCmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
: wxEVT_SET_FOCUS
443 vEvent
.SetEventObject(this);
444 return(GetEventHandler()->ProcessEvent(vEvent
));
454 } // end of wxSpinCtrl::ProcessTextCommand
456 void wxSpinCtrl::SetFocus()
458 ::WinSetFocus(HWND_DESKTOP
, GetHwnd());
459 } // end of wxSpinCtrl::SetFocus
461 bool wxSpinCtrl::SetFont(
465 if (!wxWindowBase::SetFont(rFont
))
475 } // end of wxSpinCtrl::SetFont
477 void wxSpinCtrl::SetValue(
478 const wxString
& rsText
483 lVal
= atol((char*)rsText
.c_str());
484 wxSpinButton::SetValue(lVal
);
485 } // end of wxSpinCtrl::SetValue
487 bool wxSpinCtrl::Show(
491 if (!wxControl::Show(bShow
))
496 } // end of wxSpinCtrl::Show
498 void wxSpinCtrl::SetSelection (
504 // If from and to are both -1, it means (in wxWidgets) that all text should
505 // be selected - translate into Windows convention
507 if ((lFrom
== -1) && (lTo
== -1))
511 ::WinSendMsg(m_hWnd
, EM_SETSEL
, MPFROM2SHORT((USHORT
)lFrom
, (USHORT
)lTo
), (MPARAM
)0);
512 } // end of wxSpinCtrl::SetSelection
514 #endif //wxUSE_SPINCTRL