OS/2 common controls code
[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
65 m_min = 0;
66 m_max = 100;
67 if (vId == -1)
68 m_windowId = NewControlId();
69 else
70 m_windowId = vId;
71 m_backgroundColour = pParent->GetBackgroundColour();
72 m_foregroundColour = pParent->GetForegroundColour();
73 SetName(rsName);
74 SetParent(pParent);
75 m_windowStyle = lStyle;
76
77 //
78 // Get the right size for the control
79 //
80 if (nWidth <= 0 || nHeight <= 0 )
81 {
82 wxSize vSize = DoGetBestSize();
83
84 if (nWidth <= 0 )
85 nWidth = vSize.x;
86 if (nHeight <= 0 )
87 nHeight = vSize.y;
88 }
89 if (nX < 0 )
90 nX = 0;
91 if (nY < 0 )
92 nY = 0;
93
94 long lSstyle = 0L;
95
96 lSstyle = WS_VISIBLE |
97 WS_TABSTOP |
98 SPBS_MASTER | // We use only single field spin buttons
99 SPBS_NUMERICONLY; // We default to numeric data
100
101 if (m_windowStyle & wxCLIP_SIBLINGS )
102 lSstyle |= WS_CLIPSIBLINGS;
103
104 SPBCDATA vCtrlData;
105
106 vCtrlData.cbSize = sizeof(SPBCDATA);
107 vCtrlData.ulTextLimit = 10L;
108 vCtrlData.lLowerLimit = 0L;
109 vCtrlData.lUpperLimit = 100L;
110 vCtrlData.idMasterSpb = vId;
111 vCtrlData.pHWXCtlData = NULL;
112
113 m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent)
114 ,WC_SPINBUTTON
115 ,(PSZ)NULL
116 ,lSstyle
117 ,0L, 0L, 0L, 0L
118 ,GetWinHwnd(pParent)
119 ,HWND_TOP
120 ,(HMENU)vId
121 ,(PVOID)&vCtrlData
122 ,NULL
123 );
124 if (m_hWnd == 0)
125 {
126 return FALSE;
127 }
128 if(pParent)
129 pParent->AddChild((wxSpinButton *)this);
130
131 SetFont(pParent->GetFont());
132 //
133 // For OS/2 we want to hide the text portion so we can substitute an
134 // independent text ctrl in its place. 10 device units does this
135 //
136 SetSize( nX
137 ,nY
138 ,10L
139 ,nHeight
140 );
141 wxAssociateWinWithHandle( m_hWnd
142 ,(wxWindowOS2*)this
143 );
144 ::WinSetWindowULong(GetHwnd(), QWL_USER, (LONG)this);
145 fnWndProcSpinCtrl = (WXFARPROC)::WinSubclassWindow(m_hWnd, (PFNWP)wxSpinCtrlWndProc);
146 return TRUE;
147 } // end of wxSpinButton::Create
148
149 wxSpinButton::~wxSpinButton()
150 {
151 } // end of wxSpinButton::~wxSpinButton
152
153 // ----------------------------------------------------------------------------
154 // size calculation
155 // ----------------------------------------------------------------------------
156
157 wxSize wxSpinButton::DoGetBestSize() const
158 {
159 //
160 // OS/2 PM does not really have system metrics so we'll just set our best guess
161 // Also we have no horizontal spin buttons.
162 //
163 return (wxSize(10,20));
164 } // end of wxSpinButton::DoGetBestSize
165
166 // ----------------------------------------------------------------------------
167 // Attributes
168 // ----------------------------------------------------------------------------
169
170 int wxSpinButton::GetValue() const
171 {
172 int nVal = 0;
173 long lVal = 0L;
174 char zVal[10];
175
176 ::WinSendMsg( GetHwnd()
177 ,SPBM_QUERYVALUE
178 ,MPFROMP(zVal)
179 ,MPFROM2SHORT( (USHORT)10
180 ,SPBQ_UPDATEIFVALID
181 )
182 );
183 lVal = atol(zVal);
184 return ((int)lVal);
185 } // end of wxSpinButton::GetValue
186
187 bool wxSpinButton::OS2OnScroll(
188 int nOrientation
189 , WXWORD wParam
190 , WXWORD wPos
191 , WXHWND hControl
192 )
193 {
194 wxCHECK_MSG(hControl, FALSE, wxT("scrolling what?") )
195
196 wxSpinEvent vEvent( wxEVT_SCROLL_THUMBTRACK
197 ,m_windowId
198 );
199 int nVal = (int)wPos; // cast is important for negative values!
200
201 vEvent.SetPosition(nVal);
202 vEvent.SetEventObject(this);
203 return(GetEventHandler()->ProcessEvent(vEvent));
204 } // end of wxSpinButton::OS2OnScroll
205
206 bool wxSpinButton::OS2Command(
207 WXUINT uCmd
208 , WXWORD wId
209 )
210 {
211 return FALSE;
212 } // end of wxSpinButton::OS2Command
213
214 void wxSpinButton::SetRange(
215 int nMinVal
216 , int nMaxVal
217 )
218 {
219 m_min = nMinVal;
220 m_max = nMaxVal;
221
222 ::WinSendMsg( GetHwnd()
223 ,SPBM_SETLIMITS
224 ,MPFROMLONG(nMaxVal)
225 ,MPFROMLONG(nMinVal)
226 );
227 } // end of wxSpinButton::SetRange
228
229 void wxSpinButton::SetValue(
230 int nValue
231 )
232 {
233 ::WinSendMsg(GetHwnd(), SPBM_SETCURRENTVALUE, MPFROMLONG(nValue), MPARAM(0));
234 } // end of wxSpinButton::SetValue
235
236 #endif //wxUSE_SPINBTN