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
21#include "menusettingbool.h"
22
23#include "delegate.h"
24#include "gmenu2x.h"
25#include "iconbutton.h"
26#include "surface.h"
27#include "utilities.h"
28
29#include <sstream>
30
31using std::string;
32
33MenuSettingBool::MenuSettingBool(
34        GMenu2X *gmenu2x, Touchscreen &ts,
35        const string &name, const string &description, int *value)
36    : MenuSetting(gmenu2x, name, description)
37    , ts(ts)
38{
39    _ivalue = value;
40    _value = NULL;
41    originalValue = *value != 0;
42    setValue(this->value());
43    initButton();
44}
45
46MenuSettingBool::MenuSettingBool(
47        GMenu2X *gmenu2x, Touchscreen &ts,
48        const string &name, const string &description, bool *value)
49    : MenuSetting(gmenu2x, name, description)
50    , ts(ts)
51{
52    _value = value;
53    _ivalue = NULL;
54    originalValue = *value;
55    setValue(this->value());
56    initButton();
57}
58
59void MenuSettingBool::initButton()
60{
61    IconButton *btn = new IconButton(gmenu2x, ts,
62                                     "skin:imgs/buttons/accept.png",
63                                     gmenu2x->tr["Switch"]);
64    btn->setAction(BIND(&MenuSettingBool::toggle));
65    buttonBox.add(btn);
66}
67
68void MenuSettingBool::draw(int y)
69{
70    MenuSetting::draw(y);
71    gmenu2x->s->write( gmenu2x->font, strvalue, 155, y, Font::HAlignLeft, Font::VAlignTop );
72}
73
74bool MenuSettingBool::handleButtonPress(InputManager::Button button)
75{
76    switch (button) {
77        case InputManager::ACCEPT:
78        case InputManager::LEFT:
79        case InputManager::RIGHT:
80            toggle();
81            break;
82        default:
83            return false;
84    }
85    return true;
86}
87
88void MenuSettingBool::toggle()
89{
90    setValue(!value());
91}
92
93void MenuSettingBool::setValue(int value)
94{
95    setValue(value != 0);
96}
97
98void MenuSettingBool::setValue(bool value)
99{
100    if (_value == NULL)
101        *_ivalue = value;
102    else
103        *_value = value;
104    strvalue = value ? "ON" : "OFF";
105}
106
107bool MenuSettingBool::value()
108{
109    if (_value == NULL)
110        return *_ivalue != 0;
111    else
112        return *_value;
113}
114
115bool MenuSettingBool::edited()
116{
117    return originalValue != value();
118}
119

Archive Download this file



interactive