(libinterface) Optional bitmap-ownership with BitmapMenuItem

This commit is contained in:
Jaidyn Ann 2021-08-03 10:15:04 -05:00
parent 6d8be225ca
commit 71917d4a4d
2 changed files with 29 additions and 24 deletions

View File

@ -6,17 +6,18 @@
#include "BitmapMenuItem.h" #include "BitmapMenuItem.h"
#include <Bitmap.h> #include <Bitmap.h>
BitmapMenuItem::BitmapMenuItem(const char *label, BMessage *msg,
BBitmap *bitmap, char shortcut, BitmapMenuItem::BitmapMenuItem(const char* label, BMessage* msg,
uint32 modifiers) BBitmap* bitmap, char shortcut, uint32 modifiers, bool ownership)
: :
BMenuItem(label,msg,shortcut,modifiers), BMenuItem(label, msg, shortcut, modifiers),
fBitmap(bitmap) fBitmap(bitmap),
fOwnership(ownership)
{ {
} }
BitmapMenuItem::BitmapMenuItem(BMessage *data) BitmapMenuItem::BitmapMenuItem(BMessage* data)
: :
BMenuItem(data) BMenuItem(data)
{ {
@ -26,27 +27,28 @@ BitmapMenuItem::BitmapMenuItem(BMessage *data)
BitmapMenuItem::~BitmapMenuItem(void) BitmapMenuItem::~BitmapMenuItem(void)
{ {
delete fBitmap; if (fOwnership == true)
delete fBitmap;
} }
status_t status_t
BitmapMenuItem::Archive(BMessage *data, bool deep) const BitmapMenuItem::Archive(BMessage* data, bool deep) const
{ {
status_t status = BMenuItem::Archive(data,deep); status_t status = BMenuItem::Archive(data, deep);
if (status == B_OK && fBitmap) if (status == B_OK && fBitmap)
status = fBitmap->Archive(data,deep); status = fBitmap->Archive(data, deep);
if (status == B_OK) if (status == B_OK)
status = data->AddString("class","BitmapMenuItem"); status = data->AddString("class", "BitmapMenuItem");
return status; return status;
} }
void void
BitmapMenuItem::GetContentSize(float *width, float *height) BitmapMenuItem::GetContentSize(float* width, float* height)
{ {
float w,h; float w,h;
BMenuItem::GetContentSize(&w,&h); BMenuItem::GetContentSize(&w,&h);
@ -107,7 +109,8 @@ BitmapMenuItem::DrawContent(void)
void void
BitmapMenuItem::SetBitmap(BBitmap *bitmap) BitmapMenuItem::SetBitmap(BBitmap *bitmap)
{ {
delete fBitmap; if (fOwnership == true)
delete fBitmap;
fBitmap = bitmap; fBitmap = bitmap;
} }

View File

@ -11,29 +11,31 @@
/* /*
BitmapMenuItems are simple little items, really. They provide the BitmapMenuItems are simple little items, really. They provide the
ability to show a menu item with some text, a picture, or both. The item ability to show a menu item with some text, a picture, or both. The item
takes ownership of the BBitmap given to it, so please do not delete it. optionally takes ownership of the BBitmap given to it.
Note that it is still possible to see the checkmark on marked Note that it is still possible to see the checkmark on marked
BitmapMenuItems -- the bitmap does not obscure the checkmark. BitmapMenuItems -- the bitmap does not obscure the checkmark.
*/ */
class BitmapMenuItem : public BMenuItem { class BitmapMenuItem : public BMenuItem {
public: public:
BitmapMenuItem(const char *label, BMessage *msg, BitmapMenuItem(const char* label, BMessage* msg,
BBitmap *bitmap, char shortcut = 0, BBitmap* bitmap, char shortcut = 0,
uint32 modifiers = 0); uint32 modifiers = 0, bool ownership = true);
BitmapMenuItem(BMessage *data); BitmapMenuItem(BMessage* data);
virtual ~BitmapMenuItem(void); virtual ~BitmapMenuItem(void);
virtual status_t Archive(BMessage *data, bool deep = true) const;
virtual void GetContentSize(float *width, float *height); virtual status_t Archive(BMessage* data, bool deep = true) const;
virtual void GetContentSize(float* width, float* height);
virtual void DrawContent(void); virtual void DrawContent(void);
virtual void SetBitmap(BBitmap *bitmap); virtual void SetBitmap(BBitmap* bitmap);
BBitmap* Bitmap(void) const { return fBitmap; } BBitmap* Bitmap(void) const { return fBitmap; }
private: private:
BBitmap *fBitmap; BBitmap* fBitmap;
float fBaselineOffset; float fBaselineOffset;
bool fOwnership;
}; };
#endif #endif