Improve positioning of wxSlider min/max labels in wxMSW.

Put the min/max labels on the sides or a horizontal slider or above/below a
vertical one to make them visually more distinct from the current value label.

See #11427.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65941 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2010-10-28 13:52:50 +00:00
parent 4ea1bb9d09
commit ce0ee9aec1
2 changed files with 215 additions and 84 deletions

View File

@@ -106,8 +106,8 @@ protected:
// get the boundig box for the slider and possible labels // get the boundig box for the slider and possible labels
wxRect GetBoundingBox() const; wxRect GetBoundingBox() const;
// get the height and, if the pointer is not NULL, width of our labels // Get the height and, if the pointers are non NULL, widths of both labels.
int GetLabelsSize(int *width = NULL) const; int GetLabelsSize(int *widthMin = NULL, int *widthMax = NULL) const;
// overridden base class virtuals // overridden base class virtuals
@@ -124,6 +124,8 @@ protected:
int m_pageSize; int m_pageSize;
int m_lineSize; int m_lineSize;
int m_tickFreq; int m_tickFreq;
int m_minLabelWidth;
int m_maxLabelWidth;
// flag needed to detect whether we're getting THUMBRELEASE event because // flag needed to detect whether we're getting THUMBRELEASE event because
// of dragging the thumb or scrolling the mouse wheel // of dragging the thumb or scrolling the mouse wheel

View File

@@ -40,6 +40,9 @@
// constants // constants
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
namespace
{
// indices of labels in wxSlider::m_labels // indices of labels in wxSlider::m_labels
enum enum
{ {
@@ -49,8 +52,16 @@ enum
SliderLabel_Last SliderLabel_Last
}; };
// the gap between the slider and the labels, in pixels // the gaps between the slider and the labels, in pixels
static const int HGAP = 5; const int HGAP = 5;
const int VGAP = 4;
// the width of the borders including white space
const int BORDERPAD = 8;
// these 2 values are arbitrary:
const int THUMB = 24;
const int TICK = 8;
} // anonymous namespace
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// XTI // XTI
@@ -90,7 +101,6 @@ wxBEGIN_FLAGS( wxSliderStyle )
wxFLAGS_MEMBER(wxSL_HORIZONTAL) wxFLAGS_MEMBER(wxSL_HORIZONTAL)
wxFLAGS_MEMBER(wxSL_VERTICAL) wxFLAGS_MEMBER(wxSL_VERTICAL)
wxFLAGS_MEMBER(wxSL_AUTOTICKS) wxFLAGS_MEMBER(wxSL_AUTOTICKS)
wxFLAGS_MEMBER(wxSL_LABELS)
wxFLAGS_MEMBER(wxSL_LEFT) wxFLAGS_MEMBER(wxSL_LEFT)
wxFLAGS_MEMBER(wxSL_TOP) wxFLAGS_MEMBER(wxSL_TOP)
wxFLAGS_MEMBER(wxSL_RIGHT) wxFLAGS_MEMBER(wxSL_RIGHT)
@@ -98,6 +108,9 @@ wxBEGIN_FLAGS( wxSliderStyle )
wxFLAGS_MEMBER(wxSL_BOTH) wxFLAGS_MEMBER(wxSL_BOTH)
wxFLAGS_MEMBER(wxSL_SELRANGE) wxFLAGS_MEMBER(wxSL_SELRANGE)
wxFLAGS_MEMBER(wxSL_INVERSE) wxFLAGS_MEMBER(wxSL_INVERSE)
wxFLAGS_MEMBER(wxSL_MIN_MAX_LABELS)
wxFLAGS_MEMBER(wxSL_VALUE_LABEL)
wxFLAGS_MEMBER(wxSL_LABELS)
wxEND_FLAGS( wxSliderStyle ) wxEND_FLAGS( wxSliderStyle )
@@ -145,18 +158,20 @@ void wxSlider::Init()
m_isDragging = false; m_isDragging = false;
} }
bool bool wxSlider::Create(wxWindow *parent,
wxSlider::Create(wxWindow *parent, wxWindowID id,
wxWindowID id, int value,
int value, int minValue,
int minValue, int maxValue,
int maxValue, const wxPoint& pos,
const wxPoint& pos, const wxSize& size,
const wxSize& size, long style,
long style, const wxValidator& validator,
const wxValidator& validator, const wxString& name)
const wxString& name)
{ {
wxCHECK_MSG( minValue < maxValue, false,
wxT("Slider minimum must be strictly less than the maximum.") );
// our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
// wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
// reasons we can't really change it, instead try to infer the orientation // reasons we can't really change it, instead try to infer the orientation
@@ -224,7 +239,6 @@ wxSlider::Create(wxWindow *parent,
m_labels->Set(n, wnd, lblid); m_labels->Set(n, wnd, lblid);
} }
m_labels->SetFont(GetFont()); m_labels->SetFont(GetFont());
} }
@@ -425,25 +439,23 @@ void wxSlider::DoGetPosition(int *x, int *y) const
*y = rect.y; *y = rect.y;
} }
int wxSlider::GetLabelsSize(int *width) const int wxSlider::GetLabelsSize(int *widthMin, int *widthMax) const
{ {
int cy; if ( widthMin && widthMax )
if ( width )
{ {
// find the max label width if ( HasFlag(wxSL_MIN_MAX_LABELS) )
int wLabelMin, wLabelMax; {
GetTextExtent(Format(m_rangeMin), &wLabelMin, &cy); *widthMin = GetTextExtent(Format(m_rangeMin)).x;
GetTextExtent(Format(m_rangeMax), &wLabelMax, &cy); *widthMax = GetTextExtent(Format(m_rangeMax)).x;
}
*width = wxMax(wLabelMin, wLabelMax); else
} {
else *widthMin =
{ *widthMax = 0;
cy = GetCharHeight(); }
} }
return EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); return HasFlag(wxSL_LABELS) ? GetCharHeight() : 0;
} }
void wxSlider::DoMoveWindow(int x, int y, int width, int height) void wxSlider::DoMoveWindow(int x, int y, int width, int height)
@@ -456,81 +468,196 @@ void wxSlider::DoMoveWindow(int x, int y, int width, int height)
return; return;
} }
const int labelHeight = GetLabelsSize(&m_minLabelWidth, &m_maxLabelWidth);
const int maxLabelWidth = wxMax(m_minLabelWidth, m_maxLabelWidth);
int labelOffset = 0;
int tickOffset = 0;
if ( HasFlag(wxSL_TICKS))
tickOffset = TICK;
if ( HasFlag(wxSL_BOTH))
tickOffset *= 2;
// be careful to position the slider itself after moving the labels as // be careful to position the slider itself after moving the labels as
// otherwise our GetBoundingBox(), which is called from WM_SIZE handler, // otherwise our GetBoundingBox(), which is called from WM_SIZE handler,
// would return a wrong result and wrong size would be cached internally // would return a wrong result and wrong size would be cached internally
if ( HasFlag(wxSL_VERTICAL) ) if ( HasFlag(wxSL_VERTICAL) )
{ {
int wLabel; int holdTopWidth;
int hLabel = GetLabelsSize(&wLabel); int holdTopX;
int holdBottomWidth;
int xLabel = HasFlag(wxSL_LEFT) ? x + width - wLabel : x; int holdBottomX;
int xLabel = (wxMax((THUMB + (BORDERPAD * 2)), maxLabelWidth) / 2) -
// position all labels: min at the top, value in the middle and max at (maxLabelWidth / 2) + x;
// the bottom if ( HasFlag(wxSL_LEFT) )
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Min], {
xLabel, y, wLabel, hLabel); if ( HasFlag(wxSL_MIN_MAX_LABELS) )
{
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Value], holdTopX = xLabel;
xLabel, y + (height - hLabel)/2, wLabel, hLabel); holdTopWidth = m_minLabelWidth;
holdBottomX = xLabel - ((m_maxLabelWidth - m_minLabelWidth) / 2);
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Max], holdBottomWidth = m_maxLabelWidth;
xLabel, y + height - hLabel, wLabel, hLabel); if ( HasFlag(wxSL_INVERSE) )
{
wxSwap(holdTopWidth, holdBottomWidth);
wxSwap(holdTopX, holdBottomX);
}
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Min],
holdTopX,
y,
holdTopWidth, labelHeight);
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Max],
holdBottomX,
y + height - labelHeight,
holdBottomWidth, labelHeight);
}
if ( HasFlag(wxSL_VALUE_LABEL) )
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Value],
x + THUMB + tickOffset + HGAP,
y + (height - labelHeight)/2,
maxLabelWidth, labelHeight);
}
else // wxSL_RIGHT
{
if ( HasFlag(wxSL_MIN_MAX_LABELS) )
{
holdTopX = xLabel + maxLabelWidth + ((m_maxLabelWidth - m_minLabelWidth) / 2);
holdTopWidth = m_minLabelWidth;
holdBottomX = xLabel + maxLabelWidth;
holdBottomWidth = m_maxLabelWidth;
if ( HasFlag(wxSL_INVERSE) )
{
wxSwap(holdTopWidth, holdBottomWidth);
wxSwap(holdTopX, holdBottomX);
}
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Min],
holdTopX,
y,
holdTopWidth, labelHeight);
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Max],
holdBottomX,
y + height - labelHeight,
holdBottomWidth, labelHeight);
}
if ( HasFlag(wxSL_VALUE_LABEL) )
labelOffset = maxLabelWidth + HGAP;
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Value],
x,
y + (height - labelHeight)/2,
maxLabelWidth, labelHeight);
}
// position the slider itself along the left/right edge // position the slider itself along the left/right edge
wxSliderBase::DoMoveWindow(HasFlag(wxSL_LEFT) ? x : x + wLabel + HGAP, wxSliderBase::DoMoveWindow(
y + hLabel/2, x + labelOffset,
width - wLabel - HGAP, y + labelHeight,
height - hLabel); THUMB + tickOffset + HGAP,
height - (labelHeight * 2));
} }
else // horizontal else // horizontal
{ {
int wLabel; int holdLeftWidth;
int hLabel = GetLabelsSize(&wLabel); int holdLeftX;
int holdRightWidth;
int holdRightX;
int yLabelMinMax =
(y + ((THUMB + tickOffset) / 2)) - (labelHeight / 2);
int xLabelValue =
x + m_minLabelWidth +
((width - (m_minLabelWidth + m_maxLabelWidth)) / 2) -
(m_maxLabelWidth / 2);
int yLabel = HasFlag(wxSL_TOP) ? y + height - hLabel : y; if ( HasFlag(wxSL_BOTTOM) )
{
// position all labels: min on the left, value in the middle and max to if ( HasFlag(wxSL_MIN_MAX_LABELS) )
// the right {
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Min], holdLeftX = x;
x, yLabel, wLabel, hLabel); holdLeftWidth = m_minLabelWidth;
holdRightX = x + width - m_maxLabelWidth;
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Value], holdRightWidth = m_maxLabelWidth;
x + (width - wLabel)/2, yLabel, wLabel, hLabel); if ( HasFlag(wxSL_INVERSE) )
{
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Max], wxSwap(holdLeftWidth, holdRightWidth);
x + width - wLabel, yLabel, wLabel, hLabel); wxSwap(holdLeftX, holdRightX);
}
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Min],
holdLeftX,
yLabelMinMax,
holdLeftWidth, labelHeight);
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Max],
holdRightX,
yLabelMinMax,
holdRightWidth, labelHeight);
}
if ( HasFlag(wxSL_VALUE_LABEL) )
{
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Value],
xLabelValue,
y - labelHeight,
maxLabelWidth, labelHeight);
}
}
else // wxSL_TOP
{
if ( HasFlag(wxSL_MIN_MAX_LABELS) )
{
holdLeftX = x;
holdLeftWidth = m_minLabelWidth;
holdRightX = x + width - m_maxLabelWidth;
holdRightWidth = m_maxLabelWidth;
if ( HasFlag(wxSL_INVERSE) )
{
wxSwap(holdLeftWidth, holdRightWidth);
wxSwap(holdLeftX, holdRightX);
}
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Min],
holdLeftX,
yLabelMinMax,
holdLeftWidth, labelHeight);
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Max],
holdRightX,
yLabelMinMax,
holdRightWidth, labelHeight);
}
if ( HasFlag(wxSL_VALUE_LABEL) )
DoMoveSibling((HWND)(*m_labels)[SliderLabel_Value],
xLabelValue,
y + THUMB + tickOffset,
maxLabelWidth, labelHeight);
}
// position the slider itself along the top/bottom edge // position the slider itself along the top/bottom edge
wxSliderBase::DoMoveWindow(x, if ( HasFlag(wxSL_MIN_MAX_LABELS) || HasFlag(wxSL_VALUE_LABEL) )
HasFlag(wxSL_TOP) ? y : y + hLabel, labelOffset = labelHeight;
width, wxSliderBase::DoMoveWindow(
height - hLabel); x + m_minLabelWidth + VGAP,
y,
width - (m_minLabelWidth + m_maxLabelWidth + (VGAP*2)),
THUMB + tickOffset);
} }
} }
wxSize wxSlider::DoGetBestSize() const wxSize wxSlider::DoGetBestSize() const
{ {
// these values are arbitrary // this value is arbitrary:
static const int length = 100; static const int length = 100;
static const int thumb = 24;
static const int ticks = 8;
int *width; int *width;
wxSize size; wxSize size;
if ( HasFlag(wxSL_VERTICAL) ) if ( HasFlag(wxSL_VERTICAL) )
{ {
size.x = thumb; size.x = THUMB;
size.y = length; size.y = length;
width = &size.x; width = &size.x;
if ( m_labels ) if ( m_labels )
{ {
int wLabel; int widthMin,
int hLabel = GetLabelsSize(&wLabel); widthMax;
int hLabel = GetLabelsSize(&widthMin, &widthMax);
// account for the labels // account for the labels
size.x += HGAP + wLabel; size.x += HGAP + wxMax(widthMin, widthMax);
// labels are indented relative to the slider itself // labels are indented relative to the slider itself
size.y += hLabel; size.y += hLabel;
@@ -539,26 +666,28 @@ wxSize wxSlider::DoGetBestSize() const
else // horizontal else // horizontal
{ {
size.x = length; size.x = length;
size.y = thumb; size.y = THUMB;
width = &size.y; width = &size.y;
if ( m_labels ) if ( m_labels )
{ {
// labels add extra height // labels add extra height
size.y += GetLabelsSize(); int labelSize = GetLabelsSize();
if ( HasFlag(wxSL_MIN_MAX_LABELS) )
size.y += labelSize;
if ( HasFlag(wxSL_VALUE_LABEL) )
size.y += labelSize*2.75;
} }
} }
// need extra space to show ticks // need extra space to show ticks
if ( HasFlag(wxSL_TICKS) ) if ( HasFlag(wxSL_TICKS) )
{ {
*width += ticks; *width += TICK;
// and maybe twice as much if we show them on both sides // and maybe twice as much if we show them on both sides
if ( HasFlag(wxSL_BOTH) ) if ( HasFlag(wxSL_BOTH) )
*width += ticks; *width += TICK;
} }
return size; return size;
} }
@@ -575,7 +704,7 @@ void wxSlider::SetValue(int value)
{ {
::SendMessage(GetHwnd(), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)ValueInvertOrNot(value)); ::SendMessage(GetHwnd(), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)ValueInvertOrNot(value));
if ( HasFlag(wxSL_VALUE_LABEL) ) if ( m_labels )
{ {
::SetWindowText((*m_labels)[SliderLabel_Value], Format(value).wx_str()); ::SetWindowText((*m_labels)[SliderLabel_Value], Format(value).wx_str());
} }
@@ -589,7 +718,7 @@ void wxSlider::SetRange(int minValue, int maxValue)
::SendMessage(GetHwnd(), TBM_SETRANGEMIN, TRUE, m_rangeMin); ::SendMessage(GetHwnd(), TBM_SETRANGEMIN, TRUE, m_rangeMin);
::SendMessage(GetHwnd(), TBM_SETRANGEMAX, TRUE, m_rangeMax); ::SendMessage(GetHwnd(), TBM_SETRANGEMAX, TRUE, m_rangeMax);
if ( HasFlag(wxSL_MIN_MAX_LABELS) ) if ( m_labels )
{ {
::SetWindowText((*m_labels)[SliderLabel_Min], ::SetWindowText((*m_labels)[SliderLabel_Min],
Format(ValueInvertOrNot(m_rangeMin)).wx_str()); Format(ValueInvertOrNot(m_rangeMin)).wx_str());