Spinner: Generalize actions into methods
Add an Increment(), Decrement(), and SetValueFromText() method. These can be overridden by derived classes.
This commit is contained in:
@@ -54,9 +54,12 @@ public:
|
|||||||
virtual void FrameResized(float width, float height);
|
virtual void FrameResized(float width, float height);
|
||||||
virtual void ValueChanged();
|
virtual void ValueChanged();
|
||||||
|
|
||||||
|
virtual void Decrement();
|
||||||
|
virtual void Increment();
|
||||||
virtual void MakeFocus(bool focus = true);
|
virtual void MakeFocus(bool focus = true);
|
||||||
virtual void ResizeToPreferred();
|
virtual void ResizeToPreferred();
|
||||||
virtual void SetFlags(uint32 flags);
|
virtual void SetFlags(uint32 flags);
|
||||||
|
virtual void SetValueFromText();
|
||||||
virtual void WindowActivated(bool active);
|
virtual void WindowActivated(bool active);
|
||||||
|
|
||||||
alignment Alignment() const { return fAlignment; };
|
alignment Alignment() const { return fAlignment; };
|
||||||
|
|||||||
@@ -276,8 +276,6 @@ public:
|
|||||||
virtual void MakeFocus(bool focus);
|
virtual void MakeFocus(bool focus);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void _SetValueToText();
|
|
||||||
|
|
||||||
BSpinner* fParent;
|
BSpinner* fParent;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -563,22 +561,9 @@ SpinnerArrow::_Track(BPoint where, uint32)
|
|||||||
}
|
}
|
||||||
fIsMouseDown = true;
|
fIsMouseDown = true;
|
||||||
|
|
||||||
double step = fSpinnerDirection == SPINNER_INCREMENT
|
fSpinnerDirection == SPINNER_INCREMENT
|
||||||
? fParent->Step()
|
? fParent->Increment()
|
||||||
: -fParent->Step();
|
: fParent->Decrement();
|
||||||
double newValue = fParent->Value() + step;
|
|
||||||
if (newValue < fParent->MinValue()) {
|
|
||||||
// new value is below lower bound, clip to lower bound
|
|
||||||
fParent->SetValue(fParent->MinValue());
|
|
||||||
} else if (newValue>fParent->MaxValue()) {
|
|
||||||
// new value is above upper bound, clip to upper bound
|
|
||||||
fParent->SetValue(fParent->MaxValue());
|
|
||||||
} else {
|
|
||||||
// new value is in range
|
|
||||||
fParent->SetValue(newValue);
|
|
||||||
}
|
|
||||||
fParent->Invoke();
|
|
||||||
fParent->Invalidate();
|
|
||||||
|
|
||||||
snooze(fRepeatDelay);
|
snooze(fRepeatDelay);
|
||||||
fRepeatDelay = 10000;
|
fRepeatDelay = 10000;
|
||||||
@@ -642,7 +627,7 @@ SpinnerTextView::KeyDown(const char* bytes, int32 numBytes)
|
|||||||
switch (bytes[0]) {
|
switch (bytes[0]) {
|
||||||
case B_ENTER:
|
case B_ENTER:
|
||||||
case B_SPACE:
|
case B_SPACE:
|
||||||
_SetValueToText();
|
fParent->SetValueFromText();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case B_TAB:
|
case B_TAB:
|
||||||
@@ -651,18 +636,13 @@ SpinnerTextView::KeyDown(const char* bytes, int32 numBytes)
|
|||||||
|
|
||||||
case B_UP_ARROW:
|
case B_UP_ARROW:
|
||||||
case B_PAGE_UP:
|
case B_PAGE_UP:
|
||||||
|
fParent->Increment();
|
||||||
|
break;
|
||||||
|
|
||||||
case B_DOWN_ARROW:
|
case B_DOWN_ARROW:
|
||||||
case B_PAGE_DOWN:
|
case B_PAGE_DOWN:
|
||||||
{
|
fParent->Decrement();
|
||||||
double step = fParent->Step();
|
|
||||||
if (*bytes == B_DOWN_ARROW || *bytes == B_PAGE_DOWN)
|
|
||||||
step *= -1;
|
|
||||||
|
|
||||||
fParent->SetValue(fParent->Value() + step);
|
|
||||||
fParent->Invoke();
|
|
||||||
fParent->Invalidate();
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
BTextView::KeyDown(bytes, numBytes);
|
BTextView::KeyDown(bytes, numBytes);
|
||||||
@@ -675,28 +655,15 @@ SpinnerTextView::MakeFocus(bool focus)
|
|||||||
{
|
{
|
||||||
BTextView::MakeFocus(focus);
|
BTextView::MakeFocus(focus);
|
||||||
|
|
||||||
if (focus)
|
|
||||||
SelectAll();
|
|
||||||
else
|
|
||||||
_SetValueToText();
|
|
||||||
|
|
||||||
if (fParent != NULL)
|
|
||||||
fParent->_DrawTextView(fParent->Bounds());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// #pragma mark - SpinnerTextView private methods
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
SpinnerTextView::_SetValueToText()
|
|
||||||
{
|
|
||||||
if (fParent == NULL)
|
if (fParent == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fParent->SetValue(roundTo(atof(Text()), fParent->Precision()));
|
if (focus)
|
||||||
fParent->Invoke();
|
SelectAll();
|
||||||
fParent->Invalidate();
|
else
|
||||||
|
fParent->SetValueFromText();
|
||||||
|
|
||||||
|
fParent->_DrawTextView(fParent->Bounds());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1181,6 +1148,21 @@ BSpinner::ValueChanged()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BSpinner::Decrement()
|
||||||
|
{
|
||||||
|
SetValue(Value() - Step());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BSpinner::Increment()
|
||||||
|
{
|
||||||
|
SetValue(Value() + Step());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
BSpinner::MakeFocus(bool focus)
|
BSpinner::MakeFocus(bool focus)
|
||||||
{
|
{
|
||||||
fTextView->MakeFocus(focus);
|
fTextView->MakeFocus(focus);
|
||||||
@@ -1358,9 +1340,20 @@ BSpinner::SetValue(double value)
|
|||||||
|
|
||||||
fValue = value;
|
fValue = value;
|
||||||
ValueChanged();
|
ValueChanged();
|
||||||
|
|
||||||
|
Invoke();
|
||||||
|
Invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BSpinner::SetValueFromText()
|
||||||
|
{
|
||||||
|
SetValue(roundTo(atof(TextView()->Text()), Precision()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
BSpinner::IsDecrementEnabled() const
|
BSpinner::IsDecrementEnabled() const
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user