xref: /haiku/src/apps/processcontroller/ThreadBarMenuItem.cpp (revision 97dfeb96704e5dbc5bec32ad7b21379d0125e031)
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 	rgb_color highColor = menu->HighColor();
60 	frame.right -= 24;
61 	frame.left = frame.right - kBarWidth;
62 	frame.top += 3;
63 	frame.bottom = frame.top + 8;
64 	if (fKernel < 0)
65 		return;
66 
67 	if (fGrenze1 < 0)
68 		force = true;
69 
70 	if (force) {
71 		if (selected)
72 			menu->SetHighColor(gFrameColorSelected);
73 		else
74 			menu->SetHighColor(gFrameColor);
75 		menu->StrokeRect(frame);
76 	}
77 
78 	frame.InsetBy(1, 1);
79 	BRect r = frame;
80 	float grenze1 = frame.left + (frame.right - frame.left) * fKernel;
81 	float grenze2 = frame.left + (frame.right - frame.left) * (fKernel + fUser);
82 	if (grenze1 > frame.right)
83 		grenze1 = frame.right;
84 	if (grenze2 > frame.right)
85 		grenze2 = frame.right;
86 	r.right = grenze1;
87 
88 	if (!force)
89 		r.left = fGrenze1;
90 
91 	if (r.left < r.right) {
92 		if (selected)
93 			menu->SetHighColor(gKernelColorSelected);
94 		else
95 			menu->SetHighColor(gKernelColor);
96 		menu->FillRect(r);
97 	}
98 
99 	r.left = grenze1;
100 	r.right = grenze2;
101 	if (!force) {
102 		if (fGrenze2 > r.left && r.left >= fGrenze1)
103 			r.left = fGrenze2;
104 		if (fGrenze1 < r.right && r.right  <=  fGrenze2)
105 			r.right = fGrenze1;
106 	}
107 	if (r.left < r.right) {
108 		thread_info threadInfo;
109 		bool idleThread = false;
110 		if (get_thread_info(fThreadID, &threadInfo) == B_OK)
111 			idleThread = threadInfo.priority == B_IDLE_PRIORITY;
112 
113 		if (selected) {
114 			menu->SetHighColor(
115 				idleThread ? gIdleColorSelected : gUserColorSelected);
116 		} else
117 			menu->SetHighColor(idleThread ? gIdleColor : gUserColor);
118 		menu->FillRect(r);
119 	}
120 	r.left = grenze2;
121 	r.right = frame.right;
122 	if (!force)
123 		r.right = fGrenze2;
124 	if (r.left < r.right) {
125 		if (selected)
126 			menu->SetHighColor(gWhiteSelected);
127 		else
128 			menu->SetHighColor(kWhite);
129 		menu->FillRect(r);
130 	}
131 	menu->SetHighColor(highColor);
132 	fGrenze1 = grenze1;
133 	fGrenze2 = grenze2;
134 }
135 
136 
137 void
138 ThreadBarMenuItem::GetContentSize(float* width, float* height)
139 {
140 	BMenuItem::GetContentSize(width, height);
141 //	if (*height < 16)
142 //		*height = 16;
143 	*width += 10 + kBarWidth;
144 }
145 
146 
147 void
148 ThreadBarMenuItem::Highlight(bool on)
149 {
150 	if (on) {
151 		PriorityMenu* popup = (PriorityMenu*)Submenu();
152 		if (popup)
153 			popup->Update(fThreadInfo.priority);
154 	}
155 	BMenuItem::Highlight(on);
156 }
157 
158 
159 void
160 ThreadBarMenuItem::BarUpdate()
161 {
162 	thread_info info;
163 	if (get_thread_info(fThreadID, &info) == B_OK) {
164 		bigtime_t now = system_time();
165 		fKernel = double(info.kernel_time - fThreadInfo.kernel_time) / double(now - fLastTime);
166 		fUser = double(info.user_time - fThreadInfo.user_time) / double(now - fLastTime);
167 		if (info.priority == B_IDLE_PRIORITY) {
168 			fUser += fKernel;
169 			fKernel = 0;
170 		}
171 		fThreadInfo.user_time = info.user_time;
172 		fThreadInfo.kernel_time = info.kernel_time;
173 		fLastTime = now;
174 		if (IsSelected ()) {
175 			PriorityMenu* popup = (PriorityMenu*)Submenu();
176 			if (popup && info.priority != fThreadInfo.priority)
177 				popup->Update (info.priority);
178 		}
179 		fThreadInfo.priority = info.priority;
180 	} else
181 		fKernel = -1;
182 }
183