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
76 bool bProccesed
= FALSE
;
77 MRESULT rc
= (MRESULT
)0;
79 // Forward some messages (the key ones only so far) to the spin ctrl
84 pSpin
->OS2WindowProc( uMessage
90 // The control may have been deleted at this point, so check.
92 if (!(::WinIsWindow(vHabmain
, hWnd
) && ((wxSpinCtrl
*)::WinQueryWindowULong( hWnd
100 return (fnWndProcSpinCtrl( hWnd
106 } // end of wxSpinCtrlWndProc
108 wxSpinCtrl::~wxSpinCtrl()
110 m_svAllSpins
.Remove(this);
112 // This removes spurious memory leak reporting
113 if (m_svAllSpins
.GetCount() == 0)
114 m_svAllSpins
.Clear();
115 } // end of wxSpinCtrl::~wxSpinCtrl
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 bool wxSpinCtrl::Create(
124 , const wxString
& rsValue
125 , const wxPoint
& rPos
126 , const wxSize
& rSize
131 , const wxString
& rsName
137 m_windowId
= NewControlId();
140 m_backgroundColour
= pParent
->GetBackgroundColour();
141 m_foregroundColour
= pParent
->GetForegroundColour();
144 m_windowStyle
= lStyle
;
148 lSstyle
= WS_VISIBLE
|
150 SPBS_MASTER
| // We use only single field spin buttons
151 SPBS_NUMERICONLY
; // We default to numeric data
153 if (m_windowStyle
& wxCLIP_SIBLINGS
)
154 lSstyle
|= WS_CLIPSIBLINGS
;
158 vCtrlData
.cbSize
= sizeof(SPBCDATA
);
159 vCtrlData
.ulTextLimit
= 10L;
160 vCtrlData
.lLowerLimit
= 0L;
161 vCtrlData
.lUpperLimit
= 100L;
162 vCtrlData
.idMasterSpb
= vId
;
163 vCtrlData
.pHWXCtlData
= NULL
;
165 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
)
180 m_hWndBuddy
= m_hWnd
; // One in the same for OS/2
182 pParent
->AddChild((wxSpinButton
*)this);
183 wxFont
* pTextFont
= new wxFont( 10
189 ::WinQueryWindowPos(m_hWnd
, &vSwp
);
198 SetRange(nMin
, nMax
);
202 // For OS/2 we'll just set our handle into our long data
204 wxAssociateWinWithHandle( m_hWnd
207 ::WinSetWindowULong(GetHwnd(), QWL_USER
, (LONG
)this);
208 fnWndProcSpinCtrl
= (WXFARPROC
)::WinSubclassWindow(m_hWnd
, (PFNWP
)wxSpinCtrlWndProc
);
209 m_svAllSpins
.Add(this);
212 } // end of wxSpinCtrl::Create
214 wxSize
wxSpinCtrl::DoGetBestSize() const
216 wxSize vSizeBtn
= wxSpinButton::DoGetBestSize();
219 vSizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
221 wxGetCharSize( GetHWND()
226 nHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight
);
228 if (vSizeBtn
.y
< nHeight
)
231 // Make the text tall enough
233 vSizeBtn
.y
= nHeight
;
236 } // end of wxSpinCtrl::DoGetBestSize
238 void wxSpinCtrl::DoGetPosition(
243 WXHWND hWnd
= GetHWND();
245 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= m_hWndBuddy
;
246 wxSpinButton::DoGetPosition( pnX
249 wxConstCast(this, wxSpinCtrl
)->m_hWnd
= hWnd
;
250 } // end of wxpinCtrl::DoGetPosition
252 void wxSpinCtrl::DoGetSize(
259 ::WinQueryWindowRect(GetHwnd(), &vSpinrect
);
262 *pnWidth
= vSpinrect
.xRight
- vSpinrect
.xLeft
;
264 *pnHeight
= vSpinrect
.yTop
- vSpinrect
.yBottom
;
265 } // end of wxSpinCtrl::DoGetSize
267 void wxSpinCtrl::DoMoveWindow(
274 wxWindowOS2
* pParent
= (wxWindowOS2
*)GetParent();
278 int nOS2Height
= GetOS2ParentHeight(pParent
);
280 nY
= nOS2Height
- (nY
+ nHeight
);
286 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
287 nY
= vRect
.yTop
- (nY
+ nHeight
);
289 ::WinSetWindowPos( GetHwnd()
295 ,SWP_SIZE
| SWP_MOVE
| SWP_ZORDER
| SWP_SHOW
297 } // end of wxSpinCtrl::DoMoveWindow
299 bool wxSpinCtrl::Enable(
303 if (!wxControl::Enable(bEnable
))
307 ::WinEnableWindow(GetHwnd(), bEnable
);
309 } // end of wxSpinCtrl::Enable
311 wxSpinCtrl
* wxSpinCtrl::GetSpinForTextCtrl(
315 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( (HWND
)hWndBuddy
318 int i
= m_svAllSpins
.Index(pSpin
);
320 if (i
== wxNOT_FOUND
)
324 wxASSERT_MSG( pSpin
->m_hWndBuddy
== hWndBuddy
,
325 _T("wxSpinCtrl has incorrect buddy HWND!") );
328 } // end of wxSpinCtrl::GetSpinForTextCtrl
330 int wxSpinCtrl::GetValue() const
335 ::WinSendMsg( GetHwnd()
338 ,MPFROM2SHORT( (USHORT
)10
344 } // end of wxSpinCtrl::GetValue
346 void wxSpinCtrl::OnChar (
350 switch (rEvent
.KeyCode())
354 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_ENTER
357 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
359 InitCommandEvent(vEvent
);
360 vEvent
.SetString((char*)sVal
.c_str());
361 vEvent
.SetInt(GetValue());
362 if (GetEventHandler()->ProcessEvent(vEvent
))
369 // Always produce navigation event - even if we process TAB
370 // ourselves the fact that we got here means that the user code
371 // decided to skip processing of this TAB - probably to let it
372 // do its default job.
375 wxNavigationKeyEvent vEventNav
;
377 vEventNav
.SetDirection(!rEvent
.ShiftDown());
378 vEventNav
.SetWindowChange(rEvent
.ControlDown());
379 vEventNav
.SetEventObject(this);
380 if (GetParent()->GetEventHandler()->ProcessEvent(vEventNav
))
387 // No, we didn't process it
390 } // end of wxSpinCtrl::OnChar
392 void wxSpinCtrl::OnSpinChange(
393 wxSpinEvent
& rEventSpin
396 wxCommandEvent
vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED
400 vEvent
.SetEventObject(this);
401 vEvent
.SetInt(rEventSpin
.GetPosition());
402 (void)GetEventHandler()->ProcessEvent(vEvent
);
403 if (rEventSpin
.GetSkipped())
407 } // end of wxSpinCtrl::OnSpinChange
409 void wxSpinCtrl::OnSetFocus (
414 // When we get focus, give it to our buddy window as it needs it more than
417 ::WinSetFocus(HWND_DESKTOP
, (HWND
)m_hWndBuddy
);
419 } // end of wxSpinCtrl::OnSetFocus
421 bool wxSpinCtrl::ProcessTextCommand(
430 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_UPDATED
433 vEvent
.SetEventObject(this);
435 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
437 vEvent
.SetString((char*)sVal
.c_str());
438 vEvent
.SetInt(GetValue());
439 return (GetEventHandler()->ProcessEvent(vEvent
));
445 wxFocusEvent
vEvent( wCmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
: wxEVT_SET_FOCUS
449 vEvent
.SetEventObject(this);
450 return(GetEventHandler()->ProcessEvent(vEvent
));
460 } // end of wxSpinCtrl::ProcessTextCommand
462 void wxSpinCtrl::SetFocus()
464 ::WinSetFocus(HWND_DESKTOP
, GetHwnd());
465 } // end of wxSpinCtrl::SetFocus
467 bool wxSpinCtrl::SetFont(
471 if (!wxWindowBase::SetFont(rFont
))
477 WXHANDLE hFont
= GetFont().GetResourceHandle();
482 } // end of wxSpinCtrl::SetFont
484 void wxSpinCtrl::SetValue(
485 const wxString
& rsText
490 lVal
= atol(rsText
.c_str());
491 wxSpinButton::SetValue(lVal
);
492 } // end of wxSpinCtrl::SetValue
494 bool wxSpinCtrl::Show(
498 if (!wxControl::Show(bShow
))
503 } // end of wxSpinCtrl::Show
505 void wxSpinCtrl::SetSelection (
511 // If from and to are both -1, it means (in wxWindows) that all text should
512 // be selected - translate into Windows convention
514 if ((lFrom
== -1) && (lTo
== -1))
518 ::WinSendMsg(m_hWnd
, EM_SETSEL
, MPFROM2SHORT((USHORT
)lFrom
, (USHORT
)lTo
), (MPARAM
)0);
519 } // end of wxSpinCtrl::SetSelection
521 #endif //wxUSE_SPINBTN