]>
Commit | Line | Data |
---|---|---|
b782f2e0 VZ |
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$ | |
74124ea9 | 8 | // Copyright: (c) 1999-2005 Vadim Zeitlin |
65571936 | 9 | // Licence: wxWindows licence |
b782f2e0 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
14f355c2 | 16 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
b782f2e0 VZ |
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 | ||
0e528b99 JS |
36 | #if wxUSE_SPINCTRL |
37 | ||
b782f2e0 VZ |
38 | #include "wx/spinctrl.h" |
39 | #include "wx/msw/private.h" | |
74124ea9 | 40 | #include "wx/msw/wrapcctl.h" |
b782f2e0 | 41 | |
74124ea9 VZ |
42 | #if wxUSE_TOOLTIPS |
43 | #include "wx/tooltip.h" | |
44 | #endif // wxUSE_TOOLTIPS | |
b782f2e0 | 45 | |
678cd6de VZ |
46 | #include <limits.h> // for INT_MIN |
47 | ||
b782f2e0 VZ |
48 | // ---------------------------------------------------------------------------- |
49 | // macros | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
f0a126fe | 52 | #if wxUSE_EXTENDED_RTTI |
bc9fb572 JS |
53 | WX_DEFINE_FLAGS( wxSpinCtrlStyle ) |
54 | ||
3ff066a4 | 55 | wxBEGIN_FLAGS( wxSpinCtrlStyle ) |
bc9fb572 JS |
56 | // new style border flags, we put them first to |
57 | // use them for streaming out | |
3ff066a4 SC |
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) | |
57f4f925 | 64 | |
bc9fb572 | 65 | // old style border flags |
3ff066a4 SC |
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) | |
cb0afb26 | 71 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
72 | |
73 | // standard window styles | |
3ff066a4 SC |
74 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
75 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
76 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
77 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 78 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
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 ) | |
bc9fb572 | 89 | |
51741307 | 90 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxSpinCtrl, wxControl,"wx/spinbut.h") |
9750fc42 | 91 | |
3ff066a4 SC |
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 ) | |
57f4f925 | 95 | wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent ) |
3ff066a4 SC |
96 | wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent ) |
97 | ||
af498247 | 98 | wxPROPERTY( ValueString , wxString , SetValue , GetValue , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) ; |
57f4f925 WS |
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")) | |
af498247 | 102 | wxPROPERTY_FLAGS( WindowStyle , wxSpinCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
066f1b7a | 103 | /* |
57f4f925 WS |
104 | TODO PROPERTIES |
105 | style wxSP_ARROW_KEYS | |
066f1b7a | 106 | */ |
3ff066a4 | 107 | wxEND_PROPERTIES_TABLE() |
51741307 | 108 | |
3ff066a4 SC |
109 | wxBEGIN_HANDLERS_TABLE(wxSpinCtrl) |
110 | wxEND_HANDLERS_TABLE() | |
51741307 | 111 | |
57f4f925 | 112 | wxCONSTRUCTOR_6( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , ValueString , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
51741307 SC |
113 | #else |
114 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl) | |
115 | #endif | |
066f1b7a | 116 | |
9750fc42 | 117 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton) |
e3582f7f | 118 | EVT_CHAR(wxSpinCtrl::OnChar) |
c5c04fab VZ |
119 | |
120 | EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus) | |
121 | ||
57f4f925 | 122 | EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange) |
9750fc42 | 123 | END_EVENT_TABLE() |
b782f2e0 | 124 | |
6fe19057 VZ |
125 | #define GetBuddyHwnd() (HWND)(m_hwndBuddy) |
126 | ||
b782f2e0 VZ |
127 | // ---------------------------------------------------------------------------- |
128 | // constants | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
baccb514 VZ |
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; | |
b782f2e0 VZ |
135 | |
136 | // ============================================================================ | |
137 | // implementation | |
138 | // ============================================================================ | |
139 | ||
6fe19057 VZ |
140 | wxArraySpins wxSpinCtrl::ms_allSpins; |
141 | ||
f6bcfd97 BP |
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 | { | |
975b6bcf | 151 | wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData(hwnd); |
f6bcfd97 | 152 | |
93c4157c VZ |
153 | // forward some messages (the key and focus ones only so far) to |
154 | // the spin ctrl | |
f6bcfd97 BP |
155 | switch ( message ) |
156 | { | |
93c4157c | 157 | case WM_SETFOCUS: |
c5c04fab VZ |
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 | |
c140b7e7 | 160 | if ( (WXHWND)wParam == spin->GetHWND() ) |
c5c04fab VZ |
161 | break; |
162 | //else: fall through | |
163 | ||
93c4157c | 164 | case WM_KILLFOCUS: |
f6bcfd97 BP |
165 | case WM_CHAR: |
166 | case WM_DEADCHAR: | |
167 | case WM_KEYUP: | |
168 | case WM_KEYDOWN: | |
169 | spin->MSWWindowProc(message, wParam, lParam); | |
e3582f7f JS |
170 | |
171 | // The control may have been deleted at this point, so check. | |
975b6bcf | 172 | if ( !::IsWindow(hwnd) || wxGetWindowUserData(hwnd) != spin ) |
e3582f7f | 173 | return 0; |
f6bcfd97 | 174 | break; |
350ba193 VZ |
175 | |
176 | case WM_GETDLGCODE: | |
177 | // we want to get WXK_RETURN in order to generate the event for it | |
178 | return DLGC_WANTCHARS; | |
f6bcfd97 | 179 | } |
350ba193 | 180 | |
f6bcfd97 BP |
181 | return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(), |
182 | hwnd, message, wParam, lParam); | |
183 | } | |
184 | ||
6fe19057 VZ |
185 | /* static */ |
186 | wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy) | |
187 | { | |
975b6bcf | 188 | wxSpinCtrl *spin = (wxSpinCtrl *)wxGetWindowUserData((HWND)hwndBuddy); |
6fe19057 VZ |
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 | { | |
e3582f7f | 205 | switch (cmd) |
6fe19057 | 206 | { |
e3582f7f JS |
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; | |
6fe19057 VZ |
227 | } |
228 | ||
229 | // not processed | |
57f4f925 | 230 | return false; |
6fe19057 VZ |
231 | } |
232 | ||
e3582f7f JS |
233 | void wxSpinCtrl::OnChar(wxKeyEvent& event) |
234 | { | |
77e00fe9 | 235 | switch ( event.GetKeyCode() ) |
e3582f7f JS |
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 | ||
c5c04fab VZ |
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 | ||
b782f2e0 VZ |
279 | // ---------------------------------------------------------------------------- |
280 | // construction | |
281 | // ---------------------------------------------------------------------------- | |
282 | ||
283 | bool wxSpinCtrl::Create(wxWindow *parent, | |
284 | wxWindowID id, | |
678cd6de | 285 | const wxString& value, |
b782f2e0 VZ |
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) | |
882a8f40 | 295 | style |= wxSP_VERTICAL; |
c76b1a30 JS |
296 | |
297 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) | |
5d72f195 RR |
298 | #ifdef __WXWINCE__ |
299 | style |= wxBORDER_SIMPLE; | |
300 | #else | |
c76b1a30 | 301 | style |= wxBORDER_SUNKEN; |
5d72f195 | 302 | #endif |
c76b1a30 | 303 | |
882a8f40 | 304 | SetWindowStyle(style); |
b782f2e0 | 305 | |
fe3d9123 JS |
306 | WXDWORD exStyle = 0; |
307 | WXDWORD msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ; | |
308 | ||
b782f2e0 VZ |
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; | |
baccb514 VZ |
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 | ||
b782f2e0 VZ |
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 | ||
c5c04fab VZ |
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 | |
b782f2e0 | 333 | |
c5c04fab | 334 | // create the text window |
b782f2e0 | 335 | |
b782f2e0 VZ |
336 | m_hwndBuddy = (WXHWND)::CreateWindowEx |
337 | ( | |
c5c04fab | 338 | exStyle, // sunken border |
baccb514 VZ |
339 | _T("EDIT"), // window class |
340 | NULL, // no window title | |
c5c04fab | 341 | msStyle, // style (will be shown later) |
baccb514 VZ |
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 | |
b782f2e0 VZ |
348 | ); |
349 | ||
350 | if ( !m_hwndBuddy ) | |
351 | { | |
f6bcfd97 | 352 | wxLogLastError(wxT("CreateWindow(buddy text window)")); |
b782f2e0 | 353 | |
57f4f925 | 354 | return false; |
b782f2e0 VZ |
355 | } |
356 | ||
c5c04fab VZ |
357 | |
358 | // create the spin button | |
359 | if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) ) | |
360 | { | |
57f4f925 | 361 | return false; |
c5c04fab VZ |
362 | } |
363 | ||
364 | SetRange(min, max); | |
365 | SetValue(initial); | |
366 | ||
f6bcfd97 | 367 | // subclass the text ctrl to be able to intercept some events |
975b6bcf VZ |
368 | wxSetWindowUserData(GetBuddyHwnd(), this); |
369 | m_wndProcBuddy = (WXFARPROC)wxSetWindowProc(GetBuddyHwnd(), | |
370 | wxBuddyTextWndProc); | |
f6bcfd97 | 371 | |
8d2e831b RD |
372 | // set up fonts and colours (This is nomally done in MSWCreateControl) |
373 | InheritAttributes(); | |
e0176dd9 VZ |
374 | if (!m_hasFont) |
375 | SetFont(GetDefaultAttributes().font); | |
8d2e831b | 376 | |
baccb514 VZ |
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 | { | |
882a8f40 | 381 | int cx, cy; |
7a5e53ab | 382 | wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); |
882a8f40 VZ |
383 | |
384 | sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
baccb514 VZ |
385 | } |
386 | ||
8d2e831b | 387 | SetBestSize(size); |
baccb514 | 388 | |
6fe19057 | 389 | (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW); |
b782f2e0 VZ |
390 | |
391 | // associate the text window with the spin button | |
baccb514 VZ |
392 | (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0); |
393 | ||
3d104ec3 | 394 | if ( !value.empty() ) |
678cd6de VZ |
395 | { |
396 | SetValue(value); | |
397 | } | |
398 | ||
6fe19057 VZ |
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 | ||
57f4f925 | 403 | return true; |
baccb514 VZ |
404 | } |
405 | ||
f6bcfd97 BP |
406 | wxSpinCtrl::~wxSpinCtrl() |
407 | { | |
6fe19057 VZ |
408 | ms_allSpins.Remove(this); |
409 | ||
5648c0ad JS |
410 | // This removes spurious memory leak reporting |
411 | if (ms_allSpins.GetCount() == 0) | |
412 | ms_allSpins.Clear(); | |
413 | ||
f6bcfd97 BP |
414 | // destroy the buddy window because this pointer which wxBuddyTextWndProc |
415 | // uses will not soon be valid any more | |
6fe19057 | 416 | ::DestroyWindow(GetBuddyHwnd()); |
f6bcfd97 BP |
417 | } |
418 | ||
678cd6de VZ |
419 | // ---------------------------------------------------------------------------- |
420 | // wxTextCtrl-like methods | |
421 | // ---------------------------------------------------------------------------- | |
422 | ||
423 | void wxSpinCtrl::SetValue(const wxString& text) | |
424 | { | |
6fe19057 | 425 | if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) ) |
678cd6de | 426 | { |
f6bcfd97 | 427 | wxLogLastError(wxT("SetWindowText(buddy)")); |
678cd6de VZ |
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; | |
3d104ec3 | 438 | |
ad8ddd13 RR |
439 | if (n < m_min) n = m_min; |
440 | if (n > m_max) n = m_max; | |
678cd6de VZ |
441 | |
442 | return n; | |
443 | } | |
444 | ||
07901ec9 VZ |
445 | void wxSpinCtrl::SetSelection(long from, long to) |
446 | { | |
77ffb593 | 447 | // if from and to are both -1, it means (in wxWidgets) that all text should |
07901ec9 VZ |
448 | // be selected - translate into Windows convention |
449 | if ( (from == -1) && (to == -1) ) | |
450 | { | |
451 | from = 0; | |
452 | } | |
453 | ||
7d86a2d4 | 454 | ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to); |
07901ec9 VZ |
455 | } |
456 | ||
baccb514 | 457 | // ---------------------------------------------------------------------------- |
882a8f40 | 458 | // forward some methods to subcontrols |
baccb514 VZ |
459 | // ---------------------------------------------------------------------------- |
460 | ||
461 | bool wxSpinCtrl::SetFont(const wxFont& font) | |
462 | { | |
463 | if ( !wxWindowBase::SetFont(font) ) | |
464 | { | |
465 | // nothing to do | |
57f4f925 | 466 | return false; |
baccb514 VZ |
467 | } |
468 | ||
469 | WXHANDLE hFont = GetFont().GetResourceHandle(); | |
6fe19057 | 470 | (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE); |
b782f2e0 | 471 | |
57f4f925 | 472 | return true; |
b782f2e0 VZ |
473 | } |
474 | ||
882a8f40 VZ |
475 | bool wxSpinCtrl::Show(bool show) |
476 | { | |
477 | if ( !wxControl::Show(show) ) | |
478 | { | |
57f4f925 | 479 | return false; |
882a8f40 VZ |
480 | } |
481 | ||
6fe19057 | 482 | ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE); |
882a8f40 | 483 | |
57f4f925 | 484 | return true; |
882a8f40 VZ |
485 | } |
486 | ||
487 | bool wxSpinCtrl::Enable(bool enable) | |
488 | { | |
489 | if ( !wxControl::Enable(enable) ) | |
490 | { | |
57f4f925 | 491 | return false; |
882a8f40 VZ |
492 | } |
493 | ||
6fe19057 | 494 | ::EnableWindow(GetBuddyHwnd(), enable); |
882a8f40 | 495 | |
57f4f925 | 496 | return true; |
882a8f40 VZ |
497 | } |
498 | ||
8bf3196d GRG |
499 | void wxSpinCtrl::SetFocus() |
500 | { | |
6fe19057 | 501 | ::SetFocus(GetBuddyHwnd()); |
8bf3196d GRG |
502 | } |
503 | ||
74124ea9 VZ |
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 | ||
9750fc42 VZ |
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 | ||
b782f2e0 VZ |
534 | // ---------------------------------------------------------------------------- |
535 | // size calculations | |
536 | // ---------------------------------------------------------------------------- | |
537 | ||
f68586e5 | 538 | wxSize wxSpinCtrl::DoGetBestSize() const |
baccb514 VZ |
539 | { |
540 | wxSize sizeBtn = wxSpinButton::DoGetBestSize(); | |
541 | sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN; | |
542 | ||
543 | int y; | |
7a5e53ab | 544 | wxGetCharSize(GetHWND(), NULL, &y, GetFont()); |
baccb514 VZ |
545 | y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y); |
546 | ||
09f27917 JS |
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(). | |
57f4f925 | 551 | |
09f27917 | 552 | // if ( sizeBtn.y < y ) |
baccb514 VZ |
553 | { |
554 | // make the text tall enough | |
555 | sizeBtn.y = y; | |
556 | } | |
557 | ||
558 | return sizeBtn; | |
559 | } | |
560 | ||
b782f2e0 VZ |
561 | void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height) |
562 | { | |
baccb514 | 563 | int widthBtn = wxSpinButton::DoGetBestSize().x; |
b782f2e0 VZ |
564 | int widthText = width - widthBtn - MARGIN_BETWEEN; |
565 | if ( widthText <= 0 ) | |
566 | { | |
567 | wxLogDebug(_T("not enough space for wxSpinCtrl!")); | |
568 | } | |
569 | ||
8e44f3ca | 570 | // 1) The buddy window |
7d86a2d4 | 571 | DoMoveSibling(m_hwndBuddy, x, y, widthText, height); |
b782f2e0 | 572 | |
8e44f3ca | 573 | // 2) The button window |
b782f2e0 | 574 | x += widthText + MARGIN_BETWEEN; |
7d86a2d4 | 575 | wxSpinButton::DoMoveWindow(x, y, widthBtn, height); |
b782f2e0 VZ |
576 | } |
577 | ||
f6bcfd97 BP |
578 | // get total size of the control |
579 | void wxSpinCtrl::DoGetSize(int *x, int *y) const | |
580 | { | |
581 | RECT spinrect, textrect, ctrlrect; | |
582 | GetWindowRect(GetHwnd(), &spinrect); | |
6fe19057 | 583 | GetWindowRect(GetBuddyHwnd(), &textrect); |
f6bcfd97 BP |
584 | UnionRect(&ctrlrect,&textrect, &spinrect); |
585 | ||
586 | if ( x ) | |
587 | *x = ctrlrect.right - ctrlrect.left; | |
588 | if ( y ) | |
589 | *y = ctrlrect.bottom - ctrlrect.top; | |
590 | } | |
591 | ||
592 | void wxSpinCtrl::DoGetPosition(int *x, int *y) const | |
593 | { | |
594 | // hack: pretend that our HWND is the text control just for a moment | |
595 | WXHWND hWnd = GetHWND(); | |
596 | wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy; | |
597 | ||
598 | wxSpinButton::DoGetPosition(x, y); | |
599 | ||
600 | wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd; | |
601 | } | |
602 | ||
74124ea9 | 603 | #endif // wxUSE_SPINCTRL |
0e528b99 | 604 |