blob: 7c7afa95715e162c57ea6588787e3eea66028e81 [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
2 Copyright 2013 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP 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 3 of the License, or
8 (at your option) any later version.
9
10 TWRP 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 TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
Dees_Troy51a0e822012-09-05 15:24:24 -040019#include <string.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040020#include <sys/stat.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040021#include <dirent.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040022
23extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000024#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040026}
27
28#include "rapidxml.hpp"
29#include "objects.hpp"
30#include "../data.hpp"
Dees_Troyeead9852013-02-15 14:31:06 -060031
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010032GUIListBox::GUIListBox(xml_node<>* node) : GUIScrollList(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040033{
Dees_Troyeead9852013-02-15 14:31:06 -060034 xml_attribute<>* attr;
35 xml_node<>* child;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010036 mIconSelected = mIconUnselected = NULL;
Dees_Troyeead9852013-02-15 14:31:06 -060037 mUpdate = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040038
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010039 // Get the icons, if any
Dees_Troyeead9852013-02-15 14:31:06 -060040 child = node->first_node("icon");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010041 if (child) {
thatf6ed8fc2015-02-14 20:23:16 +010042 mIconSelected = LoadAttrImage(child, "selected");
43 mIconUnselected = LoadAttrImage(child, "unselected");
Dees_Troyeead9852013-02-15 14:31:06 -060044 }
thatf6ed8fc2015-02-14 20:23:16 +010045 int iconWidth = std::max(mIconSelected->GetWidth(), mIconUnselected->GetWidth());
46 int iconHeight = std::max(mIconSelected->GetHeight(), mIconUnselected->GetHeight());
47 SetMaxIconSize(iconWidth, iconHeight);
Dees_Troyeead9852013-02-15 14:31:06 -060048
49 // Handle the result variable
50 child = node->first_node("data");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010051 if (child) {
Dees_Troyeead9852013-02-15 14:31:06 -060052 attr = child->first_attribute("name");
53 if (attr)
54 mVariable = attr->value();
55 attr = child->first_attribute("default");
56 if (attr)
57 DataManager::SetValue(mVariable, attr->value());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010058 // Get the currently selected value for the list
59 DataManager::GetValue(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -060060 }
61
Dees_Troy51a0e822012-09-05 15:24:24 -040062 // Get the data for the list
63 child = node->first_node("listitem");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020064 if (!child) return;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010065 while (child) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 ListData data;
Dees_Troy51a0e822012-09-05 15:24:24 -040067
68 attr = child->first_attribute("name");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020069 if (!attr) return;
70 data.displayName = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040071
72 data.variableValue = child->value();
Dees_Troyeead9852013-02-15 14:31:06 -060073 if (child->value() == currentValue) {
Dees_Troy51a0e822012-09-05 15:24:24 -040074 data.selected = 1;
Dees_Troyeead9852013-02-15 14:31:06 -060075 } else {
Dees_Troy51a0e822012-09-05 15:24:24 -040076 data.selected = 0;
Dees_Troyeead9852013-02-15 14:31:06 -060077 }
Dees_Troy51a0e822012-09-05 15:24:24 -040078
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020079 mList.push_back(data);
Dees_Troy51a0e822012-09-05 15:24:24 -040080
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020081 child = child->next_sibling("listitem");
82 }
Dees_Troy51a0e822012-09-05 15:24:24 -040083}
84
85GUIListBox::~GUIListBox()
86{
Dees_Troy51a0e822012-09-05 15:24:24 -040087}
88
89int GUIListBox::Update(void)
90{
Vojtech Bocekede51c52014-02-07 23:58:09 +010091 if(!isConditionTrue())
92 return 0;
93
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010094 GUIScrollList::Update();
Dees_Troyeead9852013-02-15 14:31:06 -060095
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010096 if (mUpdate) {
Dees_Troyeead9852013-02-15 14:31:06 -060097 mUpdate = 0;
98 if (Render() == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -040099 return 2;
Dees_Troyeead9852013-02-15 14:31:06 -0600100 }
Dees_Troyeead9852013-02-15 14:31:06 -0600101 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400102}
103
Vojtech Bocek07220562014-02-08 02:05:33 +0100104int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400105{
thatfa30aca2015-02-13 01:22:22 +0100106 GUIScrollList::NotifyVarChange(varName, value);
107
Vojtech Bocekede51c52014-02-07 23:58:09 +0100108 if(!isConditionTrue())
109 return 0;
110
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100111 // Check to see if the variable that we are using to store the list selected value has been updated
112 if (varName == mVariable) {
113 int i, listSize = mList.size();
Dees_Troy51a0e822012-09-05 15:24:24 -0400114
115 currentValue = value;
116
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100117 for (i = 0; i < listSize; i++) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400118 if (mList.at(i).variableValue == currentValue) {
119 mList.at(i).selected = 1;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100120 SetVisibleListLocation(i);
Dees_Troy51a0e822012-09-05 15:24:24 -0400121 } else
122 mList.at(i).selected = 0;
123 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400124 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200125 return 0;
126 }
Dees_Troyeead9852013-02-15 14:31:06 -0600127 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400128}
129
Dees_Troy51a0e822012-09-05 15:24:24 -0400130void GUIListBox::SetPageFocus(int inFocus)
131{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100132 GUIScrollList::SetPageFocus(inFocus);
133 if (inFocus) {
Dees_Troyeead9852013-02-15 14:31:06 -0600134 DataManager::GetValue(mVariable, currentValue);
135 NotifyVarChange(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -0600136 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400137}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100138
139size_t GUIListBox::GetItemCount()
140{
141 return mList.size();
142}
143
thatf6ed8fc2015-02-14 20:23:16 +0100144int GUIListBox::GetListItem(size_t item_index, ImageResource*& icon, std::string &text)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100145{
146 text = mList.at(item_index).displayName;
147 if (mList.at(item_index).selected)
148 icon = mIconSelected;
149 else
150 icon = mIconUnselected;
151 return 0;
152}
153
154void GUIListBox::NotifySelect(size_t item_selected)
155{
156 for (size_t i = 0; i < mList.size(); i++) {
157 mList.at(i).selected = 0;
158 }
159 if (item_selected < mList.size()) {
160 mList.at(item_selected).selected = 1;
161 string str = mList.at(item_selected).variableValue;
162 DataManager::SetValue(mVariable, str);
163 }
164 mUpdate = 1;
165}