blob: d82736e39b2cf06503d834baa97799a343508c6d [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
21extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000022#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040023#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040024}
25
26#include "rapidxml.hpp"
27#include "objects.hpp"
28#include "../data.hpp"
Dees_Troyeead9852013-02-15 14:31:06 -060029
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010030GUIListBox::GUIListBox(xml_node<>* node) : GUIScrollList(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040031{
Dees_Troyeead9852013-02-15 14:31:06 -060032 xml_attribute<>* attr;
33 xml_node<>* child;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010034 mIconSelected = mIconUnselected = NULL;
Dees_Troyeead9852013-02-15 14:31:06 -060035 mUpdate = 0;
Ethan Yonker56ce3322015-07-30 15:04:32 -050036 isCheckList = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040037
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010038 // Get the icons, if any
Ethan Yonker21ff02a2015-02-18 14:35:00 -060039 child = FindNode(node, "icon");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010040 if (child) {
thatf6ed8fc2015-02-14 20:23:16 +010041 mIconSelected = LoadAttrImage(child, "selected");
42 mIconUnselected = LoadAttrImage(child, "unselected");
Dees_Troyeead9852013-02-15 14:31:06 -060043 }
thatf6ed8fc2015-02-14 20:23:16 +010044 int iconWidth = std::max(mIconSelected->GetWidth(), mIconUnselected->GetWidth());
45 int iconHeight = std::max(mIconSelected->GetHeight(), mIconUnselected->GetHeight());
46 SetMaxIconSize(iconWidth, iconHeight);
Dees_Troyeead9852013-02-15 14:31:06 -060047
48 // Handle the result variable
Ethan Yonker21ff02a2015-02-18 14:35:00 -060049 child = FindNode(node, "data");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010050 if (child) {
Dees_Troyeead9852013-02-15 14:31:06 -060051 attr = child->first_attribute("name");
52 if (attr)
53 mVariable = attr->value();
54 attr = child->first_attribute("default");
55 if (attr)
56 DataManager::SetValue(mVariable, attr->value());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010057 // Get the currently selected value for the list
58 DataManager::GetValue(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -060059 }
thatc01391c2015-07-09 00:19:58 +020060 else
61 allowSelection = false; // allows using listbox as a read-only list
Dees_Troyeead9852013-02-15 14:31:06 -060062
Dees_Troy51a0e822012-09-05 15:24:24 -040063 // Get the data for the list
Ethan Yonker21ff02a2015-02-18 14:35:00 -060064 child = FindNode(node, "listitem");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020065 if (!child) return;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010066 while (child) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020067 ListData data;
Dees_Troy51a0e822012-09-05 15:24:24 -040068
69 attr = child->first_attribute("name");
thatc01391c2015-07-09 00:19:58 +020070 if (!attr)
71 continue;
72 data.displayName = gui_parse_text(attr->value());
73 data.variableValue = gui_parse_text(child->value());
Dees_Troyeead9852013-02-15 14:31:06 -060074 if (child->value() == currentValue) {
Dees_Troy51a0e822012-09-05 15:24:24 -040075 data.selected = 1;
Dees_Troyeead9852013-02-15 14:31:06 -060076 } else {
Dees_Troy51a0e822012-09-05 15:24:24 -040077 data.selected = 0;
Dees_Troyeead9852013-02-15 14:31:06 -060078 }
thatc01391c2015-07-09 00:19:58 +020079 data.action = NULL;
80 xml_node<>* action = child->first_node("action");
81 if (action) {
82 data.action = new GUIAction(action);
83 allowSelection = true;
84 }
Ethan Yonker56ce3322015-07-30 15:04:32 -050085 xml_node<>* variable_name = child->first_node("data");
86 if (variable_name) {
87 attr = variable_name->first_attribute("variable");
88 if (attr) {
89 data.variableName = attr->value();
90 if (DataManager::GetIntValue(data.variableName) == 0)
91 data.selected = 0;
92 else
93 data.selected = 1;
94 allowSelection = true;
95 isCheckList = true;
96 }
97 }
Dees_Troy51a0e822012-09-05 15:24:24 -040098
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 mList.push_back(data);
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 child = child->next_sibling("listitem");
102 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400103}
104
105GUIListBox::~GUIListBox()
106{
Dees_Troy51a0e822012-09-05 15:24:24 -0400107}
108
109int GUIListBox::Update(void)
110{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100111 if(!isConditionTrue())
112 return 0;
113
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100114 GUIScrollList::Update();
Dees_Troyeead9852013-02-15 14:31:06 -0600115
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100116 if (mUpdate) {
Dees_Troyeead9852013-02-15 14:31:06 -0600117 mUpdate = 0;
118 if (Render() == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400119 return 2;
Dees_Troyeead9852013-02-15 14:31:06 -0600120 }
Dees_Troyeead9852013-02-15 14:31:06 -0600121 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400122}
123
Vojtech Bocek07220562014-02-08 02:05:33 +0100124int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400125{
thatfa30aca2015-02-13 01:22:22 +0100126 GUIScrollList::NotifyVarChange(varName, value);
127
Vojtech Bocekede51c52014-02-07 23:58:09 +0100128 if(!isConditionTrue())
129 return 0;
130
Ethan Yonker56ce3322015-07-30 15:04:32 -0500131 if (isCheckList) {
132 int i, listSize = mList.size();
133
134 for (i = 0; i < listSize; i++) {
135 if (mList.at(i).variableName == varName) {
136 if (value == "0") {
137 mList.at(i).selected = 0;
138 } else {
139 mList.at(i).selected = 1;
140 }
141 }
142 }
143 mUpdate = 1;
144 return 0;
145 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100146 // Check to see if the variable that we are using to store the list selected value has been updated
147 if (varName == mVariable) {
148 int i, listSize = mList.size();
Dees_Troy51a0e822012-09-05 15:24:24 -0400149
150 currentValue = value;
151
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100152 for (i = 0; i < listSize; i++) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400153 if (mList.at(i).variableValue == currentValue) {
154 mList.at(i).selected = 1;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100155 SetVisibleListLocation(i);
Dees_Troy51a0e822012-09-05 15:24:24 -0400156 } else
157 mList.at(i).selected = 0;
158 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400159 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 return 0;
161 }
Dees_Troyeead9852013-02-15 14:31:06 -0600162 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400163}
164
Dees_Troy51a0e822012-09-05 15:24:24 -0400165void GUIListBox::SetPageFocus(int inFocus)
166{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100167 GUIScrollList::SetPageFocus(inFocus);
168 if (inFocus) {
Dees_Troyeead9852013-02-15 14:31:06 -0600169 DataManager::GetValue(mVariable, currentValue);
170 NotifyVarChange(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -0600171 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400172}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100173
174size_t GUIListBox::GetItemCount()
175{
176 return mList.size();
177}
178
that0af77952015-02-25 08:52:19 +0100179void GUIListBox::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100180{
that0af77952015-02-25 08:52:19 +0100181 // note: the "selected" parameter above is for the currently touched item
182 // don't confuse it with the more persistent "selected" flag per list item used below
183 ImageResource* icon = mList.at(itemindex).selected ? mIconSelected : mIconUnselected;
184 const std::string& text = mList.at(itemindex).displayName;
185
186 RenderStdItem(yPos, selected, icon, text.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100187}
188
189void GUIListBox::NotifySelect(size_t item_selected)
190{
Ethan Yonker56ce3322015-07-30 15:04:32 -0500191 if (!isCheckList) {
192 for (size_t i = 0; i < mList.size(); i++) {
193 mList.at(i).selected = 0;
194 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100195 }
196 if (item_selected < mList.size()) {
thatc01391c2015-07-09 00:19:58 +0200197 ListData& data = mList.at(item_selected);
Ethan Yonker56ce3322015-07-30 15:04:32 -0500198 if (isCheckList) {
199 if (data.selected) {
200 data.selected = 0;
201 DataManager::SetValue(data.variableName, "0");
202 } else {
203 data.selected = 1;
204 DataManager::SetValue(data.variableName, "1");
205 }
206 } else {
207 data.selected = 1;
208 string str = data.variableValue; // [check] should this set currentValue instead?
209 DataManager::SetValue(mVariable, str);
210 }
thatc01391c2015-07-09 00:19:58 +0200211 if (data.action)
212 data.action->doActions();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100213 }
214 mUpdate = 1;
215}