From 71917d4a4d5f9c53ba837f285563dba39d6c3e37 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Tue, 3 Aug 2021 10:15:04 -0500 Subject: [PATCH] (libinterface) Optional bitmap-ownership with BitmapMenuItem --- libs/libinterface/BitmapMenuItem.cpp | 29 +++++++++++++++------------- libs/libinterface/BitmapMenuItem.h | 24 ++++++++++++----------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/libs/libinterface/BitmapMenuItem.cpp b/libs/libinterface/BitmapMenuItem.cpp index e2e79bc..baf5628 100644 --- a/libs/libinterface/BitmapMenuItem.cpp +++ b/libs/libinterface/BitmapMenuItem.cpp @@ -6,17 +6,18 @@ #include "BitmapMenuItem.h" #include -BitmapMenuItem::BitmapMenuItem(const char *label, BMessage *msg, - BBitmap *bitmap, char shortcut, - uint32 modifiers) + +BitmapMenuItem::BitmapMenuItem(const char* label, BMessage* msg, + BBitmap* bitmap, char shortcut, uint32 modifiers, bool ownership) : - BMenuItem(label,msg,shortcut,modifiers), - fBitmap(bitmap) + BMenuItem(label, msg, shortcut, modifiers), + fBitmap(bitmap), + fOwnership(ownership) { } -BitmapMenuItem::BitmapMenuItem(BMessage *data) +BitmapMenuItem::BitmapMenuItem(BMessage* data) : BMenuItem(data) { @@ -26,27 +27,28 @@ BitmapMenuItem::BitmapMenuItem(BMessage *data) BitmapMenuItem::~BitmapMenuItem(void) { - delete fBitmap; + if (fOwnership == true) + delete fBitmap; } 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) - status = fBitmap->Archive(data,deep); + status = fBitmap->Archive(data, deep); if (status == B_OK) - status = data->AddString("class","BitmapMenuItem"); + status = data->AddString("class", "BitmapMenuItem"); return status; } void -BitmapMenuItem::GetContentSize(float *width, float *height) +BitmapMenuItem::GetContentSize(float* width, float* height) { float w,h; BMenuItem::GetContentSize(&w,&h); @@ -107,7 +109,8 @@ BitmapMenuItem::DrawContent(void) void BitmapMenuItem::SetBitmap(BBitmap *bitmap) { - delete fBitmap; + if (fOwnership == true) + delete fBitmap; fBitmap = bitmap; } diff --git a/libs/libinterface/BitmapMenuItem.h b/libs/libinterface/BitmapMenuItem.h index 91cf5d9..ebdd712 100644 --- a/libs/libinterface/BitmapMenuItem.h +++ b/libs/libinterface/BitmapMenuItem.h @@ -11,29 +11,31 @@ /* BitmapMenuItems are simple little items, really. They provide the 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 BitmapMenuItems -- the bitmap does not obscure the checkmark. */ class BitmapMenuItem : public BMenuItem { public: - BitmapMenuItem(const char *label, BMessage *msg, - BBitmap *bitmap, char shortcut = 0, - uint32 modifiers = 0); - BitmapMenuItem(BMessage *data); - virtual ~BitmapMenuItem(void); - virtual status_t Archive(BMessage *data, bool deep = true) const; + BitmapMenuItem(const char* label, BMessage* msg, + BBitmap* bitmap, char shortcut = 0, + uint32 modifiers = 0, bool ownership = true); + BitmapMenuItem(BMessage* data); + virtual ~BitmapMenuItem(void); + + virtual status_t Archive(BMessage* data, bool deep = true) const; - virtual void GetContentSize(float *width, float *height); + virtual void GetContentSize(float* width, float* height); virtual void DrawContent(void); - virtual void SetBitmap(BBitmap *bitmap); - BBitmap* Bitmap(void) const { return fBitmap; } + virtual void SetBitmap(BBitmap* bitmap); + BBitmap* Bitmap(void) const { return fBitmap; } private: - BBitmap *fBitmap; + BBitmap* fBitmap; float fBaselineOffset; + bool fOwnership; }; #endif