]> git.saurik.com Git - wxWidgets.git/blob - src/msw/spinctrl.cpp
Fixed test for button style - styles contain other style bits
[wxWidgets.git] / src / msw / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/spinctrl.cpp
3 // Purpose: wxSpinCtrl class implementation for Win32
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 22.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999-2005 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "spinctrlbase.h"
18 #pragma implementation "spinctrl.h"
19 #endif
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
24
25 // for compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #ifndef WX_PRECOMP
33 #include "wx/wx.h"
34 #endif
35
36 #if wxUSE_SPINCTRL
37
38 #include "wx/spinctrl.h"
39 #include "wx/msw/private.h"
40 #include "wx/msw/wrapcctl.h"
41
42 #if wxUSE_TOOLTIPS
43 #include "wx/tooltip.h"
44 #endif // wxUSE_TOOLTIPS
45
46 #include <limits.h> // for INT_MIN
47
48 // ----------------------------------------------------------------------------
49 // macros
50 // ----------------------------------------------------------------------------
51
52 #if wxUSE_EXTENDED_RTTI
53 WX_DEFINE_FLAGS( wxSpinCtrlStyle )
54
55 wxBEGIN_FLAGS( wxSpinCtrlStyle )
56 // new style border flags, we put them first to
57 // use them for streaming out
58 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
59 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
60 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
61 wxFLAGS_MEMBER(wxBORDER_RAISED)
62 wxFLAGS_MEMBER(wxBORDER_STATIC)
63 wxFLAGS_MEMBER(wxBORDER_NONE)
64
65 // old style border flags
66 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
67 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
68 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
69 wxFLAGS_MEMBER(wxRAISED_BORDER)
70 wxFLAGS_MEMBER(wxSTATIC_BORDER)
71 wxFLAGS_MEMBER(wxBORDER)
72
73 // standard window styles
74 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
75 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
76 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
77 wxFLAGS_MEMBER(wxWANTS_CHARS)
78 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
79 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
80 wxFLAGS_MEMBER(wxVSCROLL)
81 wxFLAGS_MEMBER(wxHSCROLL)
82
83 wxFLAGS_MEMBER(wxSP_HORIZONTAL)
84 wxFLAGS_MEMBER(wxSP_VERTICAL)
85 wxFLAGS_MEMBER(wxSP_ARROW_KEYS)
86 wxFLAGS_MEMBER(wxSP_WRAP)
87
88 wxEND_FLAGS( wxSpinCtrlStyle )
89
90 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl, wxControl,"wx/spinbut.h")
91
92 wxBEGIN_PROPERTIES_TABLE(wxSpinCtrl)
93 wxEVENT_RANGE_PROPERTY( Spin , wxEVT_SCROLL_TOP , wxEVT_SCROLL_ENDSCROLL , wxSpinEvent )
94 wxEVENT_PROPERTY( Updated , wxEVT_COMMAND_SPINCTRL_UPDATED , wxCommandEvent )
95 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
96 wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent )
97
98 wxPROPERTY( ValueString , wxString , SetValue , GetValue , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ;
99 wxPROPERTY( Value , int , SetValue, GetValue, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
100 wxPROPERTY( Min , int , SetMin, GetMin, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
101 wxPROPERTY( Max , int , SetMax, GetMax, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
102 wxPROPERTY_FLAGS( WindowStyle , wxSpinCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
103 /*
104 TODO PROPERTIES
105 style wxSP_ARROW_KEYS
106 */
107 wxEND_PROPERTIES_TABLE()
108
109 wxBEGIN_HANDLERS_TABLE(wxSpinCtrl)
110 wxEND_HANDLERS_TABLE()
111
112 wxCONSTRUCTOR_6( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , ValueString , wxPoint , Position , wxSize , Size , long , WindowStyle )
113 #else
114 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
115 #endif
116
117 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
118 EVT_CHAR(wxSpinCtrl::OnChar)
119
120 EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
121
122 EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange)
123 END_EVENT_TABLE()
124
125 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
126
127 // ----------------------------------------------------------------------------
128 // constants
129 // ----------------------------------------------------------------------------
130
131 // the margin between the up-down control and its buddy (can be arbitrary,
132 // choose what you like - or may be decide during run-time depending on the
133 // font size?)
134 static const int MARGIN_BETWEEN = 1;
135
136 // ============================================================================
137 // implementation
138 // ============================================================================
139
140 wxArraySpins wxSpinCtrl::ms_allSpins;
141
142 // ----------------------------------------------------------------------------
143 // wnd proc for the buddy text ctrl
144 // ----------------------------------------------------------------------------
145
146 LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
147 UINT message,
148 WPARAM wParam,
149 LPARAM lParam)
150 {
151 wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData(hwnd);
152
153 // forward some messages (the key and focus ones only so far) to
154 // the spin ctrl
155 switch ( message )
156 {
157 case WM_SETFOCUS:
158 // if the focus comes from the spin control itself, don't set it
159 // back to it -- we don't want to go into an infinite loop
160 if ( (WXHWND)wParam == spin->GetHWND() )
161 break;
162 //else: fall through
163
164 case WM_KILLFOCUS:
165 case WM_CHAR:
166 case WM_DEADCHAR:
167 case WM_KEYUP:
168 case WM_KEYDOWN:
169 spin->MSWWindowProc(message, wParam, lParam);
170
171 // The control may have been deleted at this point, so check.
172 if ( !::IsWindow(hwnd) || wxGetWindowUserData(hwnd) != spin )
173 return 0;
174 break;
175
176 case WM_GETDLGCODE:
177 // we want to get WXK_RETURN in order to generate the event for it
178 return DLGC_WANTCHARS;
179 }
180
181 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
182 hwnd, message, wParam, lParam);
183 }
184
185 /* static */
186 wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
187 {
188 wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData((HWND)hwndBuddy);
189
190 int i = ms_allSpins.Index(spin);
191
192 if ( i == wxNOT_FOUND )
193 return NULL;
194
195 // sanity check
196 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
197 _T("wxSpinCtrl has incorrect buddy HWND!") );
198
199 return spin;
200 }
201
202 // process a WM_COMMAND generated by the buddy text control
203 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
204 {
205 switch (cmd)
206 {
207 case EN_CHANGE:
208 {
209 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
210 event.SetEventObject(this);
211 wxString val = wxGetWindowText(m_hwndBuddy);
212 event.SetString(val);
213 event.SetInt(GetValue());
214 return GetEventHandler()->ProcessEvent(event);
215 }
216 case EN_SETFOCUS:
217 case EN_KILLFOCUS:
218 {
219 wxFocusEvent event(cmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
220 : wxEVT_SET_FOCUS,
221 m_windowId);
222 event.SetEventObject( this );
223 return GetEventHandler()->ProcessEvent(event);
224 }
225 default:
226 break;
227 }
228
229 // not processed
230 return false;
231 }
232
233 void wxSpinCtrl::OnChar(wxKeyEvent& event)
234 {
235 switch ( event.GetKeyCode() )
236 {
237 case WXK_RETURN:
238 {
239 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
240 InitCommandEvent(event);
241 wxString val = wxGetWindowText(m_hwndBuddy);
242 event.SetString(val);
243 event.SetInt(GetValue());
244 if ( GetEventHandler()->ProcessEvent(event) )
245 return;
246 break;
247 }
248
249 case WXK_TAB:
250 // always produce navigation event - even if we process TAB
251 // ourselves the fact that we got here means that the user code
252 // decided to skip processing of this TAB - probably to let it
253 // do its default job.
254 {
255 wxNavigationKeyEvent eventNav;
256 eventNav.SetDirection(!event.ShiftDown());
257 eventNav.SetWindowChange(event.ControlDown());
258 eventNav.SetEventObject(this);
259
260 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
261 return;
262 }
263 break;
264 }
265
266 // no, we didn't process it
267 event.Skip();
268 }
269
270 void wxSpinCtrl::OnSetFocus(wxFocusEvent& event)
271 {
272 // when we get focus, give it to our buddy window as it needs it more than
273 // we do
274 ::SetFocus((HWND)m_hwndBuddy);
275
276 event.Skip();
277 }
278
279 // ----------------------------------------------------------------------------
280 // construction
281 // ----------------------------------------------------------------------------
282
283 bool wxSpinCtrl::Create(wxWindow *parent,
284 wxWindowID id,
285 const wxString& value,
286 const wxPoint& pos,
287 const wxSize& size,
288 long style,
289 int min, int max, int initial,
290 const wxString& name)
291 {
292 // before using DoGetBestSize(), have to set style to let the base class
293 // know whether this is a horizontal or vertical control (we're always
294 // vertical)
295 style |= wxSP_VERTICAL;
296
297 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
298 #ifdef __WXWINCE__
299 style |= wxBORDER_SIMPLE;
300 #else
301 style |= wxBORDER_SUNKEN;
302 #endif
303
304 SetWindowStyle(style);
305
306 WXDWORD exStyle = 0;
307 WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
308
309 // calculate the sizes: the size given is the toal size for both controls
310 // and we need to fit them both in the given width (height is the same)
311 wxSize sizeText(size), sizeBtn(size);
312 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
313 if ( sizeText.x <= 0 )
314 {
315 // DEFAULT_ITEM_WIDTH is the default width for the text control
316 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
317 }
318
319 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
320 if ( sizeText.x <= 0 )
321 {
322 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
323 }
324
325 wxPoint posBtn(pos);
326 posBtn.x += sizeText.x + MARGIN_BETWEEN;
327
328 // we must create the text control before the spin button for the purpose
329 // of the dialog navigation: if there is a static text just before the spin
330 // control, activating it by Alt-letter should give focus to the text
331 // control, not the spin and the dialog navigation code will give focus to
332 // the next control (at Windows level), not the one after it
333
334 // create the text window
335
336 m_hwndBuddy = (WXHWND)::CreateWindowEx
337 (
338 exStyle, // sunken border
339 _T("EDIT"), // window class
340 NULL, // no window title
341 msStyle, // style (will be shown later)
342 pos.x, pos.y, // position
343 0, 0, // size (will be set later)
344 GetHwndOf(parent), // parent
345 (HMENU)-1, // control id
346 wxGetInstance(), // app instance
347 NULL // unused client data
348 );
349
350 if ( !m_hwndBuddy )
351 {
352 wxLogLastError(wxT("CreateWindow(buddy text window)"));
353
354 return false;
355 }
356
357
358 // create the spin button
359 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
360 {
361 return false;
362 }
363
364 SetRange(min, max);
365 SetValue(initial);
366
367 // subclass the text ctrl to be able to intercept some events
368 wxSetWindowUserData(GetBuddyHwnd(), this);
369 m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(),
370 wxBuddyTextWndProc);
371
372 // set up fonts and colours (This is nomally done in MSWCreateControl)
373 InheritAttributes();
374 if (!m_hasFont)
375 SetFont(GetDefaultAttributes().font);
376
377 // set the size of the text window - can do it only now, because we
378 // couldn't call DoGetBestSize() before as font wasn't set
379 if ( sizeText.y <= 0 )
380 {
381 int cx, cy;
382 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
383
384 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
385 }
386
387 SetBestSize(size);
388
389 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
390
391 // associate the text window with the spin button
392 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
393
394 if ( !value.IsEmpty() )
395 {
396 SetValue(value);
397 }
398
399 // do it after finishing with m_hwndBuddy creation to avoid generating
400 // initial wxEVT_COMMAND_TEXT_UPDATED message
401 ms_allSpins.Add(this);
402
403 return true;
404 }
405
406 wxSpinCtrl::~wxSpinCtrl()
407 {
408 ms_allSpins.Remove(this);
409
410 // This removes spurious memory leak reporting
411 if (ms_allSpins.GetCount() == 0)
412 ms_allSpins.Clear();
413
414 // destroy the buddy window because this pointer which wxBuddyTextWndProc
415 // uses will not soon be valid any more
416 ::DestroyWindow(GetBuddyHwnd());
417 }
418
419 // ----------------------------------------------------------------------------
420 // wxTextCtrl-like methods
421 // ----------------------------------------------------------------------------
422
423 void wxSpinCtrl::SetValue(const wxString& text)
424 {
425 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
426 {
427 wxLogLastError(wxT("SetWindowText(buddy)"));
428 }
429 }
430
431 int wxSpinCtrl::GetValue() const
432 {
433 wxString val = wxGetWindowText(m_hwndBuddy);
434
435 long n;
436 if ( (wxSscanf(val, wxT("%lu"), &n) != 1) )
437 n = INT_MIN;
438
439 if (n < m_min) n = m_min;
440 if (n > m_max) n = m_max;
441
442 return n;
443 }
444
445 void wxSpinCtrl::SetSelection(long from, long to)
446 {
447 // if from and to are both -1, it means (in wxWidgets) that all text should
448 // be selected - translate into Windows convention
449 if ( (from == -1) && (to == -1) )
450 {
451 from = 0;
452 }
453
454 ::SendMessage((HWND)m_hwndBuddy, EM_SETSEL, (WPARAM)from, (LPARAM)to);
455 }
456
457 // ----------------------------------------------------------------------------
458 // forward some methods to subcontrols
459 // ----------------------------------------------------------------------------
460
461 bool wxSpinCtrl::SetFont(const wxFont& font)
462 {
463 if ( !wxWindowBase::SetFont(font) )
464 {
465 // nothing to do
466 return false;
467 }
468
469 WXHANDLE hFont = GetFont().GetResourceHandle();
470 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
471
472 return true;
473 }
474
475 bool wxSpinCtrl::Show(bool show)
476 {
477 if ( !wxControl::Show(show) )
478 {
479 return false;
480 }
481
482 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
483
484 return true;
485 }
486
487 bool wxSpinCtrl::Enable(bool enable)
488 {
489 if ( !wxControl::Enable(enable) )
490 {
491 return false;
492 }
493
494 ::EnableWindow(GetBuddyHwnd(), enable);
495
496 return true;
497 }
498
499 void wxSpinCtrl::SetFocus()
500 {
501 ::SetFocus(GetBuddyHwnd());
502 }
503
504 #if wxUSE_TOOLTIPS
505
506 void wxSpinCtrl::DoSetToolTip(wxToolTip *tip)
507 {
508 wxSpinButton::DoSetToolTip(tip);
509
510 if ( tip )
511 tip->Add(m_hwndBuddy);
512 }
513
514 #endif // wxUSE_TOOLTIPS
515
516 // ----------------------------------------------------------------------------
517 // event processing
518 // ----------------------------------------------------------------------------
519
520 void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin)
521 {
522 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
523 event.SetEventObject(this);
524 event.SetInt(eventSpin.GetPosition());
525
526 (void)GetEventHandler()->ProcessEvent(event);
527
528 if ( eventSpin.GetSkipped() )
529 {
530 event.Skip();
531 }
532 }
533
534 // ----------------------------------------------------------------------------
535 // size calculations
536 // ----------------------------------------------------------------------------
537
538 wxSize wxSpinCtrl::DoGetBestSize() const
539 {
540 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
541 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
542
543 int y;
544 wxGetCharSize(GetHWND(), NULL, &y, GetFont());
545 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
546
547 // JACS: we should always use the height calculated
548 // from above, because otherwise we'll get a spin control
549 // that's too big. So never use the height calculated
550 // from wxSpinButton::DoGetBestSize().
551
552 // if ( sizeBtn.y < y )
553 {
554 // make the text tall enough
555 sizeBtn.y = y;
556 }
557
558 return sizeBtn;
559 }
560
561 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
562 {
563 int widthBtn = wxSpinButton::DoGetBestSize().x;
564 int widthText = width - widthBtn - MARGIN_BETWEEN;
565 if ( widthText <= 0 )
566 {
567 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
568 }
569
570 if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) )
571 {
572 wxLogLastError(wxT("MoveWindow(buddy)"));
573 }
574
575 x += widthText + MARGIN_BETWEEN;
576 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
577 {
578 wxLogLastError(wxT("MoveWindow"));
579 }
580 }
581
582 // get total size of the control
583 void wxSpinCtrl::DoGetSize(int *x, int *y) const
584 {
585 RECT spinrect, textrect, ctrlrect;
586 GetWindowRect(GetHwnd(), &spinrect);
587 GetWindowRect(GetBuddyHwnd(), &textrect);
588 UnionRect(&ctrlrect,&textrect, &spinrect);
589
590 if ( x )
591 *x = ctrlrect.right - ctrlrect.left;
592 if ( y )
593 *y = ctrlrect.bottom - ctrlrect.top;
594 }
595
596 void wxSpinCtrl::DoGetPosition(int *x, int *y) const
597 {
598 // hack: pretend that our HWND is the text control just for a moment
599 WXHWND hWnd = GetHWND();
600 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
601
602 wxSpinButton::DoGetPosition(x, y);
603
604 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
605 }
606
607 #endif // wxUSE_SPINCTRL
608