Remove identic implementations of wxDir::Exists from platform-specific files and...
[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 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
44
45 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
46 EVT_CHAR(wxSpinCtrl::OnChar)
47 EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange)
48 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
49 END_EVENT_TABLE()
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 // the margin between the up-down control and its buddy
55 static const int MARGIN_BETWEEN = 5;
56
57 // ============================================================================
58 // implementation
59 // ============================================================================
60 MRESULT EXPENTRY wxSpinCtrlWndProc(
61 HWND hWnd
62 , UINT uMessage
63 , MPARAM wParam
64 , MPARAM lParam
65 )
66 {
67 wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( hWnd
68 ,QWL_USER
69 );
70
71 //
72 // Forward some messages (the key ones only so far) to the spin ctrl
73 //
74 switch (uMessage )
75 {
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;
91
92 }
93 return (fnWndProcSpinCtrl( hWnd
94 ,(ULONG)uMessage
95 ,(MPARAM)wParam
96 ,(MPARAM)lParam
97 )
98 );
99 } // end of wxSpinCtrlWndProc
100
101 wxSpinCtrl::~wxSpinCtrl()
102 {
103 m_svAllSpins.Remove(this);
104
105 // This removes spurious memory leak reporting
106 if (m_svAllSpins.GetCount() == 0)
107 m_svAllSpins.Clear();
108 } // end of wxSpinCtrl::~wxSpinCtrl
109
110 // ----------------------------------------------------------------------------
111 // construction
112 // ----------------------------------------------------------------------------
113
114 bool 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 )
124 {
125 if (vId == wxID_ANY)
126 m_windowId = NewControlId();
127 else
128 m_windowId = vId;
129 if (pParent)
130 {
131 m_backgroundColour = pParent->GetBackgroundColour();
132 m_foregroundColour = pParent->GetForegroundColour();
133 }
134 SetName(rsName);
135 SetParent(pParent);
136 m_windowStyle = lStyle;
137
138 int lSstyle = 0L;
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)
169 {
170 return false;
171 }
172 m_hWndBuddy = m_hWnd; // One in the same for OS/2
173 if(pParent)
174 pParent->AddChild((wxSpinButton *)this);
175
176 SetFont(*wxSMALL_FONT);
177 SetXComp(0);
178 SetYComp(0);
179 SetSize( rPos.x, rPos.y, rSize.x, rSize.y );
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);
193 return true;
194 } // end of wxSpinCtrl::Create
195
196 wxSize wxSpinCtrl::DoGetBestSize() const
197 {
198 wxSize vSizeBtn = wxSpinButton::DoGetBestSize();
199 int nHeight;
200 wxFont vFont = (wxFont)GetFont();
201
202 vSizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
203
204 wxGetCharSize( GetHWND()
205 ,NULL
206 ,&nHeight
207 ,&vFont
208 );
209 nHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight)+4;
210
211 if (vSizeBtn.y < nHeight)
212 {
213 //
214 // Make the text tall enough
215 //
216 vSizeBtn.y = nHeight;
217 }
218 return vSizeBtn;
219 } // end of wxSpinCtrl::DoGetBestSize
220
221 void wxSpinCtrl::DoGetPosition(
222 int* pnX
223 , int* pnY
224 ) const
225 {
226 wxSpinButton::DoGetPosition( pnX,pnY );
227 } // end of wxpinCtrl::DoGetPosition
228
229 void 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
244 void wxSpinCtrl::DoMoveWindow(
245 int nX
246 , int nY
247 , int nWidth
248 , int nHeight
249 )
250 {
251 wxWindowOS2* pParent = (wxWindowOS2*)GetParent();
252
253 if (pParent)
254 {
255 int nOS2Height = GetOS2ParentHeight(pParent);
256
257 nY = nOS2Height - (nY + nHeight);
258 }
259 else
260 {
261 RECTL vRect;
262
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
276 bool wxSpinCtrl::Enable(
277 bool bEnable
278 )
279 {
280 if (!wxControl::Enable(bEnable))
281 {
282 return false;
283 }
284 ::WinEnableWindow(GetHwnd(), bEnable);
285 return true;
286 } // end of wxSpinCtrl::Enable
287
288 wxSpinCtrl* wxSpinCtrl::GetSpinForTextCtrl(
289 WXHWND hWndBuddy
290 )
291 {
292 wxSpinCtrl* pSpin = (wxSpinCtrl *)::WinQueryWindowULong( (HWND)hWndBuddy
293 ,QWL_USER
294 );
295 int i = m_svAllSpins.Index(pSpin);
296
297 if (i == wxNOT_FOUND)
298 return NULL;
299
300 // sanity check
301 wxASSERT_MSG( pSpin->m_hWndBuddy == hWndBuddy,
302 wxT("wxSpinCtrl has incorrect buddy HWND!") );
303
304 return pSpin;
305 } // end of wxSpinCtrl::GetSpinForTextCtrl
306
307 int wxSpinCtrl::GetValue() const
308 {
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 );
319 lVal = atol(zVal);
320 return (int)lVal;
321 } // end of wxSpinCtrl::GetValue
322
323 void wxSpinCtrl::OnChar (
324 wxKeyEvent& rEvent
325 )
326 {
327 switch (rEvent.GetKeyCode())
328 {
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);
337 vEvent.SetString(sVal);
338 vEvent.SetInt(GetValue());
339 if (HandleWindowEvent(vEvent))
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);
357 if (GetParent()->HandleWindowEvent(vEventNav))
358 return;
359 }
360 break;
361 }
362
363 //
364 // No, we didn't process it
365 //
366 rEvent.Skip();
367 } // end of wxSpinCtrl::OnChar
368
369 void wxSpinCtrl::OnSpinChange(
370 wxSpinEvent& rEventSpin
371 )
372 {
373 wxCommandEvent vEvent( wxEVT_COMMAND_SPINCTRL_UPDATED
374 ,GetId()
375 );
376
377 vEvent.SetEventObject(this);
378 vEvent.SetInt(rEventSpin.GetPosition());
379 (void)HandleWindowEvent(vEvent);
380 if (rEventSpin.GetSkipped())
381 {
382 vEvent.Skip();
383 }
384 } // end of wxSpinCtrl::OnSpinChange
385
386 void 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
398 bool wxSpinCtrl::ProcessTextCommand( WXWORD wCmd,
399 WXWORD WXUNUSED(wId) )
400 {
401 switch (wCmd)
402 {
403 case SPBN_CHANGE:
404 {
405 wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
406 vEvent.SetEventObject(this);
407
408 wxString sVal = wxGetWindowText(m_hWndBuddy);
409
410 vEvent.SetString(sVal);
411 vEvent.SetInt(GetValue());
412 return (HandleWindowEvent(vEvent));
413 }
414
415 case SPBN_SETFOCUS:
416 case SPBN_KILLFOCUS:
417 {
418 wxFocusEvent vEvent( wCmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS : wxEVT_SET_FOCUS
419 ,m_windowId
420 );
421
422 vEvent.SetEventObject(this);
423 return(HandleWindowEvent(vEvent));
424 }
425 default:
426 break;
427 }
428
429 //
430 // Not processed
431 //
432 return false;
433 } // end of wxSpinCtrl::ProcessTextCommand
434
435 void wxSpinCtrl::SetFocus()
436 {
437 ::WinSetFocus(HWND_DESKTOP, GetHwnd());
438 } // end of wxSpinCtrl::SetFocus
439
440 bool wxSpinCtrl::SetFont(
441 const wxFont& rFont
442 )
443 {
444 if (!wxWindowBase::SetFont(rFont))
445 {
446 // nothing to do
447 return false;
448 }
449
450 wxOS2SetFont( m_hWnd
451 ,rFont
452 );
453 return true;
454 } // end of wxSpinCtrl::SetFont
455
456 void wxSpinCtrl::SetValue(
457 const wxString& rsText
458 )
459 {
460 long lVal;
461
462 lVal = atol(rsText.c_str());
463 wxSpinButton::SetValue(lVal);
464 } // end of wxSpinCtrl::SetValue
465
466 bool wxSpinCtrl::Show(
467 bool bShow
468 )
469 {
470 if (!wxControl::Show(bShow))
471 {
472 return false;
473 }
474 return true;
475 } // end of wxSpinCtrl::Show
476
477 void wxSpinCtrl::SetSelection (
478 long lFrom
479 , long lTo
480 )
481 {
482 //
483 // If from and to are both -1, it means (in wxWidgets) that all text should
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
493 #endif //wxUSE_SPINCTRL