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