Chat-O-Matic/libs/libinterface/BitmapMenuItem.cpp

117 lines
2.3 KiB
C++
Raw Permalink Normal View History

/*
BitmapListItem.cpp: A BMenuItem with an optional picture
Written by DarkWyrm <darkwyrm@earthlink.net>, Copyright 2007
Released under the MIT license.
*/
#include "BitmapMenuItem.h"
#include <Bitmap.h>
BitmapMenuItem::BitmapMenuItem(const char* label, BMessage* msg,
BBitmap* bitmap, char shortcut, uint32 modifiers, bool ownership)
:
BMenuItem(label, msg, shortcut, modifiers),
fBitmap(bitmap),
fOwnership(ownership)
{
}
BitmapMenuItem::BitmapMenuItem(BMessage* data)
:
BMenuItem(data)
{
fBitmap = (BBitmap*) BBitmap::Instantiate(data);
}
BitmapMenuItem::~BitmapMenuItem(void)
{
if (fOwnership == true)
delete fBitmap;
}
status_t
BitmapMenuItem::Archive(BMessage* data, bool deep) const
{
status_t status = BMenuItem::Archive(data, deep);
if (status == B_OK && fBitmap)
status = fBitmap->Archive(data, deep);
if (status == B_OK)
status = data->AddString("class", "BitmapMenuItem");
return status;
}
void
BitmapMenuItem::GetContentSize(float* width, float* height)
{
float w,h;
BMenuItem::GetContentSize(&w,&h);
if (fBitmap)
w += (fBitmap->Bounds().Width() *
(Frame().Height() / fBitmap->Bounds().Height())) + 20;
if (width)
*width = w;
if (height)
*height = h;
}
void
BitmapMenuItem::DrawContent(void)
{
if (!Label() && !fBitmap)
return;
float width, height;
GetContentSize(&width, &height);
BRect drawrect(Frame());
drawrect.bottom--;
drawrect.top++;
drawrect.left = ContentLocation().x;
if (fBitmap) {
// Scale the fBitmap down to completely fit within the field's height
if (fBitmap->Bounds().Height() > drawrect.Height()) {
drawrect.right = drawrect.left +
(fBitmap->Bounds().Width() *
(Frame().Height() / fBitmap->Bounds().Height()));
} else {
drawrect.right = drawrect.left + fBitmap->Bounds().Width();
}
} else {
drawrect.right = drawrect.left - 5;
}
BPoint stringpoint(ContentLocation());
stringpoint.x = drawrect.right + 10;
Menu()->MovePenTo(stringpoint);
BMenuItem::DrawContent();
if (fBitmap) {
Menu()->PushState();
Menu()->SetDrawingMode(B_OP_ALPHA);
2010-05-10 17:42:54 -05:00
Menu()->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
Menu()->DrawBitmap(fBitmap, fBitmap->Bounds(), drawrect, B_FILTER_BITMAP_BILINEAR);
Menu()->PopState();
}
}
void
BitmapMenuItem::SetBitmap(BBitmap *bitmap)
{
if (fOwnership == true)
delete fBitmap;
fBitmap = bitmap;
}