1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSlider95, using the Win95 trackbar control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "slider95.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
28 #include "wx/slider.h"
33 #include "wx/msw/slider95.h"
34 #include "wx/msw/private.h"
36 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
40 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
43 wxSlider95::wxSlider95()
45 m_staticValue
= (WXHWND
) NULL
;
46 m_staticMin
= (WXHWND
) NULL
;
47 m_staticMax
= (WXHWND
) NULL
;
55 bool wxSlider95::Create(wxWindow
*parent
, wxWindowID id
,
56 int value
, int minValue
, int maxValue
,
58 const wxSize
& size
, long style
,
59 const wxValidator
& validator
,
64 SetValidator(validator
);
65 #endif // wxUSE_VALIDATORS
67 if (parent
) parent
->AddChild(this);
69 SetBackgroundColour(parent
->GetBackgroundColour()) ;
70 SetForegroundColour(parent
->GetForegroundColour()) ;
72 m_staticValue
= (WXHWND
) NULL
;;
73 m_staticMin
= (WXHWND
) NULL
;;
74 m_staticMax
= (WXHWND
) NULL
;;
77 m_windowStyle
= style
;
81 m_windowId
= (int)NewControlId();
93 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
94 msStyle
|= WS_CLIPSIBLINGS
;
96 if ( m_windowStyle
& wxSL_LABELS
)
98 msStyle
|= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| SS_CENTER
;
101 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
103 m_staticValue
= (WXHWND
) CreateWindowEx
105 exStyle
, wxT("STATIC"), NULL
,
107 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
108 wxGetInstance(), NULL
111 // Now create min static control
112 wxSprintf(wxBuffer
, wxT("%d"), minValue
);
113 wstyle
= STATIC_FLAGS
;
114 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
115 msStyle
|= WS_CLIPSIBLINGS
;
116 m_staticMin
= (WXHWND
) CreateWindowEx
118 0, wxT("STATIC"), wxBuffer
,
120 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
121 wxGetInstance(), NULL
127 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
128 msStyle
|= WS_CLIPSIBLINGS
;
130 if (m_windowStyle
& wxSL_VERTICAL
)
131 msStyle
= TBS_VERT
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
133 msStyle
= TBS_HORZ
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
;
135 if ( m_windowStyle
& wxSL_AUTOTICKS
)
136 msStyle
|= TBS_AUTOTICKS
;
138 if ( m_windowStyle
& wxSL_LEFT
)
140 else if ( m_windowStyle
& wxSL_RIGHT
)
141 msStyle
|= TBS_RIGHT
;
142 else if ( m_windowStyle
& wxSL_TOP
)
144 else if ( m_windowStyle
& wxSL_BOTTOM
)
145 msStyle
|= TBS_BOTTOM
;
146 else if ( m_windowStyle
& wxSL_BOTH
)
148 else if ( ! (m_windowStyle
& wxSL_AUTOTICKS
) )
149 msStyle
|= TBS_NOTICKS
;
151 if ( m_windowStyle
& wxSL_SELRANGE
)
152 msStyle
|= TBS_ENABLESELRANGE
;
154 HWND scroll_bar
= CreateWindowEx
156 MakeExtendedStyle(m_windowStyle
), TRACKBAR_CLASS
, wxBuffer
,
158 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
159 wxGetInstance(), NULL
162 m_rangeMax
= maxValue
;
163 m_rangeMin
= minValue
;
165 m_pageSize
= (int)((maxValue
-minValue
)/10);
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
);
171 m_hWnd
= (WXHWND
)scroll_bar
;
173 SubclassWin(GetHWND());
175 ::SetWindowText((HWND
) m_hWnd
, wxT(""));
177 SetFont(parent
->GetFont());
179 if ( m_windowStyle
& wxSL_LABELS
)
181 // Finally, create max value static item
182 wxSprintf(wxBuffer
, wxT("%d"), maxValue
);
183 wstyle
= STATIC_FLAGS
;
185 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
186 msStyle
|= WS_CLIPSIBLINGS
;
188 m_staticMax
= (WXHWND
) CreateWindowEx
190 0, wxT("STATIC"), wxBuffer
,
192 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)NewControlId(),
193 wxGetInstance(), NULL
199 if (GetFont().GetResourceHandle())
202 ::SendMessage((HWND
) m_staticMin
, WM_SETFONT
,
203 (WPARAM
) GetFont().GetResourceHandle(), 0L);
206 ::SendMessage((HWND
) m_staticMax
, WM_SETFONT
,
207 (WPARAM
) GetFont().GetResourceHandle(), 0L);
210 ::SendMessage((HWND
) m_staticValue
, WM_SETFONT
,
211 (WPARAM
) GetFont().GetResourceHandle(), 0L);
216 SetSize(x
, y
, width
, height
);
222 bool wxSlider95::MSWOnScroll(int WXUNUSED(orientation
), WXWORD wParam
,
223 WXWORD
WXUNUSED(pos
), WXHWND control
)
225 wxEventType scrollEvent
;
229 scrollEvent
= wxEVT_SCROLL_TOP
;
233 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
237 scrollEvent
= wxEVT_SCROLL_LINEUP
;
241 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
245 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
249 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
253 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
256 case SB_THUMBPOSITION
:
257 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
261 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
265 // unknown scroll event?
269 int newPos
= (int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0);
270 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
272 // out of range - but we did process it
278 wxScrollEvent
event(scrollEvent
, m_windowId
);
279 event
.SetPosition(newPos
);
280 event
.SetEventObject( this );
281 GetEventHandler()->ProcessEvent(event
);
283 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
284 cevent
.SetInt( newPos
);
285 cevent
.SetEventObject( this );
287 return GetEventHandler()->ProcessEvent( cevent
);
290 wxSlider95::~wxSlider95()
294 ::DestroyWindow((HWND
) m_staticMin
);
295 m_staticMin
= (WXHWND
) NULL
;
300 ::DestroyWindow((HWND
) m_staticMax
);
301 m_staticMax
= (WXHWND
) NULL
;
306 ::DestroyWindow((HWND
) m_staticValue
);
307 m_staticValue
= (WXHWND
) NULL
;
311 int wxSlider95::GetValue() const
313 return ::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0);
316 void wxSlider95::SetValue(int value
)
318 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)value
);
322 wxSprintf(wxBuffer
, wxT("%d"), value
);
323 ::SetWindowText((HWND
) m_staticValue
, wxBuffer
);
327 void wxSlider95::DoGetSize(int *width
, int *height
) const
329 GetSize(width
, height
);
332 void wxSlider95::GetSize(int *width
, int *height
) const
335 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
337 wxFindMaxSize(GetHWND(), &rect
);
340 wxFindMaxSize(m_staticMin
, &rect
);
343 wxFindMaxSize(m_staticMax
, &rect
);
346 wxFindMaxSize(m_staticValue
, &rect
);
348 *width
= rect
.right
- rect
.left
;
349 *height
= rect
.bottom
- rect
.top
;
352 void wxSlider95::GetPosition(int *x
, int *y
) const
354 wxWindow
*parent
= GetParent();
356 rect
.left
= -1; rect
.right
= -1; rect
.top
= -1; rect
.bottom
= -1;
358 wxFindMaxSize(GetHWND(), &rect
);
361 wxFindMaxSize(m_staticMin
, &rect
);
363 wxFindMaxSize(m_staticMax
, &rect
);
365 wxFindMaxSize(m_staticValue
, &rect
);
367 // Since we now have the absolute screen coords,
368 // if there's a parent we must subtract its top left corner
373 ::ScreenToClient((HWND
) parent
->GetHWND(), &point
);
375 // We may be faking the client origin.
376 // So a window that's really at (0, 30) may appear
377 // (to wxWin apps) to be at (0, 0).
380 wxPoint
pt(GetParent()->GetClientAreaOrigin());
389 // TODO one day, make sense of all this horros and replace it with a readable
391 void wxSlider95::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
398 int currentX
, currentY
;
399 GetPosition(¤tX
, ¤tY
);
400 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
402 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
405 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
412 int cx
; // slider,min,max sizes
416 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
418 if ((m_windowStyle
& wxSL_VERTICAL
) != wxSL_VERTICAL
)
420 if ( m_windowStyle
& wxSL_LABELS
)
424 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
425 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
429 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
430 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
433 int new_width
= (int)(wxMax(min_len
, max_len
));
434 int valueHeight
= (int)cyf
;
436 // For some reason, under Win95, the text edit control has
437 // a lot of space before the first character
440 // The height needs to be a bit bigger under Win95 if
441 // using native 3D effects.
442 valueHeight
= (int) (valueHeight
* 1.5) ;
443 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
444 new_width
, valueHeight
, TRUE
);
445 x_offset
+= new_width
+ cx
;
448 MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
449 (int) min_len
, cy
, TRUE
);
450 x_offset
+= (int)(min_len
+ cx
);
452 int slider_length
= (int)(w1
- x_offset
- max_len
- cx
);
454 int slider_height
= h1
;
455 if (slider_height
< 0 )
458 // Slider must have a minimum/default length/height
459 if (slider_length
< 100)
462 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
463 slider_length
, slider_height
, TRUE
);
464 x_offset
+= slider_length
+ cx
;
466 MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
467 (int) max_len
, cy
, TRUE
);
472 // If we're prepared to use the existing size, then...
477 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
488 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
493 if ( m_windowStyle
& wxSL_LABELS
)
496 ::GetWindowText((HWND
) m_staticMin
, buf
, 300);
497 GetTextExtent(buf
, &min_len
, &cyf
,NULL
,NULL
, & this->GetFont());
500 ::GetWindowText((HWND
) m_staticMax
, buf
, 300);
501 GetTextExtent(buf
, &max_len
, &cyf
,NULL
,NULL
, & this->GetFont());
505 int new_width
= (int)(wxMax(min_len
, max_len
));
506 int valueHeight
= (int)cyf
;
507 /*** Suggested change by George Tasker - remove this block...
509 // For some reason, under Win95, the text edit control has
510 // a lot of space before the first character
513 ... and replace with following line: */
516 // The height needs to be a bit bigger under Win95 if
517 // using native 3D effects.
518 valueHeight
= (int) (valueHeight
* 1.5) ;
520 ::MoveWindow((HWND
) m_staticValue
, x_offset
, y_offset
,
521 new_width
, valueHeight
, TRUE
);
522 y_offset
+= valueHeight
;
525 ::MoveWindow((HWND
) m_staticMin
, x_offset
, y_offset
,
526 (int) min_len
, cy
, TRUE
);
529 int slider_length
= (int)(h1
- y_offset
- cy
- cy
);
531 int slider_width
= w1
;
532 if (slider_width
< 0 )
535 // Slider must have a minimum/default length
536 if (slider_length
< 100)
539 ::MoveWindow(GetHwnd(), x_offset
, y_offset
,
540 slider_width
, slider_length
, TRUE
);
541 y_offset
+= slider_length
;
543 ::MoveWindow((HWND
) m_staticMax
, x_offset
, y_offset
,
544 (int)max_len
, cy
, TRUE
);
549 // If we're prepared to use the existing size, then...
552 width
== -1 && height
== -1
553 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
)
564 ::MoveWindow(GetHwnd(), x1
, y1
, w1
, h1
, TRUE
);
569 void wxSlider95::SetRange(int minValue
, int maxValue
)
571 m_rangeMin
= minValue
;
572 m_rangeMax
= maxValue
;
574 ::SendMessage(GetHwnd(), TBM_SETRANGE
, TRUE
, MAKELONG(minValue
, maxValue
));
579 wxSprintf(buf
, wxT("%d"), m_rangeMin
);
580 ::SetWindowText((HWND
) m_staticMin
, buf
);
585 wxSprintf(buf
, wxT("%d"), m_rangeMax
);
586 ::SetWindowText((HWND
) m_staticMax
, buf
);
590 WXHBRUSH
wxSlider95::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
591 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
593 if ( nCtlColor
== CTLCOLOR_SCROLLBAR
)
596 // Otherwise, it's a static
597 return wxControl::OnCtlColor(pDC
, pWnd
, nCtlColor
, message
, wParam
, lParam
);
600 // For trackbars only
601 void wxSlider95::SetTickFreq(int n
, int pos
)
604 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
607 void wxSlider95::SetPageSize(int pageSize
)
609 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
610 m_pageSize
= pageSize
;
613 int wxSlider95::GetPageSize() const
618 void wxSlider95::ClearSel()
620 ::SendMessage( GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0 );
623 void wxSlider95::ClearTicks()
625 ::SendMessage( GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0 );
628 void wxSlider95::SetLineSize(int lineSize
)
630 m_lineSize
= lineSize
;
631 ::SendMessage( GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
634 int wxSlider95::GetLineSize() const
636 return (int) ::SendMessage( GetHwnd(), TBM_GETLINESIZE
,
637 (WPARAM
) 0, (LPARAM
) 0 );
640 int wxSlider95::GetSelEnd() const
642 return (int) ::SendMessage( GetHwnd(), TBM_SETSELEND
,
643 (WPARAM
) 0, (LPARAM
) 0 );
646 int wxSlider95::GetSelStart() const
648 return (int) ::SendMessage( GetHwnd(), TBM_GETSELSTART
,
649 (WPARAM
) 0, (LPARAM
) 0 );
652 void wxSlider95::SetSelection(int minPos
, int maxPos
)
654 ::SendMessage(GetHwnd(), TBM_SETSEL
,
655 (WPARAM
) TRUE
, (LPARAM
) MAKELONG( minPos
, maxPos
) );
658 void wxSlider95::SetThumbLength(int len
)
660 ::SendMessage( GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0 );
663 int wxSlider95::GetThumbLength() const
665 return (int) ::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
,
666 (WPARAM
) 0, (LPARAM
) 0 );
669 void wxSlider95::SetTick(int tickPos
)
671 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
674 bool wxSlider95::ContainsHWND(WXHWND hWnd
) const
678 hWnd
== GetStaticMin()
679 || hWnd
== GetStaticMax()
680 || hWnd
== GetEditValue()
684 void wxSlider95::Command (wxCommandEvent
& event
)
686 SetValue (event
.GetInt());
687 ProcessCommand (event
);
690 bool wxSlider95::Show(bool show
)
692 wxWindow::Show(show
);
701 ShowWindow((HWND
) m_staticValue
, (BOOL
)cshow
);
704 ShowWindow((HWND
) m_staticMin
, (BOOL
)cshow
);
707 ShowWindow((HWND
) m_staticMax
, (BOOL
)cshow
);
715 #endif // wxUSE_SLIDER