* Added layout friendly constructor

* Use constructor lists for initializing members
* Simplified initial SetBarColor()
* Update the offscreen view with ViewColor() and LowColor(), someone might
  have changed it after AttachedToWindow() was called.
* Cleanup here and there


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26440 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2008-07-16 13:46:05 +00:00
parent c5134b4758
commit cdcfa5945d
2 changed files with 158 additions and 136 deletions
+7 -1
View File
@@ -45,6 +45,11 @@ class BSlider : public BControl {
uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP, uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP,
uint32 flags = B_NAVIGABLE | B_WILL_DRAW | B_FRAME_EVENTS); uint32 flags = B_NAVIGABLE | B_WILL_DRAW | B_FRAME_EVENTS);
BSlider(const char *name, const char *label,
BMessage *message, int32 minValue, int32 maxValue,
orientation posture, thumb_style thumbType = B_BLOCK_THUMB,
uint32 flags = B_NAVIGABLE | B_WILL_DRAW | B_FRAME_EVENTS);
BSlider(BMessage *data); BSlider(BMessage *data);
virtual ~BSlider(); virtual ~BSlider();
@@ -135,7 +140,8 @@ class BSlider : public BControl {
float BarThickness() const; float BarThickness() const;
virtual void SetBarThickness(float thickness); virtual void SetBarThickness(float thickness);
virtual void SetFont(const BFont *font, uint32 properties = B_FONT_ALL); virtual void SetFont(const BFont *font,
uint32 properties = B_FONT_ALL);
virtual void SetLimits(int32 minimum, int32 maximum); virtual void SetLimits(int32 minimum, int32 maximum);
+151 -135
View File
@@ -23,32 +23,30 @@
#include <Slider.h> #include <Slider.h>
BSlider::BSlider(BRect frame, const char *name, const char *label, BMessage *message, BSlider::BSlider(BRect frame, const char* name, const char* label,
int32 minValue, int32 maxValue, thumb_style thumbType, BMessage* message, int32 minValue, int32 maxValue,
uint32 resizingMode, uint32 flags) thumb_style thumbType, uint32 resizingMode, uint32 flags)
: BControl(frame, name, label, message, resizingMode, flags) : BControl(frame, name, label, message, resizingMode, flags),
fModificationMessage(NULL),
fSnoozeAmount(20000),
fMinLimitLabel(NULL),
fMaxLimitLabel(NULL),
fMinValue(minValue),
fMaxValue(maxValue),
fKeyIncrementValue(1),
fHashMarkCount(0),
fHashMarks(B_HASH_MARKS_NONE),
fStyle(thumbType),
fOrientation(B_HORIZONTAL),
fBarThickness(6.0)
{ {
fModificationMessage = NULL; SetBarColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
fSnoozeAmount = 20000; B_DARKEN_4_TINT));
fOrientation = B_HORIZONTAL;
fBarThickness = 6.0f;
fMinLimitLabel = NULL;
fMaxLimitLabel = NULL;
fMinValue = minValue;
fMaxValue = maxValue;
fKeyIncrementValue = 1;
fHashMarkCount = 0;
fHashMarks = B_HASH_MARKS_NONE;
fStyle = thumbType;
if (Style() == B_BLOCK_THUMB) {
SetBarColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_DARKEN_4_TINT));
} else {
SetBarColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_DARKEN_4_TINT));
}
UseFillColor(false, NULL); UseFillColor(false, NULL);
@@ -57,31 +55,63 @@ BSlider::BSlider(BRect frame, const char *name, const char *label, BMessage *mes
} }
BSlider::BSlider(BRect frame, const char *name, const char *label, BMessage *message, BSlider::BSlider(BRect frame, const char *name, const char *label,
int32 minValue, int32 maxValue, orientation posture, BMessage *message, int32 minValue, int32 maxValue,
thumb_style thumbType, uint32 resizingMode, uint32 flags) orientation posture, thumb_style thumbType, uint32 resizingMode,
: BControl(frame, name, label, message, resizingMode, flags) uint32 flags)
: BControl(frame, name, label, message, resizingMode, flags),
fModificationMessage(NULL),
fSnoozeAmount(20000),
fMinLimitLabel(NULL),
fMaxLimitLabel(NULL),
fMinValue(minValue),
fMaxValue(maxValue),
fKeyIncrementValue(1),
fHashMarkCount(0),
fHashMarks(B_HASH_MARKS_NONE),
fStyle(thumbType),
fOrientation(posture),
fBarThickness(6.0)
{ {
fModificationMessage = NULL; SetBarColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
fSnoozeAmount = 20000; B_DARKEN_4_TINT));
fOrientation = posture;
fBarThickness = 6.0f;
fMinLimitLabel = NULL;
fMaxLimitLabel = NULL;
fMinValue = minValue;
fMaxValue = maxValue;
fKeyIncrementValue = 1; UseFillColor(false, NULL);
fHashMarkCount = 0;
fHashMarks = B_HASH_MARKS_NONE;
fStyle = thumbType;
if (Style() == B_BLOCK_THUMB) _InitObject();
SetBarColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), SetValue(0);
B_DARKEN_4_TINT)); }
else
SetBarColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_DARKEN_4_TINT)); BSlider::BSlider(const char *name, const char *label, BMessage *message,
int32 minValue, int32 maxValue, orientation posture,
thumb_style thumbType, uint32 flags)
: BControl(name, label, message, flags),
fModificationMessage(NULL),
fSnoozeAmount(20000),
fMinLimitLabel(NULL),
fMaxLimitLabel(NULL),
fMinValue(minValue),
fMaxValue(maxValue),
fKeyIncrementValue(1),
fHashMarkCount(0),
fHashMarks(B_HASH_MARKS_NONE),
fStyle(thumbType),
fOrientation(posture),
fBarThickness(6.0)
{
SetBarColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_DARKEN_4_TINT));
UseFillColor(false, NULL); UseFillColor(false, NULL);
@@ -96,7 +126,7 @@ BSlider::BSlider(BMessage *archive)
fModificationMessage = NULL; fModificationMessage = NULL;
if (archive->HasMessage("_mod_msg")) { if (archive->HasMessage("_mod_msg")) {
BMessage *message = new BMessage; BMessage* message = new BMessage;
archive->FindMessage("_mod_msg", message); archive->FindMessage("_mod_msg", message);
@@ -107,14 +137,12 @@ BSlider::BSlider(BMessage *archive)
SetSnoozeAmount(20000); SetSnoozeAmount(20000);
rgb_color color; rgb_color color;
if (archive->FindInt32("_fcolor", (int32 *)&color) == B_OK)
if (archive->FindInt32("_fcolor", (int32 *)&color) == B_OK) {
UseFillColor(true, &color); UseFillColor(true, &color);
} else else
UseFillColor(false); UseFillColor(false);
int32 orient; int32 orient;
if (archive->FindInt32("_orient", &orient) == B_OK) if (archive->FindInt32("_orient", &orient) == B_OK)
fOrientation = (orientation)orient; fOrientation = (orientation)orient;
else else
@@ -123,7 +151,8 @@ BSlider::BSlider(BMessage *archive)
fMinLimitLabel = NULL; fMinLimitLabel = NULL;
fMaxLimitLabel = NULL; fMaxLimitLabel = NULL;
const char *minlbl = NULL, *maxlbl = NULL; const char* minlbl = NULL;
const char* maxlbl = NULL;
archive->FindString("_minlbl", &minlbl); archive->FindString("_minlbl", &minlbl);
archive->FindString("_maxlbl", &maxlbl); archive->FindString("_maxlbl", &maxlbl);
@@ -143,32 +172,22 @@ BSlider::BSlider(BMessage *archive)
fHashMarkCount = 11; fHashMarkCount = 11;
int16 hashloc; int16 hashloc;
if (archive->FindInt16("_hashloc", &hashloc) == B_OK) if (archive->FindInt16("_hashloc", &hashloc) == B_OK)
fHashMarks = (hash_mark_location)hashloc; fHashMarks = (hash_mark_location)hashloc;
else else
fHashMarks = B_HASH_MARKS_NONE; fHashMarks = B_HASH_MARKS_NONE;
int16 sstyle; int16 sstyle;
if (archive->FindInt16("_sstyle", &sstyle) == B_OK) if (archive->FindInt16("_sstyle", &sstyle) == B_OK)
fStyle = (thumb_style)sstyle; fStyle = (thumb_style)sstyle;
else else
fStyle = B_BLOCK_THUMB; fStyle = B_BLOCK_THUMB;
if (archive->FindInt32("_bcolor", (int32 *)&color) == B_OK) if (archive->FindInt32("_bcolor", (int32 *)&color) != B_OK)
SetBarColor(color); color = tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), B_DARKEN_4_TINT);
else { SetBarColor(color);
if (Style() == B_BLOCK_THUMB)
SetBarColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_DARKEN_4_TINT));
else
SetBarColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_DARKEN_4_TINT));
}
float bthickness; float bthickness;
if (archive->FindFloat("_bthickness", &bthickness) == B_OK) if (archive->FindFloat("_bthickness", &bthickness) == B_OK)
fBarThickness = bthickness; fBarThickness = bthickness;
else else
@@ -181,8 +200,7 @@ BSlider::BSlider(BMessage *archive)
BSlider::~BSlider() BSlider::~BSlider()
{ {
#if USE_OFF_SCREEN_VIEW #if USE_OFF_SCREEN_VIEW
if (fOffScreenBits) delete fOffScreenBits;
delete fOffScreenBits;
#endif #endif
delete fModificationMessage; delete fModificationMessage;
@@ -558,18 +576,13 @@ BSlider::SetLabel(const char *label)
void void
BSlider::SetLimitLabels(const char *minLabel, const char *maxLabel) BSlider::SetLimitLabels(const char *minLabel, const char *maxLabel)
{ {
if (minLabel) { free(fMinLimitLabel);
free(fMinLimitLabel); fMinLimitLabel = minLabel ? strdup(minLabel) : NULL;
fMinLimitLabel = strdup(minLabel);
}
if (maxLabel) { free(fMaxLimitLabel);
free(fMaxLimitLabel); fMaxLimitLabel = maxLabel ? strdup(maxLabel) : NULL;
fMaxLimitLabel = strdup(maxLabel);
}
// TODO: Auto resizing?!? I would not want this as an app programmer! InvalidateLayout();
ResizeToPreferred();
Invalidate(); Invalidate();
} }
@@ -596,74 +609,75 @@ BSlider::SetValue(int32 value)
if (value > fMaxValue) if (value > fMaxValue)
value = fMaxValue; value = fMaxValue;
if (value != Value()) { if (value == Value())
BPoint loc; return;
float range = (float)(fMaxValue - fMinValue);
if (range == 0)
range = 1;
float pos = (float)(value - fMinValue) / range * BPoint loc;
_MaxPosition() - _MinPosition(); float range = (float)(fMaxValue - fMinValue);
if (range == 0)
range = 1;
float pos = (float)(value - fMinValue) / range *
_MaxPosition() - _MinPosition();
if (fOrientation == B_HORIZONTAL) {
loc.x = ceil(_MinPosition() + pos);
loc.y = 0;
} else {
loc.x = 0;
loc.y = floor(_MaxPosition() - pos);
}
BRect oldThumbFrame = ThumbFrame();
// While it would be enough to do this dependent on fUseFillColor,
// that doesn't work out if DrawBar() has been overridden by a sub class
if (fOrientation == B_HORIZONTAL)
oldThumbFrame.top = BarFrame().top;
else
oldThumbFrame.right = BarFrame().right;
_SetLocation(loc);
BControl::SetValueNoUpdate(value);
BRect invalid = oldThumbFrame | ThumbFrame();
if (Style() == B_TRIANGLE_THUMB) {
// 1) we need to take care of pixels touched because of
// anti-aliasing
// 2) we need to update the region with the focus mark as well
// (a method BSlider::FocusMarkFrame() would be nice as well)
if (fOrientation == B_HORIZONTAL) { if (fOrientation == B_HORIZONTAL) {
loc.x = ceil(_MinPosition() + pos); if (IsFocus())
loc.y = 0; invalid.bottom += 2;
invalid.InsetBy(-1, 0);
} else { } else {
loc.x = 0; if (IsFocus())
loc.y = floor(_MaxPosition() - pos); invalid.left -= 2;
invalid.InsetBy(0, -1);
} }
}
BRect oldThumbFrame = ThumbFrame(); Invalidate(invalid);
// While it would be enough to do this dependent on fUseFillColor, // update text label
// that doesn't work out if DrawBar() has been overridden by a sub class
if (fOrientation == B_HORIZONTAL)
oldThumbFrame.top = BarFrame().top;
else
oldThumbFrame.right = BarFrame().right;
_SetLocation(loc); float oldWidth = 0.0f, width = 0.0f;
if (fUpdateText != NULL)
oldWidth = StringWidth(fUpdateText);
BControl::SetValueNoUpdate(value); fUpdateText = UpdateText();
BRect invalid = oldThumbFrame | ThumbFrame(); if (fUpdateText != NULL)
width = StringWidth(fUpdateText);
if (Style() == B_TRIANGLE_THUMB) { width = ceilf(max_c(width, oldWidth)) + 2.0f;
// 1) we need to take care of pixels touched because of if (width != 0) {
// anti-aliasing font_height fontHeight;
// 2) we need to update the region with the focus mark as well GetFontHeight(&fontHeight);
// (a method BSlider::FocusMarkFrame() would be nice as well)
if (fOrientation == B_HORIZONTAL) {
if (IsFocus())
invalid.bottom += 2;
invalid.InsetBy(-1, 0);
} else {
if (IsFocus())
invalid.left -= 2;
invalid.InsetBy(0, -1);
}
}
Invalidate(invalid); BRect rect(-width, 0, 0, ceilf(fontHeight.ascent + fontHeight.descent));
rect.OffsetBy(Bounds().Width(), 0);
// update text label Invalidate(rect);
float oldWidth = 0.0f, width = 0.0f;
if (fUpdateText != NULL)
oldWidth = StringWidth(fUpdateText);
fUpdateText = UpdateText();
if (fUpdateText != NULL)
width = StringWidth(fUpdateText);
width = ceilf(max_c(width, oldWidth)) + 2.0f;
if (width != 0) {
font_height fontHeight;
GetFontHeight(&fontHeight);
BRect rect(-width, 0, 0, ceilf(fontHeight.ascent + fontHeight.descent));
rect.OffsetBy(Bounds().Width(), 0);
Invalidate(rect);
}
} }
} }
@@ -740,6 +754,8 @@ BSlider::Draw(BRect updateRect)
return; return;
if (fOffScreenBits->Lock()) { if (fOffScreenBits->Lock()) {
fOffScreenView->SetViewColor(ViewColor());
fOffScreenView->SetLowColor(ViewColor());
#endif #endif
if (background.Frame().IsValid()) if (background.Frame().IsValid())