blob: 9fbe09234d12c02c39561cead758beae75936826 [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}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010024#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040025
26#include "rapidxml.hpp"
27#include "objects.hpp"
28#include "../data.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050029#include "pages.hpp"
30
31extern std::vector<language_struct> Language_List;
Dees_Troyeead9852013-02-15 14:31:06 -060032
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010033GUIListBox::GUIListBox(xml_node<>* node) : GUIScrollList(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040034{
Dees_Troyeead9852013-02-15 14:31:06 -060035 xml_attribute<>* attr;
36 xml_node<>* child;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010037 mIconSelected = mIconUnselected = NULL;
Dees_Troyeead9852013-02-15 14:31:06 -060038 mUpdate = 0;
Ethan Yonkerdedbb7f2016-01-17 18:17:38 -060039 isCheckList = isTextParsed = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040040
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010041 // Get the icons, if any
Ethan Yonker21ff02a2015-02-18 14:35:00 -060042 child = FindNode(node, "icon");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010043 if (child) {
thatf6ed8fc2015-02-14 20:23:16 +010044 mIconSelected = LoadAttrImage(child, "selected");
45 mIconUnselected = LoadAttrImage(child, "unselected");
Dees_Troyeead9852013-02-15 14:31:06 -060046 }
Ethan Yonker58f21322018-08-24 11:17:36 -050047 int iconWidth = 0, iconHeight = 0;
48 if (mIconSelected && mIconSelected->GetResource() && mIconUnselected && mIconUnselected->GetResource()) {
49 iconWidth = std::max(mIconSelected->GetWidth(), mIconUnselected->GetWidth());
50 iconHeight = std::max(mIconSelected->GetHeight(), mIconUnselected->GetHeight());
51 } else if (mIconSelected && mIconSelected->GetResource()) {
52 iconWidth = mIconSelected->GetWidth();
53 iconHeight = mIconSelected->GetHeight();
54 } else if (mIconUnselected && mIconUnselected->GetResource()) {
55 iconWidth = mIconUnselected->GetWidth();
56 iconHeight = mIconUnselected->GetHeight();
57 }
thatf6ed8fc2015-02-14 20:23:16 +010058 SetMaxIconSize(iconWidth, iconHeight);
Dees_Troyeead9852013-02-15 14:31:06 -060059
60 // Handle the result variable
Ethan Yonker21ff02a2015-02-18 14:35:00 -060061 child = FindNode(node, "data");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010062 if (child) {
Dees_Troyeead9852013-02-15 14:31:06 -060063 attr = child->first_attribute("name");
64 if (attr)
65 mVariable = attr->value();
66 attr = child->first_attribute("default");
67 if (attr)
68 DataManager::SetValue(mVariable, attr->value());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010069 // Get the currently selected value for the list
70 DataManager::GetValue(mVariable, currentValue);
Ethan Yonker74db1572015-10-28 12:44:49 -050071 if (mVariable == "tw_language") {
72 std::vector<language_struct>::iterator iter;
73 for (iter = Language_List.begin(); iter != Language_List.end(); iter++) {
74 ListItem data;
75 data.displayName = (*iter).displayvalue;
76 data.variableValue = (*iter).filename;
77 data.action = NULL;
78 if (currentValue == (*iter).filename) {
79 data.selected = 1;
80 DataManager::SetValue("tw_language_display", (*iter).displayvalue);
81 } else
82 data.selected = 0;
83 mListItems.push_back(data);
84 }
85 }
Dees_Troyeead9852013-02-15 14:31:06 -060086 }
thatc01391c2015-07-09 00:19:58 +020087 else
that8ab5c132015-09-13 23:00:54 +020088 allowSelection = false; // allows using listbox as a read-only list or menu
Dees_Troyeead9852013-02-15 14:31:06 -060089
Dees_Troy51a0e822012-09-05 15:24:24 -040090 // Get the data for the list
Ethan Yonker21ff02a2015-02-18 14:35:00 -060091 child = FindNode(node, "listitem");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 if (!child) return;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010093 while (child) {
that8ab5c132015-09-13 23:00:54 +020094 ListItem item;
Dees_Troy51a0e822012-09-05 15:24:24 -040095
96 attr = child->first_attribute("name");
thatc01391c2015-07-09 00:19:58 +020097 if (!attr)
98 continue;
Ethan Yonkerdedbb7f2016-01-17 18:17:38 -060099 // We will parse display names when we get page focus to ensure that translating takes place
100 item.displayName = attr->value();
that8ab5c132015-09-13 23:00:54 +0200101 item.variableValue = gui_parse_text(child->value());
102 item.selected = (child->value() == currentValue);
103 item.action = NULL;
thatc01391c2015-07-09 00:19:58 +0200104 xml_node<>* action = child->first_node("action");
Ethan Yonker64e0a652018-07-25 09:52:17 -0500105 if (!action)
106 action = child->first_node("actions");
thatc01391c2015-07-09 00:19:58 +0200107 if (action) {
Ethan Yonker64e0a652018-07-25 09:52:17 -0500108 item.action = new GUIAction(child);
thatc01391c2015-07-09 00:19:58 +0200109 allowSelection = true;
110 }
Ethan Yonker56ce3322015-07-30 15:04:32 -0500111 xml_node<>* variable_name = child->first_node("data");
112 if (variable_name) {
113 attr = variable_name->first_attribute("variable");
114 if (attr) {
that8ab5c132015-09-13 23:00:54 +0200115 item.variableName = attr->value();
116 item.selected = (DataManager::GetIntValue(item.variableName) != 0);
Ethan Yonker56ce3322015-07-30 15:04:32 -0500117 allowSelection = true;
118 isCheckList = true;
119 }
120 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400121
that8ab5c132015-09-13 23:00:54 +0200122 LoadConditions(child, item.mConditions);
123
124 mListItems.push_back(item);
125 mVisibleItems.push_back(mListItems.size()-1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400126
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200127 child = child->next_sibling("listitem");
128 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400129}
130
131GUIListBox::~GUIListBox()
132{
Dees_Troy51a0e822012-09-05 15:24:24 -0400133}
134
135int GUIListBox::Update(void)
136{
Matt Mowera8a89d12016-12-30 18:10:37 -0600137 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100138 return 0;
139
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100140 GUIScrollList::Update();
Dees_Troyeead9852013-02-15 14:31:06 -0600141
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100142 if (mUpdate) {
Dees_Troyeead9852013-02-15 14:31:06 -0600143 mUpdate = 0;
144 if (Render() == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 return 2;
Dees_Troyeead9852013-02-15 14:31:06 -0600146 }
Dees_Troyeead9852013-02-15 14:31:06 -0600147 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400148}
149
Vojtech Bocek07220562014-02-08 02:05:33 +0100150int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400151{
thatfa30aca2015-02-13 01:22:22 +0100152 GUIScrollList::NotifyVarChange(varName, value);
153
Matt Mowera8a89d12016-12-30 18:10:37 -0600154 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100155 return 0;
156
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100157 // Check to see if the variable that we are using to store the list selected value has been updated
158 if (varName == mVariable) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400159 currentValue = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400160 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200161 }
that8ab5c132015-09-13 23:00:54 +0200162
163 std::vector<size_t> mVisibleItemsOld;
164 std::swap(mVisibleItemsOld, mVisibleItems);
165 for (size_t i = 0; i < mListItems.size(); i++) {
166 ListItem& item = mListItems[i];
167 // update per-item visibility condition
168 bool itemVisible = UpdateConditions(item.mConditions, varName);
169 if (itemVisible)
170 mVisibleItems.push_back(i);
171
172 if (isCheckList)
173 {
that35f17402015-11-02 20:15:07 +0100174 if (item.variableName == varName || varName.empty()) {
175 std::string val;
176 DataManager::GetValue(item.variableName, val);
177 item.selected = (val != "0");
that8ab5c132015-09-13 23:00:54 +0200178 mUpdate = 1;
179 }
180 }
181 else if (varName == mVariable) {
182 if (item.variableValue == currentValue) {
183 item.selected = 1;
184 SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1);
185 } else {
186 item.selected = 0;
187 }
188 }
189 }
190
191 if (mVisibleItemsOld != mVisibleItems) {
192 mUpdate = 1; // some item's visibility has changed
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500193 if (firstDisplayedItem >= (int)mVisibleItems.size()) {
that8ab5c132015-09-13 23:00:54 +0200194 // all items in the view area were removed - make last item visible
195 SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1);
196 }
197 }
198
Dees_Troyeead9852013-02-15 14:31:06 -0600199 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400200}
201
Dees_Troy51a0e822012-09-05 15:24:24 -0400202void GUIListBox::SetPageFocus(int inFocus)
203{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100204 GUIScrollList::SetPageFocus(inFocus);
205 if (inFocus) {
Ethan Yonkerdedbb7f2016-01-17 18:17:38 -0600206 if (!isTextParsed) {
207 isTextParsed = true;
208 for (size_t i = 0; i < mListItems.size(); i++) {
209 ListItem& item = mListItems[i];
210 item.displayName = gui_parse_text(item.displayName);
211 }
212 }
Dees_Troyeead9852013-02-15 14:31:06 -0600213 DataManager::GetValue(mVariable, currentValue);
214 NotifyVarChange(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -0600215 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400216}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100217
218size_t GUIListBox::GetItemCount()
219{
that8ab5c132015-09-13 23:00:54 +0200220 return mVisibleItems.size();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100221}
222
that0af77952015-02-25 08:52:19 +0100223void GUIListBox::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100224{
that0af77952015-02-25 08:52:19 +0100225 // note: the "selected" parameter above is for the currently touched item
226 // don't confuse it with the more persistent "selected" flag per list item used below
that8ab5c132015-09-13 23:00:54 +0200227 ListItem& item = mListItems[mVisibleItems[itemindex]];
228 ImageResource* icon = item.selected ? mIconSelected : mIconUnselected;
229 const std::string& text = item.displayName;
that0af77952015-02-25 08:52:19 +0100230
231 RenderStdItem(yPos, selected, icon, text.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100232}
233
234void GUIListBox::NotifySelect(size_t item_selected)
235{
Ethan Yonker56ce3322015-07-30 15:04:32 -0500236 if (!isCheckList) {
that8ab5c132015-09-13 23:00:54 +0200237 // deselect all items, even invisible ones
238 for (size_t i = 0; i < mListItems.size(); i++) {
239 mListItems[i].selected = 0;
Ethan Yonker56ce3322015-07-30 15:04:32 -0500240 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100241 }
that8ab5c132015-09-13 23:00:54 +0200242
243 ListItem& item = mListItems[mVisibleItems[item_selected]];
244
245 if (isCheckList) {
246 int selected = 1 - item.selected;
247 item.selected = selected;
248 DataManager::SetValue(item.variableName, selected ? "1" : "0");
249 } else {
250 item.selected = 1;
251 string str = item.variableValue; // [check] should this set currentValue instead?
252 DataManager::SetValue(mVariable, str);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100253 }
that8ab5c132015-09-13 23:00:54 +0200254 if (item.action)
255 item.action->doActions();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100256 mUpdate = 1;
257}