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