* Fixed memory leak in CPUButton: the message runner was only deleted in the

destructor, but created everytime AttachedToWindow() was called.
* Minor cleanups.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@16184 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2006-02-01 15:57:45 +00:00
parent a71c960755
commit 4ccfb08661
5 changed files with 181 additions and 86 deletions

View File

@ -8,6 +8,7 @@
//
//****************************************************************************************
#include "CPUButton.h"
#include "PulseApp.h"
#include "PulseView.h"
@ -15,27 +16,47 @@
#include <interface/Alert.h>
#include <stdlib.h>
CPUButton::CPUButton(BRect rect, const char *name, const char *label, BMessage *message) :
BControl(rect, name, label, message, B_FOLLOW_NONE, B_WILL_DRAW) {
off_color.red = off_color.green = off_color.blue = 184;
off_color.alpha = 255;
CPUButton::CPUButton(BRect rect, const char *name, const char *label, BMessage *message)
: BControl(rect, name, label, message, B_FOLLOW_NONE, B_WILL_DRAW)
{
SetValue(B_CONTROL_ON);
SetViewColor(B_TRANSPARENT_COLOR);
replicant = false;
fReplicant = false;
_InitData();
}
CPUButton::CPUButton(BMessage *message) : BControl(message) {
off_color.red = off_color.green = off_color.blue = 184;
off_color.alpha = 255;
replicant = true;
CPUButton::CPUButton(BMessage *message)
: BControl(message)
{
fReplicant = true;
_InitData();
}
// Redraw the button depending on whether it's up or down
void CPUButton::Draw(BRect rect) {
CPUButton::~CPUButton()
{
}
void
CPUButton::_InitData()
{
fOffColor.red = fOffColor.green = fOffColor.blue = 184;
fOffColor.alpha = 255;
fCPU = atoi(Label()) - 1;
}
//! Redraw the button depending on whether it's up or down
void
CPUButton::Draw(BRect rect)
{
bool value = (bool)Value();
if (value) SetHighColor(on_color);
else SetHighColor(off_color);
SetHighColor(value ? fOnColor : fOffColor);
BRect bounds = Bounds();
BRect color_rect(bounds);
@ -45,39 +66,51 @@ void CPUButton::Draw(BRect rect) {
color_rect.right -= 1;
}
FillRect(bounds);
if (value) SetHighColor(80, 80, 80);
else SetHighColor(255, 255, 255);
if (value)
SetHighColor(80, 80, 80);
else
SetHighColor(255, 255, 255);
BPoint start(0, 0);
BPoint end(bounds.right, 0);
StrokeLine(start, end);
end.Set(0, bounds.bottom);
StrokeLine(start, end);
if (value) SetHighColor(32, 32, 32);
else SetHighColor(216, 216, 216);
if (value)
SetHighColor(32, 32, 32);
else
SetHighColor(216, 216, 216);
start.Set(1, 1);
end.Set(bounds.right - 1, 1);
StrokeLine(start, end);
end.Set(1, bounds.bottom - 1);
StrokeLine(start, end);
if (value) SetHighColor(216, 216, 216);
else SetHighColor(80, 80, 80);
if (value)
SetHighColor(216, 216, 216);
else
SetHighColor(80, 80, 80);
start.Set(bounds.left + 1, bounds.bottom - 1);
end.Set(bounds.right - 1, bounds.bottom - 1);
StrokeLine(start, end);
start.Set(bounds.right - 1, bounds.top + 1);
StrokeLine(start, end);
if (value) SetHighColor(255, 255, 255);
else SetHighColor(32, 32, 32);
if (value)
SetHighColor(255, 255, 255);
else
SetHighColor(32, 32, 32);
start.Set(bounds.left, bounds.bottom);
end.Set(bounds.right, bounds.bottom);
StrokeLine(start, end);
start.Set(bounds.right, bounds.top);
StrokeLine(start, end);
if (value) {
SetHighColor(0, 0, 0);
start.Set(bounds.left + 2, bounds.bottom - 2);
@ -86,7 +119,7 @@ void CPUButton::Draw(BRect rect) {
start.Set(bounds.right - 2, bounds.top + 2);
StrokeLine(start, end);
}
// Try to keep the text centered
BFont font;
GetFont(&font);
@ -98,36 +131,49 @@ void CPUButton::Draw(BRect rect) {
int label_height = (int)fh.ascent;
int x_pos = (int)(((double)(rect_width - label_width) / 2.0) + 0.5);
int y_pos = (rect_height - label_height) / 2 + label_height;
MovePenTo(x_pos, y_pos);
SetHighColor(0, 0, 0);
SetDrawingMode(B_OP_OVER);
DrawString(Label());
}
// Track the mouse without blocking the window
void CPUButton::MouseDown(BPoint point) {
//! Track the mouse without blocking the window
void
CPUButton::MouseDown(BPoint point)
{
SetValue(!Value());
SetTracking(true);
SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
}
void CPUButton::MouseUp(BPoint point) {
if (Bounds().Contains(point)) Invoke();
void
CPUButton::MouseUp(BPoint point)
{
if (Bounds().Contains(point))
Invoke();
SetTracking(false);
}
void CPUButton::MouseMoved(BPoint point, uint32 transit, const BMessage *message) {
void
CPUButton::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
{
if (IsTracking()) {
if (transit == B_ENTERED_VIEW || transit == B_EXITED_VIEW) SetValue(!Value());
if (transit == B_ENTERED_VIEW || transit == B_EXITED_VIEW)
SetValue(!Value());
}
}
status_t CPUButton::Invoke(BMessage *message) {
int my_cpu = atoi(Label()) - 1;
if (!LastEnabledCPU(my_cpu)) {
_kset_cpu_state_(my_cpu, Value());
status_t
CPUButton::Invoke(BMessage *message)
{
if (!LastEnabledCPU(fCPU)) {
_kset_cpu_state_(fCPU, Value());
} else {
BAlert *alert = new BAlert(NULL, "You can't disable the last active CPU.", "OK");
alert->Go(NULL);
@ -137,20 +183,31 @@ status_t CPUButton::Invoke(BMessage *message) {
return B_OK;
}
CPUButton *CPUButton::Instantiate(BMessage *data) {
if (!validate_instantiation(data, "CPUButton")) return NULL;
CPUButton *
CPUButton::Instantiate(BMessage *data)
{
if (!validate_instantiation(data, "CPUButton"))
return NULL;
return new CPUButton(data);
}
status_t CPUButton::Archive(BMessage *data, bool deep) const {
status_t
CPUButton::Archive(BMessage *data, bool deep) const
{
BControl::Archive(data, deep);
data->AddString("add_on", APP_SIGNATURE);
data->AddString("class", "CPUButton");
return B_OK;
}
void CPUButton::MessageReceived(BMessage *message) {
switch(message->what) {
void
CPUButton::MessageReceived(BMessage *message)
{
switch (message->what) {
case B_ABOUT_REQUESTED: {
BAlert *alert = new BAlert("Info", "Pulse\n\nBy David Ramsey and Arve Hjønnevåg\nRevised by Daniel Switkin", "OK");
// Use the asynchronous version so we don't block the window's thread
@ -159,8 +216,8 @@ void CPUButton::MessageReceived(BMessage *message) {
}
case PV_REPLICANT_PULSE: {
// Make sure we're consistent with our CPU
int my_cpu = atoi(Label()) - 1;
if (_kget_cpu_state_(my_cpu) != Value() && !IsTracking()) SetValue(!Value());
if (_kget_cpu_state_(fCPU) != Value() && !IsTracking())
SetValue(!Value());
break;
}
default:
@ -169,19 +226,25 @@ void CPUButton::MessageReceived(BMessage *message) {
}
}
void CPUButton::UpdateColors(int32 color) {
on_color.red = (color & 0xff000000) >> 24;
on_color.green = (color & 0x00ff0000) >> 16;
on_color.blue = (color & 0x0000ff00) >> 8;
void
CPUButton::UpdateColors(int32 color)
{
fOnColor.red = (color & 0xff000000) >> 24;
fOnColor.green = (color & 0x00ff0000) >> 16;
fOnColor.blue = (color & 0x0000ff00) >> 8;
Draw(Bounds());
}
void CPUButton::AttachedToWindow() {
void
CPUButton::AttachedToWindow()
{
SetTarget(this);
SetFont(be_plain_font);
SetFontSize(10);
if (replicant) {
if (fReplicant) {
Prefs *prefs = new Prefs();
UpdateColors(prefs->normal_bar_color);
delete prefs;
@ -189,12 +252,16 @@ void CPUButton::AttachedToWindow() {
PulseApp *pulseapp = (PulseApp *)be_app;
UpdateColors(pulseapp->prefs->normal_bar_color);
}
BMessenger messenger(this);
messagerunner = new BMessageRunner(messenger, new BMessage(PV_REPLICANT_PULSE),
fPulseRunner = new BMessageRunner(messenger, new BMessage(PV_REPLICANT_PULSE),
200000, -1);
}
CPUButton::~CPUButton() {
delete messagerunner;
void
CPUButton::DetachedFromWindow()
{
delete fPulseRunner;
}

View File

@ -18,24 +18,30 @@ class CPUButton : public BControl {
public:
CPUButton(BRect rect, const char *name, const char *label, BMessage *message);
CPUButton(BMessage *message);
void Draw(BRect rect);
void MouseDown(BPoint point);
void MouseUp(BPoint point);
void MouseMoved(BPoint point, uint32 transit, const BMessage *message);
~CPUButton();
virtual ~CPUButton();
virtual void Draw(BRect rect);
virtual void MouseDown(BPoint point);
virtual void MouseUp(BPoint point);
virtual void MouseMoved(BPoint point, uint32 transit, const BMessage *message);
virtual void MessageReceived(BMessage *message);
virtual void AttachedToWindow();
virtual void DetachedFromWindow();
status_t Invoke(BMessage *message = NULL);
static CPUButton *Instantiate(BMessage *data);
status_t Archive(BMessage *data, bool deep = true) const;
void MessageReceived(BMessage *message);
void UpdateColors(int32 color);
void AttachedToWindow();
private:
rgb_color on_color, off_color;
bool replicant;
BMessageRunner *messagerunner;
void _InitData();
rgb_color fOnColor, fOffColor;
bool fReplicant;
int32 fCPU;
BMessageRunner *fPulseRunner;
};
#endif
#endif // CPUBUTTON_H

View File

@ -249,13 +249,13 @@ NormalPulseView::AttachedToWindow()
CalculateFontSize();
fPreviousTime = system_time();
BMessenger messenger(Window());
mode1->SetTarget(messenger);
mode2->SetTarget(messenger);
preferences->SetTarget(messenger);
about->SetTarget(messenger);
system_info sys_info;
get_system_info(&sys_info);
if (sys_info.cpu_count >= 2) {
@ -273,7 +273,7 @@ NormalPulseView::UpdateColors(BMessage *message)
bool fade = message->FindBool("fade");
system_info sys_info;
get_system_info(&sys_info);
for (int x = 0; x < sys_info.cpu_count; x++) {
fProgressBars[x]->UpdateColors(color, fade);
fCpuButtons[x]->UpdateColors(color);

View File

@ -8,10 +8,13 @@
//
//****************************************************************************************
#include "ProgressBar.h"
#include "PulseApp.h"
ProgressBar::ProgressBar(BRect r, char *name) : BView(r, name, B_FOLLOW_NONE, B_WILL_DRAW) {
ProgressBar::ProgressBar(BRect r, char *name) : BView(r, name, B_FOLLOW_NONE, B_WILL_DRAW)
{
previous_value = current_value = 0;
// Create 20 segments
@ -25,17 +28,20 @@ ProgressBar::ProgressBar(BRect r, char *name) : BView(r, name, B_FOLLOW_NONE, B_
SetViewColor(B_TRANSPARENT_COLOR);
}
// New - real time updating of bar colors
void ProgressBar::UpdateColors(int32 color, bool fade) {
void
ProgressBar::UpdateColors(int32 color, bool fade)
{
unsigned char red = (color & 0xff000000) >> 24;
unsigned char green = (color & 0x00ff0000) >> 16;
unsigned char blue = (color & 0x0000ff00) >> 8;
if (fade) {
unsigned char red_base = red / 3;
unsigned char green_base = green / 3;
unsigned char blue_base = blue / 3;
for (int x = 0; x < 20; x++) {
segments[x].color.red = (uint8)(red_base + ((red - red_base) * ((float)x / 19.0)));
segments[x].color.green = (uint8)(green_base + ((green - green_base) * ((float)x / 19.0)));
@ -53,21 +59,32 @@ void ProgressBar::UpdateColors(int32 color, bool fade) {
Render(true);
}
void ProgressBar::AttachedToWindow() {
void
ProgressBar::AttachedToWindow()
{
Prefs *prefs = ((PulseApp *)be_app)->prefs;
UpdateColors(prefs->normal_bar_color, prefs->normal_fade_colors);
}
void ProgressBar::Set(int32 value) {
void
ProgressBar::Set(int32 value)
{
// How many segments to light up
current_value = (int32)(value / 4.9);
if (current_value > 20) current_value = 20;
if (current_value > 20)
current_value = 20;
Render(false);
}
// Draws the progress bar. If "all" is true the entire bar is redrawn rather
// than just the part that changed.
void ProgressBar::Render(bool all) {
void
ProgressBar::Render(bool all)
{
if (all) {
// Black border
BRect bounds = Bounds();
@ -76,7 +93,7 @@ void ProgressBar::Render(bool all) {
StrokeRect(bounds);
bounds.InsetBy(1, 1);
StrokeRect(bounds);
// Black dividers
float left = bounds.left;
BPoint start, end;
@ -86,13 +103,13 @@ void ProgressBar::Render(bool all) {
end.Set(left, bounds.bottom);
StrokeLine(start, end);
}
for (int x = 0; x < current_value; x++) {
SetHighColor(segments[x].color.red, segments[x].color.green,
segments[x].color.blue);
FillRect(segments[x].rect);
}
SetHighColor(75, 75, 75);
if (current_value < 20) {
for (int x = 19; x >= current_value; x--) {
@ -113,12 +130,15 @@ void ProgressBar::Render(bool all) {
// Special case to make sure the lowest light gets turned off
if (current_value == 0) FillRect(segments[0].rect);
}
Sync();
previous_value = current_value;
}
void ProgressBar::Draw(BRect rect) {
void
ProgressBar::Draw(BRect rect)
{
// Add bevels
SetHighColor(dkgray, dkgray, dkgray);
BRect frame = Bounds();
@ -126,12 +146,12 @@ void ProgressBar::Draw(BRect rect) {
StrokeLine(BPoint(frame.left, frame.top + 1), BPoint(frame.right, frame.top + 1));
StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.left, frame.bottom));
StrokeLine(BPoint(frame.left + 1, frame.top), BPoint(frame.left + 1, frame.bottom));
SetHighColor(ltgray, ltgray, ltgray);
StrokeLine(BPoint(frame.right-1, frame.top + 2), BPoint(frame.right - 1, frame.bottom));
StrokeLine(BPoint(frame.right, frame.top + 1), BPoint(frame.right, frame.bottom));
StrokeLine(BPoint(frame.left+1, frame.bottom - 1), BPoint(frame.right - 1, frame.bottom - 1));
StrokeLine(BPoint(frame.left, frame.bottom), BPoint(frame.right, frame.bottom));
Render(true);
}

View File

@ -229,7 +229,9 @@ LoadInDeskbar()
BAlert *alert = new BAlert(NULL, strerror(err), "OK");
alert->Go(NULL);
return false;
} else return true;
}
return true;
}