* 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
+114 -47
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);
@@ -46,32 +67,44 @@ void CPUButton::Draw(BRect rect) {
}
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);
@@ -105,29 +138,42 @@ void CPUButton::Draw(BRect rect) {
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;
@@ -191,10 +254,14 @@ void CPUButton::AttachedToWindow() {
}
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;
}
+17 -11
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
+27 -7
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,8 +28,11 @@ 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;
@@ -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();
@@ -118,7 +135,10 @@ void ProgressBar::Render(bool all) {
previous_value = current_value;
}
void ProgressBar::Draw(BRect rect) {
void
ProgressBar::Draw(BRect rect)
{
// Add bevels
SetHighColor(dkgray, dkgray, dkgray);
BRect frame = Bounds();
+3 -1
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;
}