Root/src/menusettingbool.cpp

1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20#include "menusettingbool.h"
21#include "gmenu2x.h"
22#include "utilities.h"
23
24#include <sstream>
25
26using std::string;
27using fastdelegate::MakeDelegate;
28
29MenuSettingBool::MenuSettingBool(
30        GMenu2X *gmenu2x, const string &name,
31        const string &description, int *value)
32    : MenuSetting(gmenu2x, name, description)
33{
34    _ivalue = value;
35    _value = NULL;
36    originalValue = *value != 0;
37    setValue(this->value());
38    initButton();
39}
40
41MenuSettingBool::MenuSettingBool(
42        GMenu2X *gmenu2x, const string &name,
43        const string &description, bool *value)
44    : MenuSetting(gmenu2x, name, description)
45{
46    _value = value;
47    _ivalue = NULL;
48    originalValue = *value;
49    setValue(this->value());
50    initButton();
51}
52
53void MenuSettingBool::initButton()
54{
55    IconButton *btn = new IconButton(gmenu2x, "skin:imgs/buttons/b.png",
56                                     gmenu2x->tr["Switch"]);
57    btn->setAction(MakeDelegate(this, &MenuSettingBool::toggle));
58    buttonBox.add(btn);
59}
60
61void MenuSettingBool::draw(int y)
62{
63    MenuSetting::draw(y);
64    gmenu2x->s->write( gmenu2x->font, strvalue, 155, y, ASFont::HAlignLeft, ASFont::VAlignTop );
65}
66
67void MenuSettingBool::manageInput(bevent_t *event)
68{
69    if (event->button == ACCEPT && event->state == PRESSED)
70      toggle();
71}
72
73void MenuSettingBool::toggle()
74{
75    setValue(!value());
76}
77
78void MenuSettingBool::setValue(int value)
79{
80    setValue(value != 0);
81}
82
83void MenuSettingBool::setValue(bool value)
84{
85    if (_value == NULL)
86        *_ivalue = value;
87    else
88        *_value = value;
89    strvalue = value ? "ON" : "OFF";
90}
91
92bool MenuSettingBool::value()
93{
94    if (_value == NULL)
95        return *_ivalue != 0;
96    else
97        return *_value;
98}
99
100bool MenuSettingBool::edited()
101{
102    return originalValue != value();
103}
104

Archive Download this file



interactive