Demonstrate wxSlider::SetSelection() in widgets sample
This commit is contained in:
@@ -59,6 +59,7 @@ enum
|
||||
SliderPage_Clear,
|
||||
SliderPage_SetValue,
|
||||
SliderPage_SetMinAndMax,
|
||||
SliderPage_SetRange,
|
||||
SliderPage_SetLineSize,
|
||||
SliderPage_SetPageSize,
|
||||
SliderPage_SetTickFreq,
|
||||
@@ -67,12 +68,15 @@ enum
|
||||
SliderPage_ValueText,
|
||||
SliderPage_MinText,
|
||||
SliderPage_MaxText,
|
||||
SliderPage_RangeMinText,
|
||||
SliderPage_RangeMaxText,
|
||||
SliderPage_LineSizeText,
|
||||
SliderPage_PageSizeText,
|
||||
SliderPage_TickFreqText,
|
||||
SliderPage_ThumbLenText,
|
||||
SliderPage_RadioSides,
|
||||
SliderPage_BothSides,
|
||||
SliderPage_SelectRange,
|
||||
SliderPage_Slider
|
||||
};
|
||||
|
||||
@@ -107,6 +111,7 @@ protected:
|
||||
void OnButtonClear(wxCommandEvent& event);
|
||||
void OnButtonSetValue(wxCommandEvent& event);
|
||||
void OnButtonSetMinAndMax(wxCommandEvent& event);
|
||||
void OnButtonSetRange(wxCommandEvent& event);
|
||||
void OnButtonSetLineSize(wxCommandEvent& event);
|
||||
void OnButtonSetPageSize(wxCommandEvent& event);
|
||||
void OnButtonSetTickFreq(wxCommandEvent& event);
|
||||
@@ -118,12 +123,14 @@ protected:
|
||||
|
||||
void OnUpdateUIValueButton(wxUpdateUIEvent& event);
|
||||
void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event);
|
||||
void OnUpdateUIRangeButton(wxUpdateUIEvent& event);
|
||||
void OnUpdateUILineSize(wxUpdateUIEvent& event);
|
||||
void OnUpdateUIPageSize(wxUpdateUIEvent& event);
|
||||
void OnUpdateUITickFreq(wxUpdateUIEvent& event);
|
||||
void OnUpdateUIThumbLen(wxUpdateUIEvent& event);
|
||||
void OnUpdateUIRadioSides(wxUpdateUIEvent& event);
|
||||
void OnUpdateUIBothSides(wxUpdateUIEvent& event);
|
||||
void OnUpdateUISelectRange(wxUpdateUIEvent& event);
|
||||
|
||||
void OnUpdateUIResetButton(wxUpdateUIEvent& event);
|
||||
|
||||
@@ -147,6 +154,9 @@ protected:
|
||||
// set the thumb len from the text field value
|
||||
void DoSetThumbLen();
|
||||
|
||||
// set the selection range from the text field values
|
||||
void DoSetSelectionRange();
|
||||
|
||||
// is this slider value in range?
|
||||
bool IsValidValue(int val) const
|
||||
{ return (val >= m_min) && (val <= m_max); }
|
||||
@@ -154,6 +164,9 @@ protected:
|
||||
// the slider range
|
||||
int m_min, m_max;
|
||||
|
||||
// the slider selection range
|
||||
int m_rangeMin, m_rangeMax;
|
||||
|
||||
// the controls
|
||||
// ------------
|
||||
|
||||
@@ -162,7 +175,8 @@ protected:
|
||||
*m_chkValueLabel,
|
||||
*m_chkInverse,
|
||||
*m_chkTicks,
|
||||
*m_chkBothSides;
|
||||
*m_chkBothSides,
|
||||
*m_chkSelectRange;
|
||||
|
||||
wxRadioBox *m_radioSides;
|
||||
|
||||
@@ -174,6 +188,8 @@ protected:
|
||||
wxTextCtrl *m_textValue,
|
||||
*m_textMin,
|
||||
*m_textMax,
|
||||
*m_textRangeMin,
|
||||
*m_textRangeMax,
|
||||
*m_textLineSize,
|
||||
*m_textPageSize,
|
||||
*m_textTickFreq,
|
||||
@@ -192,6 +208,7 @@ wxBEGIN_EVENT_TABLE(SliderWidgetsPage, WidgetsPage)
|
||||
EVT_BUTTON(SliderPage_Reset, SliderWidgetsPage::OnButtonReset)
|
||||
EVT_BUTTON(SliderPage_SetValue, SliderWidgetsPage::OnButtonSetValue)
|
||||
EVT_BUTTON(SliderPage_SetMinAndMax, SliderWidgetsPage::OnButtonSetMinAndMax)
|
||||
EVT_BUTTON(SliderPage_SetRange, SliderWidgetsPage::OnButtonSetRange)
|
||||
EVT_BUTTON(SliderPage_SetLineSize, SliderWidgetsPage::OnButtonSetLineSize)
|
||||
EVT_BUTTON(SliderPage_SetPageSize, SliderWidgetsPage::OnButtonSetPageSize)
|
||||
EVT_BUTTON(SliderPage_SetTickFreq, SliderWidgetsPage::OnButtonSetTickFreq)
|
||||
@@ -199,12 +216,14 @@ wxBEGIN_EVENT_TABLE(SliderWidgetsPage, WidgetsPage)
|
||||
|
||||
EVT_UPDATE_UI(SliderPage_SetValue, SliderWidgetsPage::OnUpdateUIValueButton)
|
||||
EVT_UPDATE_UI(SliderPage_SetMinAndMax, SliderWidgetsPage::OnUpdateUIMinMaxButton)
|
||||
EVT_UPDATE_UI(SliderPage_SetRange, SliderWidgetsPage::OnUpdateUIRangeButton)
|
||||
EVT_UPDATE_UI(SliderPage_SetLineSize, SliderWidgetsPage::OnUpdateUILineSize)
|
||||
EVT_UPDATE_UI(SliderPage_SetPageSize, SliderWidgetsPage::OnUpdateUIPageSize)
|
||||
EVT_UPDATE_UI(SliderPage_SetTickFreq, SliderWidgetsPage::OnUpdateUITickFreq)
|
||||
EVT_UPDATE_UI(SliderPage_SetThumbLen, SliderWidgetsPage::OnUpdateUIThumbLen)
|
||||
EVT_UPDATE_UI(SliderPage_RadioSides, SliderWidgetsPage::OnUpdateUIRadioSides)
|
||||
EVT_UPDATE_UI(SliderPage_BothSides, SliderWidgetsPage::OnUpdateUIBothSides)
|
||||
EVT_UPDATE_UI(SliderPage_SelectRange, SliderWidgetsPage::OnUpdateUISelectRange)
|
||||
|
||||
EVT_UPDATE_UI(SliderPage_Reset, SliderWidgetsPage::OnUpdateUIResetButton)
|
||||
|
||||
@@ -235,12 +254,15 @@ SliderWidgetsPage::SliderWidgetsPage(WidgetsBookCtrl *book,
|
||||
// init everything
|
||||
m_min = 0;
|
||||
m_max = 100;
|
||||
m_rangeMin = 20;
|
||||
m_rangeMax = 80;
|
||||
|
||||
m_chkInverse =
|
||||
m_chkTicks =
|
||||
m_chkMinMaxLabels =
|
||||
m_chkValueLabel =
|
||||
m_chkBothSides = (wxCheckBox *)NULL;
|
||||
m_chkBothSides =
|
||||
m_chkSelectRange =(wxCheckBox *)NULL;
|
||||
|
||||
m_radioSides = (wxRadioBox *)NULL;
|
||||
|
||||
@@ -275,8 +297,11 @@ void SliderWidgetsPage::CreateContent()
|
||||
sizerLeft->Add(m_radioSides, wxSizerFlags().Expand().Border());
|
||||
m_chkBothSides = CreateCheckBoxAndAddToSizer
|
||||
(sizerLeft, "&Both sides", SliderPage_BothSides);
|
||||
m_chkSelectRange = CreateCheckBoxAndAddToSizer
|
||||
(sizerLeft, "&Selection range", SliderPage_SelectRange);
|
||||
#if wxUSE_TOOLTIPS
|
||||
m_chkBothSides->SetToolTip("\"Both sides\" is only supported \nin Universal");
|
||||
m_chkSelectRange->SetToolTip("\"Select range\" is only supported \nin wxMSW");
|
||||
#endif // wxUSE_TOOLTIPS
|
||||
|
||||
sizerLeft->AddSpacer(5);
|
||||
@@ -315,6 +340,19 @@ void SliderWidgetsPage::CreateContent()
|
||||
|
||||
sizerMiddle->Add(sizerRow, wxSizerFlags().Expand().Border());
|
||||
|
||||
sizerRow = CreateSizerWithTextAndButton(SliderPage_SetRange,
|
||||
"&Selection",
|
||||
SliderPage_RangeMinText,
|
||||
&m_textRangeMin);
|
||||
|
||||
m_textRangeMax = new wxTextCtrl(this, SliderPage_RangeMaxText, wxEmptyString);
|
||||
sizerRow->Add(m_textRangeMax, wxSizerFlags(1).CentreVertical().Border(wxLEFT));
|
||||
|
||||
m_textRangeMin->SetValue( wxString::Format("%d", m_rangeMin) );
|
||||
m_textRangeMax->SetValue( wxString::Format("%d", m_rangeMax) );
|
||||
|
||||
sizerMiddle->Add(sizerRow, wxSizerFlags().Expand().Border());
|
||||
|
||||
sizerRow = CreateSizerWithTextAndButton(SliderPage_SetLineSize,
|
||||
"Li&ne size",
|
||||
SliderPage_LineSizeText,
|
||||
@@ -375,6 +413,7 @@ void SliderWidgetsPage::Reset()
|
||||
m_chkValueLabel->SetValue(true);
|
||||
m_chkMinMaxLabels->SetValue(true);
|
||||
m_chkBothSides->SetValue(false);
|
||||
m_chkSelectRange->SetValue(false);
|
||||
|
||||
m_radioSides->SetSelection(SliderTicks_None);
|
||||
}
|
||||
@@ -437,6 +476,11 @@ void SliderWidgetsPage::CreateSlider()
|
||||
flags |= wxSL_BOTH;
|
||||
}
|
||||
|
||||
if ( m_chkSelectRange->GetValue() )
|
||||
{
|
||||
flags |= wxSL_SELRANGE;
|
||||
}
|
||||
|
||||
int val = m_min;
|
||||
if ( m_slider )
|
||||
{
|
||||
@@ -483,6 +527,11 @@ void SliderWidgetsPage::CreateSlider()
|
||||
DoSetTickFreq();
|
||||
}
|
||||
|
||||
if ( m_chkSelectRange->GetValue() )
|
||||
{
|
||||
DoSetSelectionRange();
|
||||
}
|
||||
|
||||
Layout();
|
||||
}
|
||||
|
||||
@@ -556,6 +605,31 @@ void SliderWidgetsPage::DoSetThumbLen()
|
||||
Layout();
|
||||
}
|
||||
|
||||
void SliderWidgetsPage::DoSetSelectionRange()
|
||||
{
|
||||
long minNew,
|
||||
maxNew = 0; // init to suppress compiler warning
|
||||
if ( !m_textRangeMin->GetValue().ToLong(&minNew) ||
|
||||
!m_textRangeMax->GetValue().ToLong(&maxNew) ||
|
||||
minNew >= maxNew || minNew < m_min || maxNew > m_max )
|
||||
{
|
||||
wxLogWarning("Invalid selection range for the slider.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
m_rangeMin = minNew;
|
||||
m_rangeMax = maxNew;
|
||||
|
||||
m_slider->SetSelection(m_rangeMin, m_rangeMax);
|
||||
|
||||
if ( m_slider->GetSelStart() != m_rangeMin ||
|
||||
m_slider->GetSelEnd() != m_rangeMax )
|
||||
{
|
||||
wxLogWarning("Invalid selection range in slider.");
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// event handlers
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -612,6 +686,11 @@ void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent& WXUNUSED(event))
|
||||
}
|
||||
}
|
||||
|
||||
void SliderWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
DoSetSelectionRange();
|
||||
}
|
||||
|
||||
void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
long val;
|
||||
@@ -667,6 +746,17 @@ void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event)
|
||||
mn < mx);
|
||||
}
|
||||
|
||||
void SliderWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent& event)
|
||||
{
|
||||
long mn, mx;
|
||||
event.Enable( m_chkSelectRange->GetValue() &&
|
||||
m_textRangeMin->GetValue().ToLong(&mn) &&
|
||||
m_textRangeMax->GetValue().ToLong(&mx) &&
|
||||
mn < mx &&
|
||||
mn >= m_min && mx <= m_max );
|
||||
|
||||
}
|
||||
|
||||
void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
|
||||
{
|
||||
event.Enable( m_chkInverse->GetValue() ||
|
||||
@@ -674,6 +764,7 @@ void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
|
||||
!m_chkValueLabel->GetValue() ||
|
||||
!m_chkMinMaxLabels->GetValue() ||
|
||||
m_chkBothSides->GetValue() ||
|
||||
m_chkSelectRange->GetValue() ||
|
||||
m_radioSides->GetSelection() != SliderTicks_None );
|
||||
}
|
||||
|
||||
@@ -701,6 +792,15 @@ void SliderWidgetsPage::OnUpdateUIBothSides(wxUpdateUIEvent& event)
|
||||
#endif // defined(__WXMSW__) || defined(__WXUNIVERSAL__)
|
||||
}
|
||||
|
||||
void SliderWidgetsPage::OnUpdateUISelectRange(wxUpdateUIEvent& event)
|
||||
{
|
||||
#if defined(__WXMSW__)
|
||||
event.Enable( true );
|
||||
#else
|
||||
event.Enable( false );
|
||||
#endif // defined(__WXMSW__)
|
||||
}
|
||||
|
||||
void SliderWidgetsPage::OnSlider(wxScrollEvent& event)
|
||||
{
|
||||
wxASSERT_MSG( event.GetInt() == m_slider->GetValue(),
|
||||
|
Reference in New Issue
Block a user