xref: /haiku/src/apps/processcontroller/ThreadBarMenuItem.cpp (revision cd552c7a15cc10c36dae8d7439ba1d6c0bb168c5)
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 "ThreadBarMenuItem.h"
21 
22 #include "Colors.h"
23 #include "PriorityMenu.h"
24 #include "ProcessController.h"
25 
26 #include <stdio.h>
27 
28 
29 ThreadBarMenuItem::ThreadBarMenuItem(const char* title, thread_id thread,
30 		BMenu *menu, BMessage* msg)
31 	: BMenuItem(menu, msg), fThreadID(thread)
32 {
33 	SetLabel(title);
34 	get_thread_info(fThreadID, &fThreadInfo);
35 	fLastTime = system_time();
36 	fKernel = -1;
37 	fGrenze1 = -1;
38 	fGrenze2 = -1;
39 }
40 
41 
42 void
43 ThreadBarMenuItem::DrawContent()
44 {
45 	if (fKernel < 0)
46 		BarUpdate();
47 	DrawBar(true);
48 	Menu()->MovePenTo(ContentLocation());
49 	BMenuItem::DrawContent();
50 }
51 
52 
53 void
54 ThreadBarMenuItem::DrawBar(bool force)
55 {
56 	bool selected = IsSelected();
57 	BRect frame = Frame();
58 	BMenu* menu = Menu();
59 	frame.right -= 24;
60 	frame.left = frame.right-kBarWidth;
61 	frame.top += 3;
62 	frame.bottom = frame.top+8;
63 	if (fKernel < 0)
64 		return;
65 	if (fGrenze1 < 0)
66 		force = true;
67 	if (force) {
68 		if (selected)
69 			menu->SetHighColor(gFrameColorSelected);
70 		else
71 			menu->SetHighColor(gFrameColor);
72 		menu->StrokeRect(frame);
73 	}
74 	frame.InsetBy(1, 1);
75 	BRect r = frame;
76 	float grenze1 = frame.left+(frame.right-frame.left)*fKernel;
77 	float grenze2 = frame.left+(frame.right-frame.left)*(fKernel+fUser);
78 	if (grenze1 > frame.right)
79 		grenze1 = frame.right;
80 	if (grenze2 > frame.right)
81 		grenze2 = frame.right;
82 	r.right = grenze1;
83 	if (!force)
84 		r.left = fGrenze1;
85 	if (r.left < r.right) {
86 		if (selected)
87 			menu->SetHighColor(gKernelColorSelected);
88 		else
89 			menu->SetHighColor(gKernelColor);
90 		menu->FillRect(r);
91 	}
92 	r.left = grenze1;
93 	r.right = grenze2;
94 	if (!force) {
95 		if (fGrenze2 > r.left && r.left >= fGrenze1)
96 			r.left = fGrenze2;
97 		if (fGrenze1 < r.right && r.right  <=  fGrenze2)
98 			r.right = fGrenze1;
99 	}
100 	if (r.left < r.right) {
101 		if (selected)
102 			menu->SetHighColor(fThreadID <= gCPUcount ? gIdleColorSelected : gUserColorSelected);
103 		else
104 			menu->SetHighColor(fThreadID <= gCPUcount ? gIdleColor : gUserColor);
105 		menu->FillRect(r);
106 	}
107 	r.left = grenze2;
108 	r.right = frame.right;
109 	if (!force)
110 		r.right = fGrenze2;
111 	if (r.left < r.right) {
112 		if (selected)
113 			menu->SetHighColor(gWhiteSelected);
114 		else
115 			menu->SetHighColor(kWhite);
116 		menu->FillRect(r);
117 	}
118 	menu->SetHighColor(kBlack);
119 	fGrenze1 = grenze1;
120 	fGrenze2 = grenze2;
121 }
122 
123 
124 void
125 ThreadBarMenuItem::GetContentSize(float* width, float* height)
126 {
127 	BMenuItem::GetContentSize(width, height);
128 //	if (*height < 16)
129 //		*height = 16;
130 	*width += 10+kBarWidth;
131 }
132 
133 
134 void
135 ThreadBarMenuItem::Highlight(bool on)
136 {
137 	if (on) {
138 		PriorityMenu * popup = (PriorityMenu *) Submenu ();
139 		if (popup)
140 			popup->Update (fThreadInfo.priority);
141 	}
142 	BMenuItem::Highlight (on);
143 }
144 
145 
146 void
147 ThreadBarMenuItem::BarUpdate()
148 {
149 	thread_info info;
150 	if (get_thread_info(fThreadID, &info) == B_OK) {
151 		bigtime_t now = system_time();
152 		fKernel = double(info.kernel_time-fThreadInfo.kernel_time)/double(now-fLastTime);
153 		fUser = double(info.user_time-fThreadInfo.user_time)/double(now-fLastTime);
154 		if (fThreadID <= gCPUcount) {
155 			fUser += fKernel;
156 			fKernel = 0;
157 		}
158 		fThreadInfo.user_time = info.user_time;
159 		fThreadInfo.kernel_time = info.kernel_time;
160 		fLastTime = now;
161 		if (IsSelected ()) {
162 			PriorityMenu * popup = (PriorityMenu *) Submenu ();
163 			if (popup && info.priority != fThreadInfo.priority)
164 				popup->Update (info.priority);
165 		}
166 		fThreadInfo.priority = info.priority;
167 	} else
168 		fKernel = -1;
169 }
170 
171