* Copyright 2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "IconValueView.h"
#include <stdio.h>
#include <string.h>
#include <Bitmap.h>
#include <Region.h>
#include "PropertyItemView.h"
IconValueView::IconValueView(IconProperty* property)
: PropertyEditorView(),
fProperty(property),
fIcon(NULL)
{
SetFlags(Flags() | B_NAVIGABLE_JUMP);
}
IconValueView::~IconValueView()
{
delete fIcon;
}
void
IconValueView::Draw(BRect updateRect)
{
BRect r;
BRegion originalClippingRegion;
GetClippingRegion(&originalClippingRegion);
if (fIcon) {
BRect b(Bounds());
r = fIcon->Bounds();
r.OffsetTo(floorf(b.left + b.Width() / 2.0 - r.Width() / 2.0),
floorf(b.top + b.Height() / 2.0 - r.Height() / 2.0));
if (fIcon->ColorSpace() == B_RGBA32 || fIcon->ColorSpace() == B_RGBA32_BIG) {
SetHighColor(255, 255, 255, 255);
SetDrawingMode(B_OP_ALPHA);
SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
} else {
BRegion region = originalClippingRegion;
region.Exclude(r);
ConstrainClippingRegion(®ion);
}
}
PropertyEditorView::Draw(updateRect);
ConstrainClippingRegion(&originalClippingRegion);
if (fIcon) {
DrawBitmap(fIcon, r.LeftTop());
}
}
void
IconValueView::SetEnabled(bool enabled)
{
}
bool
IconValueView::AdoptProperty(Property* property)
{
IconProperty* p = dynamic_cast<IconProperty*>(property);
if (p) {
SetIcon(p->Icon(), p->Width(), p->Height(), p->Format());
Invalidate();
fProperty = p;
return true;
}
return false;
}
Property*
IconValueView::GetProperty() const
{
return fProperty;
}
status_t
IconValueView::SetIcon(const unsigned char* bitsFromQuickRes,
uint32 width, uint32 height, color_space format)
{
status_t status = B_BAD_VALUE;
if (bitsFromQuickRes && width > 0 && height > 0) {
delete fIcon;
fIcon = new BBitmap(BRect(0.0, 0.0, width - 1.0, height - 1.0), format);
status = fIcon ? fIcon->InitCheck() : B_ERROR;
if (status >= B_OK) {
memcpy(fIcon->Bits(), bitsFromQuickRes, fIcon->BitsLength());
} else {
delete fIcon;
fIcon = NULL;
printf("IconValueView::SetIcon() - error allocating bitmap: %s\n", strerror(status));
}
}
return status;
}