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