]> git.saurik.com Git - wxWidgets.git/blob - src/msw/slider95.cpp
Acutally use the m_maxChars parameter value instead of ignoring it
[wxWidgets.git] / src / msw / slider95.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: slider95.cpp
3 // Purpose: wxSlider95, using the Win95 trackbar control
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 #ifdef __GNUG__
13 #pragma implementation "slider95.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 #if wxUSE_SLIDER
24
25 #ifndef WX_PRECOMP
26 #include "wx/utils.h"
27 #include "wx/brush.h"
28 #include "wx/slider.h"
29 #endif
30
31 #ifdef __WIN95__
32
33 #include "wx/msw/slider95.h"
34 #include "wx/msw/private.h"
35
36 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
37 #include <commctrl.h>
38 #endif
39
40 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
41
42 // Slider
43 wxSlider95::wxSlider95()
44 {
45 m_staticValue = (WXHWND) NULL;
46 m_staticMin = (WXHWND) NULL;
47 m_staticMax = (WXHWND) NULL;
48 m_pageSize = 1;
49 m_lineSize = 1;
50 m_rangeMax = 0;
51 m_rangeMin = 0;
52 m_tickFreq = 0;
53 }
54
55 bool wxSlider95::Create(wxWindow *parent, wxWindowID id,
56 int value, int minValue, int maxValue,
57 const wxPoint& pos,
58 const wxSize& size, long style,
59 const wxValidator& validator,
60 const wxString& name)
61 {
62 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
63 style |= wxBORDER_NONE;
64
65 SetName(name);
66 #if wxUSE_VALIDATORS
67 SetValidator(validator);
68 #endif // wxUSE_VALIDATORS
69
70 if (parent) parent->AddChild(this);
71
72 SetBackgroundColour(parent->GetBackgroundColour()) ;
73 SetForegroundColour(parent->GetForegroundColour()) ;
74
75 m_staticValue = (WXHWND) NULL;;
76 m_staticMin = (WXHWND) NULL;;
77 m_staticMax = (WXHWND) NULL;;
78 m_pageSize = 1;
79 m_lineSize = 1;
80 m_windowStyle = style;
81 m_tickFreq = 0;
82
83 if ( id == -1 )
84 m_windowId = (int)NewControlId();
85 else
86 m_windowId = id;
87
88 int x = pos.x;
89 int y = pos.y;
90 int width = size.x;
91 int height = size.y;
92
93 long msStyle = 0;
94 long wstyle = 0;
95
96 if ( m_windowStyle & wxSL_LABELS )
97 {
98 msStyle |= SS_CENTER;
99
100 WXDWORD exStyle = 0;
101 msStyle |= MSWGetStyle(GetWindowStyle(), & exStyle) ;
102
103 m_staticValue = (WXHWND) CreateWindowEx
104 (
105 exStyle, wxT("STATIC"), NULL,
106 msStyle,
107 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
108 wxGetInstance(), NULL
109 );
110
111 // Now create min static control
112 wxString minLabel;
113 minLabel.Printf(wxT("%d"), minValue);
114 wstyle = STATIC_FLAGS;
115 if ( m_windowStyle & wxCLIP_SIBLINGS )
116 msStyle |= WS_CLIPSIBLINGS;
117 m_staticMin = (WXHWND) CreateWindowEx
118 (
119 0, wxT("STATIC"), minLabel,
120 wstyle,
121 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
122 wxGetInstance(), NULL
123 );
124 }
125
126 WXDWORD exStyle = 0;
127
128 msStyle = MSWGetStyle(GetWindowStyle(), & exStyle) ;
129
130 if (m_windowStyle & wxSL_VERTICAL)
131 msStyle = TBS_VERT | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
132 else
133 msStyle = TBS_HORZ | WS_CHILD | WS_VISIBLE | WS_TABSTOP ;
134
135 if ( m_windowStyle & wxSL_AUTOTICKS )
136 msStyle |= TBS_AUTOTICKS ;
137
138 if ( m_windowStyle & wxSL_LEFT )
139 msStyle |= TBS_LEFT;
140 else if ( m_windowStyle & wxSL_RIGHT )
141 msStyle |= TBS_RIGHT;
142 else if ( m_windowStyle & wxSL_TOP )
143 msStyle |= TBS_TOP;
144 else if ( m_windowStyle & wxSL_BOTTOM )
145 msStyle |= TBS_BOTTOM;
146 else if ( m_windowStyle & wxSL_BOTH )
147 msStyle |= TBS_BOTH;
148 else if ( ! (m_windowStyle & wxSL_AUTOTICKS) )
149 msStyle |= TBS_NOTICKS;
150
151 if ( m_windowStyle & wxSL_SELRANGE )
152 msStyle |= TBS_ENABLESELRANGE;
153
154 HWND scroll_bar = CreateWindowEx
155 (
156 exStyle, TRACKBAR_CLASS, wxT(""),
157 msStyle,
158 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
159 wxGetInstance(), NULL
160 );
161
162 m_rangeMax = maxValue;
163 m_rangeMin = minValue;
164
165 m_pageSize = (int)((maxValue-minValue)/10);
166
167 ::SendMessage(scroll_bar, TBM_SETRANGE, TRUE, MAKELONG(minValue, maxValue));
168 ::SendMessage(scroll_bar, TBM_SETPOS, TRUE, (LPARAM)value);
169 ::SendMessage(scroll_bar, TBM_SETPAGESIZE, 0, (LPARAM)m_pageSize);
170
171 m_hWnd = (WXHWND)scroll_bar;
172
173 SubclassWin(GetHWND());
174
175 ::SetWindowText((HWND) m_hWnd, wxT(""));
176
177 SetFont(parent->GetFont());
178
179 if ( m_windowStyle & wxSL_LABELS )
180 {
181 // Finally, create max value static item
182 wxString maxLabel;
183 maxLabel.Printf(wxT("%d"), maxValue);
184 wstyle = STATIC_FLAGS;
185
186 if ( m_windowStyle & wxCLIP_SIBLINGS )
187 msStyle |= WS_CLIPSIBLINGS;
188
189 m_staticMax = (WXHWND) CreateWindowEx
190 (
191 0, wxT("STATIC"), maxLabel,
192 wstyle,
193 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)NewControlId(),
194 wxGetInstance(), NULL
195 );
196
197
198 if (GetFont().Ok())
199 {
200 if (GetFont().GetResourceHandle())
201 {
202 if ( m_staticMin )
203 ::SendMessage((HWND) m_staticMin, WM_SETFONT,
204 (WPARAM) GetFont().GetResourceHandle(), 0L);
205
206 if ( m_staticMax )
207 ::SendMessage((HWND) m_staticMax, WM_SETFONT,
208 (WPARAM) GetFont().GetResourceHandle(), 0L);
209
210 if (m_staticValue)
211 ::SendMessage((HWND) m_staticValue, WM_SETFONT,
212 (WPARAM) GetFont().GetResourceHandle(), 0L);
213 }
214 }
215 }
216
217 SetSize(x, y, width, height);
218 SetValue(value);
219
220 return TRUE;
221 }
222
223 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
224 WXWORD WXUNUSED(pos), WXHWND control)
225 {
226 wxEventType scrollEvent;
227 switch ( wParam )
228 {
229 case SB_TOP:
230 scrollEvent = wxEVT_SCROLL_TOP;
231 break;
232
233 case SB_BOTTOM:
234 scrollEvent = wxEVT_SCROLL_BOTTOM;
235 break;
236
237 case SB_LINEUP:
238 scrollEvent = wxEVT_SCROLL_LINEUP;
239 break;
240
241 case SB_LINEDOWN:
242 scrollEvent = wxEVT_SCROLL_LINEDOWN;
243 break;
244
245 case SB_PAGEUP:
246 scrollEvent = wxEVT_SCROLL_PAGEUP;
247 break;
248
249 case SB_PAGEDOWN:
250 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
251 break;
252
253 case SB_THUMBTRACK:
254 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
255 break;
256
257 case SB_THUMBPOSITION:
258 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
259 break;
260
261 case SB_ENDSCROLL:
262 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
263 break;
264
265 default:
266 // unknown scroll event?
267 return FALSE;
268 }
269
270 int newPos = (int) ::SendMessage((HWND) control, TBM_GETPOS, 0, 0);
271 if ( (newPos < GetMin()) || (newPos > GetMax()) )
272 {
273 // out of range - but we did process it
274 return TRUE;
275 }
276
277 SetValue(newPos);
278
279 wxScrollEvent event(scrollEvent, m_windowId);
280 event.SetPosition(newPos);
281 event.SetEventObject( this );
282 GetEventHandler()->ProcessEvent(event);
283
284 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, GetId() );
285 cevent.SetInt( newPos );
286 cevent.SetEventObject( this );
287
288 return GetEventHandler()->ProcessEvent( cevent );
289 }
290
291 wxSlider95::~wxSlider95()
292 {
293 if (m_staticMin)
294 {
295 ::DestroyWindow((HWND) m_staticMin);
296 m_staticMin = (WXHWND) NULL;
297 }
298
299 if (m_staticMax)
300 {
301 ::DestroyWindow((HWND) m_staticMax);
302 m_staticMax = (WXHWND) NULL;
303 }
304
305 if (m_staticValue)
306 {
307 ::DestroyWindow((HWND) m_staticValue);
308 m_staticValue = (WXHWND) NULL;
309 }
310 }
311
312 int wxSlider95::GetValue() const
313 {
314 return ::SendMessage(GetHwnd(), TBM_GETPOS, 0, 0);
315 }
316
317 void wxSlider95::SetValue(int value)
318 {
319 ::SendMessage(GetHwnd(), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)value);
320
321 if (m_staticValue)
322 {
323 wxString str;
324 str.Printf(wxT("%d"), value);
325 ::SetWindowText((HWND) m_staticValue, str);
326 }
327 }
328
329 void wxSlider95::DoGetSize(int *width, int *height) const
330 {
331 GetSize(width, height);
332 }
333
334 void wxSlider95::GetSize(int *width, int *height) const
335 {
336 RECT rect;
337 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
338
339 wxFindMaxSize(GetHWND(), &rect);
340
341 if (m_staticMin)
342 wxFindMaxSize(m_staticMin, &rect);
343
344 if (m_staticMax)
345 wxFindMaxSize(m_staticMax, &rect);
346
347 if (m_staticValue)
348 wxFindMaxSize(m_staticValue, &rect);
349
350 *width = rect.right - rect.left;
351 *height = rect.bottom - rect.top;
352 }
353
354 void wxSlider95::GetPosition(int *x, int *y) const
355 {
356 wxWindow *parent = GetParent();
357 RECT rect;
358 rect.left = -1; rect.right = -1; rect.top = -1; rect.bottom = -1;
359
360 wxFindMaxSize(GetHWND(), &rect);
361
362 if (m_staticMin)
363 wxFindMaxSize(m_staticMin, &rect);
364 if (m_staticMax)
365 wxFindMaxSize(m_staticMax, &rect);
366 if (m_staticValue)
367 wxFindMaxSize(m_staticValue, &rect);
368
369 // Since we now have the absolute screen coords,
370 // if there's a parent we must subtract its top left corner
371 POINT point;
372 point.x = rect.left;
373 point.y = rect.top;
374 if (parent)
375 ::ScreenToClient((HWND) parent->GetHWND(), &point);
376
377 // We may be faking the client origin.
378 // So a window that's really at (0, 30) may appear
379 // (to wxWin apps) to be at (0, 0).
380 if (GetParent())
381 {
382 wxPoint pt(GetParent()->GetClientAreaOrigin());
383 point.x -= pt.x;
384 point.y -= pt.y;
385 }
386
387 *x = point.x;
388 *y = point.y;
389 }
390
391 // TODO one day, make sense of all this horros and replace it with a readable
392 // DoGetBestSize()
393 void wxSlider95::DoSetSize(int x, int y, int width, int height, int sizeFlags)
394 {
395 int x1 = x;
396 int y1 = y;
397 int w1 = width;
398 int h1 = height;
399
400 int currentX, currentY;
401 GetPosition(&currentX, &currentY);
402 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
403 x1 = currentX;
404 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
405 y1 = currentY;
406
407 AdjustForParentClientOrigin(x1, y1, sizeFlags);
408
409 wxChar buf[300];
410
411 int x_offset = x;
412 int y_offset = y;
413
414 int cx; // slider,min,max sizes
415 int cy;
416 int cyf;
417
418 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
419
420 if ((m_windowStyle & wxSL_VERTICAL) != wxSL_VERTICAL)
421 {
422 if ( m_windowStyle & wxSL_LABELS )
423 {
424 int min_len = 0;
425
426 ::GetWindowText((HWND) m_staticMin, buf, 300);
427 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
428
429 int max_len = 0;
430
431 ::GetWindowText((HWND) m_staticMax, buf, 300);
432 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
433 if (m_staticValue)
434 {
435 int new_width = (int)(wxMax(min_len, max_len));
436 int valueHeight = (int)cyf;
437 #ifdef __WIN32__
438 // For some reason, under Win95, the text edit control has
439 // a lot of space before the first character
440 new_width += 3*cx;
441 #endif
442 // The height needs to be a bit bigger under Win95 if
443 // using native 3D effects.
444 valueHeight = (int) (valueHeight * 1.5) ;
445 ::MoveWindow((HWND) m_staticValue, x_offset, y_offset,
446 new_width, valueHeight, TRUE);
447 x_offset += new_width + cx;
448 }
449
450 MoveWindow((HWND) m_staticMin, x_offset, y_offset,
451 (int) min_len, cy, TRUE);
452 x_offset += (int)(min_len + cx);
453
454 int slider_length = (int)(w1 - x_offset - max_len - cx);
455
456 int slider_height = h1;
457 if (slider_height < 0 )
458 slider_height = 20;
459
460 // Slider must have a minimum/default length/height
461 if (slider_length < 100)
462 slider_length = 100;
463
464 ::MoveWindow(GetHwnd(), x_offset, y_offset,
465 slider_length, slider_height, TRUE);
466 x_offset += slider_length + cx;
467
468 MoveWindow((HWND) m_staticMax, x_offset, y_offset,
469 (int) max_len, cy, TRUE);
470 }
471 else
472 {
473 // No labels
474 // If we're prepared to use the existing size, then...
475 if
476 (
477 width == -1
478 && height == -1
479 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)
480 )
481 {
482 GetSize(&w1, &h1);
483 }
484
485 if ( w1 < 0 )
486 w1 = 200;
487 if ( h1 < 0 )
488 h1 = 20;
489
490 ::MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
491 }
492 }
493 else
494 {
495 if ( m_windowStyle & wxSL_LABELS )
496 {
497 int min_len;
498 ::GetWindowText((HWND) m_staticMin, buf, 300);
499 GetTextExtent(buf, &min_len, &cyf,NULL,NULL, & this->GetFont());
500
501 int max_len;
502 ::GetWindowText((HWND) m_staticMax, buf, 300);
503 GetTextExtent(buf, &max_len, &cyf,NULL,NULL, & this->GetFont());
504
505 if (m_staticValue)
506 {
507 int new_width = (int)(wxMax(min_len, max_len));
508 int valueHeight = (int)cyf;
509 /*** Suggested change by George Tasker - remove this block...
510 #ifdef __WIN32__
511 // For some reason, under Win95, the text edit control has
512 // a lot of space before the first character
513 new_width += 3*cx;
514 #endif
515 ... and replace with following line: */
516 new_width += cx;
517
518 // The height needs to be a bit bigger under Win95 if
519 // using native 3D effects.
520 valueHeight = (int) (valueHeight * 1.5) ;
521
522 ::MoveWindow((HWND) m_staticValue, x_offset, y_offset,
523 new_width, valueHeight, TRUE);
524 y_offset += valueHeight;
525 }
526
527 ::MoveWindow((HWND) m_staticMin, x_offset, y_offset,
528 (int) min_len, cy, TRUE);
529 y_offset += cy;
530
531 int slider_length = (int)(h1 - y_offset - cy - cy);
532
533 int slider_width = w1;
534 if (slider_width < 0 )
535 slider_width = 20;
536
537 // Slider must have a minimum/default length
538 if (slider_length < 100)
539 slider_length = 100;
540
541 ::MoveWindow(GetHwnd(), x_offset, y_offset,
542 slider_width, slider_length, TRUE);
543 y_offset += slider_length;
544
545 ::MoveWindow((HWND) m_staticMax, x_offset, y_offset,
546 (int)max_len, cy, TRUE);
547 }
548 else
549 {
550 // No labels
551 // If we're prepared to use the existing size, then...
552 if
553 (
554 width == -1 && height == -1
555 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)
556 )
557 {
558 GetSize(&w1, &h1);
559 }
560
561 if ( w1 < 0 )
562 w1 = 20;
563 if ( h1 < 0 )
564 h1 = 200;
565
566 ::MoveWindow(GetHwnd(), x1, y1, w1, h1, TRUE);
567 }
568 }
569 }
570
571 void wxSlider95::SetRange(int minValue, int maxValue)
572 {
573 m_rangeMin = minValue;
574 m_rangeMax = maxValue;
575
576 ::SendMessage(GetHwnd(), TBM_SETRANGE, TRUE, MAKELONG(minValue, maxValue));
577
578 wxChar buf[40];
579 if ( m_staticMin )
580 {
581 wxSprintf(buf, wxT("%d"), m_rangeMin);
582 ::SetWindowText((HWND) m_staticMin, buf);
583 }
584
585 if ( m_staticMax )
586 {
587 wxSprintf(buf, wxT("%d"), m_rangeMax);
588 ::SetWindowText((HWND) m_staticMax, buf);
589 }
590 }
591
592 WXHBRUSH wxSlider95::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
593 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
594 {
595 if ( nCtlColor == CTLCOLOR_SCROLLBAR )
596 return 0;
597
598 // Otherwise, it's a static
599 return wxControl::OnCtlColor(pDC, pWnd, nCtlColor, message, wParam, lParam);
600 }
601
602 // For trackbars only
603 void wxSlider95::SetTickFreq(int n, int pos)
604 {
605 m_tickFreq = n;
606 ::SendMessage( GetHwnd(), TBM_SETTICFREQ, (WPARAM) n, (LPARAM) pos );
607 }
608
609 void wxSlider95::SetPageSize(int pageSize)
610 {
611 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE, (WPARAM) 0, (LPARAM) pageSize );
612 m_pageSize = pageSize;
613 }
614
615 int wxSlider95::GetPageSize() const
616 {
617 return m_pageSize;
618 }
619
620 void wxSlider95::ClearSel()
621 {
622 ::SendMessage( GetHwnd(), TBM_CLEARSEL, (WPARAM) TRUE, (LPARAM) 0 );
623 }
624
625 void wxSlider95::ClearTicks()
626 {
627 ::SendMessage( GetHwnd(), TBM_CLEARTICS, (WPARAM) TRUE, (LPARAM) 0 );
628 }
629
630 void wxSlider95::SetLineSize(int lineSize)
631 {
632 m_lineSize = lineSize;
633 ::SendMessage( GetHwnd(), TBM_SETLINESIZE, (WPARAM) 0, (LPARAM) lineSize );
634 }
635
636 int wxSlider95::GetLineSize() const
637 {
638 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE,
639 (WPARAM) 0, (LPARAM) 0 );
640 }
641
642 int wxSlider95::GetSelEnd() const
643 {
644 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND,
645 (WPARAM) 0, (LPARAM) 0 );
646 }
647
648 int wxSlider95::GetSelStart() const
649 {
650 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART,
651 (WPARAM) 0, (LPARAM) 0 );
652 }
653
654 void wxSlider95::SetSelection(int minPos, int maxPos)
655 {
656 ::SendMessage(GetHwnd(), TBM_SETSEL,
657 (WPARAM) TRUE, (LPARAM) MAKELONG( minPos, maxPos) );
658 }
659
660 void wxSlider95::SetThumbLength(int len)
661 {
662 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH, (WPARAM) len, (LPARAM) 0 );
663 }
664
665 int wxSlider95::GetThumbLength() const
666 {
667 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH,
668 (WPARAM) 0, (LPARAM) 0 );
669 }
670
671 void wxSlider95::SetTick(int tickPos)
672 {
673 ::SendMessage( GetHwnd(), TBM_SETTIC, (WPARAM) 0, (LPARAM) tickPos );
674 }
675
676 bool wxSlider95::ContainsHWND(WXHWND hWnd) const
677 {
678 return
679 (
680 hWnd == GetStaticMin()
681 || hWnd == GetStaticMax()
682 || hWnd == GetEditValue()
683 );
684 }
685
686 void wxSlider95::Command (wxCommandEvent & event)
687 {
688 SetValue (event.GetInt());
689 ProcessCommand (event);
690 }
691
692 bool wxSlider95::Show(bool show)
693 {
694 wxWindow::Show(show);
695
696 int cshow;
697 if (show)
698 cshow = SW_SHOW;
699 else
700 cshow = SW_HIDE;
701
702 if(m_staticValue)
703 ShowWindow((HWND) m_staticValue, (BOOL)cshow);
704
705 if(m_staticMin)
706 ShowWindow((HWND) m_staticMin, (BOOL)cshow);
707
708 if(m_staticMax)
709 ShowWindow((HWND) m_staticMax, (BOOL)cshow);
710
711 return TRUE;
712 }
713
714 #endif
715 // __WIN95__
716
717 #endif // wxUSE_SLIDER