#include "SliderTest.h"
#include <stdio.h>
#include <Message.h>
#include <Slider.h>
#include "CheckBox.h"
#include "GroupView.h"
#include "RadioButton.h"
#include "TestView.h"
enum {
MSG_ORIENTATION_CHANGED = 'orch',
MSG_THUMB_STYLE_CHANGED = 'tstc',
MSG_HASH_MARKS_CHANGED = 'hmch',
MSG_BAR_THICKNESS_CHANGED = 'btch',
MSG_LABEL_CHANGED = 'lbch',
MSG_LIMIT_LABELS_CHANGED = 'lmch',
MSG_UPDATE_TEXT_CHANGED = 'utch'
};
class SliderTest::TestSlider : public BSlider {
public:
TestSlider()
: BSlider("test slider", "Label", NULL, 1, 100, B_HORIZONTAL),
fExportUpdateText(false)
{
}
virtual char* UpdateText() const
{
if (!fExportUpdateText)
return NULL;
sprintf(fUpdateText, "%ld", Value());
return fUpdateText;
}
mutable char fUpdateText[32];
bool fExportUpdateText;
};
class SliderTest::OrientationRadioButton : public LabeledRadioButton {
public:
OrientationRadioButton(const char* label, enum orientation orientation)
: LabeledRadioButton(label),
fOrientation(orientation)
{
}
enum orientation fOrientation;
};
class SliderTest::ThumbStyleRadioButton : public LabeledRadioButton {
public:
ThumbStyleRadioButton(const char* label, enum thumb_style style)
: LabeledRadioButton(label),
fStyle(style)
{
}
thumb_style fStyle;
};
class SliderTest::HashMarkLocationRadioButton : public LabeledRadioButton {
public:
HashMarkLocationRadioButton(const char* label, enum hash_mark_location
location)
: LabeledRadioButton(label),
fLocation(location)
{
}
hash_mark_location fLocation;
};
class SliderTest::ThicknessRadioButton : public LabeledRadioButton {
public:
ThicknessRadioButton(const char* label, float thickness)
: LabeledRadioButton(label),
fThickness(thickness)
{
}
float fThickness;
};
class SliderTest::LabelRadioButton : public LabeledRadioButton {
public:
LabelRadioButton(const char* label, const char* sliderLabel)
: LabeledRadioButton(label),
fLabel(sliderLabel)
{
}
const char* fLabel;
};
class SliderTest::LimitLabelsRadioButton : public LabeledRadioButton {
public:
LimitLabelsRadioButton(const char* label, const char* minLabel,
const char* maxLabel)
: LabeledRadioButton(label),
fMinLabel(minLabel),
fMaxLabel(maxLabel)
{
}
const char* fMinLabel;
const char* fMaxLabel;
};
SliderTest::SliderTest()
: Test("Slider", NULL),
fSlider(new TestSlider()),
fOrientationRadioGroup(NULL),
fThumbStyleRadioGroup(NULL),
fHashMarkLocationRadioGroup(NULL),
fBarThicknessRadioGroup(NULL),
fLabelRadioGroup(NULL),
fLimitLabelsRadioGroup(NULL),
fUpdateTextCheckBox(NULL)
{
SetView(fSlider);
}
SliderTest::~SliderTest()
{
delete fOrientationRadioGroup;
delete fThumbStyleRadioGroup;
delete fHashMarkLocationRadioGroup;
delete fBarThicknessRadioGroup;
delete fLabelRadioGroup;
delete fLimitLabelsRadioGroup;
}
Test*
SliderTest::CreateTest()
{
return new SliderTest;
}
void
SliderTest::ActivateTest(View* controls)
{
rgb_color background = ui_color(B_PANEL_BACKGROUND_COLOR);
fSlider->SetViewColor(background);
fSlider->SetLowColor(background);
fSlider->SetHashMarkCount(10);
GroupView* group = new GroupView(B_VERTICAL);
group->SetFrame(controls->Bounds());
group->SetSpacing(0, 4);
controls->AddChild(group);
GroupView* hGroup = new GroupView(B_HORIZONTAL);
group->AddChild(hGroup);
GroupView* vGroup = new GroupView(B_VERTICAL);
vGroup->SetSpacing(0, 4);
hGroup->AddChild(vGroup);
fOrientationRadioGroup = new RadioButtonGroup(
new BMessage(MSG_ORIENTATION_CHANGED), this);
LabeledRadioButton* button = new OrientationRadioButton("Horizontal",
B_HORIZONTAL);
vGroup->AddChild(button);
fOrientationRadioGroup->AddButton(button->GetRadioButton());
button = new OrientationRadioButton("Vertical", B_VERTICAL);
vGroup->AddChild(button);
fOrientationRadioGroup->AddButton(button->GetRadioButton());
fOrientationRadioGroup->SelectButton((int32)0);
vGroup = new GroupView(B_VERTICAL);
vGroup->SetSpacing(0, 4);
hGroup->AddChild(vGroup);
fThumbStyleRadioGroup = new RadioButtonGroup(
new BMessage(MSG_THUMB_STYLE_CHANGED), this);
button = new ThumbStyleRadioButton("Block thumb", B_BLOCK_THUMB);
vGroup->AddChild(button);
fThumbStyleRadioGroup->AddButton(button->GetRadioButton());
button = new ThumbStyleRadioButton("Triangle thumb", B_TRIANGLE_THUMB);
vGroup->AddChild(button);
fThumbStyleRadioGroup->AddButton(button->GetRadioButton());
fThumbStyleRadioGroup->SelectButton((int32)0);
group->AddChild(new VStrut(10));
fHashMarkLocationRadioGroup = new RadioButtonGroup(
new BMessage(MSG_HASH_MARKS_CHANGED), this);
button = new HashMarkLocationRadioButton("No hash marks",
B_HASH_MARKS_NONE);
group->AddChild(button);
fHashMarkLocationRadioGroup->AddButton(button->GetRadioButton());
button = new HashMarkLocationRadioButton("Left/top hash marks",
B_HASH_MARKS_TOP);
group->AddChild(button);
fHashMarkLocationRadioGroup->AddButton(button->GetRadioButton());
button = new HashMarkLocationRadioButton("Right/bottom hash marks",
B_HASH_MARKS_BOTTOM);
group->AddChild(button);
fHashMarkLocationRadioGroup->AddButton(button->GetRadioButton());
button = new HashMarkLocationRadioButton("Both sides hash marks",
B_HASH_MARKS_BOTH);
group->AddChild(button);
fHashMarkLocationRadioGroup->AddButton(button->GetRadioButton());
fHashMarkLocationRadioGroup->SelectButton((int32)0);
group->AddChild(new VStrut(10));
fBarThicknessRadioGroup = new RadioButtonGroup(
new BMessage(MSG_BAR_THICKNESS_CHANGED), this);
button = new ThicknessRadioButton("Thin bar", 1.0);
group->AddChild(button);
fBarThicknessRadioGroup->AddButton(button->GetRadioButton());
button = new ThicknessRadioButton("Normal bar", fSlider->BarThickness());
group->AddChild(button);
fBarThicknessRadioGroup->AddButton(button->GetRadioButton());
button = new ThicknessRadioButton("Thick bar", 25.0);
group->AddChild(button);
fBarThicknessRadioGroup->AddButton(button->GetRadioButton());
fBarThicknessRadioGroup->SelectButton((int32)1);
group->AddChild(new VStrut(10));
fLabelRadioGroup = new RadioButtonGroup(new BMessage(MSG_LABEL_CHANGED),
this);
button = new LabelRadioButton("No label", NULL);
group->AddChild(button);
fLabelRadioGroup->AddButton(button->GetRadioButton());
button = new LabelRadioButton("Label", "Label");
group->AddChild(button);
fLabelRadioGroup->AddButton(button->GetRadioButton());
button = new LabelRadioButton("Long label",
"Quite Long Label for a BSlider");
group->AddChild(button);
fLabelRadioGroup->AddButton(button->GetRadioButton());
fLabelRadioGroup->SelectButton((int32)1);
group->AddChild(new VStrut(10));
fLimitLabelsRadioGroup = new RadioButtonGroup(
new BMessage(MSG_LIMIT_LABELS_CHANGED), this);
button = new LimitLabelsRadioButton("No limit labels", NULL, NULL);
group->AddChild(button);
fLimitLabelsRadioGroup->AddButton(button->GetRadioButton());
button = new LimitLabelsRadioButton("Normal limit labels", "Min", "Max");
group->AddChild(button);
fLimitLabelsRadioGroup->AddButton(button->GetRadioButton());
button = new LimitLabelsRadioButton("Long limit labels",
"Very long min label", "Very long max label");
group->AddChild(button);
fLimitLabelsRadioGroup->AddButton(button->GetRadioButton());
fLimitLabelsRadioGroup->SelectButton((int32)0);
group->AddChild(new VStrut(10));
fUpdateTextCheckBox = new LabeledCheckBox("Update text",
new BMessage(MSG_UPDATE_TEXT_CHANGED), this);
group->AddChild(fUpdateTextCheckBox);
group->AddChild(new Glue());
}
void
SliderTest::DectivateTest()
{
}
void
SliderTest::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_ORIENTATION_CHANGED:
_UpdateOrientation();
break;
case MSG_THUMB_STYLE_CHANGED:
_UpdateThumbStyle();
break;
case MSG_HASH_MARKS_CHANGED:
_UpdateHashMarkLocation();
break;
case MSG_BAR_THICKNESS_CHANGED:
_UpdateBarThickness();
break;
case MSG_LABEL_CHANGED:
_UpdateLabel();
break;
case MSG_LIMIT_LABELS_CHANGED:
_UpdateLimitLabels();
break;
case MSG_UPDATE_TEXT_CHANGED:
_UpdateUpdateText();
break;
default:
Test::MessageReceived(message);
break;
}
}
void
SliderTest::_UpdateOrientation()
{
if (fOrientationRadioGroup) {
AbstractButton* selectedButton
= fOrientationRadioGroup->SelectedButton();
View* parent = (selectedButton ? selectedButton->Parent() : NULL);
OrientationRadioButton* button = dynamic_cast<OrientationRadioButton*>(
parent);
if (button)
fSlider->SetOrientation(button->fOrientation);
}
}
void
SliderTest::_UpdateThumbStyle()
{
if (fThumbStyleRadioGroup) {
AbstractButton* selectedButton
= fThumbStyleRadioGroup->SelectedButton();
View* parent = (selectedButton ? selectedButton->Parent() : NULL);
ThumbStyleRadioButton* button = dynamic_cast<ThumbStyleRadioButton*>(
parent);
if (button)
fSlider->SetStyle(button->fStyle);
}
}
void
SliderTest::_UpdateHashMarkLocation()
{
if (fHashMarkLocationRadioGroup) {
AbstractButton* selectedButton
= fHashMarkLocationRadioGroup->SelectedButton();
View* parent = (selectedButton ? selectedButton->Parent() : NULL);
HashMarkLocationRadioButton* button
= dynamic_cast<HashMarkLocationRadioButton*>(parent);
if (button)
fSlider->SetHashMarks(button->fLocation);
}
}
void
SliderTest::_UpdateBarThickness()
{
if (fBarThicknessRadioGroup) {
AbstractButton* selectedButton
= fBarThicknessRadioGroup->SelectedButton();
View* parent = (selectedButton ? selectedButton->Parent() : NULL);
ThicknessRadioButton* button
= dynamic_cast<ThicknessRadioButton*>(parent);
if (button)
fSlider->SetBarThickness(button->fThickness);
}
}
void
SliderTest::_UpdateLabel()
{
if (fLabelRadioGroup) {
AbstractButton* selectedButton = fLabelRadioGroup->SelectedButton();
View* parent = (selectedButton ? selectedButton->Parent() : NULL);
LabelRadioButton* button = dynamic_cast<LabelRadioButton*>(parent);
if (button)
fSlider->SetLabel(button->fLabel);
}
}
void
SliderTest::_UpdateLimitLabels()
{
if (fLimitLabelsRadioGroup) {
AbstractButton* selectedButton
= fLimitLabelsRadioGroup->SelectedButton();
View* parent = (selectedButton ? selectedButton->Parent() : NULL);
LimitLabelsRadioButton* button = dynamic_cast<LimitLabelsRadioButton*>(
parent);
if (button)
fSlider->SetLimitLabels(button->fMinLabel, button->fMaxLabel);
}
}
void
SliderTest::_UpdateUpdateText()
{
if (!fUpdateTextCheckBox
|| fUpdateTextCheckBox->IsSelected() == fSlider->fExportUpdateText)
return;
fSlider->fExportUpdateText = fUpdateTextCheckBox->IsSelected();
fSlider->UpdateTextChanged();
}