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