]> git.saurik.com Git - wxWidgets.git/blob - src/msw/spinctrl.cpp
Added required #include
[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 #ifdef __GNUG__
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(__TWIN32__))
44 #include <commctrl.h>
45 #endif
46
47 #include <limits.h> // for INT_MIN
48
49 // ----------------------------------------------------------------------------
50 // macros
51 // ----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
54
55 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
56 EVT_CHAR(wxSpinCtrl::OnChar)
57 EVT_SPIN(-1, wxSpinCtrl::OnSpinChange)
58 END_EVENT_TABLE()
59
60 #define GetBuddyHwnd() (HWND)(m_hwndBuddy)
61
62 // ----------------------------------------------------------------------------
63 // constants
64 // ----------------------------------------------------------------------------
65
66 // the margin between the up-down control and its buddy (can be arbitrary,
67 // choose what you like - or may be decide during run-time depending on the
68 // font size?)
69 static const int MARGIN_BETWEEN = 1;
70
71 // ============================================================================
72 // implementation
73 // ============================================================================
74
75 wxArraySpins wxSpinCtrl::ms_allSpins;
76
77 // ----------------------------------------------------------------------------
78 // wnd proc for the buddy text ctrl
79 // ----------------------------------------------------------------------------
80
81 LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
82 UINT message,
83 WPARAM wParam,
84 LPARAM lParam)
85 {
86 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA);
87
88 // forward some messages (the key ones only so far) to the spin ctrl
89 switch ( message )
90 {
91 case WM_CHAR:
92 case WM_DEADCHAR:
93 case WM_KEYUP:
94 case WM_KEYDOWN:
95 spin->MSWWindowProc(message, wParam, lParam);
96
97 // The control may have been deleted at this point, so check.
98 if (!(::IsWindow(hwnd) && ((wxSpinCtrl *)::GetWindowLong(hwnd, GWL_USERDATA)) == spin))
99 return 0;
100 break;
101 }
102 return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
103 hwnd, message, wParam, lParam);
104 }
105
106 /* static */
107 wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
108 {
109 wxSpinCtrl *spin = (wxSpinCtrl *)::GetWindowLong((HWND)hwndBuddy,
110 GWL_USERDATA);
111
112 int i = ms_allSpins.Index(spin);
113
114 if ( i == wxNOT_FOUND )
115 return NULL;
116
117 // sanity check
118 wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
119 _T("wxSpinCtrl has incorrect buddy HWND!") );
120
121 return spin;
122 }
123
124 // process a WM_COMMAND generated by the buddy text control
125 bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
126 {
127 switch (cmd)
128 {
129 case EN_CHANGE:
130 {
131 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
132 event.SetEventObject(this);
133 wxString val = wxGetWindowText(m_hwndBuddy);
134 event.SetString(val);
135 event.SetInt(GetValue());
136 return GetEventHandler()->ProcessEvent(event);
137 }
138 case EN_SETFOCUS:
139 case EN_KILLFOCUS:
140 {
141 wxFocusEvent event(cmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
142 : wxEVT_SET_FOCUS,
143 m_windowId);
144 event.SetEventObject( this );
145 return GetEventHandler()->ProcessEvent(event);
146 }
147 default:
148 break;
149 }
150
151 // not processed
152 return FALSE;
153 }
154
155 void wxSpinCtrl::OnChar(wxKeyEvent& event)
156 {
157 switch ( event.KeyCode() )
158 {
159 case WXK_RETURN:
160 {
161 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
162 InitCommandEvent(event);
163 wxString val = wxGetWindowText(m_hwndBuddy);
164 event.SetString(val);
165 event.SetInt(GetValue());
166 if ( GetEventHandler()->ProcessEvent(event) )
167 return;
168 break;
169 }
170
171 case WXK_TAB:
172 // always produce navigation event - even if we process TAB
173 // ourselves the fact that we got here means that the user code
174 // decided to skip processing of this TAB - probably to let it
175 // do its default job.
176 {
177 wxNavigationKeyEvent eventNav;
178 eventNav.SetDirection(!event.ShiftDown());
179 eventNav.SetWindowChange(event.ControlDown());
180 eventNav.SetEventObject(this);
181
182 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
183 return;
184 }
185 break;
186 }
187
188 // no, we didn't process it
189 event.Skip();
190 }
191
192 // ----------------------------------------------------------------------------
193 // construction
194 // ----------------------------------------------------------------------------
195
196 bool wxSpinCtrl::Create(wxWindow *parent,
197 wxWindowID id,
198 const wxString& value,
199 const wxPoint& pos,
200 const wxSize& size,
201 long style,
202 int min, int max, int initial,
203 const wxString& name)
204 {
205 // before using DoGetBestSize(), have to set style to let the base class
206 // know whether this is a horizontal or vertical control (we're always
207 // vertical)
208 style |= wxSP_VERTICAL;
209 SetWindowStyle(style);
210
211 // calculate the sizes: the size given is the toal size for both controls
212 // and we need to fit them both in the given width (height is the same)
213 wxSize sizeText(size), sizeBtn(size);
214 sizeBtn.x = wxSpinButton::DoGetBestSize().x;
215 if ( sizeText.x <= 0 )
216 {
217 // DEFAULT_ITEM_WIDTH is the default width for the text control
218 sizeText.x = DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN + sizeBtn.x;
219 }
220
221 sizeText.x -= sizeBtn.x + MARGIN_BETWEEN;
222 if ( sizeText.x <= 0 )
223 {
224 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
225 }
226
227 wxPoint posBtn(pos);
228 posBtn.x += sizeText.x + MARGIN_BETWEEN;
229
230 // create the spin button
231 if ( !wxSpinButton::Create(parent, id, posBtn, sizeBtn, style, name) )
232 {
233 return FALSE;
234 }
235
236 SetRange(min, max);
237 SetValue(initial);
238
239 bool want3D;
240 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
241 int msStyle = WS_CHILD;
242
243 // Even with extended styles, need to combine with WS_BORDER for them to
244 // look right.
245 if ( want3D || wxStyleHasBorder(style) )
246 msStyle |= WS_BORDER;
247
248 // create the text window
249 m_hwndBuddy = (WXHWND)::CreateWindowEx
250 (
251 exStyle, // sunken border
252 _T("EDIT"), // window class
253 NULL, // no window title
254 msStyle /* | WS_CLIPSIBLINGS */, // style (will be shown later)
255 pos.x, pos.y, // position
256 0, 0, // size (will be set later)
257 GetHwndOf(parent), // parent
258 (HMENU)-1, // control id
259 wxGetInstance(), // app instance
260 NULL // unused client data
261 );
262
263 if ( !m_hwndBuddy )
264 {
265 wxLogLastError(wxT("CreateWindow(buddy text window)"));
266
267 return FALSE;
268 }
269
270 // subclass the text ctrl to be able to intercept some events
271 m_wndProcBuddy = (WXFARPROC)::GetWindowLong(GetBuddyHwnd(), GWL_WNDPROC);
272 ::SetWindowLong(GetBuddyHwnd(), GWL_USERDATA, (LONG)this);
273 ::SetWindowLong(GetBuddyHwnd(), GWL_WNDPROC, (LONG)wxBuddyTextWndProc);
274
275 // should have the same font as the other controls
276 SetFont(GetParent()->GetFont());
277
278 // set the size of the text window - can do it only now, because we
279 // couldn't call DoGetBestSize() before as font wasn't set
280 if ( sizeText.y <= 0 )
281 {
282 int cx, cy;
283 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
284
285 sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
286 }
287
288 DoMoveWindow(pos.x, pos.y,
289 sizeText.x + sizeBtn.x + MARGIN_BETWEEN, sizeText.y);
290
291 (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW);
292
293 // associate the text window with the spin button
294 (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0);
295
296 if ( !value.IsEmpty() )
297 {
298 SetValue(value);
299 }
300
301 // do it after finishing with m_hwndBuddy creation to avoid generating
302 // initial wxEVT_COMMAND_TEXT_UPDATED message
303 ms_allSpins.Add(this);
304
305 return TRUE;
306 }
307
308 wxSpinCtrl::~wxSpinCtrl()
309 {
310 ms_allSpins.Remove(this);
311
312 // destroy the buddy window because this pointer which wxBuddyTextWndProc
313 // uses will not soon be valid any more
314 ::DestroyWindow(GetBuddyHwnd());
315 }
316
317 // ----------------------------------------------------------------------------
318 // wxTextCtrl-like methods
319 // ----------------------------------------------------------------------------
320
321 void wxSpinCtrl::SetValue(const wxString& text)
322 {
323 if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) )
324 {
325 wxLogLastError(wxT("SetWindowText(buddy)"));
326 }
327 }
328
329 int wxSpinCtrl::GetValue() const
330 {
331 wxString val = wxGetWindowText(m_hwndBuddy);
332
333 long n;
334 if ( (wxSscanf(val, wxT("%lu"), &n) != 1) )
335 n = INT_MIN;
336
337 return n;
338 }
339
340 // ----------------------------------------------------------------------------
341 // forward some methods to subcontrols
342 // ----------------------------------------------------------------------------
343
344 bool wxSpinCtrl::SetFont(const wxFont& font)
345 {
346 if ( !wxWindowBase::SetFont(font) )
347 {
348 // nothing to do
349 return FALSE;
350 }
351
352 WXHANDLE hFont = GetFont().GetResourceHandle();
353 (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
354
355 return TRUE;
356 }
357
358 bool wxSpinCtrl::Show(bool show)
359 {
360 if ( !wxControl::Show(show) )
361 {
362 return FALSE;
363 }
364
365 ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
366
367 return TRUE;
368 }
369
370 bool wxSpinCtrl::Enable(bool enable)
371 {
372 if ( !wxControl::Enable(enable) )
373 {
374 return FALSE;
375 }
376
377 ::EnableWindow(GetBuddyHwnd(), enable);
378
379 return TRUE;
380 }
381
382 void wxSpinCtrl::SetFocus()
383 {
384 ::SetFocus(GetBuddyHwnd());
385 }
386
387 // ----------------------------------------------------------------------------
388 // event processing
389 // ----------------------------------------------------------------------------
390
391 void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin)
392 {
393 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
394 event.SetEventObject(this);
395 event.SetInt(eventSpin.GetPosition());
396
397 (void)GetEventHandler()->ProcessEvent(event);
398
399 if ( eventSpin.GetSkipped() )
400 {
401 event.Skip();
402 }
403 }
404
405 // ----------------------------------------------------------------------------
406 // size calculations
407 // ----------------------------------------------------------------------------
408
409 wxSize wxSpinCtrl::DoGetBestSize() const
410 {
411 wxSize sizeBtn = wxSpinButton::DoGetBestSize();
412 sizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
413
414 int y;
415 wxGetCharSize(GetHWND(), NULL, &y, &GetFont());
416 y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(y);
417
418 if ( sizeBtn.y < y )
419 {
420 // make the text tall enough
421 sizeBtn.y = y;
422 }
423
424 return sizeBtn;
425 }
426
427 void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
428 {
429 int widthBtn = wxSpinButton::DoGetBestSize().x;
430 int widthText = width - widthBtn - MARGIN_BETWEEN;
431 if ( widthText <= 0 )
432 {
433 wxLogDebug(_T("not enough space for wxSpinCtrl!"));
434 }
435
436 if ( !::MoveWindow(GetBuddyHwnd(), x, y, widthText, height, TRUE) )
437 {
438 wxLogLastError(wxT("MoveWindow(buddy)"));
439 }
440
441 x += widthText + MARGIN_BETWEEN;
442 if ( !::MoveWindow(GetHwnd(), x, y, widthBtn, height, TRUE) )
443 {
444 wxLogLastError(wxT("MoveWindow"));
445 }
446 }
447
448 // get total size of the control
449 void wxSpinCtrl::DoGetSize(int *x, int *y) const
450 {
451 RECT spinrect, textrect, ctrlrect;
452 GetWindowRect(GetHwnd(), &spinrect);
453 GetWindowRect(GetBuddyHwnd(), &textrect);
454 UnionRect(&ctrlrect,&textrect, &spinrect);
455
456 if ( x )
457 *x = ctrlrect.right - ctrlrect.left;
458 if ( y )
459 *y = ctrlrect.bottom - ctrlrect.top;
460 }
461
462 void wxSpinCtrl::DoGetPosition(int *x, int *y) const
463 {
464 // hack: pretend that our HWND is the text control just for a moment
465 WXHWND hWnd = GetHWND();
466 wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy;
467
468 wxSpinButton::DoGetPosition(x, y);
469
470 wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
471 }
472
473 #endif // __WIN95__
474
475 #endif
476 // wxUSE_SPINCTRL
477