xref: /haiku/src/apps/processcontroller/KernelMemoryBarMenuItem.cpp (revision 9ecf9d1c1d4888d341a6eac72112c72d1ae3a4cb)
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...", NULL)
32 {
33 	fTotalWriteMemory = -1;
34 	fLastSum = -1;
35 	fGrenze1 = -1;
36 	fGrenze2 = -1;
37 	fPhsysicalMemory = float(int(systemInfo.max_pages * B_PAGE_SIZE / 1024));
38 	fCommitedMemory = float(int(systemInfo.used_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 commitedMemory,
53 	float totalWriteMemory)
54 {
55 	if (commitedMemory < totalWriteMemory)
56 		totalWriteMemory = commitedMemory;
57 
58 	fCommitedMemory = commitedMemory;
59 	fTotalWriteMemory = totalWriteMemory;
60 	DrawBar(false);
61 }
62 
63 
64 void
65 KernelMemoryBarMenuItem::DrawBar(bool force)
66 {
67 	bool selected = IsSelected();
68 	BRect frame = Frame();
69 	BMenu* menu = Menu();
70 
71 	// draw the bar itself
72 	BRect cadre (frame.right - kMargin - kBarWidth, frame.top + 5,
73 		frame.right - kMargin, frame.top + 13);
74 	if (fTotalWriteMemory < 0)
75 		return;
76 
77 	if (fLastSum < 0)
78 		force = true;
79 	if (force) {
80 		if (selected)
81 			menu->SetHighColor(gFrameColorSelected);
82 		else
83 			menu->SetHighColor(gFrameColor);
84 		menu->StrokeRect (cadre);
85 	}
86 	cadre.InsetBy(1, 1);
87 	BRect r = cadre;
88 
89 	float grenze1 = cadre.left + (cadre.right - cadre.left) * fTotalWriteMemory / fPhsysicalMemory;
90 	float grenze2 = cadre.left + (cadre.right - cadre.left) * fCommitedMemory / fPhsysicalMemory;
91 	if (grenze1 > cadre.right)
92 		grenze1 = cadre.right;
93 	if (grenze2 > cadre.right)
94 		grenze2 = cadre.right;
95 	r.right = grenze1;
96 	if (!force)
97 		r.left = fGrenze1;
98 	if (r.left < r.right) {
99 		if (selected)
100 			menu->SetHighColor(gKernelColorSelected);
101 		else
102 			menu->SetHighColor(gKernelColor);
103 //		menu->SetHighColor(gKernelColor);
104 		menu->FillRect (r);
105 	}
106 	r.left = grenze1;
107 	r.right = grenze2;
108 	if (!force) {
109 		if (fGrenze2 > r.left && r.left >= fGrenze1)
110 			r.left = fGrenze2;
111 		if (fGrenze1 < r.right && r.right <= fGrenze2)
112 			r.right = fGrenze1;
113 	}
114 	if (r.left < r.right) {
115 		if (selected)
116 			menu->SetHighColor(tint_color (kLavender, B_HIGHLIGHT_BACKGROUND_TINT));
117 		else
118 			menu->SetHighColor(kLavender);
119 //		menu->SetHighColor(gUserColor);
120 		menu->FillRect (r);
121 	}
122 	r.left = grenze2;
123 	r.right = cadre.right;
124 	if (!force)
125 		r.right = fGrenze2;
126 	if (r.left < r.right) {
127 		if (selected)
128 			menu->SetHighColor(gWhiteSelected);
129 		else
130 			menu->SetHighColor(kWhite);
131 		menu->FillRect(r);
132 	}
133 	menu->SetHighColor(kBlack);
134 	fGrenze1 = grenze1;
135 	fGrenze2 = grenze2;
136 
137 	// draw the value
138 	double sum = fTotalWriteMemory * FLT_MAX + fCommitedMemory;
139 	if (force || sum != fLastSum) {
140 		if (selected) {
141 			menu->SetLowColor(gMenuBackColorSelected);
142 			menu->SetHighColor(gMenuBackColorSelected);
143 		} else {
144 			menu->SetLowColor(gMenuBackColor);
145 			menu->SetHighColor(gMenuBackColor);
146 		}
147 		BRect trect(cadre.left - kMargin - gMemoryTextWidth, frame.top,
148 			cadre.left - kMargin, frame.bottom);
149 		menu->FillRect(trect);
150 		menu->SetHighColor(kBlack);
151 
152 		char infos[128];
153 		sprintf(infos, "%.1f MB", fTotalWriteMemory / 1024.f);
154 		BPoint loc(cadre.left - kMargin - gMemoryTextWidth / 2 - menu->StringWidth(infos),
155 			cadre.bottom + 1);
156 		menu->DrawString(infos, loc);
157 		sprintf(infos, "%.1f MB", fCommitedMemory / 1024.f);
158 		loc.x = cadre.left - kMargin - menu->StringWidth(infos);
159 		menu->DrawString(infos, loc);
160 		fLastSum = sum;
161 	}
162 }
163 
164 
165 void
166 KernelMemoryBarMenuItem::GetContentSize(float* _width, float* _height)
167 {
168 	BMenuItem::GetContentSize(_width, _height);
169 	if (*_height < 16)
170 		*_height = 16;
171 
172 	*_width += 20 + kBarWidth + kMargin + gMemoryTextWidth;
173 }
174