1//--------------------------------------------------------------------
2//
3//	BitmapMenuItem.cpp
4//
5//	Written by: Owen Smith
6//
7//--------------------------------------------------------------------
8
9/*
10	Copyright 1999, Be Incorporated.   All Rights Reserved.
11	This file may be used under the terms of the Be Sample Code License.
12*/
13
14#include "BitmapMenuItem.h"
15#include "constants.h"
16
17//====================================================================
18//	BitmapMenuItem Implementation
19
20
21
22//--------------------------------------------------------------------
23//	BitmapMenuItem constructors, destructors, operators
24
25BitmapMenuItem::BitmapMenuItem(const char* name, const BBitmap& bitmap,
26	BMessage* message, char shortcut, uint32 modifiers)
27	: BMenuItem(name, message, shortcut, modifiers),
28	m_bitmap(bitmap.Bounds(), bitmap.ColorSpace())
29{
30	// Sadly, operator= for bitmaps is not yet implemented.
31	// Half of m_bitmap's initialization is above; now we copy
32	// the bits.
33	m_bitmap.SetBits(bitmap.Bits(), bitmap.BitsLength(),
34		0, bitmap.ColorSpace());
35}
36
37
38
39//--------------------------------------------------------------------
40//	BitmapMenuItem constructors, destructors, operators
41
42void BitmapMenuItem::Draw(void)
43{
44	BMenu* menu = Menu();
45	if (menu) {
46		BRect itemFrame = Frame();
47		BRect bitmapFrame = itemFrame;
48		bitmapFrame.InsetBy(2, 2); // account for 2-pixel margin
49
50		menu->SetDrawingMode(B_OP_COPY);
51		menu->SetHighColor(BKG_GREY);
52		menu->FillRect(itemFrame);
53		menu->DrawBitmap(&m_bitmap, bitmapFrame);
54
55		if (IsSelected()) {
56			// a nonstandard but simple way to draw highlights
57			menu->SetDrawingMode(B_OP_INVERT);
58			menu->SetHighColor(0,0,0);
59			menu->FillRect(itemFrame);
60		}
61	}
62}
63
64void BitmapMenuItem::GetContentSize(float* width, float* height)
65{
66	GetBitmapSize(width, height);
67}
68
69
70
71//--------------------------------------------------------------------
72//	BitmapMenuItem accessors
73
74void BitmapMenuItem::GetBitmapSize(float* width, float* height)
75{
76	BRect r = m_bitmap.Bounds();
77	*width = r.Width() + 4; // 2-pixel boundary on either side
78	*height = r.Height() + 4; // 2-pixel boundary on top/bottom
79}
80