repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / processcontroller / ThreadBarMenuItem.cpp
blob8845dcb5ec71a43e0cd3402c39c2751582dacc9b
1 /*
2 ProcessController © 2000, Georges-Edouard Berenger, All Rights Reserved.
3 Copyright (C) 2004 beunited.org
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.
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.
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
20 #include "ThreadBarMenuItem.h"
22 #include "Colors.h"
23 #include "PriorityMenu.h"
24 #include "ProcessController.h"
26 #include <stdio.h>
29 ThreadBarMenuItem::ThreadBarMenuItem(const char* title, thread_id thread,
30 BMenu *menu, BMessage* msg)
31 : BMenuItem(menu, msg), fThreadID(thread)
33 SetLabel(title);
34 get_thread_info(fThreadID, &fThreadInfo);
35 fLastTime = system_time();
36 fKernel = -1;
37 fGrenze1 = -1;
38 fGrenze2 = -1;
42 void
43 ThreadBarMenuItem::DrawContent()
45 if (fKernel < 0)
46 BarUpdate();
47 DrawBar(true);
48 Menu()->MovePenTo(ContentLocation());
49 BMenuItem::DrawContent();
53 void
54 ThreadBarMenuItem::DrawBar(bool force)
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;
67 if (fGrenze1 < 0)
68 force = true;
70 if (force) {
71 if (selected)
72 menu->SetHighColor(gFrameColorSelected);
73 else
74 menu->SetHighColor(gFrameColor);
75 menu->StrokeRect(frame);
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;
88 if (!force)
89 r.left = fGrenze1;
91 if (r.left < r.right) {
92 if (selected)
93 menu->SetHighColor(gKernelColorSelected);
94 else
95 menu->SetHighColor(gKernelColor);
96 menu->FillRect(r);
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;
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;
113 if (selected) {
114 menu->SetHighColor(
115 idleThread ? gIdleColorSelected : gUserColorSelected);
116 } else
117 menu->SetHighColor(idleThread ? gIdleColor : gUserColor);
118 menu->FillRect(r);
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);
131 menu->SetHighColor(highColor);
132 fGrenze1 = grenze1;
133 fGrenze2 = grenze2;
137 void
138 ThreadBarMenuItem::GetContentSize(float* width, float* height)
140 BMenuItem::GetContentSize(width, height);
141 // if (*height < 16)
142 // *height = 16;
143 *width += 10 + kBarWidth;
147 void
148 ThreadBarMenuItem::Highlight(bool on)
150 if (on) {
151 PriorityMenu* popup = (PriorityMenu*)Submenu();
152 if (popup)
153 popup->Update(fThreadInfo.priority);
155 BMenuItem::Highlight(on);
159 void
160 ThreadBarMenuItem::BarUpdate()
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;
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);
179 fThreadInfo.priority = info.priority;
180 } else
181 fKernel = -1;