Document domain parameter of wxTranslations::GetTranslatedString().
[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 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #if wxUSE_SPINCTRL
28
29 #include "wx/spinctrl.h"
30 #include "wx/os2/private.h"
31
32 // ----------------------------------------------------------------------------
33 // macros
34 // ----------------------------------------------------------------------------
35
36 extern void wxAssociateWinWithHandle( HWND hWnd
37 ,wxWindowOS2* pWin
38 );
39 static WXFARPROC fnWndProcSpinCtrl = (WXFARPROC)NULL;
40 wxArraySpins wxSpinCtrl::m_svAllSpins;
41
42 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
43 EVT_CHAR(wxSpinCtrl::OnChar)
44 EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange)
45 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
46 END_EVENT_TABLE()
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 // the margin between the up-down control and its buddy
52 static const int MARGIN_BETWEEN = 5;
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57 MRESULT EXPENTRY wxSpinCtrlWndProc(
58 HWND hWnd
59 , UINT uMessage
60 , MPARAM wParam
61 , MPARAM lParam
62 )
63 {
64 wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( hWnd
65 ,QWL_USER
66 );
67
68 //
69 // Forward some messages (the key ones only so far) to the spin ctrl
70 //
71 switch (uMessage )
72 {
73 case WM_CHAR:
74 pSpin->OS2WindowProc( uMessage
75 ,wParam
76 ,lParam
77 );
78
79 //
80 // The control may have been deleted at this point, so check.
81 //
82 if (!(::WinIsWindow(vHabmain, hWnd) && ((wxSpinCtrl *)::WinQueryWindowULong( hWnd
83 ,QWL_USER
84 )
85 ) == pSpin))
86 return 0;
87 break;
88
89 }
90 return (fnWndProcSpinCtrl( hWnd
91 ,(ULONG)uMessage
92 ,(MPARAM)wParam
93 ,(MPARAM)lParam
94 )
95 );
96 } // end of wxSpinCtrlWndProc
97
98 wxSpinCtrl::~wxSpinCtrl()
99 {
100 m_svAllSpins.Remove(this);
101
102 // This removes spurious memory leak reporting
103 if (m_svAllSpins.GetCount() == 0)
104 m_svAllSpins.Clear();
105 } // end of wxSpinCtrl::~wxSpinCtrl
106
107 // ----------------------------------------------------------------------------
108 // construction
109 // ----------------------------------------------------------------------------
110
111 bool wxSpinCtrl::Create( wxWindow* pParent,
112 wxWindowID vId,
113 const wxString& WXUNUSED(rsValue),
114 const wxPoint& rPos,
115 const wxSize& rSize,
116 long lStyle,
117 int nMin,
118 int nMax,
119 int nInitial,
120 const wxString& rsName )
121 {
122 if (vId == wxID_ANY)
123 m_windowId = NewControlId();
124 else
125 m_windowId = vId;
126 if (pParent)
127 {
128 m_backgroundColour = pParent->GetBackgroundColour();
129 m_foregroundColour = pParent->GetForegroundColour();
130 }
131 SetName(rsName);
132 SetParent(pParent);
133 m_windowStyle = lStyle;
134
135 int lSstyle = 0L;
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)
166 {
167 return false;
168 }
169 m_hWndBuddy = m_hWnd; // One in the same for OS/2
170 if(pParent)
171 pParent->AddChild((wxSpinButton *)this);
172
173 SetFont(*wxSMALL_FONT);
174 SetXComp(0);
175 SetYComp(0);
176 SetSize( rPos.x, rPos.y, rSize.x, rSize.y );
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);
190 return true;
191 } // end of wxSpinCtrl::Create
192
193 wxSize wxSpinCtrl::DoGetBestSize() const
194 {
195 wxSize vSizeBtn = wxSpinButton::DoGetBestSize();
196 int nHeight;
197 wxFont vFont = (wxFont)GetFont();
198
199 vSizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
200
201 wxGetCharSize( GetHWND()
202 ,NULL
203 ,&nHeight
204 ,&vFont
205 );
206 nHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight)+4;
207
208 if (vSizeBtn.y < nHeight)
209 {
210 //
211 // Make the text tall enough
212 //
213 vSizeBtn.y = nHeight;
214 }
215 return vSizeBtn;
216 } // end of wxSpinCtrl::DoGetBestSize
217
218 void wxSpinCtrl::DoGetPosition(
219 int* pnX
220 , int* pnY
221 ) const
222 {
223 wxSpinButton::DoGetPosition( pnX,pnY );
224 } // end of wxpinCtrl::DoGetPosition
225
226 void 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
241 void wxSpinCtrl::DoMoveWindow(
242 int nX
243 , int nY
244 , int nWidth
245 , int nHeight
246 )
247 {
248 wxWindowOS2* pParent = (wxWindowOS2*)GetParent();
249
250 if (pParent)
251 {
252 int nOS2Height = GetOS2ParentHeight(pParent);
253
254 nY = nOS2Height - (nY + nHeight);
255 }
256 else
257 {
258 RECTL vRect;
259
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
273 bool wxSpinCtrl::Enable(
274 bool bEnable
275 )
276 {
277 if (!wxControl::Enable(bEnable))
278 {
279 return false;
280 }
281 ::WinEnableWindow(GetHwnd(), bEnable);
282 return true;
283 } // end of wxSpinCtrl::Enable
284
285 wxSpinCtrl* wxSpinCtrl::GetSpinForTextCtrl(
286 WXHWND hWndBuddy
287 )
288 {
289 wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( (HWND)hWndBuddy
290 ,QWL_USER
291 );
292 int i = m_svAllSpins.Index(pSpin);
293
294 if (i == wxNOT_FOUND)
295 return NULL;
296
297 // sanity check
298 wxASSERT_MSG( pSpin->m_hWndBuddy == hWndBuddy,
299 wxT("wxSpinCtrl has incorrect buddy HWND!") );
300
301 return pSpin;
302 } // end of wxSpinCtrl::GetSpinForTextCtrl
303
304 int wxSpinCtrl::GetValue() const
305 {
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 );
316 lVal = atol(zVal);
317 return (int)lVal;
318 } // end of wxSpinCtrl::GetValue
319
320 void wxSpinCtrl::OnChar (
321 wxKeyEvent& rEvent
322 )
323 {
324 switch (rEvent.GetKeyCode())
325 {
326 case WXK_RETURN:
327 {
328 wxCommandEvent vEvent( wxEVT_TEXT_ENTER
329 ,m_windowId
330 );
331 wxString sVal = wxGetWindowText(m_hWndBuddy);
332
333 InitCommandEvent(vEvent);
334 vEvent.SetString(sVal);
335 vEvent.SetInt(GetValue());
336 if (HandleWindowEvent(vEvent))
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);
354 if (GetParent()->HandleWindowEvent(vEventNav))
355 return;
356 }
357 break;
358 }
359
360 //
361 // No, we didn't process it
362 //
363 rEvent.Skip();
364 } // end of wxSpinCtrl::OnChar
365
366 void wxSpinCtrl::OnSpinChange(
367 wxSpinEvent& rEventSpin
368 )
369 {
370 wxCommandEvent vEvent( wxEVT_SPINCTRL
371 ,GetId()
372 );
373
374 vEvent.SetEventObject(this);
375 vEvent.SetInt(rEventSpin.GetPosition());
376 (void)HandleWindowEvent(vEvent);
377 if (rEventSpin.GetSkipped())
378 {
379 vEvent.Skip();
380 }
381 } // end of wxSpinCtrl::OnSpinChange
382
383 void 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
395 bool wxSpinCtrl::ProcessTextCommand( WXWORD wCmd,
396 WXWORD WXUNUSED(wId) )
397 {
398 switch (wCmd)
399 {
400 case SPBN_CHANGE:
401 {
402 wxCommandEvent vEvent( wxEVT_TEXT, GetId() );
403 vEvent.SetEventObject(this);
404
405 wxString sVal = wxGetWindowText(m_hWndBuddy);
406
407 vEvent.SetString(sVal);
408 vEvent.SetInt(GetValue());
409 return (HandleWindowEvent(vEvent));
410 }
411
412 case SPBN_SETFOCUS:
413 case SPBN_KILLFOCUS:
414 {
415 wxFocusEvent vEvent( wCmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS : wxEVT_SET_FOCUS
416 ,m_windowId
417 );
418
419 vEvent.SetEventObject(this);
420 return(HandleWindowEvent(vEvent));
421 }
422 default:
423 break;
424 }
425
426 //
427 // Not processed
428 //
429 return false;
430 } // end of wxSpinCtrl::ProcessTextCommand
431
432 void wxSpinCtrl::SetFocus()
433 {
434 ::WinSetFocus(HWND_DESKTOP, GetHwnd());
435 } // end of wxSpinCtrl::SetFocus
436
437 bool wxSpinCtrl::SetFont(
438 const wxFont& rFont
439 )
440 {
441 if (!wxWindowBase::SetFont(rFont))
442 {
443 // nothing to do
444 return false;
445 }
446
447 wxOS2SetFont( m_hWnd
448 ,rFont
449 );
450 return true;
451 } // end of wxSpinCtrl::SetFont
452
453 void wxSpinCtrl::SetValue(
454 const wxString& rsText
455 )
456 {
457 long lVal;
458
459 lVal = atol(rsText.c_str());
460 wxSpinButton::SetValue(lVal);
461 } // end of wxSpinCtrl::SetValue
462
463 bool wxSpinCtrl::Show(
464 bool bShow
465 )
466 {
467 if (!wxControl::Show(bShow))
468 {
469 return false;
470 }
471 return true;
472 } // end of wxSpinCtrl::Show
473
474 void wxSpinCtrl::SetSelection (
475 long lFrom
476 , long lTo
477 )
478 {
479 //
480 // If from and to are both -1, it means (in wxWidgets) that all text should
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
490 #endif //wxUSE_SPINCTRL