Some cleanup and bug fixes: the length of the bits in BBitmap::SetBits()

was way too much, new[] was used, but not delete[].
The font size is now adapted to the processor/vendor strings, looking
much better now.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13371 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-06-30 23:05:55 +00:00
parent 793b31c5fd
commit 8bc4973900
2 changed files with 78 additions and 43 deletions
+60 -31
View File
@@ -24,6 +24,24 @@
#include <cpu_type.h> #include <cpu_type.h>
float
max_font_size(BFont font, const char* text, float maxSize, float maxWidth)
{
const float steps = 0.5f;
for (float size = maxSize; size > 4; size -= steps) {
font.SetSize(size);
if (font.StringWidth(text) <= maxWidth)
return size;
}
return 4;
}
// #pragma mark -
NormalPulseView::NormalPulseView(BRect rect) NormalPulseView::NormalPulseView(BRect rect)
: PulseView(rect, "NormalPulseView"), : PulseView(rect, "NormalPulseView"),
fHasBrandLogo(false) fHasBrandLogo(false)
@@ -40,13 +58,14 @@ NormalPulseView::NormalPulseView(BRect rect)
DetermineVendorAndProcessor(); DetermineVendorAndProcessor();
// Allocate progress bars and button pointers // Allocate progress bars and button pointers
system_info sys_info; system_info systemInfo;
get_system_info(&sys_info); get_system_info(&systemInfo);
fProgressBars = new ProgressBar *[sys_info.cpu_count]; fCpuCount = systemInfo.cpu_count;
fCpuButtons = new CPUButton *[sys_info.cpu_count]; fProgressBars = new ProgressBar *[fCpuCount];
fCpuButtons = new CPUButton *[fCpuCount];
// Set up the CPU activity bars and buttons // Set up the CPU activity bars and buttons
for (int x = 0; x < sys_info.cpu_count; x++) { for (int x = 0; x < fCpuCount; x++) {
BRect r(PROGRESS_MLEFT, PROGRESS_MTOP + ITEM_OFFSET * x, BRect r(PROGRESS_MLEFT, PROGRESS_MTOP + ITEM_OFFSET * x,
PROGRESS_MLEFT + ProgressBar::PROGRESS_WIDTH, PROGRESS_MLEFT + ProgressBar::PROGRESS_WIDTH,
PROGRESS_MTOP + ITEM_OFFSET * x + ProgressBar::PROGRESS_HEIGHT); PROGRESS_MTOP + ITEM_OFFSET * x + ProgressBar::PROGRESS_HEIGHT);
@@ -64,7 +83,7 @@ NormalPulseView::NormalPulseView(BRect rect)
// If there is only 1 cpu it will be hidden below // If there is only 1 cpu it will be hidden below
// thus, no need to add the dragger as it will still // thus, no need to add the dragger as it will still
// be visible when replicants are turned on // be visible when replicants are turned on
if (sys_info.cpu_count > 1) { if (fCpuCount > 1) {
BRect dragger_rect; BRect dragger_rect;
dragger_rect = r; dragger_rect = r;
dragger_rect.top = dragger_rect.bottom; dragger_rect.top = dragger_rect.bottom;
@@ -77,13 +96,34 @@ NormalPulseView::NormalPulseView(BRect rect)
} }
} }
if (sys_info.cpu_count == 1) { if (fCpuCount == 1) {
fProgressBars[0]->MoveBy(-3, 12); fProgressBars[0]->MoveBy(-3, 12);
fCpuButtons[0]->Hide(); fCpuButtons[0]->Hide();
} }
} }
NormalPulseView::~NormalPulseView()
{
delete fCpuLogo;
delete[] fCpuButtons;
delete[] fProgressBars;
}
void
NormalPulseView::CalculateFontSize()
{
BFont font;
GetFont(&font);
fProcessorFontSize = max_font_size(font, fProcessor, 11.0f, 46.0f);
if (!fHasBrandLogo)
fVendorFontSize = max_font_size(font, fVendor, 13.0f, 46.0f);
}
int int
NormalPulseView::CalculateCPUSpeed() NormalPulseView::CalculateCPUSpeed()
{ {
@@ -114,17 +154,18 @@ NormalPulseView::DetermineVendorAndProcessor()
get_system_info(&sys_info); get_system_info(&sys_info);
// Initialize logos // Initialize logos
BRect r(0, 0, 63, 62);
fCpuLogo = new BBitmap(r, B_COLOR_8_BIT); fCpuLogo = new BBitmap(BRect(0, 0, 63, 62), B_COLOR_8_BIT);
#if __POWERPC__ #if __POWERPC__
fCpuLogo->SetBits(Anim1, 11718, 0, B_COLOR_8_BIT); fCpuLogo->SetBits(Anim1, fCpuLogo->BitsLength(), 0, B_COLOR_8_BIT);
#endif #endif
#if __INTEL__ #if __INTEL__
if ((sys_info.cpu_type & B_CPU_x86_VENDOR_MASK) == B_CPU_INTEL_x86) { if ((sys_info.cpu_type & B_CPU_x86_VENDOR_MASK) == B_CPU_INTEL_x86) {
fCpuLogo->SetBits(IntelLogo, 11718, 0, B_COLOR_8_BIT); fCpuLogo->SetBits(IntelLogo, fCpuLogo->BitsLength(), 0, B_COLOR_8_BIT);
fHasBrandLogo = true; fHasBrandLogo = true;
} else } else
fCpuLogo->SetBits(BlankLogo, 11718, 0, B_COLOR_8_BIT); fCpuLogo->SetBits(BlankLogo, fCpuLogo->BitsLength(), 0, B_COLOR_8_BIT);
#endif #endif
get_cpu_type(fVendor, sizeof(fVendor), fProcessor, sizeof(fProcessor)); get_cpu_type(fVendor, sizeof(fVendor), fProcessor, sizeof(fProcessor));
@@ -161,10 +202,11 @@ NormalPulseView::Draw(BRect rect)
DrawBitmap(fCpuLogo, BPoint(10, 10)); DrawBitmap(fCpuLogo, BPoint(10, 10));
#if __INTEL__ #if __INTEL__
// Do nothing in the case of Intel CPUs - they already have a logo // Do nothing in the case of non-Intel CPUs - they already have a logo
if (!fHasBrandLogo) { if (!fHasBrandLogo) {
SetDrawingMode(B_OP_OVER); SetDrawingMode(B_OP_OVER);
SetHighColor(240,240,240); SetHighColor(240, 240, 240);
SetFontSize(fVendorFontSize);
float width = StringWidth(fVendor); float width = StringWidth(fVendor);
MovePenTo(10 + (32 - width / 2), 30); MovePenTo(10 + (32 - width / 2), 30);
@@ -177,6 +219,7 @@ NormalPulseView::Draw(BRect rect)
sprintf(buf, "%d MHz", CalculateCPUSpeed()); sprintf(buf, "%d MHz", CalculateCPUSpeed());
SetDrawingMode(B_OP_OVER); SetDrawingMode(B_OP_OVER);
SetHighColor(240, 240, 240); SetHighColor(240, 240, 240);
SetFontSize(fProcessorFontSize);
float width = StringWidth(fProcessor); float width = StringWidth(fProcessor);
MovePenTo(10 + (32 - width / 2), 48); MovePenTo(10 + (32 - width / 2), 48);
@@ -197,11 +240,8 @@ NormalPulseView::Pulse()
if (!IsHidden()) { if (!IsHidden()) {
Update(); Update();
if (Window()->Lock()) { if (Window()->Lock()) {
system_info sys_info;
get_system_info(&sys_info);
// Set the value of each CPU bar // Set the value of each CPU bar
for (int x = 0; x < sys_info.cpu_count; x++) { for (int x = 0; x < fCpuCount; x++) {
fProgressBars[x]->Set(max_c(0, cpu_times[x] * 100)); fProgressBars[x]->Set(max_c(0, cpu_times[x] * 100));
} }
@@ -215,13 +255,9 @@ NormalPulseView::Pulse()
void void
NormalPulseView::AttachedToWindow() NormalPulseView::AttachedToWindow()
{ {
// Use a smaller font on x86 to accomodate longer processor names
SetFont(be_bold_font); SetFont(be_bold_font);
#if __INTEL__ CalculateFontSize();
SetFontSize(7);
#else
SetFontSize(9);
#endif
fPreviousTime = system_time(); fPreviousTime = system_time();
BMessenger messenger(Window()); BMessenger messenger(Window());
@@ -254,10 +290,3 @@ NormalPulseView::UpdateColors(BMessage *message)
} }
} }
NormalPulseView::~NormalPulseView()
{
delete fCpuLogo;
delete fCpuButtons;
delete fProgressBars;
}
+10 -4
View File
@@ -19,22 +19,28 @@
class NormalPulseView : public PulseView { class NormalPulseView : public PulseView {
public: public:
NormalPulseView(BRect rect); NormalPulseView(BRect rect);
~NormalPulseView(); virtual ~NormalPulseView();
void Draw(BRect rect);
void Pulse(); virtual void Draw(BRect rect);
void AttachedToWindow(); virtual void Pulse();
virtual void AttachedToWindow();
void UpdateColors(BMessage *message); void UpdateColors(BMessage *message);
private: private:
int CalculateCPUSpeed(); int CalculateCPUSpeed();
void DetermineVendorAndProcessor(); void DetermineVendorAndProcessor();
void CalculateFontSize();
char fVendor[32], fProcessor[32]; char fVendor[32], fProcessor[32];
bigtime_t fPreviousTime; bigtime_t fPreviousTime;
ProgressBar **fProgressBars; ProgressBar **fProgressBars;
CPUButton **fCpuButtons; CPUButton **fCpuButtons;
BBitmap *fCpuLogo; BBitmap *fCpuLogo;
int32 fCpuCount;
bool fHasBrandLogo; bool fHasBrandLogo;
float fVendorFontSize, fProcessorFontSize;
}; };
#endif #endif