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 BFont font; 76 menu->GetFont(&font); 77 BRect cadre = bar_rect(frame, &font); 78 79 // draw the bar itself 80 if (fLastSum < 0) 81 force = true; 82 if (force) { 83 if (selected) 84 menu->SetHighColor(gFrameColorSelected); 85 else 86 menu->SetHighColor(gFrameColor); 87 menu->StrokeRect (cadre); 88 } 89 cadre.InsetBy(1, 1); 90 BRect r = cadre; 91 92 double grenze1 = cadre.left + (cadre.right - cadre.left) 93 * fCachedMemory / fPhysicalMemory; 94 double grenze2 = cadre.left + (cadre.right - cadre.left) 95 * fCommittedMemory / fPhysicalMemory; 96 if (grenze1 > cadre.right) 97 grenze1 = cadre.right; 98 if (grenze2 > cadre.right) 99 grenze2 = cadre.right; 100 r.right = grenze1; 101 if (!force) 102 r.left = fGrenze1; 103 if (r.left < r.right) { 104 if (selected) 105 menu->SetHighColor(gKernelColorSelected); 106 else 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->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(highColor); 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(highColor); 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