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