]> git.saurik.com Git - wxWidgets.git/blob - src/os2/spinbutt.cpp
use common bottleneck
[wxWidgets.git] / src / os2 / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15
16 #ifndef WX_PRECOMP
17 #include "wx/wx.h"
18 #endif
19 #if wxUSE_SPINBTN
20
21 // Can't resolve reference to CreateUpDownControl in
22 // TWIN32, but could probably use normal CreateWindow instead.
23
24
25 #include "wx/spinbutt.h"
26
27 extern void wxAssociateWinWithHandle( HWND hWnd
28 ,wxWindowOS2* pWin
29 );
30
31 #include "wx/os2/private.h"
32
33 // ============================================================================
34 // implementation
35 // ============================================================================
36
37 // ----------------------------------------------------------------------------
38 // wxWin macros
39 // ----------------------------------------------------------------------------
40
41 bool wxSpinButton::Create(
42 wxWindow* pParent
43 , wxWindowID vId
44 , const wxPoint& rPos
45 , const wxSize& rSize
46 , long lStyle
47 , const wxString& rsName
48 )
49 {
50 int nX = rPos.x;
51 int nY = rPos.y;
52 int nWidth = rSize.x;
53 int nHeight = rSize.y;
54 SWP vSwp;
55
56 m_min = 0;
57 m_max = 100;
58 if (vId == -1)
59 m_windowId = NewControlId();
60 else
61 m_windowId = vId;
62 if (pParent)
63 {
64 m_backgroundColour = pParent->GetBackgroundColour();
65 m_foregroundColour = pParent->GetForegroundColour();
66 }
67 SetName(rsName);
68 SetParent(pParent);
69 m_windowStyle = lStyle;
70
71 //
72 // Get the right size for the control
73 //
74 if (nWidth <= 0 || nHeight <= 0 )
75 {
76 wxSize vSize = DoGetBestSize();
77
78 if (nWidth <= 0 )
79 nWidth = vSize.x;
80 if (nHeight <= 0 )
81 nHeight = vSize.y;
82 }
83 if (nX < 0 )
84 nX = 0;
85 if (nY < 0 )
86 nY = 0;
87
88 long lSstyle = WS_VISIBLE |
89 WS_TABSTOP |
90 SPBS_MASTER | // We use only single field spin buttons
91 SPBS_NUMERICONLY; // We default to numeric data
92
93 if (m_windowStyle & wxCLIP_SIBLINGS )
94 lSstyle |= WS_CLIPSIBLINGS;
95
96 m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent)
97 ,WC_SPINBUTTON
98 ,(PSZ)NULL
99 ,lSstyle
100 ,0L, 0L, 0L, 0L
101 ,GetWinHwnd(pParent)
102 ,HWND_TOP
103 ,(HMENU)vId
104 ,NULL
105 ,NULL
106 );
107 if (m_hWnd == 0)
108 {
109 return FALSE;
110 }
111 SetRange(m_min, m_max);
112 if(pParent)
113 pParent->AddChild((wxSpinButton *)this);
114
115 ::WinQueryWindowPos(m_hWnd, &vSwp);
116 SetXComp(vSwp.x);
117 SetYComp(vSwp.y-5); // compensate for the associated TextControl border
118
119 SetFont(*wxSMALL_FONT);
120 //
121 // For OS/2 we want to hide the text portion so we can substitute an
122 // independent text ctrl in its place.
123 // Therefore we must override any user given width with our best guess.
124 //
125 SetSize( nX - GetXComp()
126 ,nY - GetYComp()
127 ,nWidth
128 ,nHeight
129 );
130 wxAssociateWinWithHandle( m_hWnd
131 ,(wxWindowOS2*)this
132 );
133 #if 0
134 // FIXME:
135 // Apparently, this does not work, as it crashes in setvalue/setrange calls
136 // What's it supposed to do anyway?
137 ::WinSetWindowULong(GetHwnd(), QWL_USER, (LONG)this);
138 fnWndProcSpinCtrl = (WXFARPROC)::WinSubclassWindow(m_hWnd, (PFNWP)wxSpinCtrlWndProc);
139 #endif
140 return TRUE;
141 } // end of wxSpinButton::Create
142
143 wxSpinButton::~wxSpinButton()
144 {
145 } // end of wxSpinButton::~wxSpinButton
146
147 // ----------------------------------------------------------------------------
148 // size calculation
149 // ----------------------------------------------------------------------------
150
151 wxSize wxSpinButton::DoGetBestSize() const
152 {
153 //
154 // OS/2 PM does not really have system metrics so we'll just set it to
155 // a square based on its height.
156 //
157 RECTL vRect;
158 ::WinQueryWindowRect(GetHwnd(),&vRect);
159 return wxSize(vRect.yTop,vRect.yTop);
160 } // end of wxSpinButton::DoGetBestSize
161
162 // ----------------------------------------------------------------------------
163 // Attributes
164 // ----------------------------------------------------------------------------
165
166 int wxSpinButton::GetValue() const
167 {
168 long lVal = 0L;
169 char zVal[10];
170
171 ::WinSendMsg( GetHwnd()
172 ,SPBM_QUERYVALUE
173 ,MPFROMP(zVal)
174 ,MPFROM2SHORT( (USHORT)10
175 ,SPBQ_UPDATEIFVALID
176 )
177 );
178 lVal = atol(zVal);
179 return ((int)lVal);
180 } // end of wxSpinButton::GetValue
181
182 bool wxSpinButton::OS2OnScroll( int WXUNUSED(nOrientation),
183 WXWORD WXUNUSED(wParam),
184 WXWORD wPos,
185 WXHWND hControl )
186 {
187 wxCHECK_MSG(hControl, false, wxT("scrolling what?") );
188
189 wxSpinEvent vEvent( wxEVT_SCROLL_THUMBTRACK, m_windowId );
190 int nVal = (int)wPos; // cast is important for negative values!
191
192 vEvent.SetPosition(nVal);
193 vEvent.SetEventObject(this);
194 return(HandleWindowEvent(vEvent));
195 } // end of wxSpinButton::OS2OnScroll
196
197 bool wxSpinButton::OS2Command( WXUINT WXUNUSED(uCmd),
198 WXWORD WXUNUSED(wId) )
199 {
200 return false;
201 } // end of wxSpinButton::OS2Command
202
203 void wxSpinButton::SetRange(
204 int nMinVal
205 , int nMaxVal
206 )
207 {
208 m_min = nMinVal;
209 m_max = nMaxVal;
210
211 ::WinSendMsg( GetHwnd()
212 ,SPBM_SETLIMITS
213 ,MPFROMLONG(nMaxVal)
214 ,MPFROMLONG(nMinVal)
215 );
216 } // end of wxSpinButton::SetRange
217
218 void wxSpinButton::SetValue(
219 int nValue
220 )
221 {
222 ::WinSendMsg(GetHwnd(), SPBM_SETCURRENTVALUE, MPFROMLONG(nValue), MPARAM(0));
223 } // end of wxSpinButton::SetValue
224
225 #endif //wxUSE_SPINBTN