xref: /haiku/src/apps/processcontroller/IconMenuItem.cpp (revision 0041b382147b19ee82ba05e36f007951892c2223)
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 #include <parsedate.h>
31 
32 
33 IconMenuItem::IconMenuItem(BBitmap* icon, const char* title,
34 	BMessage* msg, bool drawText, bool purge)
35 	: BMenuItem(title, msg),
36 	fIcon(icon),
37 	fDrawText(drawText),
38 	fPurge(purge)
39 {
40 	if (!fIcon)
41 		DefaultIcon(NULL);
42 }
43 
44 
45 IconMenuItem::IconMenuItem(BBitmap* icon, BMenu* menu, bool drawText, bool purge)
46 	: BMenuItem(menu),
47 	fIcon(icon),
48 	fDrawText(drawText),
49 	fPurge(purge)
50 
51 {
52 	if (!fIcon)
53 		DefaultIcon(NULL);
54 }
55 
56 
57 IconMenuItem::IconMenuItem(const char* mime, const char* title, BMessage* msg, bool drawText)
58 	: BMenuItem(title, msg),
59 	fIcon(NULL),
60 	fDrawText(drawText)
61 {
62 	DefaultIcon(mime);
63 }
64 
65 
66 IconMenuItem::~IconMenuItem()
67 {
68 	if (fPurge && fIcon)
69 		delete fIcon;
70 }
71 
72 
73 void IconMenuItem::DrawContent()
74 {
75 	BPoint	loc;
76 
77 	DrawIcon();
78 	if (fDrawText) {
79 		loc = ContentLocation();
80 		loc.x += 20;
81 		Menu()->MovePenTo(loc);
82 		BMenuItem::DrawContent();
83 	}
84 }
85 
86 
87 void
88 IconMenuItem::Highlight(bool hilited)
89 {
90 	BMenuItem::Highlight(hilited);
91 	DrawIcon();
92 }
93 
94 
95 void
96 IconMenuItem::DrawIcon()
97 {
98 	// TODO: exact code duplication with TeamBarMenuItem::DrawIcon()
99 	if (!fIcon)
100 		return;
101 
102 	BPoint loc = ContentLocation();
103 	BRect frame = Frame();
104 
105 	loc.y = frame.top + (frame.bottom - frame.top - 15) / 2;
106 
107 	BMenu* menu = Menu();
108 
109 	if (fIcon->ColorSpace() == B_RGBA32) {
110 		menu->SetDrawingMode(B_OP_ALPHA);
111 		menu->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
112 	} else
113 		menu->SetDrawingMode(B_OP_OVER);
114 
115 	menu->DrawBitmap(fIcon, loc);
116 
117 	menu->SetDrawingMode(B_OP_COPY);
118 }
119 
120 
121 void
122 IconMenuItem::GetContentSize(float* width, float* height)
123 {
124 	BMenuItem::GetContentSize(width, height);
125 	int	limit = IconMenuItem::MinHeight();
126 	if (*height < limit)
127 		*height = limit;
128 	if (fDrawText)
129 		*width += 20;
130 	else
131 		*width = 16;
132 }
133 
134 
135 void
136 IconMenuItem::DefaultIcon(const char* mime)
137 {
138 	BRect rect(0, 0, 15, 15);
139 	fIcon = new BBitmap(rect, B_COLOR_8_BIT);
140 	if (mime) {
141 		BMimeType mimeType(mime);
142 		if (mimeType.GetIcon(fIcon, B_MINI_ICON) != B_OK)
143 			fDrawText = true;
144 	} else {
145 		app_info info;
146 		be_app->GetAppInfo(&info);
147 		if (BNodeInfo::GetTrackerIcon(&info.ref, fIcon, B_MINI_ICON) != B_OK)
148 			fDrawText = true;
149 	}
150 	fPurge = true;
151 }
152 
153 
154 int IconMenuItem::MinHeight()
155 {
156 	static int	minheight = -1;
157 	if (minheight < 0)
158 		minheight = before_dano() ? 16 : 17;
159 	return minheight;
160 }
161 
162 
163 bool
164 before_dano()
165 {
166 	static int old_version = -1;
167 	if (old_version < 0) {
168 		system_info sys_info;
169 		get_system_info(&sys_info);
170 		time_t kernelTime = parsedate(sys_info.kernel_build_date, time(NULL));
171 		struct tm* date = gmtime(&kernelTime);
172 		old_version = (date->tm_year < 101 || (date->tm_year == 101 && date->tm_mon < 10));
173 	}
174 
175 	return old_version;
176 }
177