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