xref: /haiku/src/apps/processcontroller/TeamBarMenuItem.cpp (revision e39da397f5ff79f2db9f9a3ddf1852b6710578af)
1 /*
2 	ProcessController © 2000, Georges-Edouard Berenger, All Rights Reserved.
3 	Copyright (C) 2004 beunited.org
4 
5 	This library is free software; you can redistribute it and/or
6 	modify it under the terms of the GNU Lesser General Public
7 	License as published by the Free Software Foundation; either
8 	version 2.1 of the License, or (at your option) any later version.
9 
10 	This library is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 	Lesser General Public License for more details.
14 
15 	You should have received a copy of the GNU Lesser General Public
16 	License along with this library; if not, write to the Free Software
17 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 
20 #include "TeamBarMenuItem.h"
21 
22 #include "Colors.h"
23 #include "ProcessController.h"
24 #include "ThreadBarMenu.h"
25 #include "ThreadBarMenuItem.h"
26 
27 #include <Bitmap.h>
28 
29 
30 #define B_USAGE_SELF 0
31 
32 
33 TeamBarMenuItem::TeamBarMenuItem(BMenu *menu, BMessage *kill_team, team_id team,
34 		BBitmap* icon, bool deleteIcon)
35 	: BMenuItem(menu, kill_team),
36 	fTeamID(team),
37 	fIcon(icon),
38 	fDeleteIcon(deleteIcon)
39 {
40 	Init();
41 }
42 
43 
44 void
45 TeamBarMenuItem::Init()
46 {
47 	team_info tminfo;
48 	get_team_info(fTeamID, &tminfo);
49 	get_team_usage_info(fTeamID, B_USAGE_SELF, &fTeamUsageInfo);
50 	if (fTeamID == B_SYSTEM_TEAM) {
51 		thread_info	thinfos;
52 		bigtime_t idle = 0;
53 		for (int t = 1; t <= gCPUcount; t++)
54 			if (get_thread_info(t, &thinfos) == B_OK)
55 				idle += thinfos.kernel_time + thinfos.user_time;
56 		fTeamUsageInfo.kernel_time += fTeamUsageInfo.user_time;
57 		fTeamUsageInfo.user_time = idle;
58 	}
59 
60 	fLastTime = system_time();
61 	fKernel = -1;
62 	fGrenze1 = -1;
63 	fGrenze2 = -1;
64 }
65 
66 
67 TeamBarMenuItem::~TeamBarMenuItem()
68 {
69 	if (fDeleteIcon)
70 		delete fIcon;
71 }
72 
73 
74 void
75 TeamBarMenuItem::DrawContent()
76 {
77 	BPoint	loc;
78 
79 	DrawIcon();
80 	if (fKernel < 0)
81 		BarUpdate();
82 	else
83 		DrawBar(true);
84 	loc = ContentLocation();
85 	loc.x += 20;
86 	Menu()->MovePenTo(loc);
87 	BMenuItem::DrawContent();
88 }
89 
90 
91 void
92 TeamBarMenuItem::DrawIcon()
93 {
94 	BPoint loc = ContentLocation();
95 	BRect frame = Frame();
96 
97 	loc.y = frame.top + (frame.bottom - frame.top - 15) / 2;
98 
99 	BMenu* menu = Menu ();
100 	menu->SetDrawingMode(B_OP_OVER);
101 	if (fIcon)
102 		menu->DrawBitmap(fIcon, loc);
103 }
104 
105 
106 void
107 TeamBarMenuItem::DrawBar(bool force)
108 {
109 	bool selected = IsSelected ();
110 	BRect frame = Frame();
111 	BMenu* menu = Menu ();
112 	frame.right -=  24;
113 	frame.left = frame.right-kBarWidth;
114 	frame.top += 5;
115 	frame.bottom = frame.top+8;
116 
117 	if (fKernel < 0)
118 		return;
119 
120 	if (fGrenze1 < 0)
121 		force = true;
122 	if (force) {
123 		if (selected)
124 			menu->SetHighColor(gFrameColorSelected);
125 		else
126 			menu->SetHighColor(gFrameColor);
127 		menu->StrokeRect(frame);
128 	}
129 
130 	frame.InsetBy(1, 1);
131 	BRect r = frame;
132 	float grenze1 = frame.left+(frame.right-frame.left)*fKernel/gCPUcount;
133 	float grenze2 = frame.left+(frame.right-frame.left)*(fKernel+fUser)/gCPUcount;
134 	if (grenze1 > frame.right)
135 		grenze1 = frame.right;
136 	if (grenze2 > frame.right)
137 		grenze2 = frame.right;
138 	r.right = grenze1;
139 	if (!force)
140 		r.left = fGrenze1;
141 	if (r.left < r.right) {
142 		if (selected)
143 			menu->SetHighColor(gKernelColorSelected);
144 		else
145 			menu->SetHighColor(gKernelColor);
146 		menu->FillRect(r);
147 	}
148 
149 	r.left = grenze1;
150 	r.right = grenze2;
151 
152 	if (!force) {
153 		if (fGrenze2 > r.left && r.left >= fGrenze1)
154 			r.left = fGrenze2;
155 		if (fGrenze1 < r.right && r.right  <= fGrenze2)
156 			r.right = fGrenze1;
157 	}
158 
159 	if (r.left < r.right) {
160 		if (selected)
161 			menu->SetHighColor(fTeamID == B_SYSTEM_TEAM ? gIdleColorSelected : gUserColorSelected);
162 		else
163 			menu->SetHighColor(fTeamID == B_SYSTEM_TEAM ? gIdleColor : gUserColor);
164 		menu->FillRect(r);
165 	}
166 
167 	r.left = grenze2;
168 	r.right = frame.right;
169 
170 	if (!force)
171 		r.right = fGrenze2;
172 	if (r.left < r.right) {
173 		if (selected)
174 			menu->SetHighColor(gWhiteSelected);
175 		else
176 			menu->SetHighColor(kWhite);
177 		menu->FillRect(r);
178 	}
179 
180 	menu->SetHighColor(kBlack);
181 	fGrenze1 = grenze1;
182 	fGrenze2 = grenze2;
183 }
184 
185 
186 void
187 TeamBarMenuItem::GetContentSize(float* width, float* height)
188 {
189 	BMenuItem::GetContentSize(width, height);
190 	if (*height < 16)
191 		*height = 16;
192 	*width += 40+kBarWidth;
193 }
194 
195 
196 void
197 TeamBarMenuItem::BarUpdate()
198 {
199 	team_usage_info	usage;
200 	if (get_team_usage_info(fTeamID, B_USAGE_SELF, &usage) == B_OK) {
201 		bigtime_t now = system_time();
202 		bigtime_t idle = 0;
203 		if (fTeamID == B_SYSTEM_TEAM) {
204 			thread_info	thinfos;
205 			for (int t = 1; t <= gCPUcount; t++)
206 				if (get_thread_info(t, &thinfos) == B_OK)
207 					idle += thinfos.kernel_time + thinfos.user_time;
208 			usage.kernel_time += usage.user_time;
209 			usage.user_time = idle;
210 			idle -= fTeamUsageInfo.user_time;
211 		}
212 		fKernel = double(usage.kernel_time-fTeamUsageInfo.kernel_time-idle)/double(now-fLastTime);
213 		fUser = double(usage.user_time-fTeamUsageInfo.user_time)/double(now-fLastTime);
214 		if (fKernel < 0)
215 			fKernel = 0;
216 		fLastTime = now;
217 		fTeamUsageInfo = usage;
218 		DrawBar(false);
219 	} else
220 		fKernel = -1;
221 }
222 
223 
224 void
225 TeamBarMenuItem::Reset(char* name, team_id team, BBitmap* icon, bool deleteIcon)
226 {
227 	SetLabel(name);
228 	fTeamID = team;
229 	Init();
230 	if (fDeleteIcon)
231 		delete fIcon;
232 	fDeleteIcon = deleteIcon;
233 	fIcon = icon;
234 	Message()->ReplaceInt32("team", team);
235 	((ThreadBarMenu*) Submenu())->Reset(team);
236 	BarUpdate();
237 }
238