1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-22 12:09:41 +02:00

Fix dynamic allocation of IconButton instances

Previously, IconButton instances to be added to button boxes were
allocated with new, but never freed with delete.

unique_ptr makes sure the buttons will be freed along with the button
box that owns them, or when code calls ButtonBox::clear.

The destructor of ButtonBox has been made redundant by this change, so
it's gone.
This commit is contained in:
Nebuleon Fumika 2014-08-15 21:52:48 +00:00 committed by Maarten ter Huurne
parent 004c41e2ef
commit 891525aa94
12 changed files with 118 additions and 102 deletions

View File

@ -7,6 +7,8 @@
#include "utilities.h"
using std::string;
using std::unique_ptr;
using std::move;
BrowseDialog::BrowseDialog(
GMenu2X *gmenu2x, Touchscreen &ts_,
@ -18,24 +20,26 @@ BrowseDialog::BrowseDialog(
, ts_pressed(false)
, buttonBox(gmenu2x)
{
IconButton *btn;
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png")));
unique_ptr<IconButton> btnUp(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Up one folder"]));
btnUp->setAction(BIND(&BrowseDialog::directoryUp));
buttonBox.add(move(btnUp));
buttonBox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png"));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Up one folder"]);
btn->setAction(BIND(&BrowseDialog::directoryUp));
buttonBox.add(btn);
unique_ptr<IconButton> btnSelect(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]));
btnSelect->setAction(BIND(&BrowseDialog::directoryEnter));
buttonBox.add(move(btnSelect));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]);
btn->setAction(BIND(&BrowseDialog::directoryEnter));
buttonBox.add(btn);
unique_ptr<IconButton> btnConfirm(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/start.png", gmenu2x->tr["Confirm"]));
btnConfirm->setAction(BIND(&BrowseDialog::confirm));
buttonBox.add(move(btnConfirm));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/start.png", gmenu2x->tr["Confirm"]);
btn->setAction(BIND(&BrowseDialog::confirm));
buttonBox.add(btn);
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/select.png", gmenu2x->tr["Exit"]);
btn->setAction(BIND(&BrowseDialog::quit));
buttonBox.add(btn);
unique_ptr<IconButton> btnExit(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/select.png", gmenu2x->tr["Exit"]));
btnExit->setAction(BIND(&BrowseDialog::quit));
buttonBox.add(move(btnExit));
iconGoUp = gmenu2x->sc.skinRes("imgs/go-up.png");
iconFolder = gmenu2x->sc.skinRes("imgs/folder.png");

View File

@ -3,18 +3,16 @@
#include "gmenu2x.h"
#include "iconbutton.h"
using std::unique_ptr;
using std::move;
ButtonBox::ButtonBox(GMenu2X *gmenu2x) : gmenu2x(gmenu2x)
{
}
ButtonBox::~ButtonBox()
void ButtonBox::add(unique_ptr<IconButton> button)
{
clear();
}
void ButtonBox::add(IconButton *button)
{
buttons.push_back(button);
buttons.push_back(move(button));
}
void ButtonBox::clear()
@ -25,7 +23,7 @@ void ButtonBox::clear()
void ButtonBox::paint(Surface& s, unsigned int x)
{
const int y = gmenu2x->resY - 1;
for (auto button : buttons) {
for (auto& button : buttons) {
auto rect = button->getRect();
button->setPosition(x, y - rect.h);
button->paint(s);
@ -35,7 +33,7 @@ void ButtonBox::paint(Surface& s, unsigned int x)
void ButtonBox::handleTS()
{
for (auto button : buttons) {
for (auto& button : buttons) {
button->handleTS();
}
}

View File

@ -1,26 +1,27 @@
#ifndef __BUTTONBOX_H__
#define __BUTTONBOX_H__
#include "iconbutton.h"
#include <memory>
#include <vector>
class GMenu2X;
class IconButton;
class Surface;
class ButtonBox
{
public:
ButtonBox(GMenu2X *gmenu2x);
~ButtonBox();
void add(IconButton *button);
void add(std::unique_ptr<IconButton> button);
void clear();
void paint(Surface& s, unsigned int x);
void handleTS();
private:
std::vector<IconButton*> buttons;
std::vector<std::unique_ptr<IconButton> > buttons;
GMenu2X *gmenu2x;
};

View File

@ -98,25 +98,25 @@ InputDialog::InputDialog(GMenu2X *gmenu2x, InputManager &inputMgr_,
setKeyboard(0);
buttonbox = new ButtonBox(gmenu2x);
IconButton *btnBackspace = new IconButton(gmenu2x, ts,
"skin:imgs/buttons/l.png", gmenu2x->tr["Backspace"]);
unique_ptr<IconButton> btnBackspace(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/l.png", gmenu2x->tr["Backspace"]));
btnBackspace->setAction(BIND(&InputDialog::backspace));
buttonbox->add(btnBackspace);
buttonbox->add(move(btnBackspace));
IconButton *btnSpace = new IconButton(gmenu2x, ts,
"skin:imgs/buttons/r.png", gmenu2x->tr["Space"]);
unique_ptr<IconButton> btnSpace(
new IconButton(gmenu2x, ts, "skin:imgs/buttons/r.png", gmenu2x->tr["Space"]));
btnSpace->setAction(BIND(&InputDialog::space));
buttonbox->add(btnSpace);
buttonbox->add(move(btnSpace));
IconButton *btnConfirm = new IconButton(gmenu2x, ts,
"skin:imgs/buttons/accept.png", gmenu2x->tr["Confirm"]);
unique_ptr<IconButton> btnConfirm(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Confirm"]));
btnConfirm->setAction(BIND(&InputDialog::confirm));
buttonbox->add(btnConfirm);
buttonbox->add(move(btnConfirm));
IconButton *btnChangeKeys = new IconButton(gmenu2x, ts,
"skin:imgs/buttons/cancel.png", gmenu2x->tr["Change keys"]);
unique_ptr<IconButton> btnChangeKeys(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Change keys"]));
btnChangeKeys->setAction(BIND(&InputDialog::changeKeys));
buttonbox->add(btnChangeKeys);
buttonbox->add(move(btnChangeKeys));
}
void InputDialog::setKeyboard(int kb) {

View File

@ -29,6 +29,8 @@
#include <sstream>
using std::string;
using std::unique_ptr;
using std::move;
MenuSettingBool::MenuSettingBool(
GMenu2X *gmenu2x, Touchscreen &ts,
@ -58,11 +60,10 @@ MenuSettingBool::MenuSettingBool(
void MenuSettingBool::initButton()
{
IconButton *btn = new IconButton(gmenu2x, ts,
"skin:imgs/buttons/accept.png",
gmenu2x->tr["Switch"]);
unique_ptr<IconButton> btn(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Switch"]));
btn->setAction(BIND(&MenuSettingBool::toggle));
buttonBox.add(btn);
buttonBox.add(move(btn));
}
void MenuSettingBool::draw(int valueX, int y, int h)

View File

@ -26,6 +26,8 @@
#include "iconbutton.h"
using std::string;
using std::unique_ptr;
using std::move;
MenuSettingDir::MenuSettingDir(
GMenu2X *gmenu2x, Touchscreen &ts_,
@ -33,17 +35,15 @@ MenuSettingDir::MenuSettingDir(
: MenuSettingStringBase(gmenu2x, name, description, value)
, ts(ts_)
{
IconButton *btn;
unique_ptr<IconButton> btnCancel(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Clear"]));
btnCancel->setAction(BIND(&MenuSettingDir::clear));
buttonBox.add(move(btnCancel));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/cancel.png",
gmenu2x->tr["Clear"]);
btn->setAction(BIND(&MenuSettingDir::clear));
buttonBox.add(btn);
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png",
gmenu2x->tr["Select"]);
btn->setAction(BIND(&MenuSettingDir::edit));
buttonBox.add(btn);
unique_ptr<IconButton> btnAccept(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]));
btnAccept->setAction(BIND(&MenuSettingDir::edit));
buttonBox.add(move(btnAccept));
}
void MenuSettingDir::edit()

View File

@ -26,6 +26,8 @@
#include "iconbutton.h"
using std::string;
using std::unique_ptr;
using std::move;
MenuSettingFile::MenuSettingFile(
GMenu2X *gmenu2x, Touchscreen &ts_,
@ -35,15 +37,15 @@ MenuSettingFile::MenuSettingFile(
, ts(ts_)
, filter(filter_)
{
IconButton *btn;
unique_ptr<IconButton> btnClear(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Clear"]));
btnClear->setAction(BIND(&MenuSettingFile::clear));
buttonBox.add(move(btnClear));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Clear"]);
btn->setAction(BIND(&MenuSettingFile::clear));
buttonBox.add(btn);
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]);
btn->setAction(BIND(&MenuSettingFile::edit));
buttonBox.add(btn);
unique_ptr<IconButton> btnSelect(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]));
btnSelect->setAction(BIND(&MenuSettingFile::edit));
buttonBox.add(move(btnSelect));
}
void MenuSettingFile::edit()

View File

@ -30,6 +30,8 @@
using std::string;
using std::stringstream;
using std::unique_ptr;
using std::move;
MenuSettingInt::MenuSettingInt(
GMenu2X *gmenu2x, Touchscreen &ts,
@ -37,8 +39,6 @@ MenuSettingInt::MenuSettingInt(
int *value, int min, int max, int increment)
: MenuSetting(gmenu2x,name,description)
{
IconButton *btn;
_value = value;
originalValue = *value;
this->min = min;
@ -50,21 +50,25 @@ MenuSettingInt::MenuSettingInt(
function_t actionInc = BIND(&MenuSettingInt::inc);
function_t actionDec = BIND(&MenuSettingInt::dec);
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/l.png");
btn->setAction(actionDec);
buttonBox.add(btn);
unique_ptr<IconButton> btnL1(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/l.png"));
btnL1->setAction(actionDec);
buttonBox.add(move(btnL1));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png", gmenu2x->tr["Decrease"]);
btn->setAction(actionDec);
buttonBox.add(btn);
unique_ptr<IconButton> btnL2(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/left.png", gmenu2x->tr["Decrease"]));
btnL2->setAction(actionDec);
buttonBox.add(move(btnL2));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/r.png");
btn->setAction(actionInc);
buttonBox.add(btn);
unique_ptr<IconButton> btnR1(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/r.png"));
btnR1->setAction(actionInc);
buttonBox.add(move(btnR1));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Increase"]);
btn->setAction(actionInc);
buttonBox.add(btn);
unique_ptr<IconButton> btnR2(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Increase"]));
btnR2->setAction(actionInc);
buttonBox.add(move(btnR2));
}
void MenuSettingInt::draw(int valueX, int y, int h)

View File

@ -28,6 +28,8 @@
using std::find;
using std::string;
using std::vector;
using std::unique_ptr;
using std::move;
MenuSettingMultiString::MenuSettingMultiString(
GMenu2X *gmenu2x, Touchscreen &ts,
@ -38,15 +40,15 @@ MenuSettingMultiString::MenuSettingMultiString(
{
setSel(find(choices->begin(), choices->end(), *value) - choices->begin());
IconButton *btn;
unique_ptr<IconButton> btnDec(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/left.png"));
btnDec->setAction(BIND(&MenuSettingMultiString::decSel));
buttonBox.add(move(btnDec));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png");
btn->setAction(BIND(&MenuSettingMultiString::decSel));
buttonBox.add(btn);
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Change value"]);
btn->setAction(BIND(&MenuSettingMultiString::incSel));
buttonBox.add(btn);
unique_ptr<IconButton> btnInc(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Change value"]));
btnInc->setAction(BIND(&MenuSettingMultiString::incSel));
buttonBox.add(move(btnInc));
}
bool MenuSettingMultiString::handleButtonPress(InputManager::Button button)

View File

@ -29,6 +29,8 @@
using std::string;
using std::stringstream;
using std::unique_ptr;
using std::move;
constexpr unsigned int COMPONENT_WIDTH = 28;
@ -228,14 +230,14 @@ void MenuSettingRGBA::updateButtonBox()
{
buttonBox.clear();
if (edit) {
buttonBox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/l.png"));
buttonBox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png", gmenu2x->tr["Decrease"]));
buttonBox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/r.png"));
buttonBox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Increase"]));
buttonBox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Confirm"]));
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/l.png")));
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png", gmenu2x->tr["Decrease"])));
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/r.png")));
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Increase"])));
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Confirm"])));
} else {
buttonBox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png"));
buttonBox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Change color component"]));
buttonBox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Edit"]));
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png")));
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Change color component"])));
buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Edit"])));
}
}

View File

@ -26,6 +26,8 @@
#include "inputdialog.h"
using std::string;
using std::unique_ptr;
using std::move;
MenuSettingString::MenuSettingString(
GMenu2X *gmenu2x, Touchscreen &ts_,
@ -36,15 +38,15 @@ MenuSettingString::MenuSettingString(
, diagTitle(diagTitle_)
, diagIcon(diagIcon_)
{
IconButton *btn;
unique_ptr<IconButton> btnClear(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Clear"]));
btnClear->setAction(BIND(&MenuSettingString::clear));
buttonBox.add(move(btnClear));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Clear"]);
btn->setAction(BIND(&MenuSettingString::clear));
buttonBox.add(btn);
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Edit"]);
btn->setAction(BIND(&MenuSettingString::edit));
buttonBox.add(btn);
unique_ptr<IconButton> btnEdit(new IconButton(
gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Edit"]));
btnEdit->setAction(BIND(&MenuSettingString::edit));
buttonBox.add(move(btnEdit));
}
void MenuSettingString::edit()

View File

@ -63,8 +63,8 @@ bool WallpaperDialog::exec()
uint i, selected = 0, firstElement = 0, iY;
ButtonBox buttonbox(gmenu2x);
buttonbox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]));
buttonbox.add(new IconButton(gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Exit"]));
buttonbox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"])));
buttonbox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Exit"])));
unsigned int top, height;
tie(top, height) = gmenu2x->getContentArea();