123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- /*
- 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);
- 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;
- }
|