]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: slidermsw.cpp | |
3 | // Purpose: wxSliderMSW | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
13 | #pragma implementation "slidrmsw.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include <stdio.h> | |
25 | #endif | |
26 | ||
27 | #include "wx/utils.h" | |
28 | #include "wx/brush.h" | |
29 | #include "wx/msw/slidrmsw.h" | |
30 | #include "wx/msw/private.h" | |
31 | ||
32 | IMPLEMENT_DYNAMIC_CLASS(wxSliderMSW, wxControl) | |
33 | ||
34 | // Slider | |
35 | wxSliderMSW::wxSliderMSW() | |
36 | { | |
37 | m_staticValue = 0; | |
38 | m_staticMin = 0; | |
39 | m_staticMax = 0; | |
40 | m_pageSize = 1; | |
41 | m_lineSize = 1; | |
42 | m_rangeMax = 0; | |
43 | m_rangeMin = 0; | |
44 | } | |
45 | ||
46 | bool wxSliderMSW::Create(wxWindow *parent, wxWindowID id, | |
47 | int value, int minValue, int maxValue, | |
48 | const wxPoint& pos, | |
49 | const wxSize& size, long style, | |
50 | const wxValidator& validator, | |
51 | const wxString& name) | |
52 | { | |
53 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) | |
54 | style |= wxBORDER_NONE; | |
55 | ||
56 | SetName(name); | |
57 | #if wxUSE_VALIDATORS | |
58 | SetValidator(validator); | |
59 | #endif // wxUSE_VALIDATORS | |
60 | ||
61 | if (parent) parent->AddChild(this); | |
62 | SetBackgroundColour(parent->GetBackgroundColour()) ; | |
63 | SetForegroundColour(parent->GetForegroundColour()) ; | |
64 | ||
65 | m_staticValue = 0; | |
66 | m_staticMin = 0; | |
67 | m_staticMax = 0; | |
68 | m_pageSize = 1; | |
69 | m_lineSize = 1; | |
70 | m_windowStyle = style; | |
71 | ||
72 | if ( id == -1 ) | |
73 | m_windowId = (int)NewControlId(); | |
74 | else | |
75 | m_windowId = id; | |
76 | ||
77 | int x = pos.x; | |
78 | int y = pos.y; | |
79 | int width = size.x; | |
80 | int height = size.y; | |
81 | ||
82 | // non-Win95 implementation | |
83 | ||
84 | long msStyle = SS_CENTER; | |
85 | ||
86 | WXDWORD exStyle = 0; | |
87 | msStyle |= MSWGetStyle(GetWindowStyle(), & exStyle) ; | |
88 | ||
89 | m_staticValue = (WXHWND) CreateWindowEx(exStyle, wxT("STATIC"), NULL, | |
90 | msStyle, | |
91 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(), | |
92 | wxGetInstance(), NULL); | |
93 | ||
94 | // Now create min static control | |
95 | wxString buf; | |
96 | buf.Printf(wxT("%d"), minValue); | |
97 | DWORD wstyle = STATIC_FLAGS; | |
98 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
99 | wstyle |= WS_CLIPSIBLINGS; | |
100 | m_staticMin = (WXHWND) CreateWindowEx(0, wxT("STATIC"), buf, | |
101 | wstyle, | |
102 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(), | |
103 | wxGetInstance(), NULL); | |
104 | ||
105 | msStyle = 0; | |
106 | if (m_windowStyle & wxSL_VERTICAL) | |
107 | msStyle = SBS_VERT | WS_CHILD | WS_VISIBLE | WS_TABSTOP ; | |
108 | else | |
109 | msStyle = SBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP ; | |
110 | ||
111 | HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxEmptyString, | |
112 | msStyle, | |
113 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
114 | wxGetInstance(), NULL); | |
115 | ||
116 | m_pageSize = (int)((maxValue-minValue)/10); | |
117 | m_rangeMax = maxValue; | |
118 | m_rangeMin = minValue; | |
119 | ||
120 | ::SetScrollRange(scroll_bar, SB_CTL, minValue, maxValue, FALSE); | |
121 | ::SetScrollPos(scroll_bar, SB_CTL, value, FALSE); | |
122 | ShowWindow(scroll_bar, SW_SHOW); | |
123 | ||
124 | m_hWnd = (WXHWND)scroll_bar; | |
125 | ||
126 | // Subclass again for purposes of dialog editing mode | |
127 | SubclassWin(GetHWND()); | |
128 | ||
129 | // Finally, create max value static item | |
130 | buf.Printf(wxT("%d"), maxValue); | |
131 | wstyle = STATIC_FLAGS; | |
132 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
133 | wstyle |= WS_CLIPSIBLINGS; | |
134 | m_staticMax = (WXHWND) CreateWindowEx(0, wxT("STATIC"), buf, | |
135 | wstyle, | |
136 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(), | |
137 | wxGetInstance(), NULL); | |
138 | ||
139 | SetFont(parent->GetFont()); | |
140 | ||
141 | if (GetFont().Ok()) | |
142 | { | |
143 | // GetFont()->RealizeResource(); | |
144 | if (GetFont().GetResourceHandle()) | |
145 | { | |
146 | if ( m_staticMin ) | |
147 | SendMessage((HWND)m_staticMin,WM_SETFONT, | |
148 | (WPARAM)GetFont().GetResourceHandle(),0L); | |
149 | if ( m_staticMax ) | |
150 | SendMessage((HWND)m_staticMax,WM_SETFONT, | |
151 | (WPARAM)GetFont().GetResourceHandle(),0L); | |
152 | if (m_staticValue) | |
153 | SendMessage((HWND)m_staticValue,WM_SETFONT, | |
154 | (WPARAM)GetFont().GetResourceHandle(),0L); | |
155 | } | |
156 | } | |
157 | ||
158 | SetSize(x, y, width, height); | |
159 | SetValue(value); | |
160 | ||
161 | return TRUE; | |
162 | } | |
163 | ||
164 | bool wxSliderMSW::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam, | |
165 | WXWORD pos, WXHWND control) | |
166 | { | |
167 | int position = ::GetScrollPos((HWND)control, SB_CTL); | |
168 | ||
169 | int nScrollInc; | |
170 | wxEventType scrollEvent = wxEVT_NULL; | |
171 | switch ( wParam ) | |
172 | { | |
173 | case SB_TOP: | |
174 | nScrollInc = m_rangeMax - position; | |
175 | scrollEvent = wxEVT_SCROLL_TOP; | |
176 | break; | |
177 | ||
178 | case SB_BOTTOM: | |
179 | nScrollInc = - position; | |
180 | scrollEvent = wxEVT_SCROLL_BOTTOM; | |
181 | break; | |
182 | ||
183 | case SB_LINEUP: | |
184 | nScrollInc = - GetLineSize(); | |
185 | scrollEvent = wxEVT_SCROLL_LINEUP; | |
186 | break; | |
187 | ||
188 | case SB_LINEDOWN: | |
189 | nScrollInc = GetLineSize(); | |
190 | scrollEvent = wxEVT_SCROLL_LINEDOWN; | |
191 | break; | |
192 | ||
193 | case SB_PAGEUP: | |
194 | nScrollInc = -GetPageSize(); | |
195 | scrollEvent = wxEVT_SCROLL_PAGEUP; | |
196 | break; | |
197 | ||
198 | case SB_PAGEDOWN: | |
199 | nScrollInc = GetPageSize(); | |
200 | scrollEvent = wxEVT_SCROLL_PAGEDOWN; | |
201 | break; | |
202 | ||
203 | case SB_THUMBTRACK: | |
204 | case SB_THUMBPOSITION: | |
205 | #ifdef __WIN32__ | |
206 | nScrollInc = (signed short)pos - position; | |
207 | #else | |
208 | nScrollInc = pos - position; | |
209 | #endif | |
210 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; | |
211 | break; | |
212 | ||
213 | default: | |
214 | nScrollInc = 0; | |
215 | } | |
216 | ||
217 | if (nScrollInc == 0) | |
218 | { | |
219 | // no event... | |
220 | return FALSE; | |
221 | } | |
222 | ||
223 | int newPos = position + nScrollInc; | |
224 | ||
225 | if ( (newPos < GetMin()) || (newPos > GetMax()) ) | |
226 | { | |
227 | // out of range - but we did process it | |
228 | return TRUE; | |
229 | } | |
230 | ||
231 | SetValue(newPos); | |
232 | ||
233 | wxScrollEvent event(scrollEvent, m_windowId); | |
234 | event.SetPosition(newPos); | |
235 | event.SetEventObject( this ); | |
236 | GetEventHandler()->ProcessEvent(event); | |
237 | ||
238 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() ); | |
239 | cevent.SetInt( newPos ); | |
240 | cevent.SetEventObject( this ); | |
241 | ||
242 | return GetEventHandler()->ProcessEvent( cevent ); | |
243 | } | |
244 | ||
245 | wxSliderMSW::~wxSliderMSW() | |
246 | { | |
247 | if (m_staticMin) | |
248 | DestroyWindow((HWND) m_staticMin); | |
249 | if (m_staticMax) | |
250 | DestroyWindow((HWND) m_staticMax); | |
251 | if (m_staticValue) | |
252 | DestroyWindow((HWND) m_staticValue); | |
253 | } | |
254 | ||
255 | int wxSliderMSW::GetValue() const | |
256 | { | |
257 | return ::GetScrollPos(GetHwnd(), SB_CTL); | |
258 | } | |
259 | ||
260 | void wxSliderMSW::SetValue(int value) | |
261 | { | |
262 | ::SetScrollPos(GetHwnd(), SB_CTL, value, TRUE); | |
263 | if (m_staticValue) | |
264 | { | |
265 | wxString buf; | |
266 | buf.Printf(wxT("%d"), value); | |
267 | SetWindowText((HWND) m_staticValue, buf); | |
268 | } | |
269 | } | |
270 | ||
271 | void wxSliderMSW::GetSize(int *width, int *height) const | |
272 | { | |
273 | RECT rect; | |
274 | rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1; | |
275 | ||
276 | wxFindMaxSize(GetHWND(), &rect); | |
277 | ||
278 | if (m_staticMin) | |
279 | wxFindMaxSize(m_staticMin, &rect); | |
280 | if (m_staticMax) | |
281 | wxFindMaxSize(m_staticMax, &rect); | |
282 | if (m_staticValue) | |
283 | wxFindMaxSize(m_staticValue, &rect); | |
284 | ||
285 | *width = rect.right - rect.left; | |
286 | *height = rect.bottom - rect.top; | |
287 | } | |
288 | ||
289 | void wxSliderMSW::GetPosition(int *x, int *y) const | |
290 | { | |
291 | wxWindow *parent = GetParent(); | |
292 | RECT rect; | |
293 | rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1; | |
294 | ||
295 | wxFindMaxSize(GetHWND(), &rect); | |
296 | ||
297 | if (m_staticMin) | |
298 | wxFindMaxSize(m_staticMin, &rect); | |
299 | if (m_staticMax) | |
300 | wxFindMaxSize(m_staticMax, &rect); | |
301 | if (m_staticValue) | |
302 | wxFindMaxSize(m_staticValue, &rect); | |
303 | ||
304 | // Since we now have the absolute screen coords, | |
305 | // if there's a parent we must subtract its top left corner | |
306 | POINT point; | |
307 | point.x = rect.left; | |
308 | point.y = rect.top; | |
309 | if (parent) | |
310 | ::ScreenToClient((HWND) parent->GetHWND(), &point); | |
311 | ||
312 | // We may be faking the client origin. | |
313 | // So a window that's really at (0, 30) may appear | |
314 | // (to wxWin apps) to be at (0, 0). | |
315 | if (GetParent()) | |
316 | { | |
317 | wxPoint pt(GetParent()->GetClientAreaOrigin()); | |
318 | point.x -= pt.x; | |
319 | point.y -= pt.y; | |
320 | } | |
321 | *x = point.x; | |
322 | *y = point.y; | |
323 | } | |
324 | ||
325 | // TODO one day, make sense of all this horros and replace it with a readable | |
326 | // DoGetBestSize() | |
327 | void wxSliderMSW::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
328 | { | |
329 | int x1 = x; | |
330 | int y1 = y; | |
331 | int w1 = width; | |
332 | int h1 = height; | |
333 | ||
334 | int currentX, currentY; | |
335 | GetPosition(¤tX, ¤tY); | |
336 | if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
337 | x1 = currentX; | |
338 | if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
339 | y1 = currentY; | |
340 | ||
341 | AdjustForParentClientOrigin(x1, y1, sizeFlags); | |
342 | ||
343 | wxChar buf[300]; | |
344 | ||
345 | int x_offset = x; | |
346 | int y_offset = y; | |
347 | ||
348 | int cx; // slider,min,max sizes | |
349 | int cy; | |
350 | int cyf; | |
351 | ||
352 | wxGetCharSize(GetHWND(), &cx, &cy,& this->GetFont()); | |
353 | ||
354 | if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL) | |
355 | { | |
356 | if ( m_windowStyle & wxSL_LABELS ) | |
357 | { | |
358 | int min_len = 0; | |
359 | ||
360 | GetWindowText((HWND) m_staticMin, buf, 300); | |
361 | GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont()); | |
362 | ||
363 | int max_len = 0; | |
364 | ||
365 | GetWindowText((HWND) m_staticMax, buf, 300); | |
366 | GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont()); | |
367 | if (m_staticValue) | |
368 | { | |
369 | int new_width = (int)(wxMax(min_len, max_len)); | |
370 | int valueHeight = (int)cyf; | |
371 | #ifdef __WIN32__ | |
372 | // For some reason, under Win95, the text edit control has | |
373 | // a lot of space before the first character | |
374 | new_width += 3*cx; | |
375 | #endif | |
376 | MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE); | |
377 | x_offset += new_width + cx; | |
378 | } | |
379 | ||
380 | MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE); | |
381 | x_offset += (int)(min_len + cx); | |
382 | ||
383 | int slider_length = (int)(w1 - x_offset - max_len - cx); | |
384 | ||
385 | int slider_height = cy; | |
386 | ||
387 | // Slider must have a minimum/default length/height | |
388 | if (slider_length < 100) | |
389 | slider_length = 100; | |
390 | ||
391 | MoveWindow(GetHwnd(), x_offset, y_offset, slider_length, slider_height, TRUE); | |
392 | x_offset += slider_length + cx; | |
393 | ||
394 | MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE); | |
395 | } | |
396 | else | |
397 | { | |
398 | // No labels | |
399 | if ( w1 < 0 ) | |
400 | w1 = 200; | |
401 | if ( h1 < 0 ) | |
402 | h1 = 20; | |
403 | MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE); | |
404 | } | |
405 | } | |
406 | else | |
407 | { | |
408 | if ( m_windowStyle & wxSL_LABELS ) | |
409 | { | |
410 | int min_len; | |
411 | GetWindowText((HWND) m_staticMin, buf, 300); | |
412 | GetTextExtent(buf, &min_len, &cyf,NULL,NULL,& this->GetFont()); | |
413 | ||
414 | int max_len; | |
415 | GetWindowText((HWND) m_staticMax, buf, 300); | |
416 | GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont()); | |
417 | ||
418 | if (m_staticValue) | |
419 | { | |
420 | int new_width = (int)(wxMax(min_len, max_len)); | |
421 | int valueHeight = (int)cyf; | |
422 | /*** Suggested change by George Tasker - remove this block... | |
423 | #ifdef __WIN32__ | |
424 | // For some reason, under Win95, the text edit control has | |
425 | // a lot of space before the first character | |
426 | new_width += 3*cx; | |
427 | #endif | |
428 | ... and replace with following line: */ | |
429 | new_width += cx; | |
430 | ||
431 | MoveWindow((HWND) m_staticValue, x_offset, y_offset, new_width, valueHeight, TRUE); | |
432 | y_offset += valueHeight; | |
433 | } | |
434 | ||
435 | MoveWindow((HWND) m_staticMin, x_offset, y_offset, (int)min_len, cy, TRUE); | |
436 | y_offset += cy; | |
437 | ||
438 | int slider_length = (int)(h1 - y_offset - cy - cy); | |
439 | ||
440 | // Use character height as an estimate of slider width (yes, width) | |
441 | int slider_width = cy; | |
442 | ||
443 | // Slider must have a minimum/default length | |
444 | if (slider_length < 100) | |
445 | slider_length = 100; | |
446 | ||
447 | MoveWindow(GetHwnd(), x_offset, y_offset, slider_width, slider_length, TRUE); | |
448 | y_offset += slider_length; | |
449 | ||
450 | MoveWindow((HWND) m_staticMax, x_offset, y_offset, (int)max_len, cy, TRUE); | |
451 | } | |
452 | else | |
453 | { | |
454 | // No labels | |
455 | if ( w1 < 0 ) | |
456 | w1 = 20; | |
457 | if ( h1 < 0 ) | |
458 | h1 = 200; | |
459 | MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE); | |
460 | } | |
461 | } | |
462 | } | |
463 | ||
464 | void wxSliderMSW::SetRange(int minValue, int maxValue) | |
465 | { | |
466 | m_rangeMin = minValue; | |
467 | m_rangeMax = maxValue; | |
468 | ||
469 | ::SetScrollRange(GetHwnd(), SB_CTL, m_rangeMin, m_rangeMax, TRUE); | |
470 | wxChar buf[40]; | |
471 | if ( m_staticMin ) | |
472 | { | |
473 | wxSprintf(buf, wxT("%d"), m_rangeMin); | |
474 | SetWindowText((HWND) m_staticMin, buf); | |
475 | } | |
476 | ||
477 | if ( m_staticMax ) | |
478 | { | |
479 | wxSprintf(buf, wxT("%d"), m_rangeMax); | |
480 | SetWindowText((HWND) m_staticMax, buf); | |
481 | } | |
482 | } | |
483 | ||
484 | WXHBRUSH wxSliderMSW::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, | |
485 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) | |
486 | { | |
487 | if ( nCtlColor == CTLCOLOR_SCROLLBAR ) | |
488 | return 0; | |
489 | ||
490 | // Otherwise, it's a static | |
491 | return wxControl::OnCtlColor(pDC, pWnd, nCtlColor, message, wParam, lParam); | |
492 | } | |
493 | ||
494 | void wxSliderMSW::SetPageSize(int pageSize) | |
495 | { | |
496 | m_pageSize = pageSize; | |
497 | } | |
498 | ||
499 | int wxSliderMSW::GetPageSize() const | |
500 | { | |
501 | return m_pageSize; | |
502 | } | |
503 | ||
504 | void wxSliderMSW::SetLineSize(int lineSize) | |
505 | { | |
506 | m_lineSize = lineSize; | |
507 | } | |
508 | ||
509 | int wxSliderMSW::GetLineSize() const | |
510 | { | |
511 | return m_lineSize; | |
512 | } | |
513 | ||
514 | // Not yet implemented | |
515 | void wxSliderMSW::SetThumbLength(int WXUNUSED(lenPixels)) | |
516 | { | |
517 | } | |
518 | ||
519 | // Not yet implemented | |
520 | int wxSliderMSW::GetThumbLength() const | |
521 | { | |
522 | return 0; | |
523 | } | |
524 | ||
525 | bool wxSliderMSW::ContainsHWND(WXHWND hWnd) const | |
526 | { | |
527 | return ( hWnd == GetStaticMin() || hWnd == GetStaticMax() || hWnd == GetEditValue() ); | |
528 | } | |
529 | ||
530 | void wxSliderMSW::Command (wxCommandEvent & event) | |
531 | { | |
532 | SetValue (event.GetInt()); | |
533 | ProcessCommand (event); | |
534 | } | |
535 | ||
536 | bool wxSliderMSW::Show(bool show) | |
537 | { | |
538 | wxWindow::Show(show); | |
539 | ||
540 | int cshow; | |
541 | if (show) | |
542 | cshow = SW_SHOW; | |
543 | else | |
544 | cshow = SW_HIDE; | |
545 | ||
546 | if(m_staticValue) | |
547 | ShowWindow((HWND) m_staticValue, (BOOL)cshow); | |
548 | if(m_staticMin) | |
549 | ShowWindow((HWND) m_staticMin, (BOOL)cshow); | |
550 | if(m_staticMax) | |
551 | ShowWindow((HWND) m_staticMax, (BOOL)cshow); | |
552 | return TRUE; | |
553 | } | |
554 | ||
555 |