* 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
+123 -56
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);
@@ -45,39 +66,51 @@ void CPUButton::Draw(BRect rect) {
color_rect.right -= 1; color_rect.right -= 1;
} }
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);
start.Set(bounds.right, bounds.top); start.Set(bounds.right, bounds.top);
StrokeLine(start, end); StrokeLine(start, end);
if (value) { if (value) {
SetHighColor(0, 0, 0); SetHighColor(0, 0, 0);
start.Set(bounds.left + 2, bounds.bottom - 2); 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); start.Set(bounds.right - 2, bounds.top + 2);
StrokeLine(start, end); StrokeLine(start, end);
} }
// Try to keep the text centered // Try to keep the text centered
BFont font; BFont font;
GetFont(&font); GetFont(&font);
@@ -98,36 +131,49 @@ void CPUButton::Draw(BRect rect) {
int label_height = (int)fh.ascent; int label_height = (int)fh.ascent;
int x_pos = (int)(((double)(rect_width - label_width) / 2.0) + 0.5); int x_pos = (int)(((double)(rect_width - label_width) / 2.0) + 0.5);
int y_pos = (rect_height - label_height) / 2 + label_height; int y_pos = (rect_height - label_height) / 2 + label_height;
MovePenTo(x_pos, y_pos); MovePenTo(x_pos, y_pos);
SetHighColor(0, 0, 0); SetHighColor(0, 0, 0);
SetDrawingMode(B_OP_OVER); SetDrawingMode(B_OP_OVER);
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; status_t
CPUButton::Invoke(BMessage *message)
if (!LastEnabledCPU(my_cpu)) { {
_kset_cpu_state_(my_cpu, Value()); 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,20 +183,31 @@ 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) {
switch(message->what) { void
CPUButton::MessageReceived(BMessage *message)
{
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");
// Use the asynchronous version so we don't block the window's thread // 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: { 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;
@@ -189,12 +252,16 @@ void CPUButton::AttachedToWindow() {
PulseApp *pulseapp = (PulseApp *)be_app; PulseApp *pulseapp = (PulseApp *)be_app;
UpdateColors(pulseapp->prefs->normal_bar_color); UpdateColors(pulseapp->prefs->normal_bar_color);
} }
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
+3 -3
View File
@@ -249,13 +249,13 @@ NormalPulseView::AttachedToWindow()
CalculateFontSize(); CalculateFontSize();
fPreviousTime = system_time(); fPreviousTime = system_time();
BMessenger messenger(Window()); BMessenger messenger(Window());
mode1->SetTarget(messenger); mode1->SetTarget(messenger);
mode2->SetTarget(messenger); mode2->SetTarget(messenger);
preferences->SetTarget(messenger); preferences->SetTarget(messenger);
about->SetTarget(messenger); about->SetTarget(messenger);
system_info sys_info; system_info sys_info;
get_system_info(&sys_info); get_system_info(&sys_info);
if (sys_info.cpu_count >= 2) { if (sys_info.cpu_count >= 2) {
@@ -273,7 +273,7 @@ NormalPulseView::UpdateColors(BMessage *message)
bool fade = message->FindBool("fade"); bool fade = message->FindBool("fade");
system_info sys_info; system_info sys_info;
get_system_info(&sys_info); get_system_info(&sys_info);
for (int x = 0; x < sys_info.cpu_count; x++) { for (int x = 0; x < sys_info.cpu_count; x++) {
fProgressBars[x]->UpdateColors(color, fade); fProgressBars[x]->UpdateColors(color, fade);
fCpuButtons[x]->UpdateColors(color); fCpuButtons[x]->UpdateColors(color);
+35 -15
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,17 +28,20 @@ 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;
if (fade) { if (fade) {
unsigned char red_base = red / 3; unsigned char red_base = red / 3;
unsigned char green_base = green / 3; unsigned char green_base = green / 3;
unsigned char blue_base = blue / 3; unsigned char blue_base = blue / 3;
for (int x = 0; x < 20; x++) { for (int x = 0; x < 20; x++) {
segments[x].color.red = (uint8)(red_base + ((red - red_base) * ((float)x / 19.0))); 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))); 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); 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();
@@ -76,7 +93,7 @@ void ProgressBar::Render(bool all) {
StrokeRect(bounds); StrokeRect(bounds);
bounds.InsetBy(1, 1); bounds.InsetBy(1, 1);
StrokeRect(bounds); StrokeRect(bounds);
// Black dividers // Black dividers
float left = bounds.left; float left = bounds.left;
BPoint start, end; BPoint start, end;
@@ -86,13 +103,13 @@ void ProgressBar::Render(bool all) {
end.Set(left, bounds.bottom); end.Set(left, bounds.bottom);
StrokeLine(start, end); StrokeLine(start, end);
} }
for (int x = 0; x < current_value; x++) { for (int x = 0; x < current_value; x++) {
SetHighColor(segments[x].color.red, segments[x].color.green, SetHighColor(segments[x].color.red, segments[x].color.green,
segments[x].color.blue); segments[x].color.blue);
FillRect(segments[x].rect); FillRect(segments[x].rect);
} }
SetHighColor(75, 75, 75); SetHighColor(75, 75, 75);
if (current_value < 20) { if (current_value < 20) {
for (int x = 19; x >= current_value; x--) { 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 // Special case to make sure the lowest light gets turned off
if (current_value == 0) FillRect(segments[0].rect); if (current_value == 0) FillRect(segments[0].rect);
} }
Sync(); Sync();
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();
@@ -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 + 1), BPoint(frame.right, frame.top + 1));
StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.left, frame.bottom)); StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.left, frame.bottom));
StrokeLine(BPoint(frame.left + 1, frame.top), BPoint(frame.left + 1, frame.bottom)); StrokeLine(BPoint(frame.left + 1, frame.top), BPoint(frame.left + 1, frame.bottom));
SetHighColor(ltgray, ltgray, ltgray); SetHighColor(ltgray, ltgray, ltgray);
StrokeLine(BPoint(frame.right-1, frame.top + 2), BPoint(frame.right - 1, frame.bottom)); 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.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+1, frame.bottom - 1), BPoint(frame.right - 1, frame.bottom - 1));
StrokeLine(BPoint(frame.left, frame.bottom), BPoint(frame.right, frame.bottom)); StrokeLine(BPoint(frame.left, frame.bottom), BPoint(frame.right, frame.bottom));
Render(true); Render(true);
} }
+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;
} }