Bitmaps are now owned (and deleted) by BitmapMenuItem, small changes. Added a check for NULL which cures the symptoms of a bug in resource loading. Even if we fix the bug, this is defensive programming

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15355 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2005-12-06 08:19:58 +00:00
parent ac4fe990c9
commit 35a6c07857
4 changed files with 55 additions and 59 deletions
+24 -19
View File
@@ -1,7 +1,5 @@
// System Headers #include <Bitmap.h>
#ifndef _NODE_INFO_H
#include <NodeInfo.h> #include <NodeInfo.h>
#endif
// Project Headers // Project Headers
#include "BitmapMenuItem.h" #include "BitmapMenuItem.h"
@@ -9,32 +7,39 @@
// BitmapMenuItem class definition // BitmapMenuItem class definition
BitmapMenuItem::BitmapMenuItem(const char* name, BMessage* message, BitmapMenuItem::BitmapMenuItem(const char* name, BMessage* message,
BBitmap* bmp, char shortcut, uint32 modifiers) BBitmap* bmp, char shortcut, uint32 modifiers)
: BMenuItem(name, message, shortcut, modifiers) :
BMenuItem(name, message, shortcut, modifiers),
fBitmap(bmp),
fName(name)
{ {
fBmp = bmp;
fName.SetTo(name);
fCheckBmp = BTranslationUtils::GetBitmap(B_RAW_TYPE, "CHECK");
} }
void BitmapMenuItem::DrawContent(void)
BitmapMenuItem::~BitmapMenuItem()
{
delete fBitmap;
}
void
BitmapMenuItem::DrawContent()
{ {
BRect dr;
BMenu* menu = Menu(); BMenu* menu = Menu();
// if we don't have a menu, get out... // if we don't have a menu, get out...
if (!menu) return; if (!menu)
return;
BRect itemFrame = Frame(); BRect itemFrame = Frame();
menu->MovePenTo(itemFrame.left + 38, itemFrame.top + 2); menu->MovePenTo(itemFrame.left + 38, itemFrame.top + 2);
BMenuItem::DrawContent(); BMenuItem::DrawContent();
if (fBitmap != NULL) {
BRect bitmapFrame = fBmp->Bounds(); BRect bitmapFrame = fBitmap->Bounds();
dr.Set(itemFrame.left + 14, itemFrame.top + 2, itemFrame.left + 14 + bitmapFrame.right, itemFrame.top + 17); BRect dr(itemFrame.left + 14, itemFrame.top + 2, itemFrame.left + 14 + bitmapFrame.right, itemFrame.top + 17);
menu->SetDrawingMode(B_OP_OVER); menu->SetDrawingMode(B_OP_OVER);
menu->DrawBitmap(fBmp, bitmapFrame, dr); menu->DrawBitmap(fBitmap, bitmapFrame, dr);
menu->SetDrawingMode(B_OP_COPY); menu->SetDrawingMode(B_OP_COPY);
}
} }
+8 -13
View File
@@ -1,27 +1,22 @@
#ifndef _MBitmapMenuItem_h #ifndef _MBitmapMenuItem_h
#define _MBitmapMenuItem_h #define _MBitmapMenuItem_h
// System Headers
#include <Bitmap.h>
#include <MenuItem.h> #include <MenuItem.h>
#ifndef _TRANSLATION_UTILS_H
#include <TranslationUtils.h>
#endif
#include <String.h> #include <String.h>
class BBitmap;
// MBitmapMenuItem class declaration // MBitmapMenuItem class declaration
class BitmapMenuItem : public BMenuItem class BitmapMenuItem : public BMenuItem {
{
public: public:
BitmapMenuItem(const char* name, BMessage* message, BBitmap* bmp, BitmapMenuItem(const char* name, BMessage* message, BBitmap* bmp,
char shortcut = 0, uint32 modifiers = 0); char shortcut = 0, uint32 modifiers = 0);
virtual void DrawContent(void); ~BitmapMenuItem();
virtual void DrawContent();
private: private:
BBitmap *fBmp; BBitmap *fBitmap;
BString fName; BString fName;
BBitmap *fCheckBmp;
}; };
#endif // _MBitmapMenuItem_h #endif // _MBitmapMenuItem_h
+2 -3
View File
@@ -48,7 +48,6 @@
class MenuBar : public BMenuBar { class MenuBar : public BMenuBar {
public: public:
MenuBar(); MenuBar();
virtual ~MenuBar();
void set_menu(); void set_menu();
void build_menu(); void build_menu();
virtual void Update(); virtual void Update();
@@ -59,12 +58,12 @@
menu_info info; menu_info info;
//bitmaps //bitmaps
BBitmap *fCtlBmp; /*BBitmap *fCtlBmp;
BBitmap *fAltBmp; BBitmap *fAltBmp;
BBitmap *fSep0Bmp; BBitmap *fSep0Bmp;
BBitmap *fSep1Bmp; BBitmap *fSep1Bmp;
BBitmap *fSep2Bmp; BBitmap *fSep2Bmp;
*/
//seperator submenu //seperator submenu
BMenu *separatorStyleMenu; BMenu *separatorStyleMenu;
BMenuItem *separatorStyleZero; BMenuItem *separatorStyleZero;
+21 -24
View File
@@ -1,26 +1,20 @@
#include "MenuApp.h" #include "MenuApp.h"
#include <stdlib.h>
#include <Application.h>
#include <Resources.h> #include <Resources.h>
#include <Application.h> #include <TranslationUtils.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
MenuBar::MenuBar() MenuBar::MenuBar()
:BMenuBar(BRect(40,10,10,10), "menu", B_FOLLOW_TOP|B_FRAME_EVENTS, B_ITEMS_IN_COLUMN, true) :BMenuBar(BRect(40,10,10,10), "menu", B_FOLLOW_TOP|B_FRAME_EVENTS, B_ITEMS_IN_COLUMN, true)
{ {
fCtlBmp = BTranslationUtils::GetBitmap(B_RAW_TYPE, "CTL");
fAltBmp = BTranslationUtils::GetBitmap(B_RAW_TYPE, "ALT");
fSep0Bmp = BTranslationUtils::GetBitmap(B_RAW_TYPE, "SEP0");
fSep1Bmp = BTranslationUtils::GetBitmap(B_RAW_TYPE, "SEP1");
fSep2Bmp = BTranslationUtils::GetBitmap(B_RAW_TYPE, "SEP2");
get_menu_info(&info); get_menu_info(&info);
build_menu(); build_menu();
set_menu(); set_menu();
} }
MenuBar::~MenuBar()
{ /*nothing to clean up*/}
void void
MenuBar::build_menu() MenuBar::build_menu()
@@ -33,8 +27,10 @@
clickToOpenItem = new BMenuItem("Click To Open", new BMessage(CLICK_OPEN_MSG), 0, 0); clickToOpenItem = new BMenuItem("Click To Open", new BMessage(CLICK_OPEN_MSG), 0, 0);
alwaysShowTriggersItem = new BMenuItem("Always Show Triggers", new BMessage(ALLWAYS_TRIGGERS_MSG), 0, 0); alwaysShowTriggersItem = new BMenuItem("Always Show Triggers", new BMessage(ALLWAYS_TRIGGERS_MSG), 0, 0);
separatorStyleItem = new BMenuItem("Separator Style", new BMessage(DEFAULT_MSG), 0, 0); separatorStyleItem = new BMenuItem("Separator Style", new BMessage(DEFAULT_MSG), 0, 0);
ctlAsShortcutItem = new BitmapMenuItem("as Shortcut Key", new BMessage(CTL_MARKED_MSG), fCtlBmp); ctlAsShortcutItem = new BitmapMenuItem("as Shortcut Key",
altAsShortcutItem = new BitmapMenuItem("as Shortcut Key", new BMessage(ALT_MARKED_MSG), fAltBmp); new BMessage(CTL_MARKED_MSG), BTranslationUtils::GetBitmap(B_RAW_TYPE, "CTL"));
altAsShortcutItem = new BitmapMenuItem("as Shortcut Key",
new BMessage(ALT_MARKED_MSG), BTranslationUtils::GetBitmap(B_RAW_TYPE, "ALT"));
// color menu // color menu
colorSchemeItem = new BMenuItem("Color Scheme...", new BMessage(COLOR_SCHEME_MSG), 0, 0); colorSchemeItem = new BMenuItem("Color Scheme...", new BMessage(COLOR_SCHEME_MSG), 0, 0);
@@ -44,13 +40,14 @@
separatorStyleMenu->SetRadioMode(true); separatorStyleMenu->SetRadioMode(true);
BMessage *msg = new BMessage(MENU_SEP_TYPE); BMessage *msg = new BMessage(MENU_SEP_TYPE);
msg->AddInt32("sep", 0); msg->AddInt32("sep", 0);
separatorStyleZero = new BitmapMenuItem(" ", msg, fSep0Bmp); separatorStyleZero = new BitmapMenuItem(" ", msg,
BTranslationUtils::GetBitmap(B_RAW_TYPE, "SEP0"));
msg = new BMessage(MENU_SEP_TYPE); msg = new BMessage(MENU_SEP_TYPE);
msg->AddInt32("sep", 1); msg->AddInt32("sep", 1);
separatorStyleOne = new BitmapMenuItem("", msg, fSep1Bmp); separatorStyleOne = new BitmapMenuItem("", msg, BTranslationUtils::GetBitmap(B_RAW_TYPE, "SEP1"));
msg = new BMessage(MENU_SEP_TYPE); msg = new BMessage(MENU_SEP_TYPE);
msg->AddInt32("sep", 2); msg->AddInt32("sep", 2);
separatorStyleTwo = new BitmapMenuItem("", msg, fSep2Bmp); separatorStyleTwo = new BitmapMenuItem("", msg, BTranslationUtils::GetBitmap(B_RAW_TYPE, "SEP2"));
if (info.separator == 0) if (info.separator == 0)
separatorStyleZero->SetMarked(true); separatorStyleZero->SetMarked(true);
if (info.separator == 1) if (info.separator == 1)
@@ -81,8 +78,8 @@
MenuBar::set_menu() MenuBar::set_menu()
{ {
key_map *keys; key_map *keys;
char *chars; char *chars;
bool altAsShortcut; bool altAsShortcut;
// get up-to-date menu info // get up-to-date menu info
get_menu_info(&info); get_menu_info(&info);
@@ -94,12 +91,12 @@
get_key_map(&keys, &chars); get_key_map(&keys, &chars);
altAsShortcut = (keys->left_command_key == 0x5d) && (keys->right_command_key == 0x5f); altAsShortcut = (keys->left_command_key == 0x5d) && (keys->right_command_key == 0x5f);
altAsShortcutItem->SetMarked(altAsShortcut); altAsShortcutItem->SetMarked(altAsShortcut);
ctlAsShortcutItem->SetMarked(!altAsShortcut); ctlAsShortcutItem->SetMarked(!altAsShortcut);
free(chars); free(chars);
free(keys); free(keys);
} }
void void