xref: /haiku/src/apps/processcontroller/KernelMemoryBarMenuItem.cpp (revision 579f1dbca962a2a03df54f69fdc6e9423f91f20e)
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 = systemInfo.max_pages * B_PAGE_SIZE / 1024LL;
43 	fCommittedMemory = systemInfo.used_pages * B_PAGE_SIZE / 1024LL;
44 	fCachedMemory = 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 
74 	// draw the bar itself
75 	BRect cadre (frame.right - kMargin - kBarWidth, frame.top + 5,
76 		frame.right - kMargin, frame.top + 13);
77 
78 	if (fLastSum < 0)
79 		force = true;
80 	if (force) {
81 		if (selected)
82 			menu->SetHighColor(gFrameColorSelected);
83 		else
84 			menu->SetHighColor(gFrameColor);
85 		menu->StrokeRect (cadre);
86 	}
87 	cadre.InsetBy(1, 1);
88 	BRect r = cadre;
89 
90 	double grenze1 = cadre.left + (cadre.right - cadre.left)
91 						* fCachedMemory / fPhysicalMemory;
92 	double grenze2 = cadre.left + (cadre.right - cadre.left)
93 						* fCommittedMemory / fPhysicalMemory;
94 	if (grenze1 > cadre.right)
95 		grenze1 = cadre.right;
96 	if (grenze2 > cadre.right)
97 		grenze2 = cadre.right;
98 	r.right = grenze1;
99 	if (!force)
100 		r.left = fGrenze1;
101 	if (r.left < r.right) {
102 		if (selected)
103 			menu->SetHighColor(gKernelColorSelected);
104 		else
105 			menu->SetHighColor(gKernelColor);
106 //		menu->SetHighColor(gKernelColor);
107 		menu->FillRect (r);
108 	}
109 	r.left = grenze1;
110 	r.right = grenze2;
111 	if (!force) {
112 		if (fGrenze2 > r.left && r.left >= fGrenze1)
113 			r.left = fGrenze2;
114 		if (fGrenze1 < r.right && r.right <= fGrenze2)
115 			r.right = fGrenze1;
116 	}
117 	if (r.left < r.right) {
118 		if (selected)
119 			menu->SetHighColor(tint_color (kLavender, B_HIGHLIGHT_BACKGROUND_TINT));
120 		else
121 			menu->SetHighColor(kLavender);
122 //		menu->SetHighColor(gUserColor);
123 		menu->FillRect (r);
124 	}
125 	r.left = grenze2;
126 	r.right = cadre.right;
127 	if (!force)
128 		r.right = fGrenze2;
129 	if (r.left < r.right) {
130 		if (selected)
131 			menu->SetHighColor(gWhiteSelected);
132 		else
133 			menu->SetHighColor(kWhite);
134 		menu->FillRect(r);
135 	}
136 	menu->SetHighColor(kBlack);
137 	fGrenze1 = grenze1;
138 	fGrenze2 = grenze2;
139 
140 	// draw the value
141 	double sum = fCachedMemory * FLT_MAX + fCommittedMemory;
142 	if (force || sum != fLastSum) {
143 		if (selected) {
144 			menu->SetLowColor(gMenuBackColorSelected);
145 			menu->SetHighColor(gMenuBackColorSelected);
146 		} else {
147 			menu->SetLowColor(gMenuBackColor);
148 			menu->SetHighColor(gMenuBackColor);
149 		}
150 		BRect trect(cadre.left - kMargin - gMemoryTextWidth, frame.top,
151 			cadre.left - kMargin, frame.bottom);
152 		menu->FillRect(trect);
153 		menu->SetHighColor(kBlack);
154 
155 		char infos[128];
156 		string_for_size(fCachedMemory * 1024.0, infos, sizeof(infos));
157 		BPoint loc(cadre.left, cadre.bottom + 1);
158 		loc.x -= kMargin + gMemoryTextWidth / 2 + menu->StringWidth(infos);
159 		menu->DrawString(infos, loc);
160 		string_for_size(fCommittedMemory * 1024.0, infos, sizeof(infos));
161 		loc.x = cadre.left - kMargin - menu->StringWidth(infos);
162 		menu->DrawString(infos, loc);
163 		fLastSum = sum;
164 	}
165 }
166 
167 
168 void
169 KernelMemoryBarMenuItem::GetContentSize(float* _width, float* _height)
170 {
171 	BMenuItem::GetContentSize(_width, _height);
172 	if (*_height < 16)
173 		*_height = 16;
174 
175 	*_width += 20 + kBarWidth + kMargin + gMemoryTextWidth;
176 }
177 
178