📄 slider.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// Name: slider.cpp
// Purpose: wxSlider
// Author: Stefan Csomor
// Modified by:
// Created: 1998-01-01
// RCS-ID: $Id: slider.cpp,v 1.53 2006/04/11 05:45:31 SC Exp $
// Copyright: (c) Stefan Csomor
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "wx/wxprec.h"
#if wxUSE_SLIDER
#include "wx/slider.h"
#include "wx/mac/uma.h"
IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
BEGIN_EVENT_TABLE(wxSlider, wxControl)
END_EVENT_TABLE()
// The dimensions of the different styles of sliders (from Aqua document)
#define wxSLIDER_DIMENSIONACROSS 15
#define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
#define wxSLIDER_DIMENSIONACROSS_ARROW 18
// Distance between slider and text
#define wxSLIDER_BORDERTEXT 5
// NB: The default orientation for a slider is horizontal; however, if the user specifies
// some slider styles but doesn't specify the orientation we have to assume he wants a
// horizontal one. Therefore in this file when testing for the slider's orientation
// vertical is tested for if this is not set then we use the horizontal one
// e.g., if (GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }.
wxSlider::wxSlider()
{
m_pageSize = 1;
m_lineSize = 1;
m_rangeMax = 0;
m_rangeMin = 0;
m_tickFreq = 0;
m_macMinimumStatic = NULL;
m_macMaximumStatic = NULL;
m_macValueStatic = NULL;
}
bool wxSlider::Create(wxWindow *parent,
wxWindowID id,
int value, int minValue, int maxValue,
const wxPoint& pos,
const wxSize& size, long style,
const wxValidator& validator,
const wxString& name)
{
m_macIsUserPane = false;
m_macMinimumStatic = NULL;
m_macMaximumStatic = NULL;
m_macValueStatic = NULL;
m_lineSize = 1;
m_tickFreq = 0;
m_rangeMax = maxValue;
m_rangeMin = minValue;
m_pageSize = (int)((maxValue - minValue) / 10);
// our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
// wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
// reasons we can't really change it, instead try to infer the orientation
// from the flags given to us here
switch ( style & (wxSL_LEFT | wxSL_RIGHT | wxSL_TOP | wxSL_BOTTOM) )
{
case wxSL_LEFT:
case wxSL_RIGHT:
style |= wxSL_VERTICAL;
break;
case wxSL_TOP:
case wxSL_BOTTOM:
style |= wxSL_HORIZONTAL;
break;
case 0:
default:
// no specific direction, do we have at least the orientation?
if ( !(style & (wxSL_HORIZONTAL | wxSL_VERTICAL)) )
// no: choose default
style |= wxSL_BOTTOM | wxSL_HORIZONTAL;
break;
}
wxASSERT_MSG( !(style & wxSL_VERTICAL) || !(style & wxSL_HORIZONTAL),
wxT("incompatible slider direction and orientation") );
if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
return false;
Rect bounds = wxMacGetBoundsForControl( this , pos , size );
// NB: (RN) Ticks here are sometimes off in the GUI if there
// are not as many tick marks as there are values
//
int tickMarks = 0;
if ( style & wxSL_AUTOTICKS )
tickMarks = (maxValue - minValue) + 1; // +1 for the 0 value
// keep the number of tickmarks from becoming unwieldly, therefore below it is ok to cast
// it to a UInt16
while (tickMarks > 20)
tickMarks /= 5;
m_peer = new wxMacControl( this );
OSStatus err = CreateSliderControl(
MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
value, minValue, maxValue,
kControlSliderPointsDownOrRight,
(UInt16) tickMarks, true /* liveTracking */,
GetwxMacLiveScrollbarActionProc(),
m_peer->GetControlRefAddr() );
verify_noerr( err );
if (style & wxSL_VERTICAL)
// Forces SetSize to use the proper width
SetSizeHints(10, -1, 10, -1);
else
// Forces SetSize to use the proper height
SetSizeHints(-1, 10, -1, 10);
// NB: SetSizeHints is overloaded by wxSlider and will substitute 10 with the
// proper dimensions, it also means other people cannot bugger the slider with
// other values
if (style & wxSL_LABELS)
{
m_macMinimumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
m_macMaximumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
m_macValueStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
}
SetRange(minValue, maxValue);
SetValue(value);
MacPostControlCreate(pos, size);
return true;
}
wxSlider::~wxSlider()
{
// this is a special case, as we had to add windows as siblings we are
// responsible for their disposal, but only if we are not part of a DestroyAllChildren
if ( m_parent && !m_parent->IsBeingDeleted() )
{
delete m_macMinimumStatic;
delete m_macMaximumStatic;
delete m_macValueStatic;
}
}
int wxSlider::GetValue() const
{
// We may need to invert the value returned by the widget
return ValueInvertOrNot( m_peer->GetValue() ) ;
}
void wxSlider::SetValue(int value)
{
if ( m_macValueStatic )
{
wxString valuestring;
valuestring.Printf( wxT("%d"), value );
m_macValueStatic->SetLabel( valuestring );
}
// We only invert for the setting of the actual native widget
m_peer->SetValue( ValueInvertOrNot( value ) );
}
void wxSlider::SetRange(int minValue, int maxValue)
{
wxString value;
m_rangeMin = minValue;
m_rangeMax = maxValue;
m_peer->SetMinimum( m_rangeMin );
m_peer->SetMaximum( m_rangeMax );
if (m_macMinimumStatic)
{
value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
m_macMinimumStatic->SetLabel( value );
}
if (m_macMaximumStatic)
{
value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
m_macMaximumStatic->SetLabel( value );
}
// If the range is out of bounds, set it to a
// value that is within bounds
// RN: Testing reveals OSX does its own
// bounding, perhaps this isn't needed?
int currentValue = GetValue();
if(currentValue < m_rangeMin)
SetValue(m_rangeMin);
else if(currentValue > m_rangeMax)
SetValue(m_rangeMax);
}
// For trackbars only
void wxSlider::SetTickFreq(int n, int pos)
{
// TODO
m_tickFreq = n;
}
void wxSlider::SetPageSize(int pageSize)
{
// TODO
m_pageSize = pageSize;
}
int wxSlider::GetPageSize() const
{
return m_pageSize;
}
void wxSlider::ClearSel()
{
// TODO
}
void wxSlider::ClearTicks()
{
// TODO
}
void wxSlider::SetLineSize(int lineSize)
{
m_lineSize = lineSize;
// TODO
}
int wxSlider::GetLineSize() const
{
// TODO
return 0;
}
int wxSlider::GetSelEnd() const
{
// TODO
return 0;
}
int wxSlider::GetSelStart() const
{
// TODO
return 0;
}
void wxSlider::SetSelection(int minPos, int maxPos)
{
// TODO
}
void wxSlider::SetThumbLength(int len)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -