1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-22 17:34:40 +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" #include "utilities.h"
using std::string; using std::string;
using std::unique_ptr;
using std::move;
BrowseDialog::BrowseDialog( BrowseDialog::BrowseDialog(
GMenu2X *gmenu2x, Touchscreen &ts_, GMenu2X *gmenu2x, Touchscreen &ts_,
@ -18,24 +20,26 @@ BrowseDialog::BrowseDialog(
, ts_pressed(false) , ts_pressed(false)
, buttonBox(gmenu2x) , 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")); unique_ptr<IconButton> btnSelect(new IconButton(
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Up one folder"]); gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]));
btn->setAction(BIND(&BrowseDialog::directoryUp)); btnSelect->setAction(BIND(&BrowseDialog::directoryEnter));
buttonBox.add(btn); buttonBox.add(move(btnSelect));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/accept.png", gmenu2x->tr["Select"]); unique_ptr<IconButton> btnConfirm(new IconButton(
btn->setAction(BIND(&BrowseDialog::directoryEnter)); gmenu2x, ts, "skin:imgs/buttons/start.png", gmenu2x->tr["Confirm"]));
buttonBox.add(btn); btnConfirm->setAction(BIND(&BrowseDialog::confirm));
buttonBox.add(move(btnConfirm));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/start.png", gmenu2x->tr["Confirm"]); unique_ptr<IconButton> btnExit(new IconButton(
btn->setAction(BIND(&BrowseDialog::confirm)); gmenu2x, ts, "skin:imgs/buttons/select.png", gmenu2x->tr["Exit"]));
buttonBox.add(btn); btnExit->setAction(BIND(&BrowseDialog::quit));
buttonBox.add(move(btnExit));
btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/select.png", gmenu2x->tr["Exit"]);
btn->setAction(BIND(&BrowseDialog::quit));
buttonBox.add(btn);
iconGoUp = gmenu2x->sc.skinRes("imgs/go-up.png"); iconGoUp = gmenu2x->sc.skinRes("imgs/go-up.png");
iconFolder = gmenu2x->sc.skinRes("imgs/folder.png"); iconFolder = gmenu2x->sc.skinRes("imgs/folder.png");

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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