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