* 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
+113 -46
View File
@@ -8,6 +8,7 @@
// //
//**************************************************************************************** //****************************************************************************************
#include "CPUButton.h" #include "CPUButton.h"
#include "PulseApp.h" #include "PulseApp.h"
#include "PulseView.h" #include "PulseView.h"
@@ -15,27 +16,47 @@
#include <interface/Alert.h> #include <interface/Alert.h>
#include <stdlib.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; CPUButton::CPUButton(BRect rect, const char *name, const char *label, BMessage *message)
off_color.alpha = 255; : BControl(rect, name, label, message, B_FOLLOW_NONE, B_WILL_DRAW)
{
SetValue(B_CONTROL_ON); SetValue(B_CONTROL_ON);
SetViewColor(B_TRANSPARENT_COLOR); 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; CPUButton::CPUButton(BMessage *message)
off_color.alpha = 255; : BControl(message)
replicant = true; {
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(); bool value = (bool)Value();
if (value) SetHighColor(on_color); SetHighColor(value ? fOnColor : fOffColor);
else SetHighColor(off_color);
BRect bounds = Bounds(); BRect bounds = Bounds();
BRect color_rect(bounds); BRect color_rect(bounds);
@@ -46,32 +67,44 @@ void CPUButton::Draw(BRect rect) {
} }
FillRect(bounds); FillRect(bounds);
if (value) SetHighColor(80, 80, 80); if (value)
else SetHighColor(255, 255, 255); SetHighColor(80, 80, 80);
else
SetHighColor(255, 255, 255);
BPoint start(0, 0); BPoint start(0, 0);
BPoint end(bounds.right, 0); BPoint end(bounds.right, 0);
StrokeLine(start, end); StrokeLine(start, end);
end.Set(0, bounds.bottom); end.Set(0, bounds.bottom);
StrokeLine(start, end); StrokeLine(start, end);
if (value) SetHighColor(32, 32, 32); if (value)
else SetHighColor(216, 216, 216); SetHighColor(32, 32, 32);
else
SetHighColor(216, 216, 216);
start.Set(1, 1); start.Set(1, 1);
end.Set(bounds.right - 1, 1); end.Set(bounds.right - 1, 1);
StrokeLine(start, end); StrokeLine(start, end);
end.Set(1, bounds.bottom - 1); end.Set(1, bounds.bottom - 1);
StrokeLine(start, end); StrokeLine(start, end);
if (value) SetHighColor(216, 216, 216); if (value)
else SetHighColor(80, 80, 80); SetHighColor(216, 216, 216);
else
SetHighColor(80, 80, 80);
start.Set(bounds.left + 1, bounds.bottom - 1); start.Set(bounds.left + 1, bounds.bottom - 1);
end.Set(bounds.right - 1, bounds.bottom - 1); end.Set(bounds.right - 1, bounds.bottom - 1);
StrokeLine(start, end); StrokeLine(start, end);
start.Set(bounds.right - 1, bounds.top + 1); start.Set(bounds.right - 1, bounds.top + 1);
StrokeLine(start, end); StrokeLine(start, end);
if (value) SetHighColor(255, 255, 255); if (value)
else SetHighColor(32, 32, 32); SetHighColor(255, 255, 255);
else
SetHighColor(32, 32, 32);
start.Set(bounds.left, bounds.bottom); start.Set(bounds.left, bounds.bottom);
end.Set(bounds.right, bounds.bottom); end.Set(bounds.right, bounds.bottom);
StrokeLine(start, end); StrokeLine(start, end);
@@ -105,29 +138,42 @@ void CPUButton::Draw(BRect rect) {
DrawString(Label()); 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()); SetValue(!Value());
SetTracking(true); SetTracking(true);
SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS); 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); 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 (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)) { status_t
_kset_cpu_state_(my_cpu, Value()); CPUButton::Invoke(BMessage *message)
{
if (!LastEnabledCPU(fCPU)) {
_kset_cpu_state_(fCPU, Value());
} else { } else {
BAlert *alert = new BAlert(NULL, "You can't disable the last active CPU.", "OK"); BAlert *alert = new BAlert(NULL, "You can't disable the last active CPU.", "OK");
alert->Go(NULL); alert->Go(NULL);
@@ -137,19 +183,30 @@ status_t CPUButton::Invoke(BMessage *message) {
return B_OK; 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); 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); BControl::Archive(data, deep);
data->AddString("add_on", APP_SIGNATURE); data->AddString("add_on", APP_SIGNATURE);
data->AddString("class", "CPUButton"); data->AddString("class", "CPUButton");
return B_OK; return B_OK;
} }
void CPUButton::MessageReceived(BMessage *message) {
void
CPUButton::MessageReceived(BMessage *message)
{
switch (message->what) { switch (message->what) {
case B_ABOUT_REQUESTED: { case B_ABOUT_REQUESTED: {
BAlert *alert = new BAlert("Info", "Pulse\n\nBy David Ramsey and Arve Hjønnevåg\nRevised by Daniel Switkin", "OK"); BAlert *alert = new BAlert("Info", "Pulse\n\nBy David Ramsey and Arve Hjønnevåg\nRevised by Daniel Switkin", "OK");
@@ -159,8 +216,8 @@ void CPUButton::MessageReceived(BMessage *message) {
} }
case PV_REPLICANT_PULSE: { case PV_REPLICANT_PULSE: {
// Make sure we're consistent with our CPU // Make sure we're consistent with our CPU
int my_cpu = atoi(Label()) - 1; if (_kget_cpu_state_(fCPU) != Value() && !IsTracking())
if (_kget_cpu_state_(my_cpu) != Value() && !IsTracking()) SetValue(!Value()); SetValue(!Value());
break; break;
} }
default: default:
@@ -169,19 +226,25 @@ void CPUButton::MessageReceived(BMessage *message) {
} }
} }
void CPUButton::UpdateColors(int32 color) {
on_color.red = (color & 0xff000000) >> 24; void
on_color.green = (color & 0x00ff0000) >> 16; CPUButton::UpdateColors(int32 color)
on_color.blue = (color & 0x0000ff00) >> 8; {
fOnColor.red = (color & 0xff000000) >> 24;
fOnColor.green = (color & 0x00ff0000) >> 16;
fOnColor.blue = (color & 0x0000ff00) >> 8;
Draw(Bounds()); Draw(Bounds());
} }
void CPUButton::AttachedToWindow() {
void
CPUButton::AttachedToWindow()
{
SetTarget(this); SetTarget(this);
SetFont(be_plain_font); SetFont(be_plain_font);
SetFontSize(10); SetFontSize(10);
if (replicant) { if (fReplicant) {
Prefs *prefs = new Prefs(); Prefs *prefs = new Prefs();
UpdateColors(prefs->normal_bar_color); UpdateColors(prefs->normal_bar_color);
delete prefs; delete prefs;
@@ -191,10 +254,14 @@ void CPUButton::AttachedToWindow() {
} }
BMessenger messenger(this); BMessenger messenger(this);
messagerunner = new BMessageRunner(messenger, new BMessage(PV_REPLICANT_PULSE), fPulseRunner = new BMessageRunner(messenger, new BMessage(PV_REPLICANT_PULSE),
200000, -1); 200000, -1);
} }
CPUButton::~CPUButton() {
delete messagerunner; void
CPUButton::DetachedFromWindow()
{
delete fPulseRunner;
} }
+17 -11
View File
@@ -18,24 +18,30 @@ class CPUButton : public BControl {
public: public:
CPUButton(BRect rect, const char *name, const char *label, BMessage *message); CPUButton(BRect rect, const char *name, const char *label, BMessage *message);
CPUButton(BMessage *message); CPUButton(BMessage *message);
void Draw(BRect rect); virtual ~CPUButton();
void MouseDown(BPoint point);
void MouseUp(BPoint point); virtual void Draw(BRect rect);
void MouseMoved(BPoint point, uint32 transit, const BMessage *message); virtual void MouseDown(BPoint point);
~CPUButton(); 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); status_t Invoke(BMessage *message = NULL);
static CPUButton *Instantiate(BMessage *data); static CPUButton *Instantiate(BMessage *data);
status_t Archive(BMessage *data, bool deep = true) const; status_t Archive(BMessage *data, bool deep = true) const;
void MessageReceived(BMessage *message);
void UpdateColors(int32 color); void UpdateColors(int32 color);
void AttachedToWindow();
private: private:
rgb_color on_color, off_color; void _InitData();
bool replicant;
BMessageRunner *messagerunner; 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 "ProgressBar.h"
#include "PulseApp.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; previous_value = current_value = 0;
// Create 20 segments // Create 20 segments
@@ -25,8 +28,11 @@ ProgressBar::ProgressBar(BRect r, char *name) : BView(r, name, B_FOLLOW_NONE, B_
SetViewColor(B_TRANSPARENT_COLOR); SetViewColor(B_TRANSPARENT_COLOR);
} }
// New - real time updating of bar colors // 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 red = (color & 0xff000000) >> 24;
unsigned char green = (color & 0x00ff0000) >> 16; unsigned char green = (color & 0x00ff0000) >> 16;
unsigned char blue = (color & 0x0000ff00) >> 8; unsigned char blue = (color & 0x0000ff00) >> 8;
@@ -53,21 +59,32 @@ void ProgressBar::UpdateColors(int32 color, bool fade) {
Render(true); Render(true);
} }
void ProgressBar::AttachedToWindow() {
void
ProgressBar::AttachedToWindow()
{
Prefs *prefs = ((PulseApp *)be_app)->prefs; Prefs *prefs = ((PulseApp *)be_app)->prefs;
UpdateColors(prefs->normal_bar_color, prefs->normal_fade_colors); 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 // How many segments to light up
current_value = (int32)(value / 4.9); current_value = (int32)(value / 4.9);
if (current_value > 20) current_value = 20; if (current_value > 20)
current_value = 20;
Render(false); Render(false);
} }
// Draws the progress bar. If "all" is true the entire bar is redrawn rather // Draws the progress bar. If "all" is true the entire bar is redrawn rather
// than just the part that changed. // than just the part that changed.
void ProgressBar::Render(bool all) { void
ProgressBar::Render(bool all)
{
if (all) { if (all) {
// Black border // Black border
BRect bounds = Bounds(); BRect bounds = Bounds();
@@ -118,7 +135,10 @@ void ProgressBar::Render(bool all) {
previous_value = current_value; previous_value = current_value;
} }
void ProgressBar::Draw(BRect rect) {
void
ProgressBar::Draw(BRect rect)
{
// Add bevels // Add bevels
SetHighColor(dkgray, dkgray, dkgray); SetHighColor(dkgray, dkgray, dkgray);
BRect frame = Bounds(); BRect frame = Bounds();
+3 -1
View File
@@ -229,7 +229,9 @@ LoadInDeskbar()
BAlert *alert = new BAlert(NULL, strerror(err), "OK"); BAlert *alert = new BAlert(NULL, strerror(err), "OK");
alert->Go(NULL); alert->Go(NULL);
return false; return false;
} else return true; }
return true;
} }