repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / processcontroller / KernelMemoryBarMenuItem.cpp
blob30b5fe0a1a9291a94a6c5292b60d354d3319b7d6
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
21 #include "KernelMemoryBarMenuItem.h"
23 #include "Colors.h"
24 #include "MemoryBarMenu.h"
25 #include "ProcessController.h"
27 #include <stdio.h>
29 #include <Catalog.h>
30 #include <StringForSize.h>
32 #undef B_TRANSLATION_CONTEXT
33 #define B_TRANSLATION_CONTEXT "ProcessController"
36 KernelMemoryBarMenuItem::KernelMemoryBarMenuItem(system_info& systemInfo)
37 : BMenuItem(B_TRANSLATE("System resources & caches" B_UTF8_ELLIPSIS), NULL)
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;
48 void
49 KernelMemoryBarMenuItem::DrawContent()
51 DrawBar(true);
52 Menu()->MovePenTo(ContentLocation());
53 BMenuItem::DrawContent();
57 void
58 KernelMemoryBarMenuItem::UpdateSituation(int64 committedMemory,
59 int64 cachedMemory)
61 fCommittedMemory = committedMemory;
62 fCachedMemory = cachedMemory;
63 DrawBar(false);
67 void
68 KernelMemoryBarMenuItem::DrawBar(bool force)
70 bool selected = IsSelected();
71 BRect frame = Frame();
72 BMenu* menu = Menu();
73 rgb_color highColor = menu->HighColor();
75 // draw the bar itself
76 BRect cadre (frame.right - kMargin - kBarWidth, frame.top + 5,
77 frame.right - kMargin, frame.top + 13);
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);
88 cadre.InsetBy(1, 1);
89 BRect r = cadre;
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);
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;
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);
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);
137 menu->SetHighColor(highColor);
138 fGrenze1 = grenze1;
139 fGrenze2 = grenze2;
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);
151 BRect trect(cadre.left - kMargin - gMemoryTextWidth, frame.top,
152 cadre.left - kMargin, frame.bottom);
153 menu->FillRect(trect);
154 menu->SetHighColor(highColor);
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;
169 void
170 KernelMemoryBarMenuItem::GetContentSize(float* _width, float* _height)
172 BMenuItem::GetContentSize(_width, _height);
173 if (*_height < 16)
174 *_height = 16;
176 *_width += 20 + kBarWidth + kMargin + gMemoryTextWidth;