1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxSpinButton
)
44 EVT_CHAR(wxSpinCtrl::OnChar
)
45 EVT_SPIN(wxID_ANY
, wxSpinCtrl::OnSpinChange
)
46 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus
)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // the margin between the up-down control and its buddy
53 static const int MARGIN_BETWEEN
= 5;
55 // ============================================================================
57 // ============================================================================
58 MRESULT EXPENTRY
wxSpinCtrlWndProc(
65 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( hWnd
70 // Forward some messages (the key ones only so far) to the spin ctrl
75 pSpin
->OS2WindowProc( uMessage
81 // The control may have been deleted at this point, so check.
83 if (!(::WinIsWindow(vHabmain
, hWnd
) && ((wxSpinCtrl
*)::WinQueryWindowULong( hWnd
91 return (fnWndProcSpinCtrl( hWnd
97 } // end of wxSpinCtrlWndProc
99 wxSpinCtrl::~wxSpinCtrl()
101 m_svAllSpins
.Remove(this);
103 // This removes spurious memory leak reporting
104 if (m_svAllSpins
.GetCount() == 0)
105 m_svAllSpins
.Clear();
106 } // end of wxSpinCtrl::~wxSpinCtrl
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 bool wxSpinCtrl::Create( wxWindow
* pParent
,
114 const wxString
& WXUNUSED(rsValue
),
121 const wxString
& rsName
)
124 m_windowId
= NewControlId();
129 m_backgroundColour
= pParent
->GetBackgroundColour();
130 m_foregroundColour
= pParent
->GetForegroundColour();
134 m_windowStyle
= lStyle
;
138 lSstyle
= WS_VISIBLE
|
140 SPBS_MASTER
| // We use only single field spin buttons
141 SPBS_NUMERICONLY
; // We default to numeric data
143 if (m_windowStyle
& wxCLIP_SIBLINGS
)
144 lSstyle
|= WS_CLIPSIBLINGS
;
148 vCtrlData
.cbSize
= sizeof(SPBCDATA
);
149 vCtrlData
.ulTextLimit
= 10L;
150 vCtrlData
.lLowerLimit
= 0L;
151 vCtrlData
.lUpperLimit
= 100L;
152 vCtrlData
.idMasterSpb
= vId
;
153 vCtrlData
.pHWXCtlData
= NULL
;
155 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
)
170 m_hWndBuddy
= m_hWnd
; // One in the same for OS/2
172 pParent
->AddChild((wxSpinButton
*)this);
174 SetFont(*wxSMALL_FONT
);
177 SetSize( rPos
.x
, rPos
.y
, rSize
.x
, rSize
.y
);
179 SetRange(nMin
, nMax
);
183 // For OS/2 we'll just set our handle into our long data
185 wxAssociateWinWithHandle( m_hWnd
188 ::WinSetWindowULong(GetHwnd(), QWL_USER
, (LONG
)this);
189 fnWndProcSpinCtrl
= (WXFARPROC
)::WinSubclassWindow(m_hWnd
, (PFNWP
)wxSpinCtrlWndProc
);
190 m_svAllSpins
.Add(this);
192 } // end of wxSpinCtrl::Create
194 wxSize
wxSpinCtrl::DoGetBestSize() const
196 wxSize vSizeBtn
= wxSpinButton::DoGetBestSize();
198 wxFont vFont
= (wxFont
)GetFont();
200 vSizeBtn
.x
+= DEFAULT_ITEM_WIDTH
+ MARGIN_BETWEEN
;
202 wxGetCharSize( GetHWND()
207 nHeight
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight
)+4;
209 if (vSizeBtn
.y
< nHeight
)
212 // Make the text tall enough
214 vSizeBtn
.y
= nHeight
;
217 } // end of wxSpinCtrl::DoGetBestSize
219 void wxSpinCtrl::DoGetPosition(
224 wxSpinButton::DoGetPosition( pnX
,pnY
);
225 } // end of wxpinCtrl::DoGetPosition
227 void wxSpinCtrl::DoGetSize(
234 ::WinQueryWindowRect(GetHwnd(), &vSpinrect
);
237 *pnWidth
= vSpinrect
.xRight
- vSpinrect
.xLeft
;
239 *pnHeight
= vSpinrect
.yTop
- vSpinrect
.yBottom
;
240 } // end of wxSpinCtrl::DoGetSize
242 void wxSpinCtrl::DoMoveWindow(
249 wxWindowOS2
* pParent
= (wxWindowOS2
*)GetParent();
253 int nOS2Height
= GetOS2ParentHeight(pParent
);
255 nY
= nOS2Height
- (nY
+ nHeight
);
261 ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
);
262 nY
= vRect
.yTop
- (nY
+ nHeight
);
264 ::WinSetWindowPos( GetHwnd()
270 ,SWP_SIZE
| SWP_MOVE
| SWP_ZORDER
| SWP_SHOW
272 } // end of wxSpinCtrl::DoMoveWindow
274 bool wxSpinCtrl::Enable(
278 if (!wxControl::Enable(bEnable
))
282 ::WinEnableWindow(GetHwnd(), bEnable
);
284 } // end of wxSpinCtrl::Enable
286 wxSpinCtrl
* wxSpinCtrl::GetSpinForTextCtrl(
290 wxSpinCtrl
* pSpin
= (wxSpinCtrl
*)::WinQueryWindowULong( (HWND
)hWndBuddy
293 int i
= m_svAllSpins
.Index(pSpin
);
295 if (i
== wxNOT_FOUND
)
299 wxASSERT_MSG( pSpin
->m_hWndBuddy
== hWndBuddy
,
300 wxT("wxSpinCtrl has incorrect buddy HWND!") );
303 } // end of wxSpinCtrl::GetSpinForTextCtrl
305 int wxSpinCtrl::GetValue() const
310 ::WinSendMsg( GetHwnd()
313 ,MPFROM2SHORT( (USHORT
)10
319 } // end of wxSpinCtrl::GetValue
321 void wxSpinCtrl::OnChar (
325 switch (rEvent
.GetKeyCode())
329 wxCommandEvent
vEvent( wxEVT_TEXT_ENTER
332 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
334 InitCommandEvent(vEvent
);
335 vEvent
.SetString(sVal
);
336 vEvent
.SetInt(GetValue());
337 if (HandleWindowEvent(vEvent
))
344 // Always produce navigation event - even if we process TAB
345 // ourselves the fact that we got here means that the user code
346 // decided to skip processing of this TAB - probably to let it
347 // do its default job.
350 wxNavigationKeyEvent vEventNav
;
352 vEventNav
.SetDirection(!rEvent
.ShiftDown());
353 vEventNav
.SetWindowChange(rEvent
.ControlDown());
354 vEventNav
.SetEventObject(this);
355 if (GetParent()->HandleWindowEvent(vEventNav
))
362 // No, we didn't process it
365 } // end of wxSpinCtrl::OnChar
367 void wxSpinCtrl::OnSpinChange(
368 wxSpinEvent
& rEventSpin
371 wxCommandEvent
vEvent( wxEVT_SPINCTRL
375 vEvent
.SetEventObject(this);
376 vEvent
.SetInt(rEventSpin
.GetPosition());
377 (void)HandleWindowEvent(vEvent
);
378 if (rEventSpin
.GetSkipped())
382 } // end of wxSpinCtrl::OnSpinChange
384 void wxSpinCtrl::OnSetFocus (
389 // When we get focus, give it to our buddy window as it needs it more than
392 ::WinSetFocus(HWND_DESKTOP
, (HWND
)m_hWndBuddy
);
394 } // end of wxSpinCtrl::OnSetFocus
396 bool wxSpinCtrl::ProcessTextCommand( WXWORD wCmd
,
397 WXWORD
WXUNUSED(wId
) )
403 wxCommandEvent
vEvent( wxEVT_TEXT
, GetId() );
404 vEvent
.SetEventObject(this);
406 wxString sVal
= wxGetWindowText(m_hWndBuddy
);
408 vEvent
.SetString(sVal
);
409 vEvent
.SetInt(GetValue());
410 return (HandleWindowEvent(vEvent
));
416 wxFocusEvent
vEvent( wCmd
== EN_KILLFOCUS
? wxEVT_KILL_FOCUS
: wxEVT_SET_FOCUS
420 vEvent
.SetEventObject(this);
421 return(HandleWindowEvent(vEvent
));
431 } // end of wxSpinCtrl::ProcessTextCommand
433 void wxSpinCtrl::SetFocus()
435 ::WinSetFocus(HWND_DESKTOP
, GetHwnd());
436 } // end of wxSpinCtrl::SetFocus
438 bool wxSpinCtrl::SetFont(
442 if (!wxWindowBase::SetFont(rFont
))
452 } // end of wxSpinCtrl::SetFont
454 void wxSpinCtrl::SetValue(
455 const wxString
& rsText
460 lVal
= atol(rsText
.c_str());
461 wxSpinButton::SetValue(lVal
);
462 } // end of wxSpinCtrl::SetValue
464 bool wxSpinCtrl::Show(
468 if (!wxControl::Show(bShow
))
473 } // end of wxSpinCtrl::Show
475 void wxSpinCtrl::SetSelection (
481 // If from and to are both -1, it means (in wxWidgets) that all text should
482 // be selected - translate into Windows convention
484 if ((lFrom
== -1) && (lTo
== -1))
488 ::WinSendMsg(m_hWnd
, EM_SETSEL
, MPFROM2SHORT((USHORT
)lFrom
, (USHORT
)lTo
), (MPARAM
)0);
489 } // end of wxSpinCtrl::SetSelection
491 #endif //wxUSE_SPINCTRL