xref: /haiku/src/apps/processcontroller/IconMenuItem.cpp (revision 410ed2fbba58819ac21e27d3676739728416761d)
1 /*
2  * Copyright 2000, Georges-Edouard Berenger. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "IconMenuItem.h"
7 #include <Application.h>
8 #include <NodeInfo.h>
9 #include <Bitmap.h>
10 #include <Roster.h>
11 
12 
13 IconMenuItem::IconMenuItem(BBitmap* icon, const char* title,
14 	BMessage* msg, bool drawText, bool purge)
15 	: BMenuItem(title, msg),
16 	fIcon(icon),
17 	fDrawText(drawText),
18 	fPurge(purge)
19 {
20 	if (!fIcon)
21 		DefaultIcon(NULL);
22 }
23 
24 
25 IconMenuItem::IconMenuItem(BBitmap* icon, BMenu* menu, bool drawText, bool purge)
26 	: BMenuItem(menu),
27 	fIcon(icon),
28 	fDrawText(drawText),
29 	fPurge(purge)
30 
31 {
32 	if (!fIcon)
33 		DefaultIcon(NULL);
34 }
35 
36 
37 IconMenuItem::IconMenuItem(const char* mime, const char* title, BMessage* msg, bool drawText)
38 	: BMenuItem(title, msg),
39 	fIcon(NULL),
40 	fDrawText(drawText)
41 {
42 	DefaultIcon(mime);
43 }
44 
45 
46 IconMenuItem::~IconMenuItem()
47 {
48 	if (fPurge && fIcon)
49 		delete fIcon;
50 }
51 
52 
53 void IconMenuItem::DrawContent()
54 {
55 	BPoint	loc;
56 
57 	DrawIcon();
58 	if (fDrawText) {
59 		loc = ContentLocation();
60 		loc.x += 20;
61 		Menu()->MovePenTo(loc);
62 		BMenuItem::DrawContent();
63 	}
64 }
65 
66 
67 void
68 IconMenuItem::Highlight(bool hilited)
69 {
70 	BMenuItem::Highlight(hilited);
71 	DrawIcon();
72 }
73 
74 
75 void
76 IconMenuItem::DrawIcon()
77 {
78 	// TODO: exact code duplication with TeamBarMenuItem::DrawIcon()
79 	if (!fIcon)
80 		return;
81 
82 	BPoint loc = ContentLocation();
83 	BRect frame = Frame();
84 
85 	loc.y = frame.top + (frame.bottom - frame.top - 15) / 2;
86 
87 	BMenu* menu = Menu();
88 
89 	if (fIcon->ColorSpace() == B_RGBA32) {
90 		menu->SetDrawingMode(B_OP_ALPHA);
91 		menu->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
92 	} else
93 		menu->SetDrawingMode(B_OP_OVER);
94 
95 	menu->DrawBitmap(fIcon, loc);
96 
97 	menu->SetDrawingMode(B_OP_COPY);
98 }
99 
100 
101 void
102 IconMenuItem::GetContentSize(float* width, float* height)
103 {
104 	BMenuItem::GetContentSize(width, height);
105 	int	limit = IconMenuItem::MinHeight();
106 	if (*height < limit)
107 		*height = limit;
108 	if (fDrawText)
109 		*width += 20;
110 	else
111 		*width = 16;
112 }
113 
114 
115 void
116 IconMenuItem::DefaultIcon(const char* mime)
117 {
118 	BRect rect(0, 0, 15, 15);
119 	fIcon = new BBitmap(rect, B_COLOR_8_BIT);
120 	if (mime) {
121 		BMimeType mimeType(mime);
122 		if (mimeType.GetIcon(fIcon, B_MINI_ICON) != B_OK)
123 			fDrawText = true;
124 	} else {
125 		app_info info;
126 		be_app->GetAppInfo(&info);
127 		if (BNodeInfo::GetTrackerIcon(&info.ref, fIcon, B_MINI_ICON) != B_OK)
128 			fDrawText = true;
129 	}
130 	fPurge = true;
131 }
132 
133 
134 int IconMenuItem::MinHeight()
135 {
136 	static int	minheight = -1;
137 	if (minheight < 0)
138 		minheight = 17;
139 	return minheight;
140 }
141