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