1 /////////////////////////////////////////////////////////////////////////////
2 // Name: os2/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for OS/2
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);
175 SetFont(*wxSMALL_FONT
);
184 SetRange(nMin
, nMax
);
188 // For OS/2 we'll just set our handle into our long data
190 wxAssociateWinWithHandle( m_hWnd
193 ::WinSetWindowULong(GetHwnd(), QWL_USER
, (LONG
)this);
194 fnWndProcSpinCtrl
= (WXFARPROC
)::WinSubclassWindow(m_hWnd
, (PFNWP
)wxSpinCtrlWndProc
);
195 m_svAllSpins
.Add(this);
197 } // end of wxSpinCtrl::Create
199 wxSize
wxSpinCtrl::DoGetBestSize() const
201 wxSize vSizeBtn
= wxSpinButton::DoGetBestSize();
203 wxFont vFont
= (wxFont
)GetFont();
205 vSizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
207 wxGetCharSize( GetHWND()
212 nHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight
)+4;
214 if (vSizeBtn
.y
< nHeight
)
217 // Make the text tall enough
219 vSizeBtn
.y
= nHeight
;
222 } // end of wxSpinCtrl::DoGetBestSize
224 void wxSpinCtrl::DoGetPosition(
229 wxSpinButton::DoGetPosition( pnX
,pnY
);
230 } // end of wxpinCtrl::DoGetPosition
232 void wxSpinCtrl::DoGetSize(
239 ::WinQueryWindowRect(GetHwnd(), &vSpinrect
);
242 *pnWidth
= vSpinrect
.xRight
- vSpinrect
.xLeft
;
244 *pnHeight
= vSpinrect
.yTop
- vSpinrect
.yBottom
;
245 } // end of wxSpinCtrl::DoGetSize
247 void wxSpinCtrl::DoMoveWindow(
254 wxWindowOS2
* pParent
= (wxWindowOS2
*)GetParent();
258 int nOS2Height
= GetOS2ParentHeight(pParent
);
260 nY
= nOS2Height
- (nY
+ nHeight
);
266 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
267 nY
= vRect
.yTop
- (nY
+ nHeight
);
269 ::WinSetWindowPos( GetHwnd()
275 ,SWP_SIZE
| SWP_MOVE
| SWP_ZORDER
| SWP_SHOW
277 } // end of wxSpinCtrl::DoMoveWindow
279 bool wxSpinCtrl::Enable(
283 if (!wxControl::Enable(bEnable
))
287 ::WinEnableWindow(GetHwnd(), bEnable
);
289 } // end of wxSpinCtrl::Enable
291 wxSpinCtrl
* wxSpinCtrl::GetSpinForTextCtrl(
295 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( (HWND
)hWndBuddy
298 int i
= m_svAllSpins
.Index(pSpin
);
300 if (i
== wxNOT_FOUND
)
304 wxASSERT_MSG( pSpin
->m_hWndBuddy
== hWndBuddy
,
305 _T("wxSpinCtrl has incorrect buddy HWND!") );
308 } // end of wxSpinCtrl::GetSpinForTextCtrl
310 int wxSpinCtrl::GetValue() const
315 ::WinSendMsg( GetHwnd()
318 ,MPFROM2SHORT( (USHORT
)10
324 } // end of wxSpinCtrl::GetValue
326 void wxSpinCtrl::OnChar (
330 switch (rEvent
.GetKeyCode())
334 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_ENTER
337 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
339 InitCommandEvent(vEvent
);
340 vEvent
.SetString(sVal
);
341 vEvent
.SetInt(GetValue());
342 if (GetEventHandler()->ProcessEvent(vEvent
))
349 // Always produce navigation event - even if we process TAB
350 // ourselves the fact that we got here means that the user code
351 // decided to skip processing of this TAB - probably to let it
352 // do its default job.
355 wxNavigationKeyEvent vEventNav
;
357 vEventNav
.SetDirection(!rEvent
.ShiftDown());
358 vEventNav
.SetWindowChange(rEvent
.ControlDown());
359 vEventNav
.SetEventObject(this);
360 if (GetParent()->GetEventHandler()->ProcessEvent(vEventNav
))
367 // No, we didn't process it
370 } // end of wxSpinCtrl::OnChar
372 void wxSpinCtrl::OnSpinChange(
373 wxSpinEvent
& rEventSpin
376 wxCommandEvent
vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED
380 vEvent
.SetEventObject(this);
381 vEvent
.SetInt(rEventSpin
.GetPosition());
382 (void)GetEventHandler()->ProcessEvent(vEvent
);
383 if (rEventSpin
.GetSkipped())
387 } // end of wxSpinCtrl::OnSpinChange
389 void wxSpinCtrl::OnSetFocus (
394 // When we get focus, give it to our buddy window as it needs it more than
397 ::WinSetFocus(HWND_DESKTOP
, (HWND
)m_hWndBuddy
);
399 } // end of wxSpinCtrl::OnSetFocus
401 bool wxSpinCtrl::ProcessTextCommand( WXWORD wCmd
,
402 WXWORD
WXUNUSED(wId
) )
408 wxCommandEvent
vEvent( wxEVT_COMMAND_TEXT_UPDATED
, GetId() );
409 vEvent
.SetEventObject(this);
411 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
413 vEvent
.SetString(sVal
);
414 vEvent
.SetInt(GetValue());
415 return (GetEventHandler()->ProcessEvent(vEvent
));
421 wxFocusEvent
vEvent( wCmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
: wxEVT_SET_FOCUS
425 vEvent
.SetEventObject(this);
426 return(GetEventHandler()->ProcessEvent(vEvent
));
436 } // end of wxSpinCtrl::ProcessTextCommand
438 void wxSpinCtrl::SetFocus()
440 ::WinSetFocus(HWND_DESKTOP
, GetHwnd());
441 } // end of wxSpinCtrl::SetFocus
443 bool wxSpinCtrl::SetFont(
447 if (!wxWindowBase::SetFont(rFont
))
457 } // end of wxSpinCtrl::SetFont
459 void wxSpinCtrl::SetValue(
460 const wxString
& rsText
465 lVal
= atol((char*)rsText
.c_str());
466 wxSpinButton::SetValue(lVal
);
467 } // end of wxSpinCtrl::SetValue
469 bool wxSpinCtrl::Show(
473 if (!wxControl::Show(bShow
))
478 } // end of wxSpinCtrl::Show
480 void wxSpinCtrl::SetSelection (
486 // If from and to are both -1, it means (in wxWidgets) that all text should
487 // be selected - translate into Windows convention
489 if ((lFrom
== -1) && (lTo
== -1))
493 ::WinSendMsg(m_hWnd
, EM_SETSEL
, MPFROM2SHORT((USHORT
)lFrom
, (USHORT
)lTo
), (MPARAM
)0);
494 } // end of wxSpinCtrl::SetSelection
496 #endif //wxUSE_SPINCTRL