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