Root/src/menusettingmultistring.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 "delegate.h"
22#include "menusettingmultistring.h"
23#include "gmenu2x.h"
24#include "iconbutton.h"
25
26#include <algorithm>
27
28using std::find;
29using std::string;
30using std::vector;
31
32MenuSettingMultiString::MenuSettingMultiString(
33        GMenu2X *gmenu2x, Touchscreen &ts,
34        const string &name, const string &description,
35        string *value, const vector<string> *choices_)
36    : MenuSettingStringBase(gmenu2x, name, description, value)
37    , choices(choices_)
38{
39    setSel(find(choices->begin(), choices->end(), *value) - choices->begin());
40
41    IconButton *btn;
42
43    btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/left.png");
44    btn->setAction(BIND(&MenuSettingMultiString::decSel));
45    buttonBox.add(btn);
46
47    btn = new IconButton(gmenu2x, ts, "skin:imgs/buttons/right.png", gmenu2x->tr["Change value"]);
48    btn->setAction(BIND(&MenuSettingMultiString::incSel));
49    buttonBox.add(btn);
50}
51
52bool MenuSettingMultiString::handleButtonPress(InputManager::Button button)
53{
54    switch (button) {
55        case InputManager::LEFT:
56            decSel();
57            break;
58        case InputManager::RIGHT:
59            incSel();
60            break;
61        default:
62            return false;
63    }
64    return true;
65}
66
67void MenuSettingMultiString::incSel()
68{
69    setSel(selected + 1);
70}
71
72void MenuSettingMultiString::decSel()
73{
74    setSel(selected - 1);
75}
76
77void MenuSettingMultiString::setSel(int sel)
78{
79    if (sel < 0) {
80        sel = choices->size()-1;
81    } else if (sel >= (int)choices->size()) {
82        sel = 0;
83    }
84    selected = sel;
85    setValue((*choices)[sel]);
86}
87

Archive Download this file



interactive