xref: /haiku/src/apps/processcontroller/IconMenuItem.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  * Copyright 2000, Georges-Edouard Berenger. All rights reserved.
3  * Copyright 2022, Haiku, Inc. All rights reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 #include "IconMenuItem.h"
7 
8 #include <ControlLook.h>
9 #include <Bitmap.h>
10 
11 
12 IconMenuItem::IconMenuItem(BBitmap* icon, const char* title,
13 	BMessage* msg, bool drawText, bool purge)
14 	:
15 	BMenuItem(title, msg),
16 	fIcon(icon),
17 	fDrawText(drawText),
18 	fPurge(purge)
19 {
20 }
21 
22 
23 IconMenuItem::IconMenuItem(BBitmap* icon, BMenu* menu, bool drawText, bool purge)
24 	:
25 	BMenuItem(menu),
26 	fIcon(icon),
27 	fDrawText(drawText),
28 	fPurge(purge)
29 {
30 }
31 
32 
33 IconMenuItem::~IconMenuItem()
34 {
35 	if (fPurge)
36 		delete fIcon;
37 }
38 
39 
40 void
41 IconMenuItem::Reset(BBitmap* icon, bool purge)
42 {
43 	if (fPurge)
44 		delete fIcon;
45 
46 	fPurge = purge;
47 	fIcon = icon;
48 }
49 
50 
51 void
52 IconMenuItem::DrawContent()
53 {
54 	DrawIcon();
55 
56 	if (fDrawText) {
57 		BPoint loc = ContentLocation();
58 		loc.x += ceilf(be_control_look->DefaultLabelSpacing() * 3.3f);
59 		Menu()->MovePenTo(loc);
60 		BMenuItem::DrawContent();
61 	}
62 }
63 
64 
65 void
66 IconMenuItem::Highlight(bool hilited)
67 {
68 	BMenuItem::Highlight(hilited);
69 	DrawIcon();
70 }
71 
72 
73 void
74 IconMenuItem::DrawIcon()
75 {
76 	if (fIcon == NULL)
77 		return;
78 
79 	BPoint loc = ContentLocation();
80 	BRect frame = Frame();
81 
82 	loc.y = frame.top + (frame.bottom - frame.top - fIcon->Bounds().Height()) / 2;
83 
84 	BMenu* menu = Menu();
85 
86 	if (fIcon->ColorSpace() == B_RGBA32) {
87 		menu->SetDrawingMode(B_OP_ALPHA);
88 		menu->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
89 	} else
90 		menu->SetDrawingMode(B_OP_OVER);
91 
92 	menu->DrawBitmap(fIcon, loc);
93 
94 	menu->SetDrawingMode(B_OP_COPY);
95 }
96 
97 
98 void
99 IconMenuItem::GetContentSize(float* width, float* height)
100 {
101 	BMenuItem::GetContentSize(width, height);
102 	if (fIcon == NULL)
103 		return;
104 
105 	const float limit = ceilf(fIcon->Bounds().Height() +
106 		(be_control_look->DefaultLabelSpacing() / 3.0f));
107 	if (*height < limit)
108 		*height = limit;
109 	if (fDrawText)
110 		*width += fIcon->Bounds().Width() + be_control_look->DefaultLabelSpacing();
111 	else
112 		*width = fIcon->Bounds().Width() + 1;
113 }
114