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