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 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/spinctrl.h"
31 #include "wx/os2/private.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 extern void wxAssociateWinWithHandle( HWND hWnd
40 static WXFARPROC fnWndProcSpinCtrl
= (WXFARPROC
)NULL
;
41 wxArraySpins
wxSpinCtrl::m_svAllSpins
;
43 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
, wxControl
)
45 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
46 EVT_CHAR(wxSpinCtrl::OnChar
)
47 EVT_SPIN(wxID_ANY
, wxSpinCtrl::OnSpinChange
)
48 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // the margin between the up-down control and its buddy
55 static const int MARGIN_BETWEEN
= 5;
57 // ============================================================================
59 // ============================================================================
60 MRESULT EXPENTRY
wxSpinCtrlWndProc(
67 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( hWnd
72 // Forward some messages (the key ones only so far) to the spin ctrl
77 pSpin
->OS2WindowProc( uMessage
83 // The control may have been deleted at this point, so check.
85 if (!(::WinIsWindow(vHabmain
, hWnd
) && ((wxSpinCtrl
*)::WinQueryWindowULong( hWnd
93 return (fnWndProcSpinCtrl( hWnd
99 } // end of wxSpinCtrlWndProc
101 wxSpinCtrl::~wxSpinCtrl()
103 m_svAllSpins
.Remove(this);
105 // This removes spurious memory leak reporting
106 if (m_svAllSpins
.GetCount() == 0)
107 m_svAllSpins
.Clear();
108 } // end of wxSpinCtrl::~wxSpinCtrl
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 bool wxSpinCtrl::Create( wxWindow
* pParent
,
116 const wxString
& WXUNUSED(rsValue
),
123 const wxString
& rsName
)
128 m_windowId
= NewControlId();
131 m_backgroundColour
= pParent
->GetBackgroundColour();
132 m_foregroundColour
= pParent
->GetForegroundColour();
135 m_windowStyle
= lStyle
;
139 lSstyle
= WS_VISIBLE
|
141 SPBS_MASTER
| // We use only single field spin buttons
142 SPBS_NUMERICONLY
; // We default to numeric data
144 if (m_windowStyle
& wxCLIP_SIBLINGS
)
145 lSstyle
|= WS_CLIPSIBLINGS
;
149 vCtrlData
.cbSize
= sizeof(SPBCDATA
);
150 vCtrlData
.ulTextLimit
= 10L;
151 vCtrlData
.lLowerLimit
= 0L;
152 vCtrlData
.lUpperLimit
= 100L;
153 vCtrlData
.idMasterSpb
= vId
;
154 vCtrlData
.pHWXCtlData
= NULL
;
156 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
)
171 m_hWndBuddy
= m_hWnd
; // One in the same for OS/2
173 pParent
->AddChild((wxSpinButton
*)this);
174 wxFont
* pTextFont
= new wxFont( 10
180 ::WinQueryWindowPos(m_hWnd
, &vSwp
);
189 SetRange(nMin
, nMax
);
193 // For OS/2 we'll just set our handle into our long data
195 wxAssociateWinWithHandle( m_hWnd
198 ::WinSetWindowULong(GetHwnd(), QWL_USER
, (LONG
)this);
199 fnWndProcSpinCtrl
= (WXFARPROC
)::WinSubclassWindow(m_hWnd
, (PFNWP
)wxSpinCtrlWndProc
);
200 m_svAllSpins
.Add(this);
203 } // end of wxSpinCtrl::Create
205 wxSize
wxSpinCtrl::DoGetBestSize() const
207 wxSize vSizeBtn
= wxSpinButton::DoGetBestSize();
209 wxFont vFont
= (wxFont
)GetFont();
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
.GetKeyCode())
346 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_ENTER
349 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
351 InitCommandEvent(vEvent
);
352 vEvent
.SetString(sVal
);
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 void wxSpinCtrl::OnSetFocus (
406 // When we get focus, give it to our buddy window as it needs it more than
409 ::WinSetFocus(HWND_DESKTOP
, (HWND
)m_hWndBuddy
);
411 } // end of wxSpinCtrl::OnSetFocus
413 bool wxSpinCtrl::ProcessTextCommand( WXWORD wCmd
,
414 WXWORD
WXUNUSED(wId
) )
420 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_UPDATED
, GetId() );
421 vEvent
.SetEventObject(this);
423 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
425 vEvent
.SetString(sVal
);
426 vEvent
.SetInt(GetValue());
427 return (GetEventHandler()->ProcessEvent(vEvent
));
433 wxFocusEvent
vEvent( wCmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
: wxEVT_SET_FOCUS
437 vEvent
.SetEventObject(this);
438 return(GetEventHandler()->ProcessEvent(vEvent
));
448 } // end of wxSpinCtrl::ProcessTextCommand
450 void wxSpinCtrl::SetFocus()
452 ::WinSetFocus(HWND_DESKTOP
, GetHwnd());
453 } // end of wxSpinCtrl::SetFocus
455 bool wxSpinCtrl::SetFont(
459 if (!wxWindowBase::SetFont(rFont
))
469 } // end of wxSpinCtrl::SetFont
471 void wxSpinCtrl::SetValue(
472 const wxString
& rsText
477 lVal
= atol((char*)rsText
.c_str());
478 wxSpinButton::SetValue(lVal
);
479 } // end of wxSpinCtrl::SetValue
481 bool wxSpinCtrl::Show(
485 if (!wxControl::Show(bShow
))
490 } // end of wxSpinCtrl::Show
492 void wxSpinCtrl::SetSelection (
498 // If from and to are both -1, it means (in wxWidgets) that all text should
499 // be selected - translate into Windows convention
501 if ((lFrom
== -1) && (lTo
== -1))
505 ::WinSendMsg(m_hWnd
, EM_SETSEL
, MPFROM2SHORT((USHORT
)lFrom
, (USHORT
)lTo
), (MPARAM
)0);
506 } // end of wxSpinCtrl::SetSelection
508 #endif //wxUSE_SPINCTRL