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
46 #define USE_DEFER_BUG_WORKAROUND 0
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // indices of labels in wxSlider::m_labels
61 // the gap between the slider and the labels, in pixels
62 static const int HGAP
= 5;
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 #if wxUSE_EXTENDED_RTTI
69 WX_DEFINE_FLAGS( wxSliderStyle
)
71 wxBEGIN_FLAGS( wxSliderStyle
)
72 // new style border flags, we put them first to
73 // use them for streaming out
74 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
75 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
76 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
77 wxFLAGS_MEMBER(wxBORDER_RAISED
)
78 wxFLAGS_MEMBER(wxBORDER_STATIC
)
79 wxFLAGS_MEMBER(wxBORDER_NONE
)
81 // old style border flags
82 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
83 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
84 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
85 wxFLAGS_MEMBER(wxRAISED_BORDER
)
86 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
87 wxFLAGS_MEMBER(wxBORDER
)
89 // standard window styles
90 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
91 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
92 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
93 wxFLAGS_MEMBER(wxWANTS_CHARS
)
94 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
95 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
96 wxFLAGS_MEMBER(wxVSCROLL
)
97 wxFLAGS_MEMBER(wxHSCROLL
)
99 wxFLAGS_MEMBER(wxSL_HORIZONTAL
)
100 wxFLAGS_MEMBER(wxSL_VERTICAL
)
101 wxFLAGS_MEMBER(wxSL_AUTOTICKS
)
102 wxFLAGS_MEMBER(wxSL_LABELS
)
103 wxFLAGS_MEMBER(wxSL_LEFT
)
104 wxFLAGS_MEMBER(wxSL_TOP
)
105 wxFLAGS_MEMBER(wxSL_RIGHT
)
106 wxFLAGS_MEMBER(wxSL_BOTTOM
)
107 wxFLAGS_MEMBER(wxSL_BOTH
)
108 wxFLAGS_MEMBER(wxSL_SELRANGE
)
109 wxFLAGS_MEMBER(wxSL_INVERSE
)
111 wxEND_FLAGS( wxSliderStyle
)
113 IMPLEMENT_DYNAMIC_CLASS_XTI(wxSlider
, wxControl
,"wx/scrolbar.h")
115 wxBEGIN_PROPERTIES_TABLE(wxSlider
)
116 wxEVENT_RANGE_PROPERTY( Scroll
, wxEVT_SCROLL_TOP
, wxEVT_SCROLL_ENDSCROLL
, wxScrollEvent
)
117 wxEVENT_PROPERTY( Updated
, wxEVT_COMMAND_SLIDER_UPDATED
, wxCommandEvent
)
119 wxPROPERTY( Value
, int , SetValue
, GetValue
, 0, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
120 wxPROPERTY( Minimum
, int , SetMin
, GetMin
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
121 wxPROPERTY( Maximum
, int , SetMax
, GetMax
, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
122 wxPROPERTY( PageSize
, int , SetPageSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
123 wxPROPERTY( LineSize
, int , SetLineSize
, GetLineSize
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
124 wxPROPERTY( ThumbLength
, int , SetThumbLength
, GetThumbLength
, 1 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
125 wxPROPERTY_FLAGS( WindowStyle
, wxSliderStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
126 wxEND_PROPERTIES_TABLE()
128 wxBEGIN_HANDLERS_TABLE(wxSlider
)
129 wxEND_HANDLERS_TABLE()
131 wxCONSTRUCTOR_8( wxSlider
, wxWindow
* , Parent
, wxWindowID
, Id
, int , Value
, int , Minimum
, int , Maximum
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
133 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
136 // ============================================================================
137 // wxSlider implementation
138 // ============================================================================
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 void wxSlider::Init()
156 wxSlider::Create(wxWindow
*parent
,
164 const wxValidator
& validator
,
165 const wxString
& name
)
167 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
168 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
169 // reasons we can't really change it, instead try to infer the orientation
170 // from the flags given to us here
171 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
175 style
|= wxSL_VERTICAL
;
180 style
|= wxSL_HORIZONTAL
;
184 // no specific direction, do we have at least the orientation?
185 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
187 // no, choose default
188 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
192 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) | !(style
& wxSL_HORIZONTAL
),
193 _T("incompatible slider direction and orientation") );
196 // initialize everything
197 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
200 // ensure that we have correct values for GetLabelsSize()
201 m_rangeMin
= minValue
;
202 m_rangeMax
= maxValue
;
204 // create the labels first, so that our DoGetBestSize() could take them
207 // note that we could simply create 3 wxStaticTexts here but it could
208 // result in some observable side effects at wx level (e.g. the parent of
209 // wxSlider would have 3 more children than expected) and so we prefer not
210 // to do it like this
211 if ( m_windowStyle
& wxSL_LABELS
)
213 m_labels
= new wxSubwindows(SliderLabel_Last
);
215 HWND hwndParent
= GetHwndOf(parent
);
216 for ( size_t n
= 0; n
< SliderLabel_Last
; n
++ )
218 (*m_labels
)[n
] = ::CreateWindow
222 WS_CHILD
| WS_VISIBLE
| SS_CENTER
,
225 (HMENU
)NewControlId(),
231 m_labels
->SetFont(GetFont());
234 // now create the main control too
235 if ( !MSWCreateControl(TRACKBAR_CLASS
, wxEmptyString
, pos
, size
) )
238 // and initialize everything
239 SetRange(minValue
, maxValue
);
241 SetPageSize((maxValue
- minValue
)/10);
243 // we need to position the labels correctly if we have them and if
244 // SetSize() hadn't been called before (when best size was determined by
245 // MSWCreateControl()) as in this case they haven't been put in place yet
246 if ( m_labels
&& size
.x
!= wxDefaultCoord
&& size
.y
!= wxDefaultCoord
)
254 WXDWORD
wxSlider::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
256 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
258 // TBS_HORZ, TBS_RIGHT and TBS_BOTTOM are 0 but do include them for clarity
259 msStyle
|= style
& wxSL_VERTICAL
? TBS_VERT
: TBS_HORZ
;
261 if ( style
& wxSL_BOTH
)
263 // this fully specifies the style combined with TBS_VERT/HORZ above
266 else // choose one direction
268 if ( style
& wxSL_LEFT
)
270 else if ( style
& wxSL_RIGHT
)
271 msStyle
|= TBS_RIGHT
;
272 else if ( style
& wxSL_TOP
)
274 else if ( style
& wxSL_BOTTOM
)
275 msStyle
|= TBS_BOTTOM
;
278 if ( style
& wxSL_AUTOTICKS
)
279 msStyle
|= TBS_AUTOTICKS
;
281 msStyle
|= TBS_NOTICKS
;
283 if ( style
& wxSL_SELRANGE
)
284 msStyle
|= TBS_ENABLESELRANGE
;
289 wxSlider::~wxSlider()
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 bool wxSlider::MSWOnScroll(int WXUNUSED(orientation
),
300 WXWORD
WXUNUSED(pos
),
303 wxEventType scrollEvent
;
307 scrollEvent
= wxEVT_SCROLL_TOP
;
311 scrollEvent
= wxEVT_SCROLL_BOTTOM
;
315 scrollEvent
= wxEVT_SCROLL_LINEUP
;
319 scrollEvent
= wxEVT_SCROLL_LINEDOWN
;
323 scrollEvent
= wxEVT_SCROLL_PAGEUP
;
327 scrollEvent
= wxEVT_SCROLL_PAGEDOWN
;
331 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
334 case SB_THUMBPOSITION
:
335 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
339 scrollEvent
= wxEVT_SCROLL_ENDSCROLL
;
343 // unknown scroll event?
347 int newPos
= ValueInvertOrNot((int) ::SendMessage((HWND
) control
, TBM_GETPOS
, 0, 0));
348 if ( (newPos
< GetMin()) || (newPos
> GetMax()) )
350 // out of range - but we did process it
356 wxScrollEvent
event(scrollEvent
, m_windowId
);
357 event
.SetPosition(newPos
);
358 event
.SetEventObject( this );
359 GetEventHandler()->ProcessEvent(event
);
361 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, GetId() );
362 cevent
.SetInt( newPos
);
363 cevent
.SetEventObject( this );
365 return GetEventHandler()->ProcessEvent( cevent
);
368 void wxSlider::Command (wxCommandEvent
& event
)
370 SetValue (event
.GetInt());
371 ProcessCommand (event
);
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 wxRect
wxSlider::GetBoundingBox() const
380 // take care not to call our own functions which would call us recursively
382 wxSliderBase::DoGetPosition(&x
, &y
);
383 wxSliderBase::DoGetSize(&w
, &h
);
385 wxRect
rect(x
, y
, w
, h
);
388 wxRect lrect
= m_labels
->GetBoundingBox();
389 GetParent()->ScreenToClient(&lrect
.x
, &lrect
.y
);
396 void wxSlider::DoGetSize(int *width
, int *height
) const
398 wxRect rect
= GetBoundingBox();
403 *height
= rect
.height
;
406 void wxSlider::DoGetPosition(int *x
, int *y
) const
408 wxRect rect
= GetBoundingBox();
416 int wxSlider::GetLabelsSize(int *width
) const
422 // find the max label width
423 int wLabelMin
, wLabelMax
;
424 GetTextExtent(Format(m_rangeMin
), &wLabelMin
, &cy
);
425 GetTextExtent(Format(m_rangeMax
), &wLabelMax
, &cy
);
427 *width
= wxMax(wLabelMin
, wLabelMax
);
431 cy
= GetCharHeight();
434 return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy
);
437 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
439 // all complications below are because we need to position the labels,
440 // without them everything is easy
443 wxSliderBase::DoMoveWindow(x
, y
, width
, height
);
447 // if our parent had prepared a defer window handle for us, use it (unless
448 // we are a top level window)
449 wxWindowMSW
*parent
= GetParent();
451 #if USE_DEFERRED_SIZING
452 HDWP hdwp
= parent
&& !IsTopLevel() ? (HDWP
)parent
->m_hDWP
: NULL
;
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 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Min
],
470 xLabel
, y
, wLabel
, hLabel
);
472 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Value
],
473 xLabel
, y
+ (height
- hLabel
)/2, wLabel
, hLabel
);
475 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Max
],
476 xLabel
, y
+ height
- hLabel
, wLabel
, hLabel
);
478 // position the slider itself along the left/right edge
479 wxMoveWindowDeferred(hdwp
, this, GetHwnd(),
480 HasFlag(wxSL_LEFT
) ? x
: x
+ wLabel
+ HGAP
,
482 width
- wLabel
- HGAP
,
488 int hLabel
= GetLabelsSize(&wLabel
);
490 int yLabel
= HasFlag(wxSL_TOP
) ? y
+ height
- hLabel
: y
;
492 // position all labels: min on the left, value in the middle and max to
494 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Min
],
495 x
, yLabel
, wLabel
, hLabel
);
497 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Value
],
498 x
+ (width
- wLabel
)/2, yLabel
, wLabel
, hLabel
);
500 wxMoveWindowDeferred(hdwp
, this, (*m_labels
)[SliderLabel_Max
],
501 x
+ width
- wLabel
, yLabel
, wLabel
, hLabel
);
503 // position the slider itself along the top/bottom edge
504 wxMoveWindowDeferred(hdwp
, this, GetHwnd(),
506 HasFlag(wxSL_TOP
) ? y
: y
+ hLabel
,
512 // Store the size so we can report it accurately
513 wxExtraWindowData
* extraData
= (wxExtraWindowData
*) m_windowReserved
;
516 extraData
= new wxExtraWindowData
;
517 m_windowReserved
= (void*) extraData
;
519 extraData
->m_pos
= wxPoint(x
, y
);
520 extraData
->m_size
= wxSize(width
, height
);
521 extraData
->m_deferring
= true;
523 // hdwp must be updated as it may have been changed
524 parent
->m_hDWP
= (WXHANDLE
)hdwp
;
528 wxSize
wxSlider::DoGetBestSize() const
530 // these values are arbitrary
531 static const int length
= 100;
532 static const int thumb
= 24;
533 static const int ticks
= 8;
537 if ( HasFlag(wxSL_VERTICAL
) )
546 int hLabel
= GetLabelsSize(&wLabel
);
548 // account for the labels
549 size
.x
+= HGAP
+ wLabel
;
551 // labels are indented relative to the slider itself
563 // labels add extra height
564 size
.y
+= GetLabelsSize();
568 // need extra space to show ticks
569 if ( HasFlag(wxSL_TICKS
) )
573 // and maybe twice as much if we show them on both sides
574 if ( HasFlag(wxSL_BOTH
) )
581 // ----------------------------------------------------------------------------
582 // slider-specific methods
583 // ----------------------------------------------------------------------------
585 int wxSlider::GetValue() const
587 return ValueInvertOrNot(::SendMessage(GetHwnd(), TBM_GETPOS
, 0, 0));
590 void wxSlider::SetValue(int value
)
592 ::SendMessage(GetHwnd(), TBM_SETPOS
, (WPARAM
)TRUE
, (LPARAM
)ValueInvertOrNot(value
));
596 ::SetWindowText((*m_labels
)[SliderLabel_Value
], Format(value
));
600 void wxSlider::SetRange(int minValue
, int maxValue
)
602 m_rangeMin
= minValue
;
603 m_rangeMax
= maxValue
;
605 ::SendMessage(GetHwnd(), TBM_SETRANGEMIN
, TRUE
, m_rangeMin
);
606 ::SendMessage(GetHwnd(), TBM_SETRANGEMAX
, TRUE
, m_rangeMax
);
610 ::SetWindowText((*m_labels
)[SliderLabel_Min
], Format(ValueInvertOrNot(m_rangeMin
)));
611 ::SetWindowText((*m_labels
)[SliderLabel_Max
], Format(ValueInvertOrNot(m_rangeMax
)));
615 void wxSlider::SetTickFreq(int n
, int pos
)
618 ::SendMessage( GetHwnd(), TBM_SETTICFREQ
, (WPARAM
) n
, (LPARAM
) pos
);
621 void wxSlider::SetPageSize(int pageSize
)
623 ::SendMessage( GetHwnd(), TBM_SETPAGESIZE
, (WPARAM
) 0, (LPARAM
) pageSize
);
624 m_pageSize
= pageSize
;
627 int wxSlider::GetPageSize() const
632 void wxSlider::ClearSel()
634 ::SendMessage(GetHwnd(), TBM_CLEARSEL
, (WPARAM
) TRUE
, (LPARAM
) 0);
637 void wxSlider::ClearTicks()
639 ::SendMessage(GetHwnd(), TBM_CLEARTICS
, (WPARAM
) TRUE
, (LPARAM
) 0);
642 void wxSlider::SetLineSize(int lineSize
)
644 m_lineSize
= lineSize
;
645 ::SendMessage(GetHwnd(), TBM_SETLINESIZE
, (WPARAM
) 0, (LPARAM
) lineSize
);
648 int wxSlider::GetLineSize() const
650 return (int)::SendMessage(GetHwnd(), TBM_GETLINESIZE
, 0, 0);
653 int wxSlider::GetSelEnd() const
655 return (int)::SendMessage(GetHwnd(), TBM_SETSELEND
, 0, 0);
658 int wxSlider::GetSelStart() const
660 return (int)::SendMessage(GetHwnd(), TBM_GETSELSTART
, 0, 0);
663 void wxSlider::SetSelection(int minPos
, int maxPos
)
665 ::SendMessage(GetHwnd(), TBM_SETSEL
,
666 (WPARAM
) TRUE
/* redraw */,
667 (LPARAM
) MAKELONG( minPos
, maxPos
) );
670 void wxSlider::SetThumbLength(int len
)
672 ::SendMessage(GetHwnd(), TBM_SETTHUMBLENGTH
, (WPARAM
) len
, (LPARAM
) 0);
675 int wxSlider::GetThumbLength() const
677 return (int)::SendMessage( GetHwnd(), TBM_GETTHUMBLENGTH
, 0, 0);
680 void wxSlider::SetTick(int tickPos
)
682 ::SendMessage( GetHwnd(), TBM_SETTIC
, (WPARAM
) 0, (LPARAM
) tickPos
);
685 // ----------------------------------------------------------------------------
686 // composite control methods
687 // ----------------------------------------------------------------------------
689 WXHWND
wxSlider::GetStaticMin() const
691 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Min
] : NULL
;
694 WXHWND
wxSlider::GetStaticMax() const
696 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Max
] : NULL
;
699 WXHWND
wxSlider::GetEditValue() const
701 return m_labels
? (WXHWND
)(*m_labels
)[SliderLabel_Value
] : NULL
;
704 WX_FORWARD_STD_METHODS_TO_SUBWINDOWS(wxSlider
, wxSliderBase
, m_labels
)
706 #endif // wxUSE_SLIDER