BColorControl: Remove fOffscreenView pointer

...and rename fBitmap to fOffscreenBitmap to make it more clear what it is.

We don't need to save a pointer to both the offscreen bitmap and
the offscreen view, just the bitmap. We can access the view by calling
fOffscreenBitmap->ChildAt((int32)0). This gives us back a _reserved private
variable slot.

In the (unlikely) case that _InitData() is called with offscreen = false but
the fOffscreenBitmap is not NULL, delete fOffscreenBitmap before
setting it to NULL so that memory is not leaked.
This commit is contained in:
John Scipione
2016-03-12 19:14:26 -08:00
parent 76b9d53bd0
commit 53f75ce5d6
2 changed files with 25 additions and 25 deletions
+2 -3
View File
@@ -125,12 +125,11 @@ private:
BTextControl* fGreenText; BTextControl* fGreenText;
BTextControl* fBlueText; BTextControl* fBlueText;
BBitmap* fBitmap; BBitmap* fOffscreenBitmap;
BView* fOffscreenView;
int16 fFocusedRamp; int16 fFocusedRamp;
int16 fClickedRamp; int16 fClickedRamp;
uint32 _reserved[2]; uint32 _reserved[3];
}; };
inline void inline void
+23 -22
View File
@@ -56,8 +56,7 @@ BColorControl::BColorControl(BPoint leftTop, color_control_layout layout,
fRedText(NULL), fRedText(NULL),
fGreenText(NULL), fGreenText(NULL),
fBlueText(NULL), fBlueText(NULL),
fBitmap(NULL), fOffscreenBitmap(NULL)
fOffscreenView(NULL)
{ {
_InitData(layout, cellSize, useOffscreen, NULL); _InitData(layout, cellSize, useOffscreen, NULL);
} }
@@ -69,8 +68,7 @@ BColorControl::BColorControl(BMessage* data)
fRedText(NULL), fRedText(NULL),
fGreenText(NULL), fGreenText(NULL),
fBlueText(NULL), fBlueText(NULL),
fBitmap(NULL), fOffscreenBitmap(NULL)
fOffscreenView(NULL)
{ {
int32 layout; int32 layout;
float cellSize; float cellSize;
@@ -86,7 +84,7 @@ BColorControl::BColorControl(BMessage* data)
BColorControl::~BColorControl() BColorControl::~BColorControl()
{ {
delete fBitmap; delete fOffscreenBitmap;
} }
@@ -184,18 +182,18 @@ BColorControl::_InitData(color_control_layout layout, float size,
ResizeToPreferred(); ResizeToPreferred();
if (useOffscreen) { if (useOffscreen) {
if (fOffscreenView != NULL) { if (fOffscreenBitmap != NULL) {
BRect bounds = _PaletteFrame(); BRect bounds = _PaletteFrame();
fBitmap = new BBitmap(bounds, B_RGB32, true, false); fOffscreenBitmap = new BBitmap(bounds, B_RGB32, true, false);
fOffscreenView = new BView(bounds, "off_view", 0, 0); BView* offscreenView = new BView(bounds, "off_view", 0, 0);
fBitmap->Lock(); fOffscreenBitmap->Lock();
fBitmap->AddChild(fOffscreenView); fOffscreenBitmap->AddChild(offscreenView);
fBitmap->Unlock(); fOffscreenBitmap->Unlock();
} }
} else { } else {
fBitmap = NULL; delete fOffscreenBitmap;
fOffscreenView = NULL; fOffscreenBitmap = NULL;
} }
} }
@@ -254,7 +252,7 @@ BColorControl::Archive(BMessage* data, bool deep) const
status = data->AddFloat("_csize", fCellSize); status = data->AddFloat("_csize", fCellSize);
if (status == B_OK) if (status == B_OK)
status = data->AddBool("_use_off", fOffscreenView != NULL); status = data->AddBool("_use_off", fOffscreenBitmap != NULL);
return status; return status;
} }
@@ -354,7 +352,7 @@ BColorControl::AttachedToWindow()
fGreenText->SetTarget(this); fGreenText->SetTarget(this);
fBlueText->SetTarget(this); fBlueText->SetTarget(this);
if (fBitmap != NULL) if (fOffscreenBitmap != NULL)
_InitOffscreen(); _InitOffscreen();
} }
@@ -393,7 +391,7 @@ BColorControl::MessageReceived(BMessage* message)
data->AddInt32("_val", Value()); data->AddInt32("_val", Value());
// reinititialize // reinititialize
bool useOffscreen = fOffscreenView != NULL; bool useOffscreen = fOffscreenBitmap != NULL;
_InitData((color_control_layout)fColumns, fCellSize, _InitData((color_control_layout)fColumns, fCellSize,
useOffscreen, data); useOffscreen, data);
if (useOffscreen) if (useOffscreen)
@@ -414,8 +412,8 @@ BColorControl::MessageReceived(BMessage* message)
void void
BColorControl::Draw(BRect updateRect) BColorControl::Draw(BRect updateRect)
{ {
if (fBitmap != NULL) if (fOffscreenBitmap != NULL)
DrawBitmap(fBitmap, B_ORIGIN); DrawBitmap(fOffscreenBitmap, B_ORIGIN);
else else
_DrawColorArea(this, updateRect); _DrawColorArea(this, updateRect);
@@ -657,10 +655,13 @@ BColorControl::_PaletteSelectorFrame(uint8 colorIndex) const
void void
BColorControl::_InitOffscreen() BColorControl::_InitOffscreen()
{ {
if (fBitmap->Lock()) { if (fOffscreenBitmap->Lock()) {
_DrawColorArea(fOffscreenView, _PaletteFrame()); BView* offscreenView = fOffscreenBitmap->ChildAt((int32)0);
fOffscreenView->Sync(); if (offscreenView != NULL) {
fBitmap->Unlock(); _DrawColorArea(offscreenView, _PaletteFrame());
offscreenView->Sync();
}
fOffscreenBitmap->Unlock();
} }
} }