xref: /haiku/src/apps/processcontroller/IconMenuItem.cpp (revision 97dfeb96704e5dbc5bec32ad7b21379d0125e031)
1 /*
2 
3 	IconMenuItem.cpp
4 
5 	ProcessController
6 	(c) 2000, Georges-Edouard Berenger, All Rights Reserved.
7 	Copyright (C) 2004 beunited.org
8 
9 	This library is free software; you can redistribute it and/or
10 	modify it under the terms of the GNU Lesser General Public
11 	License as published by the Free Software Foundation; either
12 	version 2.1 of the License, or (at your option) any later version.
13 
14 	This library is distributed in the hope that it will be useful,
15 	but WITHOUT ANY WARRANTY; without even the implied warranty of
16 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 	Lesser General Public License for more details.
18 
19 	You should have received a copy of the GNU Lesser General Public
20 	License along with this library; if not, write to the Free Software
21 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 
23 */
24 
25 #include "IconMenuItem.h"
26 #include <Application.h>
27 #include <NodeInfo.h>
28 #include <Bitmap.h>
29 #include <Roster.h>
30 
31 
32 IconMenuItem::IconMenuItem(BBitmap* icon, const char* title,
33 	BMessage* msg, bool drawText, bool purge)
34 	: BMenuItem(title, msg),
35 	fIcon(icon),
36 	fDrawText(drawText),
37 	fPurge(purge)
38 {
39 	if (!fIcon)
40 		DefaultIcon(NULL);
41 }
42 
43 
44 IconMenuItem::IconMenuItem(BBitmap* icon, BMenu* menu, bool drawText, bool purge)
45 	: BMenuItem(menu),
46 	fIcon(icon),
47 	fDrawText(drawText),
48 	fPurge(purge)
49 
50 {
51 	if (!fIcon)
52 		DefaultIcon(NULL);
53 }
54 
55 
56 IconMenuItem::IconMenuItem(const char* mime, const char* title, BMessage* msg, bool drawText)
57 	: BMenuItem(title, msg),
58 	fIcon(NULL),
59 	fDrawText(drawText)
60 {
61 	DefaultIcon(mime);
62 }
63 
64 
65 IconMenuItem::~IconMenuItem()
66 {
67 	if (fPurge && fIcon)
68 		delete fIcon;
69 }
70 
71 
72 void IconMenuItem::DrawContent()
73 {
74 	BPoint	loc;
75 
76 	DrawIcon();
77 	if (fDrawText) {
78 		loc = ContentLocation();
79 		loc.x += 20;
80 		Menu()->MovePenTo(loc);
81 		BMenuItem::DrawContent();
82 	}
83 }
84 
85 
86 void
87 IconMenuItem::Highlight(bool hilited)
88 {
89 	BMenuItem::Highlight(hilited);
90 	DrawIcon();
91 }
92 
93 
94 void
95 IconMenuItem::DrawIcon()
96 {
97 	// TODO: exact code duplication with TeamBarMenuItem::DrawIcon()
98 	if (!fIcon)
99 		return;
100 
101 	BPoint loc = ContentLocation();
102 	BRect frame = Frame();
103 
104 	loc.y = frame.top + (frame.bottom - frame.top - 15) / 2;
105 
106 	BMenu* menu = Menu();
107 
108 	if (fIcon->ColorSpace() == B_RGBA32) {
109 		menu->SetDrawingMode(B_OP_ALPHA);
110 		menu->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
111 	} else
112 		menu->SetDrawingMode(B_OP_OVER);
113 
114 	menu->DrawBitmap(fIcon, loc);
115 
116 	menu->SetDrawingMode(B_OP_COPY);
117 }
118 
119 
120 void
121 IconMenuItem::GetContentSize(float* width, float* height)
122 {
123 	BMenuItem::GetContentSize(width, height);
124 	int	limit = IconMenuItem::MinHeight();
125 	if (*height < limit)
126 		*height = limit;
127 	if (fDrawText)
128 		*width += 20;
129 	else
130 		*width = 16;
131 }
132 
133 
134 void
135 IconMenuItem::DefaultIcon(const char* mime)
136 {
137 	BRect rect(0, 0, 15, 15);
138 	fIcon = new BBitmap(rect, B_COLOR_8_BIT);
139 	if (mime) {
140 		BMimeType mimeType(mime);
141 		if (mimeType.GetIcon(fIcon, B_MINI_ICON) != B_OK)
142 			fDrawText = true;
143 	} else {
144 		app_info info;
145 		be_app->GetAppInfo(&info);
146 		if (BNodeInfo::GetTrackerIcon(&info.ref, fIcon, B_MINI_ICON) != B_OK)
147 			fDrawText = true;
148 	}
149 	fPurge = true;
150 }
151 
152 
153 int IconMenuItem::MinHeight()
154 {
155 	static int	minheight = -1;
156 	if (minheight < 0)
157 		minheight = 17;
158 	return minheight;
159 }
160