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