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