]> git.saurik.com Git - wxWidgets.git/blob - src/os2/spinctrl.cpp
don't reset the selection after event was vetoed if there is no old selection
[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 m_backgroundColour = pParent->GetBackgroundColour();
130 m_foregroundColour = pParent->GetForegroundColour();
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 _T("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_COMMAND_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 (GetEventHandler()->ProcessEvent(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()->GetEventHandler()->ProcessEvent(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_COMMAND_SPINCTRL_UPDATED
371 ,GetId()
372 );
373
374 vEvent.SetEventObject(this);
375 vEvent.SetInt(rEventSpin.GetPosition());
376 (void)GetEventHandler()->ProcessEvent(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_COMMAND_TEXT_UPDATED, GetId() );
403 vEvent.SetEventObject(this);
404
405 wxString sVal = wxGetWindowText(m_hWndBuddy);
406
407 vEvent.SetString(sVal);
408 vEvent.SetInt(GetValue());
409 return (GetEventHandler()->ProcessEvent(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(GetEventHandler()->ProcessEvent(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((char*)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