1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/slider.cpp
3 // Purpose: wxSlider, using the Win95 (and later) trackbar control
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart 1998
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
34 #include "wx/slider.h"
35 #include "wx/msw/subwin.h"
37 #if !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // indices of labels in wxSlider::m_labels
54 // the gap between the slider and the labels, in pixels
55 static const int HGAP
= 5;
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 #if wxUSE_EXTENDED_RTTI
62 WX_DEFINE_FLAGS( wxSliderStyle
)
64 wxBEGIN_FLAGS( wxSliderStyle
)
65 // new style border flags, we put them first to
66 // use them for streaming out
67 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
68 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
69 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
70 wxFLAGS_MEMBER(wxBORDER_RAISED
)
71 wxFLAGS_MEMBER(wxBORDER_STATIC
)
72 wxFLAGS_MEMBER(wxBORDER_NONE
)
74 // old style border flags
75 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
76 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
77 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
78 wxFLAGS_MEMBER(wxRAISED_BORDER
)
79 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
80 wxFLAGS_MEMBER(wxBORDER
)
82 // standard window styles
83 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
84 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
85 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
86 wxFLAGS_MEMBER(wxWANTS_CHARS
)
87 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
88 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
89 wxFLAGS_MEMBER(wxVSCROLL
)
90 wxFLAGS_MEMBER(wxHSCROLL
)
92 wxFLAGS_MEMBER(wxSL_HORIZONTAL
)
93 wxFLAGS_MEMBER(wxSL_VERTICAL
)
94 wxFLAGS_MEMBER(wxSL_AUTOTICKS
)
95 wxFLAGS_MEMBER(wxSL_LABELS
)
96 wxFLAGS_MEMBER(wxSL_LEFT
)
97 wxFLAGS_MEMBER(wxSL_TOP
)
98 wxFLAGS_MEMBER(wxSL_RIGHT
)
99 wxFLAGS_MEMBER(wxSL_BOTTOM
)
100 wxFLAGS_MEMBER(wxSL_BOTH
)
101 wxFLAGS_MEMBER(wxSL_SELRANGE
)
102 wxFLAGS_MEMBER(wxSL_INVERSE
)
104 wxEND_FLAGS( wxSliderStyle
)
106 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider
, wxControl
,"wx/scrolbar.h")
108 wxBEGIN_PROPERTIES_TABLE(wxSlider
)
109 wxEVENT_RANGE_PROPERTY( Scroll
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_CHANGED
, wxScrollEvent
)
110 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SLIDER_UPDATED
, wxCommandEvent
)
112 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
113 wxPROPERTY( Minimum
, int , SetMin
, GetMin
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
114 wxPROPERTY( Maximum
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
115 wxPROPERTY( PageSize
, int , SetPageSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
116 wxPROPERTY( LineSize
, int , SetLineSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
117 wxPROPERTY( ThumbLength
, int , SetThumbLength
, GetThumbLength
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
118 wxPROPERTY_FLAGS( WindowStyle
, wxSliderStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
119 wxEND_PROPERTIES_TABLE()
121 wxBEGIN_HANDLERS_TABLE(wxSlider
)
122 wxEND_HANDLERS_TABLE()
124 wxCONSTRUCTOR_8( wxSlider
, wxWindow
* , Parent
, wxWindowID
, Id
, int , Value
, int , Minimum
, int , Maximum
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
126 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
129 // ============================================================================
130 // wxSlider implementation
131 // ============================================================================
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 void wxSlider::Init()
147 m_isDragging
= false;
151 wxSlider::Create(wxWindow
*parent
,
159 const wxValidator
& validator
,
160 const wxString
& name
)
162 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
163 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
164 // reasons we can't really change it, instead try to infer the orientation
165 // from the flags given to us here
166 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
170 style
|= wxSL_VERTICAL
;
175 style
|= wxSL_HORIZONTAL
;
179 // no specific direction, do we have at least the orientation?
180 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
182 // no, choose default
183 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
187 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) || !(style
& wxSL_HORIZONTAL
),
188 _T("incompatible slider direction and orientation") );
191 // initialize everything
192 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
195 // ensure that we have correct values for GetLabelsSize()
196 m_rangeMin
= minValue
;
197 m_rangeMax
= maxValue
;
199 // create the labels first, so that our DoGetBestSize() could take them
202 // note that we could simply create 3 wxStaticTexts here but it could
203 // result in some observable side effects at wx level (e.g. the parent of
204 // wxSlider would have 3 more children than expected) and so we prefer not
205 // to do it like this
206 if ( m_windowStyle
& wxSL_LABELS
)
208 m_labels
= new wxSubwindows(SliderLabel_Last
);
210 HWND hwndParent
= GetHwndOf(parent
);
211 for ( size_t n
= 0; n
< SliderLabel_Last
; n
++ )
213 (*m_labels
)[n
] = ::CreateWindow
217 WS_CHILD
| WS_VISIBLE
| SS_CENTER
,
220 (HMENU
)NewControlId(),
226 m_labels
->SetFont(GetFont());
229 // now create the main control too
230 if ( !MSWCreateControl(TRACKBAR_CLASS
, wxEmptyString
, pos
, size
) )
233 // and initialize everything
234 SetRange(minValue
, maxValue
);
236 SetPageSize((maxValue
- minValue
)/10);
238 // we need to position the labels correctly if we have them and if
239 // SetSize() hadn't been called before (when best size was determined by
240 // MSWCreateControl()) as in this case they haven't been put in place yet
241 if ( m_labels
&& size
.x
!= wxDefaultCoord
&& size
.y
!= wxDefaultCoord
)
249 WXDWORD
wxSlider::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
251 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
253 // TBS_HORZ, TBS_RIGHT and TBS_BOTTOM are 0 but do include them for clarity
254 msStyle
|= style
& wxSL_VERTICAL
? TBS_VERT
: TBS_HORZ
;
256 if ( style
& wxSL_BOTH
)
258 // this fully specifies the style combined with TBS_VERT/HORZ above
261 else // choose one direction
263 if ( style
& wxSL_LEFT
)
265 else if ( style
& wxSL_RIGHT
)
266 msStyle
|= TBS_RIGHT
;
267 else if ( style
& wxSL_TOP
)
269 else if ( style
& wxSL_BOTTOM
)
270 msStyle
|= TBS_BOTTOM
;
273 if ( style
& wxSL_AUTOTICKS
)
274 msStyle
|= TBS_AUTOTICKS
;
276 msStyle
|= TBS_NOTICKS
;
278 if ( style
& wxSL_SELRANGE
)
279 msStyle
|= TBS_ENABLESELRANGE
;
284 wxSlider::~wxSlider()
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 bool wxSlider::MSWOnScroll(int WXUNUSED(orientation
),
295 WXWORD
WXUNUSED(pos
),
298 wxEventType scrollEvent
;
302 scrollEvent
= wxEVT_SCROLL_TOP
;
306 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
310 scrollEvent
= wxEVT_SCROLL_LINEUP
;
314 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
318 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
322 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
326 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
330 case SB_THUMBPOSITION
:
333 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
334 m_isDragging
= false;
338 // this seems to only happen when the mouse wheel is used: in
339 // this case, as it might be unexpected to get THUMBRELEASE
340 // without preceding THUMBTRACKs, we don't generate it at all
341 // but generate CHANGED event because the control itself does
342 // not send us SB_ENDSCROLL for whatever reason when mouse
344 scrollEvent
= wxEVT_SCROLL_CHANGED
;
349 scrollEvent
= wxEVT_SCROLL_CHANGED
;
353 // unknown scroll event?
357 int newPos
= ValueInvertOrNot((int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0));
358 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
360 // out of range - but we did process it
366 wxScrollEvent
event(scrollEvent
, m_windowId
);
367 event
.SetPosition(newPos
);
368 event
.SetEventObject( this );
369 GetEventHandler()->ProcessEvent(event
);
371 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
372 cevent
.SetInt( newPos
);
373 cevent
.SetEventObject( this );
375 return GetEventHandler()->ProcessEvent( cevent
);
378 void wxSlider::Command (wxCommandEvent
& event
)
380 SetValue (event
.GetInt());
381 ProcessCommand (event
);
384 // ----------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------
388 wxRect
wxSlider::GetBoundingBox() const
390 // take care not to call our own functions which would call us recursively
392 wxSliderBase::DoGetPosition(&x
, &y
);
393 wxSliderBase::DoGetSize(&w
, &h
);
395 wxRect
rect(x
, y
, w
, h
);
398 wxRect lrect
= m_labels
->GetBoundingBox();
399 GetParent()->ScreenToClient(&lrect
.x
, &lrect
.y
);
406 void wxSlider::DoGetSize(int *width
, int *height
) const
408 wxRect rect
= GetBoundingBox();
413 *height
= rect
.height
;
416 void wxSlider::DoGetPosition(int *x
, int *y
) const
418 wxRect rect
= GetBoundingBox();
426 int wxSlider::GetLabelsSize(int *width
) const
432 // find the max label width
433 int wLabelMin
, wLabelMax
;
434 GetTextExtent(Format(m_rangeMin
), &wLabelMin
, &cy
);
435 GetTextExtent(Format(m_rangeMax
), &wLabelMax
, &cy
);
437 *width
= wxMax(wLabelMin
, wLabelMax
);
441 cy
= GetCharHeight();
444 return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
447 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
449 // all complications below are because we need to position the labels,
450 // without them everything is easy
453 wxSliderBase::DoMoveWindow(x
, y
, width
, height
);
457 // be careful to position the slider itself after moving the labels as
458 // otherwise our GetBoundingBox(), which is called from WM_SIZE handler,
459 // would return a wrong result and wrong size would be cached internally
460 if ( HasFlag(wxSL_VERTICAL
) )
463 int hLabel
= GetLabelsSize(&wLabel
);
465 int xLabel
= HasFlag(wxSL_LEFT
) ? x
+ width
- wLabel
: x
;
467 // position all labels: min at the top, value in the middle and max at
469 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Min
],
470 xLabel
, y
, wLabel
, hLabel
);
472 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Value
],
473 xLabel
, y
+ (height
- hLabel
)/2, wLabel
, hLabel
);
475 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Max
],
476 xLabel
, y
+ height
- hLabel
, wLabel
, hLabel
);
478 // position the slider itself along the left/right edge
479 wxSliderBase::DoMoveWindow(HasFlag(wxSL_LEFT
) ? x
: x
+ wLabel
+ HGAP
,
481 width
- wLabel
- HGAP
,
487 int hLabel
= GetLabelsSize(&wLabel
);
489 int yLabel
= HasFlag(wxSL_TOP
) ? y
+ height
- hLabel
: y
;
491 // position all labels: min on the left, value in the middle and max to
493 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Min
],
494 x
, yLabel
, wLabel
, hLabel
);
496 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Value
],
497 x
+ (width
- wLabel
)/2, yLabel
, wLabel
, hLabel
);
499 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Max
],
500 x
+ width
- wLabel
, yLabel
, wLabel
, hLabel
);
502 // position the slider itself along the top/bottom edge
503 wxSliderBase::DoMoveWindow(x
,
504 HasFlag(wxSL_TOP
) ? y
: y
+ hLabel
,
510 wxSize
wxSlider::DoGetBestSize() const
512 // these values are arbitrary
513 static const int length
= 100;
514 static const int thumb
= 24;
515 static const int ticks
= 8;
519 if ( HasFlag(wxSL_VERTICAL
) )
528 int hLabel
= GetLabelsSize(&wLabel
);
530 // account for the labels
531 size
.x
+= HGAP
+ wLabel
;
533 // labels are indented relative to the slider itself
545 // labels add extra height
546 size
.y
+= GetLabelsSize();
550 // need extra space to show ticks
551 if ( HasFlag(wxSL_TICKS
) )
555 // and maybe twice as much if we show them on both sides
556 if ( HasFlag(wxSL_BOTH
) )
563 // ----------------------------------------------------------------------------
564 // slider-specific methods
565 // ----------------------------------------------------------------------------
567 int wxSlider::GetValue() const
569 return ValueInvertOrNot(::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0));
572 void wxSlider::SetValue(int value
)
574 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)ValueInvertOrNot(value
));
578 ::SetWindowText((*m_labels
)[SliderLabel_Value
], Format(value
));
582 void wxSlider::SetRange(int minValue
, int maxValue
)
584 m_rangeMin
= minValue
;
585 m_rangeMax
= maxValue
;
587 ::SendMessage(GetHwnd(), TBM_SETRANGEMIN
, TRUE
, m_rangeMin
);
588 ::SendMessage(GetHwnd(), TBM_SETRANGEMAX
, TRUE
, m_rangeMax
);
592 ::SetWindowText((*m_labels
)[SliderLabel_Min
], Format(ValueInvertOrNot(m_rangeMin
)));
593 ::SetWindowText((*m_labels
)[SliderLabel_Max
], Format(ValueInvertOrNot(m_rangeMax
)));
597 void wxSlider::SetTickFreq(int n
, int pos
)
600 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
603 void wxSlider::SetPageSize(int pageSize
)
605 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
606 m_pageSize
= pageSize
;
609 int wxSlider::GetPageSize() const
614 void wxSlider::ClearSel()
616 ::SendMessage(GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0);
619 void wxSlider::ClearTicks()
621 ::SendMessage(GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0);
624 void wxSlider::SetLineSize(int lineSize
)
626 m_lineSize
= lineSize
;
627 ::SendMessage(GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
630 int wxSlider::GetLineSize() const
632 return (int)::SendMessage(GetHwnd(), TBM_GETLINESIZE
, 0, 0);
635 int wxSlider::GetSelEnd() const
637 return (int)::SendMessage(GetHwnd(), TBM_GETSELEND
, 0, 0);
640 int wxSlider::GetSelStart() const
642 return (int)::SendMessage(GetHwnd(), TBM_GETSELSTART
, 0, 0);
645 void wxSlider::SetSelection(int minPos
, int maxPos
)
647 ::SendMessage(GetHwnd(), TBM_SETSEL
,
648 (WPARAM
) TRUE
/* redraw */,
649 (LPARAM
) MAKELONG( minPos
, maxPos
) );
652 void wxSlider::SetThumbLength(int len
)
654 ::SendMessage(GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0);
657 int wxSlider::GetThumbLength() const
659 return (int)::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
, 0, 0);
662 void wxSlider::SetTick(int tickPos
)
664 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
667 // ----------------------------------------------------------------------------
668 // composite control methods
669 // ----------------------------------------------------------------------------
671 WXHWND
wxSlider::GetStaticMin() const
673 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Min
] : NULL
;
676 WXHWND
wxSlider::GetStaticMax() const
678 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Max
] : NULL
;
681 WXHWND
wxSlider::GetEditValue() const
683 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Value
] : NULL
;
686 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxSlider
, wxSliderBase
, m_labels
)
688 #endif // wxUSE_SLIDER