1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // include <commctrl.h> "properly"
38 #include "wx/msw/wrapcctl.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // indices of labels in wxSlider::m_labels
53 // the gap between the slider and the labels, in pixels
54 static const int HGAP
= 5;
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 #if wxUSE_EXTENDED_RTTI
61 WX_DEFINE_FLAGS( wxSliderStyle
)
63 wxBEGIN_FLAGS( wxSliderStyle
)
64 // new style border flags, we put them first to
65 // use them for streaming out
66 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
67 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
68 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
69 wxFLAGS_MEMBER(wxBORDER_RAISED
)
70 wxFLAGS_MEMBER(wxBORDER_STATIC
)
71 wxFLAGS_MEMBER(wxBORDER_NONE
)
73 // old style border flags
74 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
75 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
76 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
77 wxFLAGS_MEMBER(wxRAISED_BORDER
)
78 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
79 wxFLAGS_MEMBER(wxBORDER
)
81 // standard window styles
82 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
83 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
84 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
85 wxFLAGS_MEMBER(wxWANTS_CHARS
)
86 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
87 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
88 wxFLAGS_MEMBER(wxVSCROLL
)
89 wxFLAGS_MEMBER(wxHSCROLL
)
91 wxFLAGS_MEMBER(wxSL_HORIZONTAL
)
92 wxFLAGS_MEMBER(wxSL_VERTICAL
)
93 wxFLAGS_MEMBER(wxSL_AUTOTICKS
)
94 wxFLAGS_MEMBER(wxSL_LABELS
)
95 wxFLAGS_MEMBER(wxSL_LEFT
)
96 wxFLAGS_MEMBER(wxSL_TOP
)
97 wxFLAGS_MEMBER(wxSL_RIGHT
)
98 wxFLAGS_MEMBER(wxSL_BOTTOM
)
99 wxFLAGS_MEMBER(wxSL_BOTH
)
100 wxFLAGS_MEMBER(wxSL_SELRANGE
)
101 wxFLAGS_MEMBER(wxSL_INVERSE
)
103 wxEND_FLAGS( wxSliderStyle
)
105 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider
, wxControl
,"wx/slider.h")
107 wxBEGIN_PROPERTIES_TABLE(wxSlider
)
108 wxEVENT_RANGE_PROPERTY( Scroll
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_CHANGED
, wxScrollEvent
)
109 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SLIDER_UPDATED
, wxCommandEvent
)
111 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
112 wxPROPERTY( Minimum
, int , SetMin
, GetMin
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
113 wxPROPERTY( Maximum
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
114 wxPROPERTY( PageSize
, int , SetPageSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
115 wxPROPERTY( LineSize
, int , SetLineSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
116 wxPROPERTY( ThumbLength
, int , SetThumbLength
, GetThumbLength
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
117 wxPROPERTY_FLAGS( WindowStyle
, wxSliderStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
118 wxEND_PROPERTIES_TABLE()
120 wxBEGIN_HANDLERS_TABLE(wxSlider
)
121 wxEND_HANDLERS_TABLE()
123 wxCONSTRUCTOR_8( wxSlider
, wxWindow
* , Parent
, wxWindowID
, Id
, int , Value
, int , Minimum
, int , Maximum
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
125 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
128 // ============================================================================
129 // wxSlider implementation
130 // ============================================================================
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 void wxSlider::Init()
146 m_isDragging
= false;
150 wxSlider::Create(wxWindow
*parent
,
158 const wxValidator
& validator
,
159 const wxString
& name
)
161 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
162 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
163 // reasons we can't really change it, instead try to infer the orientation
164 // from the flags given to us here
165 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
169 style
|= wxSL_VERTICAL
;
174 style
|= wxSL_HORIZONTAL
;
178 // no specific direction, do we have at least the orientation?
179 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
181 // no, choose default
182 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
186 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) || !(style
& wxSL_HORIZONTAL
),
187 _T("incompatible slider direction and orientation") );
190 // initialize everything
191 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
194 // ensure that we have correct values for GetLabelsSize()
195 m_rangeMin
= minValue
;
196 m_rangeMax
= maxValue
;
198 // create the labels first, so that our DoGetBestSize() could take them
201 // note that we could simply create 3 wxStaticTexts here but it could
202 // result in some observable side effects at wx level (e.g. the parent of
203 // wxSlider would have 3 more children than expected) and so we prefer not
204 // to do it like this
205 if ( m_windowStyle
& wxSL_LABELS
)
207 m_labels
= new wxSubwindows(SliderLabel_Last
);
209 HWND hwndParent
= GetHwndOf(parent
);
210 for ( size_t n
= 0; n
< SliderLabel_Last
; n
++ )
212 (*m_labels
)[n
] = ::CreateWindow
216 WS_CHILD
| WS_VISIBLE
| SS_CENTER
,
219 (HMENU
)NewControlId(),
225 m_labels
->SetFont(GetFont());
228 // now create the main control too
229 if ( !MSWCreateControl(TRACKBAR_CLASS
, wxEmptyString
, pos
, size
) )
232 // and initialize everything
233 SetRange(minValue
, maxValue
);
235 SetPageSize((maxValue
- minValue
)/10);
237 // we need to position the labels correctly if we have them and if
238 // SetSize() hadn't been called before (when best size was determined by
239 // MSWCreateControl()) as in this case they haven't been put in place yet
240 if ( m_labels
&& size
.x
!= wxDefaultCoord
&& size
.y
!= wxDefaultCoord
)
248 WXDWORD
wxSlider::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
250 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
252 // TBS_HORZ, TBS_RIGHT and TBS_BOTTOM are 0 but do include them for clarity
253 msStyle
|= style
& wxSL_VERTICAL
? TBS_VERT
: TBS_HORZ
;
255 if ( style
& wxSL_BOTH
)
257 // this fully specifies the style combined with TBS_VERT/HORZ above
260 else // choose one direction
262 if ( style
& wxSL_LEFT
)
264 else if ( style
& wxSL_RIGHT
)
265 msStyle
|= TBS_RIGHT
;
266 else if ( style
& wxSL_TOP
)
268 else if ( style
& wxSL_BOTTOM
)
269 msStyle
|= TBS_BOTTOM
;
272 if ( style
& wxSL_AUTOTICKS
)
273 msStyle
|= TBS_AUTOTICKS
;
275 msStyle
|= TBS_NOTICKS
;
277 if ( style
& wxSL_SELRANGE
)
278 msStyle
|= TBS_ENABLESELRANGE
;
283 wxSlider::~wxSlider()
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 bool wxSlider::MSWOnScroll(int WXUNUSED(orientation
),
294 WXWORD
WXUNUSED(pos
),
297 wxEventType scrollEvent
;
301 scrollEvent
= wxEVT_SCROLL_TOP
;
305 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
309 scrollEvent
= wxEVT_SCROLL_LINEUP
;
313 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
317 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
321 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
325 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
329 case SB_THUMBPOSITION
:
332 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
333 m_isDragging
= false;
337 // this seems to only happen when the mouse wheel is used: in
338 // this case, as it might be unexpected to get THUMBRELEASE
339 // without preceding THUMBTRACKs, we don't generate it at all
340 // but generate CHANGED event because the control itself does
341 // not send us SB_ENDSCROLL for whatever reason when mouse
343 scrollEvent
= wxEVT_SCROLL_CHANGED
;
348 scrollEvent
= wxEVT_SCROLL_CHANGED
;
352 // unknown scroll event?
356 int newPos
= ValueInvertOrNot((int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0));
357 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
359 // out of range - but we did process it
365 wxScrollEvent
event(scrollEvent
, m_windowId
);
366 event
.SetPosition(newPos
);
367 event
.SetEventObject( this );
368 GetEventHandler()->ProcessEvent(event
);
370 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
371 cevent
.SetInt( newPos
);
372 cevent
.SetEventObject( this );
374 return GetEventHandler()->ProcessEvent( cevent
);
377 void wxSlider::Command (wxCommandEvent
& event
)
379 SetValue (event
.GetInt());
380 ProcessCommand (event
);
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 wxRect
wxSlider::GetBoundingBox() const
389 // take care not to call our own functions which would call us recursively
391 wxSliderBase::DoGetPosition(&x
, &y
);
392 wxSliderBase::DoGetSize(&w
, &h
);
394 wxRect
rect(x
, y
, w
, h
);
397 wxRect lrect
= m_labels
->GetBoundingBox();
398 GetParent()->ScreenToClient(&lrect
.x
, &lrect
.y
);
405 void wxSlider::DoGetSize(int *width
, int *height
) const
407 wxRect rect
= GetBoundingBox();
412 *height
= rect
.height
;
415 void wxSlider::DoGetPosition(int *x
, int *y
) const
417 wxRect rect
= GetBoundingBox();
425 int wxSlider::GetLabelsSize(int *width
) const
431 // find the max label width
432 int wLabelMin
, wLabelMax
;
433 GetTextExtent(Format(m_rangeMin
), &wLabelMin
, &cy
);
434 GetTextExtent(Format(m_rangeMax
), &wLabelMax
, &cy
);
436 *width
= wxMax(wLabelMin
, wLabelMax
);
440 cy
= GetCharHeight();
443 return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
446 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
448 // all complications below are because we need to position the labels,
449 // without them everything is easy
452 wxSliderBase::DoMoveWindow(x
, y
, width
, height
);
456 // be careful to position the slider itself after moving the labels as
457 // otherwise our GetBoundingBox(), which is called from WM_SIZE handler,
458 // would return a wrong result and wrong size would be cached internally
459 if ( HasFlag(wxSL_VERTICAL
) )
462 int hLabel
= GetLabelsSize(&wLabel
);
464 int xLabel
= HasFlag(wxSL_LEFT
) ? x
+ width
- wLabel
: x
;
466 // position all labels: min at the top, value in the middle and max at
468 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Min
],
469 xLabel
, y
, wLabel
, hLabel
);
471 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Value
],
472 xLabel
, y
+ (height
- hLabel
)/2, wLabel
, hLabel
);
474 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Max
],
475 xLabel
, y
+ height
- hLabel
, wLabel
, hLabel
);
477 // position the slider itself along the left/right edge
478 wxSliderBase::DoMoveWindow(HasFlag(wxSL_LEFT
) ? x
: x
+ wLabel
+ HGAP
,
480 width
- wLabel
- HGAP
,
486 int hLabel
= GetLabelsSize(&wLabel
);
488 int yLabel
= HasFlag(wxSL_TOP
) ? y
+ height
- hLabel
: y
;
490 // position all labels: min on the left, value in the middle and max to
492 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Min
],
493 x
, yLabel
, wLabel
, hLabel
);
495 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Value
],
496 x
+ (width
- wLabel
)/2, yLabel
, wLabel
, hLabel
);
498 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Max
],
499 x
+ width
- wLabel
, yLabel
, wLabel
, hLabel
);
501 // position the slider itself along the top/bottom edge
502 wxSliderBase::DoMoveWindow(x
,
503 HasFlag(wxSL_TOP
) ? y
: y
+ hLabel
,
509 wxSize
wxSlider::DoGetBestSize() const
511 // these values are arbitrary
512 static const int length
= 100;
513 static const int thumb
= 24;
514 static const int ticks
= 8;
518 if ( HasFlag(wxSL_VERTICAL
) )
527 int hLabel
= GetLabelsSize(&wLabel
);
529 // account for the labels
530 size
.x
+= HGAP
+ wLabel
;
532 // labels are indented relative to the slider itself
544 // labels add extra height
545 size
.y
+= GetLabelsSize();
549 // need extra space to show ticks
550 if ( HasFlag(wxSL_TICKS
) )
554 // and maybe twice as much if we show them on both sides
555 if ( HasFlag(wxSL_BOTH
) )
562 // ----------------------------------------------------------------------------
563 // slider-specific methods
564 // ----------------------------------------------------------------------------
566 int wxSlider::GetValue() const
568 return ValueInvertOrNot(::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0));
571 void wxSlider::SetValue(int value
)
573 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)ValueInvertOrNot(value
));
577 ::SetWindowText((*m_labels
)[SliderLabel_Value
], Format(value
));
581 void wxSlider::SetRange(int minValue
, int maxValue
)
583 m_rangeMin
= minValue
;
584 m_rangeMax
= maxValue
;
586 ::SendMessage(GetHwnd(), TBM_SETRANGEMIN
, TRUE
, m_rangeMin
);
587 ::SendMessage(GetHwnd(), TBM_SETRANGEMAX
, TRUE
, m_rangeMax
);
591 ::SetWindowText((*m_labels
)[SliderLabel_Min
], Format(ValueInvertOrNot(m_rangeMin
)));
592 ::SetWindowText((*m_labels
)[SliderLabel_Max
], Format(ValueInvertOrNot(m_rangeMax
)));
596 void wxSlider::SetTickFreq(int n
, int pos
)
599 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
602 void wxSlider::SetPageSize(int pageSize
)
604 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
605 m_pageSize
= pageSize
;
608 int wxSlider::GetPageSize() const
613 void wxSlider::ClearSel()
615 ::SendMessage(GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0);
618 void wxSlider::ClearTicks()
620 ::SendMessage(GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0);
623 void wxSlider::SetLineSize(int lineSize
)
625 m_lineSize
= lineSize
;
626 ::SendMessage(GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
629 int wxSlider::GetLineSize() const
631 return (int)::SendMessage(GetHwnd(), TBM_GETLINESIZE
, 0, 0);
634 int wxSlider::GetSelEnd() const
636 return (int)::SendMessage(GetHwnd(), TBM_GETSELEND
, 0, 0);
639 int wxSlider::GetSelStart() const
641 return (int)::SendMessage(GetHwnd(), TBM_GETSELSTART
, 0, 0);
644 void wxSlider::SetSelection(int minPos
, int maxPos
)
646 ::SendMessage(GetHwnd(), TBM_SETSEL
,
647 (WPARAM
) TRUE
/* redraw */,
648 (LPARAM
) MAKELONG( minPos
, maxPos
) );
651 void wxSlider::SetThumbLength(int len
)
653 ::SendMessage(GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0);
656 int wxSlider::GetThumbLength() const
658 return (int)::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
, 0, 0);
661 void wxSlider::SetTick(int tickPos
)
663 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
666 // ----------------------------------------------------------------------------
667 // composite control methods
668 // ----------------------------------------------------------------------------
670 WXHWND
wxSlider::GetStaticMin() const
672 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Min
] : NULL
;
675 WXHWND
wxSlider::GetStaticMax() const
677 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Max
] : NULL
;
680 WXHWND
wxSlider::GetEditValue() const
682 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Value
] : NULL
;
685 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxSlider
, wxSliderBase
, m_labels
)
687 #endif // wxUSE_SLIDER