* use std::nothrow

* implement min/max labels
* fix drawing for horizontal sliders
* removed commented code value drawing code
* changed 'tooltip' to show left/right, top/bottom of the knob



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28858 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Karsten Heimrich
2009-01-08 16:16:58 +00:00
parent 45735be6c9
commit 5fa4ff6a86
+177 -143
View File
@@ -12,6 +12,9 @@
#include <Window.h> #include <Window.h>
#include <new>
const static unsigned char const static unsigned char
kVerticalKnobData[] = { kVerticalKnobData[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
@@ -64,7 +67,7 @@ sPropertyInfo[] = {
}; };
const static float kPadding = 10.0; const static float kPadding = 3.0;
BChannelSlider::BChannelSlider(BRect area, const char *name, const char *label, BChannelSlider::BChannelSlider(BRect area, const char *name, const char *label,
@@ -110,9 +113,9 @@ BArchivable *
BChannelSlider::Instantiate(BMessage *archive) BChannelSlider::Instantiate(BMessage *archive)
{ {
if (validate_instantiation(archive, "BChannelSlider")) if (validate_instantiation(archive, "BChannelSlider"))
return new BChannelSlider(archive); return new (std::nothrow) BChannelSlider(archive);
else
return NULL; return NULL;
} }
@@ -242,11 +245,37 @@ BChannelSlider::Draw(BRect updateRect)
_UpdateFontDimens(); _UpdateFontDimens();
_DrawThumbs(); _DrawThumbs();
BRect bounds(Bounds());
if (Label()) { if (Label()) {
BRect bounds(Bounds());
float labelWidth = StringWidth(Label()); float labelWidth = StringWidth(Label());
DrawString(Label(), BPoint((bounds.Width() - labelWidth) / 2.0,
fBaseLine));
}
DrawString(Label(), BPoint((bounds.Width() - labelWidth) / 2, fBaseLine)); if (MinLimitLabel()) {
if (_Vertical()) {
if (MinLimitLabel()) {
float x = (bounds.Width() - StringWidth(MinLimitLabel())) / 2.0;
DrawString(MinLimitLabel(), BPoint(x, bounds.bottom - kPadding));
}
} else {
if (MinLimitLabel())
DrawString(MinLimitLabel(), BPoint(kPadding, bounds.bottom - kPadding));
}
}
if (MaxLimitLabel()) {
if (_Vertical()) {
if (MaxLimitLabel()) {
float x = (bounds.Width() - StringWidth(MaxLimitLabel())) / 2.0;
DrawString(MaxLimitLabel(), BPoint(x, 2 * fLineFeed));
}
} else {
if (MaxLimitLabel()) {
DrawString(MaxLimitLabel(), BPoint(bounds.right - kPadding -
StringWidth(MaxLimitLabel()), bounds.bottom - kPadding));
}
}
} }
} }
@@ -301,13 +330,16 @@ BChannelSlider::MouseDown(BPoint where)
} }
if (fInitialValues == NULL) if (fInitialValues == NULL)
fInitialValues = new int32[numChannels]; fInitialValues = new (std::nothrow) int32[numChannels];
if (fAllChannels) { if (fInitialValues) {
for (int32 i = 0; i < numChannels; i++) if (fAllChannels) {
fInitialValues[i] = ValueFor(i); for (int32 i = 0; i < numChannels; i++)
} else fInitialValues[i] = ValueFor(i);
fInitialValues[fCurrentChannel] = ValueFor(fCurrentChannel); } else {
fInitialValues[fCurrentChannel] = ValueFor(fCurrentChannel);
}
}
if (Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) { if (Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) {
if (!IsTracking()) { if (!IsTracking()) {
@@ -342,8 +374,9 @@ BChannelSlider::MouseUp(BPoint where)
fAllChannels = false; fAllChannels = false;
fCurrentChannel = -1; fCurrentChannel = -1;
fMinpoint = 0; fMinpoint = 0;
} else } else {
BChannelControl::MouseUp(where); BChannelControl::MouseUp(where);
}
} }
@@ -412,19 +445,23 @@ BChannelSlider::SetEnabled(bool on)
void void
BChannelSlider::GetPreferredSize(float *width, float *height) BChannelSlider::GetPreferredSize(float *width, float *height)
{ {
if (width) { _UpdateFontDimens();
float _width = (float)ceil(StringWidth(Label()));
if (_Vertical()) { if (_Vertical()) {
*width = max_c(_width, 2 + 12 * CountChannels()); *width = 11.0 * CountChannels();
} else { *width = max_c(*width, ceilf(StringWidth(Label())));
*width = max_c(_width, 64); *width = max_c(*width, ceilf(StringWidth(MinLimitLabel())));
} *width = max_c(*width, ceilf(StringWidth(MaxLimitLabel())));
} *width += kPadding * 2.0;
if (height) {
if (_Vertical()) *height = (fLineFeed * 3.0) + (kPadding * 2.0) + 147.0;
*height = 195; } else {
else *width = max_c(64.0, ceilf(StringWidth(Label())));
*height = 71; *width = max_c(*width, ceilf(StringWidth(MinLimitLabel())) +
ceilf(StringWidth(MaxLimitLabel())) + 10.0);
*width += kPadding * 2.0;
*height = 11.0 * CountChannels() + (fLineFeed * 2.0) + (kPadding * 2.0);
} }
} }
@@ -512,9 +549,9 @@ BChannelSlider::DrawThumb(BView* into, int32 channel, BPoint where, bool pressed
if (thumb == NULL) if (thumb == NULL)
return; return;
BRect bitmapBounds = thumb->Bounds(); BRect bitmapBounds(thumb->Bounds());
where.x -= bitmapBounds.right / 2; where.x -= bitmapBounds.right / 2.0;
where.y -= bitmapBounds.bottom / 2; where.y -= bitmapBounds.bottom / 2.0;
into->PushState(); into->PushState();
@@ -545,11 +582,11 @@ BChannelSlider::ThumbFor(int32 channel, bool pressed)
// TODO: Finish me // TODO: Finish me
if (fLeftKnob == NULL) { if (fLeftKnob == NULL) {
if (_Vertical()) { if (_Vertical()) {
fLeftKnob = new BBitmap(BRect(0, 0, 11, 14), B_CMAP8); fLeftKnob = new (std::nothrow) BBitmap(BRect(0, 0, 11, 14), B_CMAP8);
fLeftKnob->SetBits(kVerticalKnobData, sizeof(kVerticalKnobData), fLeftKnob->SetBits(kVerticalKnobData, sizeof(kVerticalKnobData), 0,
0, B_CMAP8); B_CMAP8);
} else { } else {
fLeftKnob = new BBitmap(BRect(0, 0, 14, 11), B_CMAP8); fLeftKnob = new (std::nothrow) BBitmap(BRect(0, 0, 14, 11), B_CMAP8);
fLeftKnob->SetBits(kHorizontalKnobData, sizeof(kHorizontalKnobData), fLeftKnob->SetBits(kHorizontalKnobData, sizeof(kHorizontalKnobData),
0, B_CMAP8); 0, B_CMAP8);
} }
@@ -564,16 +601,17 @@ BChannelSlider::ThumbFrameFor(int32 channel)
{ {
_UpdateFontDimens(); _UpdateFontDimens();
BRect frame(0, 0, 0, 0); BRect frame(0.0, 0.0, 0.0, 0.0);
const BBitmap *thumb = ThumbFor(channel, false); const BBitmap *thumb = ThumbFor(channel, false);
if (thumb != NULL) { if (thumb != NULL) {
frame = thumb->Bounds(); frame = thumb->Bounds();
if (_Vertical()) if (_Vertical()) {
frame.OffsetBy(channel * frame.Width(), fLineFeed + kPadding); frame.OffsetBy(channel * frame.Width(), (frame.Height() / 2.0) -
else (kPadding * 2.0) - 1.0);
frame.OffsetBy(kPadding, fLineFeed + channel * frame.Height()); } else {
frame.OffsetBy(frame.Width() / 2.0, channel * frame.Height() + 1.0);
}
} }
return frame; return frame;
} }
@@ -581,7 +619,7 @@ BChannelSlider::ThumbFrameFor(int32 channel)
float float
BChannelSlider::ThumbDeltaFor(int32 channel) BChannelSlider::ThumbDeltaFor(int32 channel)
{ {
float delta = 0; float delta = 0.0;
if (channel >= 0 && channel < MaxChannelCount()) { if (channel >= 0 && channel < MaxChannelCount()) {
float range = ThumbRangeFor(channel); float range = ThumbRangeFor(channel);
int32 limitRange = MaxLimitList()[channel] - MinLimitList()[channel]; int32 limitRange = MaxLimitList()[channel] - MinLimitList()[channel];
@@ -603,11 +641,14 @@ BChannelSlider::ThumbRangeFor(int32 channel)
float range = 0; float range = 0;
BRect bounds = Bounds(); BRect bounds = Bounds();
BRect frame = ThumbFrameFor(channel); BRect frame = ThumbFrameFor(channel);
if (_Vertical()) if (_Vertical()) {
range = bounds.Height() - frame.Height() - (kPadding + fLineFeed) * 2; // *height = (fLineFeed * 3.0) + (kPadding * 2.0) + 100.0;
else range = bounds.Height() - frame.Height() - (fLineFeed * 3.0) -
range = bounds.Width() - frame.Width() - kPadding * 2; (kPadding * 2.0);
} else {
// *width = some width + kPadding * 2.0;
range = bounds.Width() - frame.Width() - (kPadding * 2.0);
}
return range; return range;
} }
@@ -642,10 +683,12 @@ BChannelSlider::_FinishChange()
bool *inMask = NULL; bool *inMask = NULL;
int32 numChannels = CountChannels(); int32 numChannels = CountChannels();
if (!fAllChannels) { if (!fAllChannels) {
inMask = new bool[CountChannels()]; inMask = new (std::nothrow) bool[CountChannels()];
for (int i=0; i<numChannels; i++) if (inMask) {
inMask[i] = false; for (int i=0; i<numChannels; i++)
inMask[fCurrentChannel] = true; inMask[i] = false;
inMask[fCurrentChannel] = true;
}
} }
InvokeChannel(NULL, 0, numChannels, inMask); InvokeChannel(NULL, 0, numChannels, inMask);
} }
@@ -673,125 +716,116 @@ BChannelSlider::_DrawThumbs()
// of the first and last thumb frames (top/left and bottom/right) // of the first and last thumb frames (top/left and bottom/right)
BRect first = ThumbFrameFor(0); BRect first = ThumbFrameFor(0);
BRect last = ThumbFrameFor(CountChannels() - 1); BRect last = ThumbFrameFor(CountChannels() - 1);
BRect bitmapFrame(first.LeftTop(), last.RightBottom()); BRect rect(first.LeftTop(), last.RightBottom());
if (_Vertical()) if (_Vertical())
bitmapFrame.top -= ThumbRangeFor(0); rect.top -= ThumbRangeFor(0);
else else
bitmapFrame.right += ThumbRangeFor(0); rect.right += ThumbRangeFor(0);
bitmapFrame.OffsetTo(B_ORIGIN); rect.OffsetTo(B_ORIGIN);
fBacking = new BBitmap(bitmapFrame, B_RGBA32, true, false); fBacking = new (std::nothrow) BBitmap(rect, B_RGB32, true);
if (fBacking->Lock()) { if (fBacking) {
fBackingView = new BView(bitmapFrame, "", 0, B_WILL_DRAW); fBackingView = new (std::nothrow) BView(rect, "", 0, B_WILL_DRAW);
fBacking->AddChild(fBackingView); if (fBackingView) {
fBackingView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); if (fBacking->Lock()) {
fBackingView->SetLowColor(fBackingView->ViewColor()); fBacking->AddChild(fBackingView);
fBacking->Unlock(); fBackingView->SetFontSize(10.0);
fBackingView->SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fBackingView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fBacking->Unlock();
}
} else {
delete fBacking;
fBacking = NULL;
}
} }
} }
BPoint drawHere; BPoint drawHere;
drawHere.x = (Bounds().Width() - fBacking->Bounds().Width()) / 2; BRect bounds(fBacking->Bounds());
drawHere.y = (Bounds().Height() - fBacking->Bounds().Height() + fLineFeed) / 2; drawHere.x = (Bounds().Width() - bounds.Width()) / 2.0;
drawHere.y = (Bounds().Height() - bounds.Height()) - kPadding - fLineFeed;
if (fBacking->Lock()) { if (fBacking && fBackingView) {
// Clear the view's background if (fBacking->Lock()) {
fBackingView->FillRect(fBackingView->Bounds(), B_SOLID_LOW); // Clear the view's background
fBackingView->FillRect(fBackingView->Bounds(), B_SOLID_LOW);
BRect channelArea; BRect channelArea;
int32 channelCount = CountChannels(); // draw the entire control
for (int32 channel = 0; channel < channelCount; channel++) { for (int32 channel = 0; channel < CountChannels(); channel++) {
channelArea = ThumbFrameFor(channel); channelArea = ThumbFrameFor(channel);
// TODO: This is (apparently) needed because ThumbFrameFor() doesn't bool pressed = IsTracking()
// take into account that the view we draw on is attached to an offscreen && (channel == fCurrentChannel || fAllChannels);
// bitmap. Still this doesn't make much sense: DrawChannel(fBackingView, channel, channelArea, pressed);
// could be that I'm simply missing something. }
if (_Vertical())
channelArea.OffsetBy(0, -channelArea.top);
else
channelArea.OffsetBy(0, -channelArea.Height());
bool pressed = fMinpoint != 0 && (channel == fCurrentChannel || fAllChannels); // draw some kind of current value tool tip
DrawChannel(fBackingView, channel, channelArea, pressed); if (fCurrentChannel != -1 && fMinpoint != 0) {
char valueString[32];
snprintf(valueString, 32, "%ld", ValueFor(fCurrentChannel));
float stringWidth = fBackingView->StringWidth(valueString);
float width = max_c(10.0, stringWidth);
BRect valueRect(0.0, 0.0, width, 10.0);
BRect thumbFrame(ThumbFrameFor(fCurrentChannel));
float thumbDelta(ThumbDeltaFor(fCurrentChannel));
if (_Vertical()) {
valueRect.OffsetTo((thumbFrame.Width() - width) / 2.0 +
fCurrentChannel * thumbFrame.Width(),
thumbDelta + thumbFrame.Height() + 2.0);
if (valueRect.bottom > fBackingView->Frame().bottom)
valueRect.OffsetBy(0.0, -(thumbFrame.Height() + 12.0));
} else {
valueRect.OffsetTo((thumbDelta - (width + 2.0)), thumbFrame.top);
if (valueRect.left < fBackingView->Frame().left)
valueRect.OffsetBy(thumbFrame.Width() + width + 2.0, 0.0);
}
rgb_color oldColor = fBackingView->HighColor();
fBackingView->SetHighColor(255, 255, 172);
fBackingView->FillRect(valueRect);
fBackingView->SetHighColor(0, 0, 0);
fBackingView->DrawString(valueString, BPoint(valueRect.left +
(valueRect.Width() - stringWidth) / 2.0, valueRect.bottom -1.0));
fBackingView->StrokeRect(valueRect.InsetByCopy(-0.5, -0.5));
fBackingView->SetHighColor(oldColor);
}
fBackingView->Sync();
fBacking->Unlock();
} }
#if 1
// This part draws the current value over the thumb.
// TODO: make it nicer. Simplify the code.
if (fCurrentChannel != -1 && fMinpoint != 0) {
char valueString[32];
snprintf(valueString, 32, "%ld", ValueFor(fCurrentChannel));
float width = fBackingView->StringWidth(valueString);
BRect valueRect(0, 0, width, 10);
rgb_color oldColor = fBackingView->HighColor();
if (_Vertical())
valueRect.OffsetTo((ThumbFrameFor(fCurrentChannel).Width() - width) / 2 + fCurrentChannel * ThumbFrameFor(fCurrentChannel).Width(),
ThumbDeltaFor(fCurrentChannel));
else
valueRect.OffsetTo(ThumbDeltaFor(fCurrentChannel), ThumbFrameFor(fCurrentChannel).top - 10);
fBackingView->SetHighColor(255, 255, 172);
fBackingView->FillRect(valueRect);
fBackingView->SetHighColor(0, 0, 0);
valueRect.OffsetBy(1 , 9);
fBackingView->DrawString(valueString, valueRect.LeftTop());
fBackingView->SetHighColor(oldColor);
}
#endif
fBackingView->Sync();
fBacking->Unlock();
} }
DrawBitmapAsync(fBacking, drawHere); if (fBacking)
DrawBitmapAsync(fBacking, drawHere);
#if 0
// this part draws the value at the bottom of the sliders.
if (fCurrentChannel != -1 && fMinpoint != 0) {
char valueString[32];
snprintf(valueString, 32, "%ld", ValueFor(fCurrentChannel));
BPoint stringPoint = drawHere;
float stringWidth = StringWidth(valueString);
stringPoint.x += (fBacking->Bounds().Width() - stringWidth) / 2;
stringPoint.y += fBacking->Bounds().Height() + fBaseLine;
BRect stringRect(stringPoint, stringPoint);
stringRect.left -= 10;
stringRect.right += StringWidth("100");
stringRect.top -= fLineFeed;
SetHighColor(ViewColor());
FillRect(stringRect);
SetHighColor(0, 0, 0);
DrawString(valueString, stringPoint);
}
#endif
// fClickDelta is used in MouseMoved() // fClickDelta is used in MouseMoved()
fClickDelta = drawHere; fClickDelta = drawHere;
// TODO: See above
if (_Vertical())
fClickDelta.y -= ThumbFrameFor(0).top;
else
fClickDelta.y -= ThumbFrameFor(0).Height();
} }
void void
BChannelSlider::_DrawGrooveFrame(BView *into, const BRect &area) BChannelSlider::_DrawGrooveFrame(BView *into, const BRect &area)
{ {
rgb_color oldColor = into->HighColor(); if (into) {
rgb_color oldColor = into->HighColor();
into->SetHighColor(255, 255, 255); into->SetHighColor(255, 255, 255);
into->StrokeRect(area); into->StrokeRect(area);
into->SetHighColor(tint_color(into->ViewColor(), B_DARKEN_1_TINT)); into->SetHighColor(tint_color(into->ViewColor(), B_DARKEN_1_TINT));
into->StrokeLine(area.LeftTop(), BPoint(area.right, area.top)); into->StrokeLine(area.LeftTop(), BPoint(area.right, area.top));
into->StrokeLine(area.LeftTop(), BPoint(area.left, area.bottom - 1)); into->StrokeLine(area.LeftTop(), BPoint(area.left, area.bottom - 1));
into->SetHighColor(tint_color(into->ViewColor(), B_DARKEN_2_TINT)); into->SetHighColor(tint_color(into->ViewColor(), B_DARKEN_2_TINT));
into->StrokeLine(BPoint(area.left + 1, area.top + 1), BPoint(area.right - 1, area.top + 1)); into->StrokeLine(BPoint(area.left + 1, area.top + 1),
into->StrokeLine(BPoint(area.left + 1, area.top + 1), BPoint(area.left + 1, area.bottom - 2)); BPoint(area.right - 1, area.top + 1));
into->StrokeLine(BPoint(area.left + 1, area.top + 1),
BPoint(area.left + 1, area.bottom - 2));
into->SetHighColor(oldColor); into->SetHighColor(oldColor);
}
} }