]> git.saurik.com Git - wxWidgets.git/blame - src/os2/spinctrl.cpp
Fix harmless MSVC warning about int to bool conversion in Scintilla.
[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;
adfe3164
SN
129 if (pParent)
130 {
131 m_backgroundColour = pParent->GetBackgroundColour();
132 m_foregroundColour = pParent->GetForegroundColour();
133 }
3c299c3a
DW
134 SetName(rsName);
135 SetParent(pParent);
136 m_windowStyle = lStyle;
137
9de10271 138 int lSstyle = 0L;
3c299c3a
DW
139
140 lSstyle = WS_VISIBLE |
141 WS_TABSTOP |
142 SPBS_MASTER | // We use only single field spin buttons
143 SPBS_NUMERICONLY; // We default to numeric data
144
145 if (m_windowStyle & wxCLIP_SIBLINGS )
146 lSstyle |= WS_CLIPSIBLINGS;
147
148 SPBCDATA vCtrlData;
149
150 vCtrlData.cbSize = sizeof(SPBCDATA);
151 vCtrlData.ulTextLimit = 10L;
152 vCtrlData.lLowerLimit = 0L;
153 vCtrlData.lUpperLimit = 100L;
154 vCtrlData.idMasterSpb = vId;
155 vCtrlData.pHWXCtlData = NULL;
156
157 m_hWnd = (WXHWND)::WinCreateWindow( GetWinHwnd(pParent)
158 ,WC_SPINBUTTON
159 ,(PSZ)NULL
160 ,lSstyle
161 ,0L, 0L, 0L, 0L
162 ,GetWinHwnd(pParent)
163 ,HWND_TOP
164 ,(HMENU)vId
165 ,(PVOID)&vCtrlData
166 ,NULL
167 );
168 if (m_hWnd == 0)
409c9842 169 {
312ebad4 170 return false;
409c9842 171 }
3c299c3a
DW
172 m_hWndBuddy = m_hWnd; // One in the same for OS/2
173 if(pParent)
174 pParent->AddChild((wxSpinButton *)this);
61a028dc
SN
175
176 SetFont(*wxSMALL_FONT);
177 SetXComp(0);
178 SetYComp(0);
9de10271 179 SetSize( rPos.x, rPos.y, rSize.x, rSize.y );
3c299c3a
DW
180
181 SetRange(nMin, nMax);
182 SetValue(nInitial);
183
184 //
185 // For OS/2 we'll just set our handle into our long data
186 //
187 wxAssociateWinWithHandle( m_hWnd
188 ,(wxWindowOS2*)this
189 );
190 ::WinSetWindowULong(GetHwnd(), QWL_USER, (LONG)this);
191 fnWndProcSpinCtrl = (WXFARPROC)::WinSubclassWindow(m_hWnd, (PFNWP)wxSpinCtrlWndProc);
192 m_svAllSpins.Add(this);
312ebad4 193 return true;
3c299c3a 194} // end of wxSpinCtrl::Create
409c9842 195
3c299c3a
DW
196wxSize wxSpinCtrl::DoGetBestSize() const
197{
198 wxSize vSizeBtn = wxSpinButton::DoGetBestSize();
199 int nHeight;
0d598bae 200 wxFont vFont = (wxFont)GetFont();
409c9842 201
3c299c3a 202 vSizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
409c9842 203
3c299c3a
DW
204 wxGetCharSize( GetHWND()
205 ,NULL
206 ,&nHeight
0d598bae 207 ,&vFont
3c299c3a 208 );
61a028dc 209 nHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight)+4;
3d62dcb6 210
3c299c3a 211 if (vSizeBtn.y < nHeight)
3d62dcb6 212 {
3c299c3a
DW
213 //
214 // Make the text tall enough
215 //
216 vSizeBtn.y = nHeight;
3d62dcb6 217 }
3c299c3a
DW
218 return vSizeBtn;
219} // end of wxSpinCtrl::DoGetBestSize
3d62dcb6 220
3c299c3a
DW
221void wxSpinCtrl::DoGetPosition(
222 int* pnX
223, int* pnY
224) const
225{
61a028dc 226 wxSpinButton::DoGetPosition( pnX,pnY );
3c299c3a
DW
227} // end of wxpinCtrl::DoGetPosition
228
229void wxSpinCtrl::DoGetSize(
230 int* pnWidth
231, int* pnHeight
232) const
233{
234 RECTL vSpinrect;
235
236 ::WinQueryWindowRect(GetHwnd(), &vSpinrect);
237
238 if (pnWidth)
239 *pnWidth = vSpinrect.xRight - vSpinrect.xLeft;
240 if (pnHeight)
241 *pnHeight = vSpinrect.yTop - vSpinrect.yBottom;
242} // end of wxSpinCtrl::DoGetSize
243
244void wxSpinCtrl::DoMoveWindow(
245 int nX
246, int nY
247, int nWidth
248, int nHeight
249)
250{
251 wxWindowOS2* pParent = (wxWindowOS2*)GetParent();
3d62dcb6 252
3c299c3a 253 if (pParent)
3d62dcb6 254 {
d8a3f66c
DW
255 int nOS2Height = GetOS2ParentHeight(pParent);
256
257 nY = nOS2Height - (nY + nHeight);
3d62dcb6 258 }
3c299c3a
DW
259 else
260 {
261 RECTL vRect;
3d62dcb6 262
3c299c3a
DW
263 ::WinQueryWindowRect(HWND_DESKTOP, &vRect);
264 nY = vRect.yTop - (nY + nHeight);
265 }
266 ::WinSetWindowPos( GetHwnd()
267 ,HWND_TOP
268 ,nX
269 ,nY
270 ,nWidth
271 ,nHeight
272 ,SWP_SIZE | SWP_MOVE | SWP_ZORDER | SWP_SHOW
273 );
274} // end of wxSpinCtrl::DoMoveWindow
275
276bool wxSpinCtrl::Enable(
277 bool bEnable
278)
3d62dcb6 279{
3c299c3a 280 if (!wxControl::Enable(bEnable))
3d62dcb6 281 {
312ebad4 282 return false;
3d62dcb6 283 }
3c299c3a 284 ::WinEnableWindow(GetHwnd(), bEnable);
312ebad4 285 return true;
3c299c3a 286} // end of wxSpinCtrl::Enable
3d62dcb6 287
3c299c3a
DW
288wxSpinCtrl* wxSpinCtrl::GetSpinForTextCtrl(
289 WXHWND hWndBuddy
290)
3d62dcb6 291{
3c299c3a
DW
292 wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( (HWND)hWndBuddy
293 ,QWL_USER
294 );
295 int i = m_svAllSpins.Index(pSpin);
3d62dcb6 296
3c299c3a
DW
297 if (i == wxNOT_FOUND)
298 return NULL;
3d62dcb6 299
3c299c3a
DW
300 // sanity check
301 wxASSERT_MSG( pSpin->m_hWndBuddy == hWndBuddy,
9a83f860 302 wxT("wxSpinCtrl has incorrect buddy HWND!") );
3d62dcb6 303
3c299c3a
DW
304 return pSpin;
305} // end of wxSpinCtrl::GetSpinForTextCtrl
3d62dcb6 306
3c299c3a 307int wxSpinCtrl::GetValue() const
3d62dcb6 308{
3c299c3a
DW
309 long lVal = 0L;
310 char zVal[10];
311
312 ::WinSendMsg( GetHwnd()
313 ,SPBM_QUERYVALUE
314 ,MPFROMP(zVal)
315 ,MPFROM2SHORT( (USHORT)10
316 ,SPBQ_UPDATEIFVALID
317 )
318 );
70a2c656
DW
319 lVal = atol(zVal);
320 return (int)lVal;
3c299c3a
DW
321} // end of wxSpinCtrl::GetValue
322
323void wxSpinCtrl::OnChar (
324 wxKeyEvent& rEvent
325)
326{
9923c37d 327 switch (rEvent.GetKeyCode())
3d62dcb6 328 {
3c299c3a
DW
329 case WXK_RETURN:
330 {
331 wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_ENTER
332 ,m_windowId
333 );
334 wxString sVal = wxGetWindowText(m_hWndBuddy);
335
336 InitCommandEvent(vEvent);
0fba44b4 337 vEvent.SetString(sVal);
3c299c3a 338 vEvent.SetInt(GetValue());
937013e0 339 if (HandleWindowEvent(vEvent))
3c299c3a
DW
340 return;
341 break;
342 }
343
344 case WXK_TAB:
345 //
346 // Always produce navigation event - even if we process TAB
347 // ourselves the fact that we got here means that the user code
348 // decided to skip processing of this TAB - probably to let it
349 // do its default job.
350 //
351 {
352 wxNavigationKeyEvent vEventNav;
353
354 vEventNav.SetDirection(!rEvent.ShiftDown());
355 vEventNav.SetWindowChange(rEvent.ControlDown());
356 vEventNav.SetEventObject(this);
937013e0 357 if (GetParent()->HandleWindowEvent(vEventNav))
3c299c3a
DW
358 return;
359 }
360 break;
3d62dcb6
DW
361 }
362
3c299c3a
DW
363 //
364 // No, we didn't process it
365 //
366 rEvent.Skip();
367} // end of wxSpinCtrl::OnChar
3d62dcb6 368
3c299c3a
DW
369void wxSpinCtrl::OnSpinChange(
370 wxSpinEvent& rEventSpin
371)
3d62dcb6 372{
3c299c3a
DW
373 wxCommandEvent vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED
374 ,GetId()
375 );
376
377 vEvent.SetEventObject(this);
378 vEvent.SetInt(rEventSpin.GetPosition());
937013e0 379 (void)HandleWindowEvent(vEvent);
3c299c3a 380 if (rEventSpin.GetSkipped())
3d62dcb6 381 {
3c299c3a 382 vEvent.Skip();
3d62dcb6 383 }
3c299c3a 384} // end of wxSpinCtrl::OnSpinChange
3d62dcb6 385
6e348b12
DW
386void wxSpinCtrl::OnSetFocus (
387 wxFocusEvent& rEvent
388)
389{
390 //
391 // When we get focus, give it to our buddy window as it needs it more than
392 // we do
393 //
394 ::WinSetFocus(HWND_DESKTOP, (HWND)m_hWndBuddy);
395 rEvent.Skip();
396} // end of wxSpinCtrl::OnSetFocus
397
6670f564
WS
398bool wxSpinCtrl::ProcessTextCommand( WXWORD wCmd,
399 WXWORD WXUNUSED(wId) )
3d62dcb6 400{
3c299c3a 401 switch (wCmd)
3d62dcb6 402 {
3c299c3a
DW
403 case SPBN_CHANGE:
404 {
6670f564 405 wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
3c299c3a
DW
406 vEvent.SetEventObject(this);
407
6670f564 408 wxString sVal = wxGetWindowText(m_hWndBuddy);
3c299c3a 409
0fba44b4 410 vEvent.SetString(sVal);
3c299c3a 411 vEvent.SetInt(GetValue());
937013e0 412 return (HandleWindowEvent(vEvent));
3c299c3a
DW
413 }
414
415 case SPBN_SETFOCUS:
416 case SPBN_KILLFOCUS:
417 {
6670f564
WS
418 wxFocusEvent vEvent( wCmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS : wxEVT_SET_FOCUS
419 ,m_windowId
420 );
3c299c3a
DW
421
422 vEvent.SetEventObject(this);
937013e0 423 return(HandleWindowEvent(vEvent));
3c299c3a
DW
424 }
425 default:
426 break;
3d62dcb6
DW
427 }
428
3c299c3a
DW
429 //
430 // Not processed
431 //
312ebad4 432 return false;
3c299c3a 433} // end of wxSpinCtrl::ProcessTextCommand
3d62dcb6 434
3c299c3a 435void wxSpinCtrl::SetFocus()
3d62dcb6 436{
3c299c3a
DW
437 ::WinSetFocus(HWND_DESKTOP, GetHwnd());
438} // end of wxSpinCtrl::SetFocus
3d62dcb6 439
3c299c3a
DW
440bool wxSpinCtrl::SetFont(
441 const wxFont& rFont
442)
443{
444 if (!wxWindowBase::SetFont(rFont))
3d62dcb6 445 {
3c299c3a 446 // nothing to do
312ebad4 447 return false;
3d62dcb6 448 }
3d62dcb6 449
3c299c3a
DW
450 wxOS2SetFont( m_hWnd
451 ,rFont
452 );
312ebad4 453 return true;
3c299c3a 454} // end of wxSpinCtrl::SetFont
409c9842 455
3c299c3a
DW
456void wxSpinCtrl::SetValue(
457 const wxString& rsText
458)
3d62dcb6 459{
3c299c3a 460 long lVal;
3d62dcb6 461
a8988cb3 462 lVal = atol(rsText.c_str());
3c299c3a
DW
463 wxSpinButton::SetValue(lVal);
464} // end of wxSpinCtrl::SetValue
3d62dcb6 465
3c299c3a
DW
466bool wxSpinCtrl::Show(
467 bool bShow
468)
409c9842 469{
3c299c3a 470 if (!wxControl::Show(bShow))
409c9842 471 {
312ebad4 472 return false;
409c9842 473 }
312ebad4 474 return true;
3c299c3a 475} // end of wxSpinCtrl::Show
7e99520b 476
cfcebdb1
DW
477void wxSpinCtrl::SetSelection (
478 long lFrom
479, long lTo
480)
481{
482 //
77ffb593 483 // If from and to are both -1, it means (in wxWidgets) that all text should
cfcebdb1
DW
484 // be selected - translate into Windows convention
485 //
486 if ((lFrom == -1) && (lTo == -1))
487 {
488 lFrom = 0;
489 }
490 ::WinSendMsg(m_hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), (MPARAM)0);
491} // end of wxSpinCtrl::SetSelection
492
312ebad4 493#endif //wxUSE_SPINCTRL