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"
30 #include "wx/slider.h"
33 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
37 #include "wx/msw/subwin.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // indices of labels in wxSlider::m_labels
52 // the gap between the slider and the labels, in pixels
53 static const int HGAP
= 5;
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 #if wxUSE_EXTENDED_RTTI
60 WX_DEFINE_FLAGS( wxSliderStyle
)
62 wxBEGIN_FLAGS( wxSliderStyle
)
63 // new style border flags, we put them first to
64 // use them for streaming out
65 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
66 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
67 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
68 wxFLAGS_MEMBER(wxBORDER_RAISED
)
69 wxFLAGS_MEMBER(wxBORDER_STATIC
)
70 wxFLAGS_MEMBER(wxBORDER_NONE
)
72 // old style border flags
73 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
74 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
75 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
76 wxFLAGS_MEMBER(wxRAISED_BORDER
)
77 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
78 wxFLAGS_MEMBER(wxBORDER
)
80 // standard window styles
81 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
82 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
83 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
84 wxFLAGS_MEMBER(wxWANTS_CHARS
)
85 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
86 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
87 wxFLAGS_MEMBER(wxVSCROLL
)
88 wxFLAGS_MEMBER(wxHSCROLL
)
90 wxFLAGS_MEMBER(wxSL_HORIZONTAL
)
91 wxFLAGS_MEMBER(wxSL_VERTICAL
)
92 wxFLAGS_MEMBER(wxSL_AUTOTICKS
)
93 wxFLAGS_MEMBER(wxSL_LABELS
)
94 wxFLAGS_MEMBER(wxSL_LEFT
)
95 wxFLAGS_MEMBER(wxSL_TOP
)
96 wxFLAGS_MEMBER(wxSL_RIGHT
)
97 wxFLAGS_MEMBER(wxSL_BOTTOM
)
98 wxFLAGS_MEMBER(wxSL_BOTH
)
99 wxFLAGS_MEMBER(wxSL_SELRANGE
)
100 wxFLAGS_MEMBER(wxSL_INVERSE
)
102 wxEND_FLAGS( wxSliderStyle
)
104 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider
, wxControl
,"wx/slider.h")
106 wxBEGIN_PROPERTIES_TABLE(wxSlider
)
107 wxEVENT_RANGE_PROPERTY( Scroll
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_CHANGED
, wxScrollEvent
)
108 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SLIDER_UPDATED
, wxCommandEvent
)
110 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
111 wxPROPERTY( Minimum
, int , SetMin
, GetMin
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
112 wxPROPERTY( Maximum
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
113 wxPROPERTY( PageSize
, int , SetPageSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
114 wxPROPERTY( LineSize
, int , SetLineSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
115 wxPROPERTY( ThumbLength
, int , SetThumbLength
, GetThumbLength
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
116 wxPROPERTY_FLAGS( WindowStyle
, wxSliderStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
117 wxEND_PROPERTIES_TABLE()
119 wxBEGIN_HANDLERS_TABLE(wxSlider
)
120 wxEND_HANDLERS_TABLE()
122 wxCONSTRUCTOR_8( wxSlider
, wxWindow
* , Parent
, wxWindowID
, Id
, int , Value
, int , Minimum
, int , Maximum
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
124 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
127 // ============================================================================
128 // wxSlider implementation
129 // ============================================================================
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 void wxSlider::Init()
145 m_isDragging
= false;
149 wxSlider::Create(wxWindow
*parent
,
157 const wxValidator
& validator
,
158 const wxString
& name
)
160 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
161 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
162 // reasons we can't really change it, instead try to infer the orientation
163 // from the flags given to us here
164 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
168 style
|= wxSL_VERTICAL
;
173 style
|= wxSL_HORIZONTAL
;
177 // no specific direction, do we have at least the orientation?
178 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
180 // no, choose default
181 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
185 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) || !(style
& wxSL_HORIZONTAL
),
186 _T("incompatible slider direction and orientation") );
189 // initialize everything
190 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
193 // ensure that we have correct values for GetLabelsSize()
194 m_rangeMin
= minValue
;
195 m_rangeMax
= maxValue
;
197 // create the labels first, so that our DoGetBestSize() could take them
200 // note that we could simply create 3 wxStaticTexts here but it could
201 // result in some observable side effects at wx level (e.g. the parent of
202 // wxSlider would have 3 more children than expected) and so we prefer not
203 // to do it like this
204 if ( m_windowStyle
& wxSL_LABELS
)
206 m_labels
= new wxSubwindows(SliderLabel_Last
);
208 HWND hwndParent
= GetHwndOf(parent
);
209 for ( size_t n
= 0; n
< SliderLabel_Last
; n
++ )
211 wxWindowIDRef lblid
= NewControlId();
213 HWND wnd
= ::CreateWindow
217 WS_CHILD
| WS_VISIBLE
| SS_CENTER
,
220 (HMENU
)lblid
.GetValue(),
225 m_labels
->Set(n
, wnd
, lblid
);
228 m_labels
->SetFont(GetFont());
231 // now create the main control too
232 if ( !MSWCreateControl(TRACKBAR_CLASS
, wxEmptyString
, pos
, size
) )
235 // and initialize everything
236 SetRange(minValue
, maxValue
);
238 SetPageSize((maxValue
- minValue
)/10);
240 // we need to position the labels correctly if we have them and if
241 // SetSize() hadn't been called before (when best size was determined by
242 // MSWCreateControl()) as in this case they haven't been put in place yet
243 if ( m_labels
&& size
.x
!= wxDefaultCoord
&& size
.y
!= wxDefaultCoord
)
251 WXDWORD
wxSlider::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
253 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
255 // TBS_HORZ, TBS_RIGHT and TBS_BOTTOM are 0 but do include them for clarity
256 msStyle
|= style
& wxSL_VERTICAL
? TBS_VERT
: TBS_HORZ
;
258 if ( style
& wxSL_BOTH
)
260 // this fully specifies the style combined with TBS_VERT/HORZ above
263 else // choose one direction
265 if ( style
& wxSL_LEFT
)
267 else if ( style
& wxSL_RIGHT
)
268 msStyle
|= TBS_RIGHT
;
269 else if ( style
& wxSL_TOP
)
271 else if ( style
& wxSL_BOTTOM
)
272 msStyle
|= TBS_BOTTOM
;
275 if ( style
& wxSL_AUTOTICKS
)
276 msStyle
|= TBS_AUTOTICKS
;
278 msStyle
|= TBS_NOTICKS
;
280 if ( style
& wxSL_SELRANGE
)
281 msStyle
|= TBS_ENABLESELRANGE
;
286 wxSlider::~wxSlider()
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
295 bool wxSlider::MSWOnScroll(int WXUNUSED(orientation
),
297 WXWORD
WXUNUSED(pos
),
300 wxEventType scrollEvent
;
304 scrollEvent
= wxEVT_SCROLL_TOP
;
308 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
312 scrollEvent
= wxEVT_SCROLL_LINEUP
;
316 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
320 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
324 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
328 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
332 case SB_THUMBPOSITION
:
335 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
336 m_isDragging
= false;
340 // this seems to only happen when the mouse wheel is used: in
341 // this case, as it might be unexpected to get THUMBRELEASE
342 // without preceding THUMBTRACKs, we don't generate it at all
343 // but generate CHANGED event because the control itself does
344 // not send us SB_ENDSCROLL for whatever reason when mouse
346 scrollEvent
= wxEVT_SCROLL_CHANGED
;
351 scrollEvent
= wxEVT_SCROLL_CHANGED
;
355 // unknown scroll event?
359 int newPos
= ValueInvertOrNot((int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0));
360 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
362 // out of range - but we did process it
368 wxScrollEvent
event(scrollEvent
, m_windowId
);
369 event
.SetPosition(newPos
);
370 event
.SetEventObject( this );
371 HandleWindowEvent(event
);
373 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
374 cevent
.SetInt( newPos
);
375 cevent
.SetEventObject( this );
377 return HandleWindowEvent( cevent
);
380 void wxSlider::Command (wxCommandEvent
& event
)
382 SetValue (event
.GetInt());
383 ProcessCommand (event
);
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 wxRect
wxSlider::GetBoundingBox() const
392 // take care not to call our own functions which would call us recursively
394 wxSliderBase::DoGetPosition(&x
, &y
);
395 wxSliderBase::DoGetSize(&w
, &h
);
397 wxRect
rect(x
, y
, w
, h
);
400 wxRect lrect
= m_labels
->GetBoundingBox();
401 GetParent()->ScreenToClient(&lrect
.x
, &lrect
.y
);
408 void wxSlider::DoGetSize(int *width
, int *height
) const
410 wxRect rect
= GetBoundingBox();
415 *height
= rect
.height
;
418 void wxSlider::DoGetPosition(int *x
, int *y
) const
420 wxRect rect
= GetBoundingBox();
428 int wxSlider::GetLabelsSize(int *width
) const
434 // find the max label width
435 int wLabelMin
, wLabelMax
;
436 GetTextExtent(Format(m_rangeMin
), &wLabelMin
, &cy
);
437 GetTextExtent(Format(m_rangeMax
), &wLabelMax
, &cy
);
439 *width
= wxMax(wLabelMin
, wLabelMax
);
443 cy
= GetCharHeight();
446 return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
449 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
451 // all complications below are because we need to position the labels,
452 // without them everything is easy
455 wxSliderBase::DoMoveWindow(x
, y
, width
, height
);
459 // be careful to position the slider itself after moving the labels as
460 // otherwise our GetBoundingBox(), which is called from WM_SIZE handler,
461 // would return a wrong result and wrong size would be cached internally
462 if ( HasFlag(wxSL_VERTICAL
) )
465 int hLabel
= GetLabelsSize(&wLabel
);
467 int xLabel
= HasFlag(wxSL_LEFT
) ? x
+ width
- wLabel
: x
;
469 // position all labels: min at the top, value in the middle and max at
471 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Min
],
472 xLabel
, y
, wLabel
, hLabel
);
474 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Value
],
475 xLabel
, y
+ (height
- hLabel
)/2, wLabel
, hLabel
);
477 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Max
],
478 xLabel
, y
+ height
- hLabel
, wLabel
, hLabel
);
480 // position the slider itself along the left/right edge
481 wxSliderBase::DoMoveWindow(HasFlag(wxSL_LEFT
) ? x
: x
+ wLabel
+ HGAP
,
483 width
- wLabel
- HGAP
,
489 int hLabel
= GetLabelsSize(&wLabel
);
491 int yLabel
= HasFlag(wxSL_TOP
) ? y
+ height
- hLabel
: y
;
493 // position all labels: min on the left, value in the middle and max to
495 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Min
],
496 x
, yLabel
, wLabel
, hLabel
);
498 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Value
],
499 x
+ (width
- wLabel
)/2, yLabel
, wLabel
, hLabel
);
501 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Max
],
502 x
+ width
- wLabel
, yLabel
, wLabel
, hLabel
);
504 // position the slider itself along the top/bottom edge
505 wxSliderBase::DoMoveWindow(x
,
506 HasFlag(wxSL_TOP
) ? y
: y
+ hLabel
,
512 wxSize
wxSlider::DoGetBestSize() const
514 // these values are arbitrary
515 static const int length
= 100;
516 static const int thumb
= 24;
517 static const int ticks
= 8;
521 if ( HasFlag(wxSL_VERTICAL
) )
530 int hLabel
= GetLabelsSize(&wLabel
);
532 // account for the labels
533 size
.x
+= HGAP
+ wLabel
;
535 // labels are indented relative to the slider itself
547 // labels add extra height
548 size
.y
+= GetLabelsSize();
552 // need extra space to show ticks
553 if ( HasFlag(wxSL_TICKS
) )
557 // and maybe twice as much if we show them on both sides
558 if ( HasFlag(wxSL_BOTH
) )
565 // ----------------------------------------------------------------------------
566 // slider-specific methods
567 // ----------------------------------------------------------------------------
569 int wxSlider::GetValue() const
571 return ValueInvertOrNot(::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0));
574 void wxSlider::SetValue(int value
)
576 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)ValueInvertOrNot(value
));
580 ::SetWindowText((*m_labels
)[SliderLabel_Value
], Format(value
).wx_str());
584 void wxSlider::SetRange(int minValue
, int maxValue
)
586 m_rangeMin
= minValue
;
587 m_rangeMax
= maxValue
;
589 ::SendMessage(GetHwnd(), TBM_SETRANGEMIN
, TRUE
, m_rangeMin
);
590 ::SendMessage(GetHwnd(), TBM_SETRANGEMAX
, TRUE
, m_rangeMax
);
594 ::SetWindowText((*m_labels
)[SliderLabel_Min
],
595 Format(ValueInvertOrNot(m_rangeMin
)).wx_str());
596 ::SetWindowText((*m_labels
)[SliderLabel_Max
],
597 Format(ValueInvertOrNot(m_rangeMax
)).wx_str());
601 void wxSlider::SetTickFreq(int n
, int pos
)
604 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
607 void wxSlider::SetPageSize(int pageSize
)
609 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
610 m_pageSize
= pageSize
;
613 int wxSlider::GetPageSize() const
618 void wxSlider::ClearSel()
620 ::SendMessage(GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0);
623 void wxSlider::ClearTicks()
625 ::SendMessage(GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0);
628 void wxSlider::SetLineSize(int lineSize
)
630 m_lineSize
= lineSize
;
631 ::SendMessage(GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
634 int wxSlider::GetLineSize() const
636 return (int)::SendMessage(GetHwnd(), TBM_GETLINESIZE
, 0, 0);
639 int wxSlider::GetSelEnd() const
641 return (int)::SendMessage(GetHwnd(), TBM_GETSELEND
, 0, 0);
644 int wxSlider::GetSelStart() const
646 return (int)::SendMessage(GetHwnd(), TBM_GETSELSTART
, 0, 0);
649 void wxSlider::SetSelection(int minPos
, int maxPos
)
651 ::SendMessage(GetHwnd(), TBM_SETSEL
,
652 (WPARAM
) TRUE
/* redraw */,
653 (LPARAM
) MAKELONG( minPos
, maxPos
) );
656 void wxSlider::SetThumbLength(int len
)
658 ::SendMessage(GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0);
661 int wxSlider::GetThumbLength() const
663 return (int)::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
, 0, 0);
666 void wxSlider::SetTick(int tickPos
)
668 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
671 // ----------------------------------------------------------------------------
672 // composite control methods
673 // ----------------------------------------------------------------------------
675 WXHWND
wxSlider::GetStaticMin() const
677 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Min
] : NULL
;
680 WXHWND
wxSlider::GetStaticMax() const
682 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Max
] : NULL
;
685 WXHWND
wxSlider::GetEditValue() const
687 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Value
] : NULL
;
690 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxSlider
, wxSliderBase
, m_labels
)
692 #endif // wxUSE_SLIDER