]> git.saurik.com Git - wxWidgets.git/blame - src/os2/spinctrl.cpp
miscellaneous small enhancements
[wxWidgets.git] / src / os2 / spinctrl.cpp
CommitLineData
409c9842 1/////////////////////////////////////////////////////////////////////////////
9de10271 2// Name: src/os2/spinctrl.cpp
42782237 3// Purpose: wxSpinCtrl class implementation for OS/2
409c9842
DW
4// Author: David Webster
5// Modified by:
6// Created: 10/15/99
7// RCS-ID: $Id$
8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
409c9842
DW
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
409c9842
DW
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23
24#ifndef WX_PRECOMP
25 #include "wx/wx.h"
26#endif
27
312ebad4 28#if wxUSE_SPINCTRL
7e99520b 29
409c9842
DW
30#include "wx/spinctrl.h"
31#include "wx/os2/private.h"
32
33// ----------------------------------------------------------------------------
34// macros
35// ----------------------------------------------------------------------------
36
3c299c3a
DW
37extern void wxAssociateWinWithHandle( HWND hWnd
38 ,wxWindowOS2* pWin
39 );
40static WXFARPROC fnWndProcSpinCtrl = (WXFARPROC)NULL;
32334453 41wxArraySpins wxSpinCtrl::m_svAllSpins;
3c299c3a 42
3d62dcb6 43IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
409c9842 44
3d62dcb6 45BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
6e348b12 46 EVT_CHAR(wxSpinCtrl::OnChar)
312ebad4 47 EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange)
6e348b12 48 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
3d62dcb6 49END_EVENT_TABLE()
409c9842
DW
50// ----------------------------------------------------------------------------
51// constants
52// ----------------------------------------------------------------------------
53
54// the margin between the up-down control and its buddy
55static const int MARGIN_BETWEEN = 5;
56
57// ============================================================================
58// implementation
59// ============================================================================
3c299c3a
DW
60MRESULT EXPENTRY wxSpinCtrlWndProc(
61 HWND hWnd
62, UINT uMessage
63, MPARAM wParam
64, MPARAM lParam
65)
409c9842 66{
3c299c3a
DW
67 wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( hWnd
68 ,QWL_USER
69 );
9923c37d 70
3c299c3a
DW
71 //
72 // Forward some messages (the key ones only so far) to the spin ctrl
73 //
74 switch (uMessage )
3d62dcb6 75 {
3c299c3a
DW
76 case WM_CHAR:
77 pSpin->OS2WindowProc( uMessage
78 ,wParam
79 ,lParam
80 );
81
82 //
83 // The control may have been deleted at this point, so check.
84 //
85 if (!(::WinIsWindow(vHabmain, hWnd) && ((wxSpinCtrl *)::WinQueryWindowULong( hWnd
86 ,QWL_USER
87 )
88 ) == pSpin))
89 return 0;
90 break;
3d62dcb6 91
409c9842 92 }
3c299c3a
DW
93 return (fnWndProcSpinCtrl( hWnd
94 ,(ULONG)uMessage
95 ,(MPARAM)wParam
96 ,(MPARAM)lParam
97 )
98 );
99} // end of wxSpinCtrlWndProc
100
101wxSpinCtrl::~wxSpinCtrl()
102{
103 m_svAllSpins.Remove(this);
409c9842 104
3c299c3a
DW
105 // This removes spurious memory leak reporting
106 if (m_svAllSpins.GetCount() == 0)
107 m_svAllSpins.Clear();
108} // end of wxSpinCtrl::~wxSpinCtrl
409c9842 109
3c299c3a
DW
110// ----------------------------------------------------------------------------
111// construction
112// ----------------------------------------------------------------------------
113
6670f564
WS
114bool wxSpinCtrl::Create( wxWindow* pParent,
115 wxWindowID vId,
116 const wxString& WXUNUSED(rsValue),
117 const wxPoint& rPos,
118 const wxSize& rSize,
119 long lStyle,
120 int nMin,
121 int nMax,
122 int nInitial,
123 const wxString& rsName )
3c299c3a 124{
312ebad4 125 if (vId == wxID_ANY)
3c299c3a
DW
126 m_windowId = NewControlId();
127 else
128 m_windowId = vId;
129 m_backgroundColour = pParent->GetBackgroundColour();
130 m_foregroundColour = pParent->GetForegroundColour();
131 SetName(rsName);
132 SetParent(pParent);
133 m_windowStyle = lStyle;
134
9de10271 135 int lSstyle = 0L;
3c299c3a
DW
136
137 lSstyle = WS_VISIBLE |
138 WS_TABSTOP |
139 SPBS_MASTER | // We use only single field spin buttons
140 SPBS_NUMERICONLY; // We default to numeric data
141
142 if (m_windowStyle & wxCLIP_SIBLINGS )
143 lSstyle |= WS_CLIPSIBLINGS;
144
145 SPBCDATA vCtrlData;
146
147 vCtrlData.cbSize = sizeof(SPBCDATA);
148 vCtrlData.ulTextLimit = 10L;
149 vCtrlData.lLowerLimit = 0L;
150 vCtrlData.lUpperLimit = 100L;
151 vCtrlData.idMasterSpb = vId;
152 vCtrlData.pHWXCtlData = NULL;
153
154 m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent)
155 ,WC_SPINBUTTON
156 ,(PSZ)NULL
157 ,lSstyle
158 ,0L, 0L, 0L, 0L
159 ,GetWinHwnd(pParent)
160 ,HWND_TOP
161 ,(HMENU)vId
162 ,(PVOID)&vCtrlData
163 ,NULL
164 );
165 if (m_hWnd == 0)
409c9842 166 {
312ebad4 167 return false;
409c9842 168 }
3c299c3a
DW
169 m_hWndBuddy = m_hWnd; // One in the same for OS/2
170 if(pParent)
171 pParent->AddChild((wxSpinButton *)this);
61a028dc
SN
172
173 SetFont(*wxSMALL_FONT);
174 SetXComp(0);
175 SetYComp(0);
9de10271 176 SetSize( rPos.x, rPos.y, rSize.x, rSize.y );
3c299c3a
DW
177
178 SetRange(nMin, nMax);
179 SetValue(nInitial);
180
181 //
182 // For OS/2 we'll just set our handle into our long data
183 //
184 wxAssociateWinWithHandle( m_hWnd
185 ,(wxWindowOS2*)this
186 );
187 ::WinSetWindowULong(GetHwnd(), QWL_USER, (LONG)this);
188 fnWndProcSpinCtrl = (WXFARPROC)::WinSubclassWindow(m_hWnd, (PFNWP)wxSpinCtrlWndProc);
189 m_svAllSpins.Add(this);
312ebad4 190 return true;
3c299c3a 191} // end of wxSpinCtrl::Create
409c9842 192
3c299c3a
DW
193wxSize wxSpinCtrl::DoGetBestSize() const
194{
195 wxSize vSizeBtn = wxSpinButton::DoGetBestSize();
196 int nHeight;
0d598bae 197 wxFont vFont = (wxFont)GetFont();
409c9842 198
3c299c3a 199 vSizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
409c9842 200
3c299c3a
DW
201 wxGetCharSize( GetHWND()
202 ,NULL
203 ,&nHeight
0d598bae 204 ,&vFont
3c299c3a 205 );
61a028dc 206 nHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight)+4;
3d62dcb6 207
3c299c3a 208 if (vSizeBtn.y < nHeight)
3d62dcb6 209 {
3c299c3a
DW
210 //
211 // Make the text tall enough
212 //
213 vSizeBtn.y = nHeight;
3d62dcb6 214 }
3c299c3a
DW
215 return vSizeBtn;
216} // end of wxSpinCtrl::DoGetBestSize
3d62dcb6 217
3c299c3a
DW
218void wxSpinCtrl::DoGetPosition(
219 int* pnX
220, int* pnY
221) const
222{
61a028dc 223 wxSpinButton::DoGetPosition( pnX,pnY );
3c299c3a
DW
224} // end of wxpinCtrl::DoGetPosition
225
226void wxSpinCtrl::DoGetSize(
227 int* pnWidth
228, int* pnHeight
229) const
230{
231 RECTL vSpinrect;
232
233 ::WinQueryWindowRect(GetHwnd(), &vSpinrect);
234
235 if (pnWidth)
236 *pnWidth = vSpinrect.xRight - vSpinrect.xLeft;
237 if (pnHeight)
238 *pnHeight = vSpinrect.yTop - vSpinrect.yBottom;
239} // end of wxSpinCtrl::DoGetSize
240
241void wxSpinCtrl::DoMoveWindow(
242 int nX
243, int nY
244, int nWidth
245, int nHeight
246)
247{
248 wxWindowOS2* pParent = (wxWindowOS2*)GetParent();
3d62dcb6 249
3c299c3a 250 if (pParent)
3d62dcb6 251 {
d8a3f66c
DW
252 int nOS2Height = GetOS2ParentHeight(pParent);
253
254 nY = nOS2Height - (nY + nHeight);
3d62dcb6 255 }
3c299c3a
DW
256 else
257 {
258 RECTL vRect;
3d62dcb6 259
3c299c3a
DW
260 ::WinQueryWindowRect(HWND_DESKTOP, &vRect);
261 nY = vRect.yTop - (nY + nHeight);
262 }
263 ::WinSetWindowPos( GetHwnd()
264 ,HWND_TOP
265 ,nX
266 ,nY
267 ,nWidth
268 ,nHeight
269 ,SWP_SIZE | SWP_MOVE | SWP_ZORDER | SWP_SHOW
270 );
271} // end of wxSpinCtrl::DoMoveWindow
272
273bool wxSpinCtrl::Enable(
274 bool bEnable
275)
3d62dcb6 276{
3c299c3a 277 if (!wxControl::Enable(bEnable))
3d62dcb6 278 {
312ebad4 279 return false;
3d62dcb6 280 }
3c299c3a 281 ::WinEnableWindow(GetHwnd(), bEnable);
312ebad4 282 return true;
3c299c3a 283} // end of wxSpinCtrl::Enable
3d62dcb6 284
3c299c3a
DW
285wxSpinCtrl* wxSpinCtrl::GetSpinForTextCtrl(
286 WXHWND hWndBuddy
287)
3d62dcb6 288{
3c299c3a
DW
289 wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( (HWND)hWndBuddy
290 ,QWL_USER
291 );
292 int i = m_svAllSpins.Index(pSpin);
3d62dcb6 293
3c299c3a
DW
294 if (i == wxNOT_FOUND)
295 return NULL;
3d62dcb6 296
3c299c3a
DW
297 // sanity check
298 wxASSERT_MSG( pSpin->m_hWndBuddy == hWndBuddy,
299 _T("wxSpinCtrl has incorrect buddy HWND!") );
3d62dcb6 300
3c299c3a
DW
301 return pSpin;
302} // end of wxSpinCtrl::GetSpinForTextCtrl
3d62dcb6 303
3c299c3a 304int wxSpinCtrl::GetValue() const
3d62dcb6 305{
3c299c3a
DW
306 long lVal = 0L;
307 char zVal[10];
308
309 ::WinSendMsg( GetHwnd()
310 ,SPBM_QUERYVALUE
311 ,MPFROMP(zVal)
312 ,MPFROM2SHORT( (USHORT)10
313 ,SPBQ_UPDATEIFVALID
314 )
315 );
70a2c656
DW
316 lVal = atol(zVal);
317 return (int)lVal;
3c299c3a
DW
318} // end of wxSpinCtrl::GetValue
319
320void wxSpinCtrl::OnChar (
321 wxKeyEvent& rEvent
322)
323{
9923c37d 324 switch (rEvent.GetKeyCode())
3d62dcb6 325 {
3c299c3a
DW
326 case WXK_RETURN:
327 {
328 wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_ENTER
329 ,m_windowId
330 );
331 wxString sVal = wxGetWindowText(m_hWndBuddy);
332
333 InitCommandEvent(vEvent);
0fba44b4 334 vEvent.SetString(sVal);
3c299c3a 335 vEvent.SetInt(GetValue());
937013e0 336 if (HandleWindowEvent(vEvent))
3c299c3a
DW
337 return;
338 break;
339 }
340
341 case WXK_TAB:
342 //
343 // Always produce navigation event - even if we process TAB
344 // ourselves the fact that we got here means that the user code
345 // decided to skip processing of this TAB - probably to let it
346 // do its default job.
347 //
348 {
349 wxNavigationKeyEvent vEventNav;
350
351 vEventNav.SetDirection(!rEvent.ShiftDown());
352 vEventNav.SetWindowChange(rEvent.ControlDown());
353 vEventNav.SetEventObject(this);
937013e0 354 if (GetParent()->HandleWindowEvent(vEventNav))
3c299c3a
DW
355 return;
356 }
357 break;
3d62dcb6
DW
358 }
359
3c299c3a
DW
360 //
361 // No, we didn't process it
362 //
363 rEvent.Skip();
364} // end of wxSpinCtrl::OnChar
3d62dcb6 365
3c299c3a
DW
366void wxSpinCtrl::OnSpinChange(
367 wxSpinEvent& rEventSpin
368)
3d62dcb6 369{
3c299c3a
DW
370 wxCommandEvent vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED
371 ,GetId()
372 );
373
374 vEvent.SetEventObject(this);
375 vEvent.SetInt(rEventSpin.GetPosition());
937013e0 376 (void)HandleWindowEvent(vEvent);
3c299c3a 377 if (rEventSpin.GetSkipped())
3d62dcb6 378 {
3c299c3a 379 vEvent.Skip();
3d62dcb6 380 }
3c299c3a 381} // end of wxSpinCtrl::OnSpinChange
3d62dcb6 382
6e348b12
DW
383void wxSpinCtrl::OnSetFocus (
384 wxFocusEvent& rEvent
385)
386{
387 //
388 // When we get focus, give it to our buddy window as it needs it more than
389 // we do
390 //
391 ::WinSetFocus(HWND_DESKTOP, (HWND)m_hWndBuddy);
392 rEvent.Skip();
393} // end of wxSpinCtrl::OnSetFocus
394
6670f564
WS
395bool wxSpinCtrl::ProcessTextCommand( WXWORD wCmd,
396 WXWORD WXUNUSED(wId) )
3d62dcb6 397{
3c299c3a 398 switch (wCmd)
3d62dcb6 399 {
3c299c3a
DW
400 case SPBN_CHANGE:
401 {
6670f564 402 wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
3c299c3a
DW
403 vEvent.SetEventObject(this);
404
6670f564 405 wxString sVal = wxGetWindowText(m_hWndBuddy);
3c299c3a 406
0fba44b4 407 vEvent.SetString(sVal);
3c299c3a 408 vEvent.SetInt(GetValue());
937013e0 409 return (HandleWindowEvent(vEvent));
3c299c3a
DW
410 }
411
412 case SPBN_SETFOCUS:
413 case SPBN_KILLFOCUS:
414 {
6670f564
WS
415 wxFocusEvent vEvent( wCmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS : wxEVT_SET_FOCUS
416 ,m_windowId
417 );
3c299c3a
DW
418
419 vEvent.SetEventObject(this);
937013e0 420 return(HandleWindowEvent(vEvent));
3c299c3a
DW
421 }
422 default:
423 break;
3d62dcb6
DW
424 }
425
3c299c3a
DW
426 //
427 // Not processed
428 //
312ebad4 429 return false;
3c299c3a 430} // end of wxSpinCtrl::ProcessTextCommand
3d62dcb6 431
3c299c3a 432void wxSpinCtrl::SetFocus()
3d62dcb6 433{
3c299c3a
DW
434 ::WinSetFocus(HWND_DESKTOP, GetHwnd());
435} // end of wxSpinCtrl::SetFocus
3d62dcb6 436
3c299c3a
DW
437bool wxSpinCtrl::SetFont(
438 const wxFont& rFont
439)
440{
441 if (!wxWindowBase::SetFont(rFont))
3d62dcb6 442 {
3c299c3a 443 // nothing to do
312ebad4 444 return false;
3d62dcb6 445 }
3d62dcb6 446
3c299c3a
DW
447 wxOS2SetFont( m_hWnd
448 ,rFont
449 );
312ebad4 450 return true;
3c299c3a 451} // end of wxSpinCtrl::SetFont
409c9842 452
3c299c3a
DW
453void wxSpinCtrl::SetValue(
454 const wxString& rsText
455)
3d62dcb6 456{
3c299c3a 457 long lVal;
3d62dcb6 458
a8988cb3 459 lVal = atol(rsText.c_str());
3c299c3a
DW
460 wxSpinButton::SetValue(lVal);
461} // end of wxSpinCtrl::SetValue
3d62dcb6 462
3c299c3a
DW
463bool wxSpinCtrl::Show(
464 bool bShow
465)
409c9842 466{
3c299c3a 467 if (!wxControl::Show(bShow))
409c9842 468 {
312ebad4 469 return false;
409c9842 470 }
312ebad4 471 return true;
3c299c3a 472} // end of wxSpinCtrl::Show
7e99520b 473
cfcebdb1
DW
474void wxSpinCtrl::SetSelection (
475 long lFrom
476, long lTo
477)
478{
479 //
77ffb593 480 // If from and to are both -1, it means (in wxWidgets) that all text should
cfcebdb1
DW
481 // be selected - translate into Windows convention
482 //
483 if ((lFrom == -1) && (lTo == -1))
484 {
485 lFrom = 0;
486 }
487 ::WinSendMsg(m_hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), (MPARAM)0);
488} // end of wxSpinCtrl::SetSelection
489
312ebad4 490#endif //wxUSE_SPINCTRL