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 #define USE_DEFERRED_SIZING 1
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // indices of labels in wxSlider::m_labels
60 // the gap between the slider and the labels, in pixels
61 static const int HGAP
= 5;
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 #if wxUSE_EXTENDED_RTTI
68 WX_DEFINE_FLAGS( wxSliderStyle
)
70 wxBEGIN_FLAGS( wxSliderStyle
)
71 // new style border flags, we put them first to
72 // use them for streaming out
73 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
74 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
75 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
76 wxFLAGS_MEMBER(wxBORDER_RAISED
)
77 wxFLAGS_MEMBER(wxBORDER_STATIC
)
78 wxFLAGS_MEMBER(wxBORDER_NONE
)
80 // old style border flags
81 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
82 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
83 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
84 wxFLAGS_MEMBER(wxRAISED_BORDER
)
85 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
86 wxFLAGS_MEMBER(wxBORDER
)
88 // standard window styles
89 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
90 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
91 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
92 wxFLAGS_MEMBER(wxWANTS_CHARS
)
93 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
94 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
95 wxFLAGS_MEMBER(wxVSCROLL
)
96 wxFLAGS_MEMBER(wxHSCROLL
)
98 wxFLAGS_MEMBER(wxSL_HORIZONTAL
)
99 wxFLAGS_MEMBER(wxSL_VERTICAL
)
100 wxFLAGS_MEMBER(wxSL_AUTOTICKS
)
101 wxFLAGS_MEMBER(wxSL_LABELS
)
102 wxFLAGS_MEMBER(wxSL_LEFT
)
103 wxFLAGS_MEMBER(wxSL_TOP
)
104 wxFLAGS_MEMBER(wxSL_RIGHT
)
105 wxFLAGS_MEMBER(wxSL_BOTTOM
)
106 wxFLAGS_MEMBER(wxSL_BOTH
)
107 wxFLAGS_MEMBER(wxSL_SELRANGE
)
108 wxFLAGS_MEMBER(wxSL_INVERSE
)
110 wxEND_FLAGS( wxSliderStyle
)
112 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider
, wxControl
,"wx/scrolbar.h")
114 wxBEGIN_PROPERTIES_TABLE(wxSlider
)
115 wxEVENT_RANGE_PROPERTY( Scroll
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_CHANGED
, wxScrollEvent
)
116 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SLIDER_UPDATED
, wxCommandEvent
)
118 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
119 wxPROPERTY( Minimum
, int , SetMin
, GetMin
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
120 wxPROPERTY( Maximum
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
121 wxPROPERTY( PageSize
, int , SetPageSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
122 wxPROPERTY( LineSize
, int , SetLineSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
123 wxPROPERTY( ThumbLength
, int , SetThumbLength
, GetThumbLength
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
124 wxPROPERTY_FLAGS( WindowStyle
, wxSliderStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
125 wxEND_PROPERTIES_TABLE()
127 wxBEGIN_HANDLERS_TABLE(wxSlider
)
128 wxEND_HANDLERS_TABLE()
130 wxCONSTRUCTOR_8( wxSlider
, wxWindow
* , Parent
, wxWindowID
, Id
, int , Value
, int , Minimum
, int , Maximum
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
132 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
135 // ============================================================================
136 // wxSlider implementation
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 void wxSlider::Init()
153 m_isDragging
= false;
157 wxSlider::Create(wxWindow
*parent
,
165 const wxValidator
& validator
,
166 const wxString
& name
)
168 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
169 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
170 // reasons we can't really change it, instead try to infer the orientation
171 // from the flags given to us here
172 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
176 style
|= wxSL_VERTICAL
;
181 style
|= wxSL_HORIZONTAL
;
185 // no specific direction, do we have at least the orientation?
186 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
188 // no, choose default
189 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
193 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) | !(style
& wxSL_HORIZONTAL
),
194 _T("incompatible slider direction and orientation") );
197 // initialize everything
198 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
201 // ensure that we have correct values for GetLabelsSize()
202 m_rangeMin
= minValue
;
203 m_rangeMax
= maxValue
;
205 // create the labels first, so that our DoGetBestSize() could take them
208 // note that we could simply create 3 wxStaticTexts here but it could
209 // result in some observable side effects at wx level (e.g. the parent of
210 // wxSlider would have 3 more children than expected) and so we prefer not
211 // to do it like this
212 if ( m_windowStyle
& wxSL_LABELS
)
214 m_labels
= new wxSubwindows(SliderLabel_Last
);
216 HWND hwndParent
= GetHwndOf(parent
);
217 for ( size_t n
= 0; n
< SliderLabel_Last
; n
++ )
219 (*m_labels
)[n
] = ::CreateWindow
223 WS_CHILD
| WS_VISIBLE
| SS_CENTER
,
226 (HMENU
)NewControlId(),
232 m_labels
->SetFont(GetFont());
235 // now create the main control too
236 if ( !MSWCreateControl(TRACKBAR_CLASS
, wxEmptyString
, pos
, size
) )
239 // and initialize everything
240 SetRange(minValue
, maxValue
);
242 SetPageSize((maxValue
- minValue
)/10);
244 // we need to position the labels correctly if we have them and if
245 // SetSize() hadn't been called before (when best size was determined by
246 // MSWCreateControl()) as in this case they haven't been put in place yet
247 if ( m_labels
&& size
.x
!= wxDefaultCoord
&& size
.y
!= wxDefaultCoord
)
255 WXDWORD
wxSlider::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
257 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
259 // TBS_HORZ, TBS_RIGHT and TBS_BOTTOM are 0 but do include them for clarity
260 msStyle
|= style
& wxSL_VERTICAL
? TBS_VERT
: TBS_HORZ
;
262 if ( style
& wxSL_BOTH
)
264 // this fully specifies the style combined with TBS_VERT/HORZ above
267 else // choose one direction
269 if ( style
& wxSL_LEFT
)
271 else if ( style
& wxSL_RIGHT
)
272 msStyle
|= TBS_RIGHT
;
273 else if ( style
& wxSL_TOP
)
275 else if ( style
& wxSL_BOTTOM
)
276 msStyle
|= TBS_BOTTOM
;
279 if ( style
& wxSL_AUTOTICKS
)
280 msStyle
|= TBS_AUTOTICKS
;
282 msStyle
|= TBS_NOTICKS
;
284 if ( style
& wxSL_SELRANGE
)
285 msStyle
|= TBS_ENABLESELRANGE
;
290 wxSlider::~wxSlider()
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 bool wxSlider::MSWOnScroll(int WXUNUSED(orientation
),
301 WXWORD
WXUNUSED(pos
),
304 wxEventType scrollEvent
;
308 scrollEvent
= wxEVT_SCROLL_TOP
;
312 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
316 scrollEvent
= wxEVT_SCROLL_LINEUP
;
320 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
324 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
328 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
332 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
336 case SB_THUMBPOSITION
:
339 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
340 m_isDragging
= false;
344 // this seems to only happen when the mouse wheel is used: in
345 // this case, as it might be unexpected to get THUMBRELEASE
346 // without preceding THUMBTRACKs, we don't generate it at all
347 // but generate CHANGED event because the control itself does
348 // not send us SB_ENDSCROLL for whatever reason when mouse
350 scrollEvent
= wxEVT_SCROLL_CHANGED
;
355 scrollEvent
= wxEVT_SCROLL_CHANGED
;
359 // unknown scroll event?
363 int newPos
= ValueInvertOrNot((int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0));
364 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
366 // out of range - but we did process it
372 wxScrollEvent
event(scrollEvent
, m_windowId
);
373 event
.SetPosition(newPos
);
374 event
.SetEventObject( this );
375 GetEventHandler()->ProcessEvent(event
);
377 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
378 cevent
.SetInt( newPos
);
379 cevent
.SetEventObject( this );
381 return GetEventHandler()->ProcessEvent( cevent
);
384 void wxSlider::Command (wxCommandEvent
& event
)
386 SetValue (event
.GetInt());
387 ProcessCommand (event
);
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
394 wxRect
wxSlider::GetBoundingBox() const
396 // take care not to call our own functions which would call us recursively
398 wxSliderBase::DoGetPosition(&x
, &y
);
399 wxSliderBase::DoGetSize(&w
, &h
);
401 wxRect
rect(x
, y
, w
, h
);
404 wxRect lrect
= m_labels
->GetBoundingBox();
405 GetParent()->ScreenToClient(&lrect
.x
, &lrect
.y
);
412 void wxSlider::DoGetSize(int *width
, int *height
) const
414 wxRect rect
= GetBoundingBox();
419 *height
= rect
.height
;
422 void wxSlider::DoGetPosition(int *x
, int *y
) const
424 wxRect rect
= GetBoundingBox();
432 int wxSlider::GetLabelsSize(int *width
) const
438 // find the max label width
439 int wLabelMin
, wLabelMax
;
440 GetTextExtent(Format(m_rangeMin
), &wLabelMin
, &cy
);
441 GetTextExtent(Format(m_rangeMax
), &wLabelMax
, &cy
);
443 *width
= wxMax(wLabelMin
, wLabelMax
);
447 cy
= GetCharHeight();
450 return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
453 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
455 // all complications below are because we need to position the labels,
456 // without them everything is easy
459 wxSliderBase::DoMoveWindow(x
, y
, width
, height
);
463 // if our parent had prepared a defer window handle for us, use it (unless
464 // we are a top level window)
465 wxWindowMSW
*parent
= GetParent();
467 #if USE_DEFERRED_SIZING
468 HDWP hdwp
= parent
&& !IsTopLevel() ? (HDWP
)parent
->m_hDWP
: NULL
;
473 // be careful to position the slider itself after moving the labels as
474 // otherwise our GetBoundingBox(), which is called from WM_SIZE handler,
475 // would return a wrong result and wrong size would be cached internally
476 if ( HasFlag(wxSL_VERTICAL
) )
479 int hLabel
= GetLabelsSize(&wLabel
);
481 int xLabel
= HasFlag(wxSL_LEFT
) ? x
+ width
- wLabel
: x
;
483 // position all labels: min at the top, value in the middle and max at
485 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Min
],
486 xLabel
, y
, wLabel
, hLabel
);
488 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Value
],
489 xLabel
, y
+ (height
- hLabel
)/2, wLabel
, hLabel
);
491 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Max
],
492 xLabel
, y
+ height
- hLabel
, wLabel
, hLabel
);
494 // position the slider itself along the left/right edge
495 wxMoveWindowDeferred(hdwp
, this, GetHwnd(),
496 HasFlag(wxSL_LEFT
) ? x
: x
+ wLabel
+ HGAP
,
498 width
- wLabel
- HGAP
,
504 int hLabel
= GetLabelsSize(&wLabel
);
506 int yLabel
= HasFlag(wxSL_TOP
) ? y
+ height
- hLabel
: y
;
508 // position all labels: min on the left, value in the middle and max to
510 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Min
],
511 x
, yLabel
, wLabel
, hLabel
);
513 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Value
],
514 x
+ (width
- wLabel
)/2, yLabel
, wLabel
, hLabel
);
516 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Max
],
517 x
+ width
- wLabel
, yLabel
, wLabel
, hLabel
);
519 // position the slider itself along the top/bottom edge
520 wxMoveWindowDeferred(hdwp
, this, GetHwnd(),
522 HasFlag(wxSL_TOP
) ? y
: y
+ hLabel
,
527 #if USE_DEFERRED_SIZING
530 // hdwp must be updated as it may have been changed
531 parent
->m_hDWP
= (WXHANDLE
)hdwp
;
536 wxSize
wxSlider::DoGetBestSize() const
538 // these values are arbitrary
539 static const int length
= 100;
540 static const int thumb
= 24;
541 static const int ticks
= 8;
545 if ( HasFlag(wxSL_VERTICAL
) )
554 int hLabel
= GetLabelsSize(&wLabel
);
556 // account for the labels
557 size
.x
+= HGAP
+ wLabel
;
559 // labels are indented relative to the slider itself
571 // labels add extra height
572 size
.y
+= GetLabelsSize();
576 // need extra space to show ticks
577 if ( HasFlag(wxSL_TICKS
) )
581 // and maybe twice as much if we show them on both sides
582 if ( HasFlag(wxSL_BOTH
) )
589 // ----------------------------------------------------------------------------
590 // slider-specific methods
591 // ----------------------------------------------------------------------------
593 int wxSlider::GetValue() const
595 return ValueInvertOrNot(::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0));
598 void wxSlider::SetValue(int value
)
600 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)ValueInvertOrNot(value
));
604 ::SetWindowText((*m_labels
)[SliderLabel_Value
], Format(value
));
608 void wxSlider::SetRange(int minValue
, int maxValue
)
610 m_rangeMin
= minValue
;
611 m_rangeMax
= maxValue
;
613 ::SendMessage(GetHwnd(), TBM_SETRANGEMIN
, TRUE
, m_rangeMin
);
614 ::SendMessage(GetHwnd(), TBM_SETRANGEMAX
, TRUE
, m_rangeMax
);
618 ::SetWindowText((*m_labels
)[SliderLabel_Min
], Format(ValueInvertOrNot(m_rangeMin
)));
619 ::SetWindowText((*m_labels
)[SliderLabel_Max
], Format(ValueInvertOrNot(m_rangeMax
)));
623 void wxSlider::SetTickFreq(int n
, int pos
)
626 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
629 void wxSlider::SetPageSize(int pageSize
)
631 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
632 m_pageSize
= pageSize
;
635 int wxSlider::GetPageSize() const
640 void wxSlider::ClearSel()
642 ::SendMessage(GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0);
645 void wxSlider::ClearTicks()
647 ::SendMessage(GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0);
650 void wxSlider::SetLineSize(int lineSize
)
652 m_lineSize
= lineSize
;
653 ::SendMessage(GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
656 int wxSlider::GetLineSize() const
658 return (int)::SendMessage(GetHwnd(), TBM_GETLINESIZE
, 0, 0);
661 int wxSlider::GetSelEnd() const
663 return (int)::SendMessage(GetHwnd(), TBM_SETSELEND
, 0, 0);
666 int wxSlider::GetSelStart() const
668 return (int)::SendMessage(GetHwnd(), TBM_GETSELSTART
, 0, 0);
671 void wxSlider::SetSelection(int minPos
, int maxPos
)
673 ::SendMessage(GetHwnd(), TBM_SETSEL
,
674 (WPARAM
) TRUE
/* redraw */,
675 (LPARAM
) MAKELONG( minPos
, maxPos
) );
678 void wxSlider::SetThumbLength(int len
)
680 ::SendMessage(GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0);
683 int wxSlider::GetThumbLength() const
685 return (int)::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
, 0, 0);
688 void wxSlider::SetTick(int tickPos
)
690 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
693 // ----------------------------------------------------------------------------
694 // composite control methods
695 // ----------------------------------------------------------------------------
697 WXHWND
wxSlider::GetStaticMin() const
699 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Min
] : NULL
;
702 WXHWND
wxSlider::GetStaticMax() const
704 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Max
] : NULL
;
707 WXHWND
wxSlider::GetEditValue() const
709 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Value
] : NULL
;
712 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxSlider
, wxSliderBase
, m_labels
)
714 #endif // wxUSE_SLIDER