xref: /haiku/src/apps/processcontroller/KernelMemoryBarMenuItem.cpp (revision 2069f565e8ed3556b30cf2f5e3aa54450128b8c2)
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 
21 #include "KernelMemoryBarMenuItem.h"
22 
23 #include "Colors.h"
24 #include "MemoryBarMenu.h"
25 #include "ProcessController.h"
26 
27 #include <stdio.h>
28 
29 
30 KernelMemoryBarMenuItem::KernelMemoryBarMenuItem(system_info& systemInfo)
31 	: BMenuItem("System resources & caches" B_UTF8_ELLIPSIS, NULL)
32 {
33 	fLastSum = -1;
34 	fGrenze1 = -1;
35 	fGrenze2 = -1;
36 	fPhysicalMemory = float(int(systemInfo.max_pages * B_PAGE_SIZE / 1024));
37 	fCommittedMemory = float(int(systemInfo.used_pages * B_PAGE_SIZE / 1024));
38 	fCachedMemory = float(int(systemInfo.cached_pages * B_PAGE_SIZE / 1024));
39 }
40 
41 
42 void
43 KernelMemoryBarMenuItem::DrawContent()
44 {
45 	DrawBar(true);
46 	Menu()->MovePenTo(ContentLocation());
47 	BMenuItem::DrawContent();
48 }
49 
50 
51 void
52 KernelMemoryBarMenuItem::UpdateSituation(float committedMemory,
53 	float cachedMemory)
54 {
55 	fCommittedMemory = committedMemory;
56 	fCachedMemory = cachedMemory;
57 	DrawBar(false);
58 }
59 
60 
61 void
62 KernelMemoryBarMenuItem::DrawBar(bool force)
63 {
64 	bool selected = IsSelected();
65 	BRect frame = Frame();
66 	BMenu* menu = Menu();
67 
68 	// draw the bar itself
69 	BRect cadre (frame.right - kMargin - kBarWidth, frame.top + 5,
70 		frame.right - kMargin, frame.top + 13);
71 
72 	if (fLastSum < 0)
73 		force = true;
74 	if (force) {
75 		if (selected)
76 			menu->SetHighColor(gFrameColorSelected);
77 		else
78 			menu->SetHighColor(gFrameColor);
79 		menu->StrokeRect (cadre);
80 	}
81 	cadre.InsetBy(1, 1);
82 	BRect r = cadre;
83 
84 	float grenze1 = cadre.left + (cadre.right - cadre.left) * fCachedMemory / fPhysicalMemory;
85 	float grenze2 = cadre.left + (cadre.right - cadre.left) * fCommittedMemory / fPhysicalMemory;
86 	if (grenze1 > cadre.right)
87 		grenze1 = cadre.right;
88 	if (grenze2 > cadre.right)
89 		grenze2 = cadre.right;
90 	r.right = grenze1;
91 	if (!force)
92 		r.left = fGrenze1;
93 	if (r.left < r.right) {
94 		if (selected)
95 			menu->SetHighColor(gKernelColorSelected);
96 		else
97 			menu->SetHighColor(gKernelColor);
98 //		menu->SetHighColor(gKernelColor);
99 		menu->FillRect (r);
100 	}
101 	r.left = grenze1;
102 	r.right = grenze2;
103 	if (!force) {
104 		if (fGrenze2 > r.left && r.left >= fGrenze1)
105 			r.left = fGrenze2;
106 		if (fGrenze1 < r.right && r.right <= fGrenze2)
107 			r.right = fGrenze1;
108 	}
109 	if (r.left < r.right) {
110 		if (selected)
111 			menu->SetHighColor(tint_color (kLavender, B_HIGHLIGHT_BACKGROUND_TINT));
112 		else
113 			menu->SetHighColor(kLavender);
114 //		menu->SetHighColor(gUserColor);
115 		menu->FillRect (r);
116 	}
117 	r.left = grenze2;
118 	r.right = cadre.right;
119 	if (!force)
120 		r.right = fGrenze2;
121 	if (r.left < r.right) {
122 		if (selected)
123 			menu->SetHighColor(gWhiteSelected);
124 		else
125 			menu->SetHighColor(kWhite);
126 		menu->FillRect(r);
127 	}
128 	menu->SetHighColor(kBlack);
129 	fGrenze1 = grenze1;
130 	fGrenze2 = grenze2;
131 
132 	// draw the value
133 	double sum = fCachedMemory * FLT_MAX + fCommittedMemory;
134 	if (force || sum != fLastSum) {
135 		if (selected) {
136 			menu->SetLowColor(gMenuBackColorSelected);
137 			menu->SetHighColor(gMenuBackColorSelected);
138 		} else {
139 			menu->SetLowColor(gMenuBackColor);
140 			menu->SetHighColor(gMenuBackColor);
141 		}
142 		BRect trect(cadre.left - kMargin - gMemoryTextWidth, frame.top,
143 			cadre.left - kMargin, frame.bottom);
144 		menu->FillRect(trect);
145 		menu->SetHighColor(kBlack);
146 
147 		char infos[128];
148 		sprintf(infos, "%.1f MB", fCachedMemory / 1024.f);
149 		BPoint loc(cadre.left - kMargin - gMemoryTextWidth / 2 - menu->StringWidth(infos),
150 			cadre.bottom + 1);
151 		menu->DrawString(infos, loc);
152 		sprintf(infos, "%.1f MB", fCommittedMemory / 1024.f);
153 		loc.x = cadre.left - kMargin - menu->StringWidth(infos);
154 		menu->DrawString(infos, loc);
155 		fLastSum = sum;
156 	}
157 }
158 
159 
160 void
161 KernelMemoryBarMenuItem::GetContentSize(float* _width, float* _height)
162 {
163 	BMenuItem::GetContentSize(_width, _height);
164 	if (*_height < 16)
165 		*_height = 16;
166 
167 	*_width += 20 + kBarWidth + kMargin + gMemoryTextWidth;
168 }
169