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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
18 #pragma implementation "slider95.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
38 #include "wx/slider.h"
39 #include "wx/msw/subwin.h"
41 #if !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // indices of labels in wxSlider::m_labels
58 // the gap between the slider and the labels, in pixels
59 static const int HGAP
= 5;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 #if wxUSE_EXTENDED_RTTI
66 WX_DEFINE_FLAGS( wxSliderStyle
)
68 wxBEGIN_FLAGS( wxSliderStyle
)
69 // new style border flags, we put them first to
70 // use them for streaming out
71 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
72 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
73 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
74 wxFLAGS_MEMBER(wxBORDER_RAISED
)
75 wxFLAGS_MEMBER(wxBORDER_STATIC
)
76 wxFLAGS_MEMBER(wxBORDER_NONE
)
78 // old style border flags
79 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
80 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
81 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
82 wxFLAGS_MEMBER(wxRAISED_BORDER
)
83 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
84 wxFLAGS_MEMBER(wxBORDER
)
86 // standard window styles
87 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
88 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
89 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
90 wxFLAGS_MEMBER(wxWANTS_CHARS
)
91 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
92 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
93 wxFLAGS_MEMBER(wxVSCROLL
)
94 wxFLAGS_MEMBER(wxHSCROLL
)
96 wxFLAGS_MEMBER(wxSL_HORIZONTAL
)
97 wxFLAGS_MEMBER(wxSL_VERTICAL
)
98 wxFLAGS_MEMBER(wxSL_AUTOTICKS
)
99 wxFLAGS_MEMBER(wxSL_LABELS
)
100 wxFLAGS_MEMBER(wxSL_LEFT
)
101 wxFLAGS_MEMBER(wxSL_TOP
)
102 wxFLAGS_MEMBER(wxSL_RIGHT
)
103 wxFLAGS_MEMBER(wxSL_BOTTOM
)
104 wxFLAGS_MEMBER(wxSL_BOTH
)
105 wxFLAGS_MEMBER(wxSL_SELRANGE
)
106 wxFLAGS_MEMBER(wxSL_INVERSE
)
108 wxEND_FLAGS( wxSliderStyle
)
110 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider
, wxControl
,"wx/scrolbar.h")
112 wxBEGIN_PROPERTIES_TABLE(wxSlider
)
113 wxEVENT_RANGE_PROPERTY( Scroll
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_CHANGED
, wxScrollEvent
)
114 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SLIDER_UPDATED
, wxCommandEvent
)
116 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
117 wxPROPERTY( Minimum
, int , SetMin
, GetMin
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
118 wxPROPERTY( Maximum
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
119 wxPROPERTY( PageSize
, int , SetPageSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
120 wxPROPERTY( LineSize
, int , SetLineSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
121 wxPROPERTY( ThumbLength
, int , SetThumbLength
, GetThumbLength
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
122 wxPROPERTY_FLAGS( WindowStyle
, wxSliderStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
123 wxEND_PROPERTIES_TABLE()
125 wxBEGIN_HANDLERS_TABLE(wxSlider
)
126 wxEND_HANDLERS_TABLE()
128 wxCONSTRUCTOR_8( wxSlider
, wxWindow
* , Parent
, wxWindowID
, Id
, int , Value
, int , Minimum
, int , Maximum
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
130 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
133 // ============================================================================
134 // wxSlider implementation
135 // ============================================================================
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 void wxSlider::Init()
151 m_isDragging
= false;
155 wxSlider::Create(wxWindow
*parent
,
163 const wxValidator
& validator
,
164 const wxString
& name
)
166 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
167 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
168 // reasons we can't really change it, instead try to infer the orientation
169 // from the flags given to us here
170 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
174 style
|= wxSL_VERTICAL
;
179 style
|= wxSL_HORIZONTAL
;
183 // no specific direction, do we have at least the orientation?
184 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
186 // no, choose default
187 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
191 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) || !(style
& wxSL_HORIZONTAL
),
192 _T("incompatible slider direction and orientation") );
195 // initialize everything
196 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
199 // ensure that we have correct values for GetLabelsSize()
200 m_rangeMin
= minValue
;
201 m_rangeMax
= maxValue
;
203 // create the labels first, so that our DoGetBestSize() could take them
206 // note that we could simply create 3 wxStaticTexts here but it could
207 // result in some observable side effects at wx level (e.g. the parent of
208 // wxSlider would have 3 more children than expected) and so we prefer not
209 // to do it like this
210 if ( m_windowStyle
& wxSL_LABELS
)
212 m_labels
= new wxSubwindows(SliderLabel_Last
);
214 HWND hwndParent
= GetHwndOf(parent
);
215 for ( size_t n
= 0; n
< SliderLabel_Last
; n
++ )
217 (*m_labels
)[n
] = ::CreateWindow
221 WS_CHILD
| WS_VISIBLE
| SS_CENTER
,
224 (HMENU
)NewControlId(),
230 m_labels
->SetFont(GetFont());
233 // now create the main control too
234 if ( !MSWCreateControl(TRACKBAR_CLASS
, wxEmptyString
, pos
, size
) )
237 // and initialize everything
238 SetRange(minValue
, maxValue
);
240 SetPageSize((maxValue
- minValue
)/10);
242 // we need to position the labels correctly if we have them and if
243 // SetSize() hadn't been called before (when best size was determined by
244 // MSWCreateControl()) as in this case they haven't been put in place yet
245 if ( m_labels
&& size
.x
!= wxDefaultCoord
&& size
.y
!= wxDefaultCoord
)
253 WXDWORD
wxSlider::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
255 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
257 // TBS_HORZ, TBS_RIGHT and TBS_BOTTOM are 0 but do include them for clarity
258 msStyle
|= style
& wxSL_VERTICAL
? TBS_VERT
: TBS_HORZ
;
260 if ( style
& wxSL_BOTH
)
262 // this fully specifies the style combined with TBS_VERT/HORZ above
265 else // choose one direction
267 if ( style
& wxSL_LEFT
)
269 else if ( style
& wxSL_RIGHT
)
270 msStyle
|= TBS_RIGHT
;
271 else if ( style
& wxSL_TOP
)
273 else if ( style
& wxSL_BOTTOM
)
274 msStyle
|= TBS_BOTTOM
;
277 if ( style
& wxSL_AUTOTICKS
)
278 msStyle
|= TBS_AUTOTICKS
;
280 msStyle
|= TBS_NOTICKS
;
282 if ( style
& wxSL_SELRANGE
)
283 msStyle
|= TBS_ENABLESELRANGE
;
288 wxSlider::~wxSlider()
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 bool wxSlider::MSWOnScroll(int WXUNUSED(orientation
),
299 WXWORD
WXUNUSED(pos
),
302 wxEventType scrollEvent
;
306 scrollEvent
= wxEVT_SCROLL_TOP
;
310 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
314 scrollEvent
= wxEVT_SCROLL_LINEUP
;
318 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
322 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
326 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
330 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
334 case SB_THUMBPOSITION
:
337 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
338 m_isDragging
= false;
342 // this seems to only happen when the mouse wheel is used: in
343 // this case, as it might be unexpected to get THUMBRELEASE
344 // without preceding THUMBTRACKs, we don't generate it at all
345 // but generate CHANGED event because the control itself does
346 // not send us SB_ENDSCROLL for whatever reason when mouse
348 scrollEvent
= wxEVT_SCROLL_CHANGED
;
353 scrollEvent
= wxEVT_SCROLL_CHANGED
;
357 // unknown scroll event?
361 int newPos
= ValueInvertOrNot((int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0));
362 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
364 // out of range - but we did process it
370 wxScrollEvent
event(scrollEvent
, m_windowId
);
371 event
.SetPosition(newPos
);
372 event
.SetEventObject( this );
373 GetEventHandler()->ProcessEvent(event
);
375 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
376 cevent
.SetInt( newPos
);
377 cevent
.SetEventObject( this );
379 return GetEventHandler()->ProcessEvent( cevent
);
382 void wxSlider::Command (wxCommandEvent
& event
)
384 SetValue (event
.GetInt());
385 ProcessCommand (event
);
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 wxRect
wxSlider::GetBoundingBox() const
394 // take care not to call our own functions which would call us recursively
396 wxSliderBase::DoGetPosition(&x
, &y
);
397 wxSliderBase::DoGetSize(&w
, &h
);
399 wxRect
rect(x
, y
, w
, h
);
402 wxRect lrect
= m_labels
->GetBoundingBox();
403 GetParent()->ScreenToClient(&lrect
.x
, &lrect
.y
);
410 void wxSlider::DoGetSize(int *width
, int *height
) const
412 wxRect rect
= GetBoundingBox();
417 *height
= rect
.height
;
420 void wxSlider::DoGetPosition(int *x
, int *y
) const
422 wxRect rect
= GetBoundingBox();
430 int wxSlider::GetLabelsSize(int *width
) const
436 // find the max label width
437 int wLabelMin
, wLabelMax
;
438 GetTextExtent(Format(m_rangeMin
), &wLabelMin
, &cy
);
439 GetTextExtent(Format(m_rangeMax
), &wLabelMax
, &cy
);
441 *width
= wxMax(wLabelMin
, wLabelMax
);
445 cy
= GetCharHeight();
448 return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
451 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
453 // all complications below are because we need to position the labels,
454 // without them everything is easy
457 wxSliderBase::DoMoveWindow(x
, y
, width
, height
);
461 // be careful to position the slider itself after moving the labels as
462 // otherwise our GetBoundingBox(), which is called from WM_SIZE handler,
463 // would return a wrong result and wrong size would be cached internally
464 if ( HasFlag(wxSL_VERTICAL
) )
467 int hLabel
= GetLabelsSize(&wLabel
);
469 int xLabel
= HasFlag(wxSL_LEFT
) ? x
+ width
- wLabel
: x
;
471 // position all labels: min at the top, value in the middle and max at
473 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Min
],
474 xLabel
, y
, wLabel
, hLabel
);
476 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Value
],
477 xLabel
, y
+ (height
- hLabel
)/2, wLabel
, hLabel
);
479 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Max
],
480 xLabel
, y
+ height
- hLabel
, wLabel
, hLabel
);
482 // position the slider itself along the left/right edge
483 wxSliderBase::DoMoveWindow(HasFlag(wxSL_LEFT
) ? x
: x
+ wLabel
+ HGAP
,
485 width
- wLabel
- HGAP
,
491 int hLabel
= GetLabelsSize(&wLabel
);
493 int yLabel
= HasFlag(wxSL_TOP
) ? y
+ height
- hLabel
: y
;
495 // position all labels: min on the left, value in the middle and max to
497 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Min
],
498 x
, yLabel
, wLabel
, hLabel
);
500 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Value
],
501 x
+ (width
- wLabel
)/2, yLabel
, wLabel
, hLabel
);
503 DoMoveSibling((HWND
)(*m_labels
)[SliderLabel_Max
],
504 x
+ width
- wLabel
, yLabel
, wLabel
, hLabel
);
506 // position the slider itself along the top/bottom edge
507 wxSliderBase::DoMoveWindow(x
,
508 HasFlag(wxSL_TOP
) ? y
: y
+ hLabel
,
514 wxSize
wxSlider::DoGetBestSize() const
516 // these values are arbitrary
517 static const int length
= 100;
518 static const int thumb
= 24;
519 static const int ticks
= 8;
523 if ( HasFlag(wxSL_VERTICAL
) )
532 int hLabel
= GetLabelsSize(&wLabel
);
534 // account for the labels
535 size
.x
+= HGAP
+ wLabel
;
537 // labels are indented relative to the slider itself
549 // labels add extra height
550 size
.y
+= GetLabelsSize();
554 // need extra space to show ticks
555 if ( HasFlag(wxSL_TICKS
) )
559 // and maybe twice as much if we show them on both sides
560 if ( HasFlag(wxSL_BOTH
) )
567 // ----------------------------------------------------------------------------
568 // slider-specific methods
569 // ----------------------------------------------------------------------------
571 int wxSlider::GetValue() const
573 return ValueInvertOrNot(::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0));
576 void wxSlider::SetValue(int value
)
578 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)ValueInvertOrNot(value
));
582 ::SetWindowText((*m_labels
)[SliderLabel_Value
], Format(value
));
586 void wxSlider::SetRange(int minValue
, int maxValue
)
588 m_rangeMin
= minValue
;
589 m_rangeMax
= maxValue
;
591 ::SendMessage(GetHwnd(), TBM_SETRANGEMIN
, TRUE
, m_rangeMin
);
592 ::SendMessage(GetHwnd(), TBM_SETRANGEMAX
, TRUE
, m_rangeMax
);
596 ::SetWindowText((*m_labels
)[SliderLabel_Min
], Format(ValueInvertOrNot(m_rangeMin
)));
597 ::SetWindowText((*m_labels
)[SliderLabel_Max
], Format(ValueInvertOrNot(m_rangeMax
)));
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