Lots of fixes for scrolling
[wxWidgets.git] / src / os2 / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: spinbutt.cpp
3 // Purpose: wxSpinButton
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/15/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "spinbutt.h"
14 #pragma implementation "spinbutbase.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24 #if wxUSE_SPINBTN
25
26 // Can't resolve reference to CreateUpDownControl in
27 // TWIN32, but could probably use normal CreateWindow instead.
28
29
30 #include "wx/spinbutt.h"
31
32 extern void wxAssociateWinWithHandle( HWND hWnd
33 ,wxWindowOS2* pWin
34 );
35 static WXFARPROC fnWndProcSpinCtrl = (WXFARPROC)NULL;
36
37 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
38
39 #include "wx/os2/private.h"
40
41 // ============================================================================
42 // implementation
43 // ============================================================================
44
45 // ----------------------------------------------------------------------------
46 // wxWin macros
47 // ----------------------------------------------------------------------------
48
49 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl)
50
51 bool wxSpinButton::Create(
52 wxWindow* pParent
53 , wxWindowID vId
54 , const wxPoint& rPos
55 , const wxSize& rSize
56 , long lStyle
57 , const wxString& rsName
58 )
59 {
60 int nX = rPos.x;
61 int nY = rPos.y;
62 int nWidth = rSize.x;
63 int nHeight = rSize.y;
64 SWP vSwp;
65
66 m_min = 0;
67 m_max = 100;
68 if (vId == -1)
69 m_windowId = NewControlId();
70 else
71 m_windowId = vId;
72 m_backgroundColour = pParent->GetBackgroundColour();
73 m_foregroundColour = pParent->GetForegroundColour();
74 SetName(rsName);
75 SetParent(pParent);
76 m_windowStyle = lStyle;
77
78 //
79 // Get the right size for the control
80 //
81 if (nWidth <= 0 || nHeight <= 0 )
82 {
83 wxSize vSize = DoGetBestSize();
84
85 if (nWidth <= 0 )
86 nWidth = vSize.x;
87 if (nHeight <= 0 )
88 nHeight = vSize.y;
89 }
90 if (nX < 0 )
91 nX = 0;
92 if (nY < 0 )
93 nY = 0;
94
95 long lSstyle = 0L;
96
97 lSstyle = WS_VISIBLE |
98 WS_TABSTOP |
99 SPBS_MASTER | // We use only single field spin buttons
100 SPBS_NUMERICONLY; // We default to numeric data
101
102 if (m_windowStyle & wxCLIP_SIBLINGS )
103 lSstyle |= WS_CLIPSIBLINGS;
104
105 SPBCDATA vCtrlData;
106
107 vCtrlData.cbSize = sizeof(SPBCDATA);
108 vCtrlData.ulTextLimit = 10L;
109 vCtrlData.lLowerLimit = 0L;
110 vCtrlData.lUpperLimit = 100L;
111 vCtrlData.idMasterSpb = vId;
112 vCtrlData.pHWXCtlData = NULL;
113
114 m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent)
115 ,WC_SPINBUTTON
116 ,(PSZ)NULL
117 ,lSstyle
118 ,0L, 0L, 0L, 0L
119 ,GetWinHwnd(pParent)
120 ,HWND_TOP
121 ,(HMENU)vId
122 ,(PVOID)&vCtrlData
123 ,NULL
124 );
125 if (m_hWnd == 0)
126 {
127 return FALSE;
128 }
129 if(pParent)
130 pParent->AddChild((wxSpinButton *)this);
131
132 ::WinQueryWindowPos(m_hWnd, &vSwp);
133 SetXComp(vSwp.x);
134 SetYComp(vSwp.y);
135 wxFont* pTextFont = new wxFont( 10
136 ,wxMODERN
137 ,wxNORMAL
138 ,wxNORMAL
139 );
140 SetFont(*pTextFont);
141 //
142 // For OS/2 we want to hide the text portion so we can substitute an
143 // independent text ctrl in its place. 10 device units does this
144 //
145 SetSize( nX
146 ,nY
147 ,10L
148 ,nHeight
149 );
150 wxAssociateWinWithHandle( m_hWnd
151 ,(wxWindowOS2*)this
152 );
153 ::WinSetWindowULong(GetHwnd(), QWL_USER, (LONG)this);
154 fnWndProcSpinCtrl = (WXFARPROC)::WinSubclassWindow(m_hWnd, (PFNWP)wxSpinCtrlWndProc);
155 delete pTextFont;
156 return TRUE;
157 } // end of wxSpinButton::Create
158
159 wxSpinButton::~wxSpinButton()
160 {
161 } // end of wxSpinButton::~wxSpinButton
162
163 // ----------------------------------------------------------------------------
164 // size calculation
165 // ----------------------------------------------------------------------------
166
167 wxSize wxSpinButton::DoGetBestSize() const
168 {
169 //
170 // OS/2 PM does not really have system metrics so we'll just set our best guess
171 // Also we have no horizontal spin buttons.
172 //
173 return (wxSize(10,20));
174 } // end of wxSpinButton::DoGetBestSize
175
176 // ----------------------------------------------------------------------------
177 // Attributes
178 // ----------------------------------------------------------------------------
179
180 int wxSpinButton::GetValue() const
181 {
182 int nVal = 0;
183 long lVal = 0L;
184 char zVal[10];
185
186 ::WinSendMsg( GetHwnd()
187 ,SPBM_QUERYVALUE
188 ,MPFROMP(zVal)
189 ,MPFROM2SHORT( (USHORT)10
190 ,SPBQ_UPDATEIFVALID
191 )
192 );
193 lVal = atol(zVal);
194 return ((int)lVal);
195 } // end of wxSpinButton::GetValue
196
197 bool wxSpinButton::OS2OnScroll(
198 int nOrientation
199 , WXWORD wParam
200 , WXWORD wPos
201 , WXHWND hControl
202 )
203 {
204 wxCHECK_MSG(hControl, FALSE, wxT("scrolling what?") )
205
206 wxSpinEvent vEvent( wxEVT_SCROLL_THUMBTRACK
207 ,m_windowId
208 );
209 int nVal = (int)wPos; // cast is important for negative values!
210
211 vEvent.SetPosition(nVal);
212 vEvent.SetEventObject(this);
213 return(GetEventHandler()->ProcessEvent(vEvent));
214 } // end of wxSpinButton::OS2OnScroll
215
216 bool wxSpinButton::OS2Command(
217 WXUINT uCmd
218 , WXWORD wId
219 )
220 {
221 return FALSE;
222 } // end of wxSpinButton::OS2Command
223
224 void wxSpinButton::SetRange(
225 int nMinVal
226 , int nMaxVal
227 )
228 {
229 m_min = nMinVal;
230 m_max = nMaxVal;
231
232 ::WinSendMsg( GetHwnd()
233 ,SPBM_SETLIMITS
234 ,MPFROMLONG(nMaxVal)
235 ,MPFROMLONG(nMinVal)
236 );
237 } // end of wxSpinButton::SetRange
238
239 void wxSpinButton::SetValue(
240 int nValue
241 )
242 {
243 ::WinSendMsg(GetHwnd(), SPBM_SETCURRENTVALUE, MPFROMLONG(nValue), MPARAM(0));
244 } // end of wxSpinButton::SetValue
245
246 #endif //wxUSE_SPINBTN