blob: b0d2731fe3cfa22b7f367aa8b7bbcc66b2659d85 [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"
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 }
thatf6ed8fc2015-02-14 20:23:16 +010047 int iconWidth = std::max(mIconSelected->GetWidth(), mIconUnselected->GetWidth());
48 int iconHeight = std::max(mIconSelected->GetHeight(), mIconUnselected->GetHeight());
49 SetMaxIconSize(iconWidth, iconHeight);
Dees_Troyeead9852013-02-15 14:31:06 -060050
51 // Handle the result variable
Ethan Yonker21ff02a2015-02-18 14:35:00 -060052 child = FindNode(node, "data");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010053 if (child) {
Dees_Troyeead9852013-02-15 14:31:06 -060054 attr = child->first_attribute("name");
55 if (attr)
56 mVariable = attr->value();
57 attr = child->first_attribute("default");
58 if (attr)
59 DataManager::SetValue(mVariable, attr->value());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010060 // Get the currently selected value for the list
61 DataManager::GetValue(mVariable, currentValue);
Ethan Yonker74db1572015-10-28 12:44:49 -050062 if (mVariable == "tw_language") {
63 std::vector<language_struct>::iterator iter;
64 for (iter = Language_List.begin(); iter != Language_List.end(); iter++) {
65 ListItem data;
66 data.displayName = (*iter).displayvalue;
67 data.variableValue = (*iter).filename;
68 data.action = NULL;
69 if (currentValue == (*iter).filename) {
70 data.selected = 1;
71 DataManager::SetValue("tw_language_display", (*iter).displayvalue);
72 } else
73 data.selected = 0;
74 mListItems.push_back(data);
75 }
76 }
Dees_Troyeead9852013-02-15 14:31:06 -060077 }
thatc01391c2015-07-09 00:19:58 +020078 else
that8ab5c132015-09-13 23:00:54 +020079 allowSelection = false; // allows using listbox as a read-only list or menu
Dees_Troyeead9852013-02-15 14:31:06 -060080
Dees_Troy51a0e822012-09-05 15:24:24 -040081 // Get the data for the list
Ethan Yonker21ff02a2015-02-18 14:35:00 -060082 child = FindNode(node, "listitem");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 if (!child) return;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010084 while (child) {
that8ab5c132015-09-13 23:00:54 +020085 ListItem item;
Dees_Troy51a0e822012-09-05 15:24:24 -040086
87 attr = child->first_attribute("name");
thatc01391c2015-07-09 00:19:58 +020088 if (!attr)
89 continue;
Ethan Yonkerdedbb7f2016-01-17 18:17:38 -060090 // We will parse display names when we get page focus to ensure that translating takes place
91 item.displayName = attr->value();
that8ab5c132015-09-13 23:00:54 +020092 item.variableValue = gui_parse_text(child->value());
93 item.selected = (child->value() == currentValue);
94 item.action = NULL;
thatc01391c2015-07-09 00:19:58 +020095 xml_node<>* action = child->first_node("action");
96 if (action) {
that8ab5c132015-09-13 23:00:54 +020097 item.action = new GUIAction(action);
thatc01391c2015-07-09 00:19:58 +020098 allowSelection = true;
99 }
Ethan Yonker56ce3322015-07-30 15:04:32 -0500100 xml_node<>* variable_name = child->first_node("data");
101 if (variable_name) {
102 attr = variable_name->first_attribute("variable");
103 if (attr) {
that8ab5c132015-09-13 23:00:54 +0200104 item.variableName = attr->value();
105 item.selected = (DataManager::GetIntValue(item.variableName) != 0);
Ethan Yonker56ce3322015-07-30 15:04:32 -0500106 allowSelection = true;
107 isCheckList = true;
108 }
109 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400110
that8ab5c132015-09-13 23:00:54 +0200111 LoadConditions(child, item.mConditions);
112
113 mListItems.push_back(item);
114 mVisibleItems.push_back(mListItems.size()-1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400115
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200116 child = child->next_sibling("listitem");
117 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400118}
119
120GUIListBox::~GUIListBox()
121{
Dees_Troy51a0e822012-09-05 15:24:24 -0400122}
123
124int GUIListBox::Update(void)
125{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100126 if(!isConditionTrue())
127 return 0;
128
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100129 GUIScrollList::Update();
Dees_Troyeead9852013-02-15 14:31:06 -0600130
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100131 if (mUpdate) {
Dees_Troyeead9852013-02-15 14:31:06 -0600132 mUpdate = 0;
133 if (Render() == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400134 return 2;
Dees_Troyeead9852013-02-15 14:31:06 -0600135 }
Dees_Troyeead9852013-02-15 14:31:06 -0600136 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400137}
138
Vojtech Bocek07220562014-02-08 02:05:33 +0100139int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400140{
thatfa30aca2015-02-13 01:22:22 +0100141 GUIScrollList::NotifyVarChange(varName, value);
142
Vojtech Bocekede51c52014-02-07 23:58:09 +0100143 if(!isConditionTrue())
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) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400148 currentValue = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 }
that8ab5c132015-09-13 23:00:54 +0200151
152 std::vector<size_t> mVisibleItemsOld;
153 std::swap(mVisibleItemsOld, mVisibleItems);
154 for (size_t i = 0; i < mListItems.size(); i++) {
155 ListItem& item = mListItems[i];
156 // update per-item visibility condition
157 bool itemVisible = UpdateConditions(item.mConditions, varName);
158 if (itemVisible)
159 mVisibleItems.push_back(i);
160
161 if (isCheckList)
162 {
that35f17402015-11-02 20:15:07 +0100163 if (item.variableName == varName || varName.empty()) {
164 std::string val;
165 DataManager::GetValue(item.variableName, val);
166 item.selected = (val != "0");
that8ab5c132015-09-13 23:00:54 +0200167 mUpdate = 1;
168 }
169 }
170 else if (varName == mVariable) {
171 if (item.variableValue == currentValue) {
172 item.selected = 1;
173 SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1);
174 } else {
175 item.selected = 0;
176 }
177 }
178 }
179
180 if (mVisibleItemsOld != mVisibleItems) {
181 mUpdate = 1; // some item's visibility has changed
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500182 if (firstDisplayedItem >= (int)mVisibleItems.size()) {
that8ab5c132015-09-13 23:00:54 +0200183 // all items in the view area were removed - make last item visible
184 SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1);
185 }
186 }
187
Dees_Troyeead9852013-02-15 14:31:06 -0600188 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400189}
190
Dees_Troy51a0e822012-09-05 15:24:24 -0400191void GUIListBox::SetPageFocus(int inFocus)
192{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100193 GUIScrollList::SetPageFocus(inFocus);
194 if (inFocus) {
Ethan Yonkerdedbb7f2016-01-17 18:17:38 -0600195 if (!isTextParsed) {
196 isTextParsed = true;
197 for (size_t i = 0; i < mListItems.size(); i++) {
198 ListItem& item = mListItems[i];
199 item.displayName = gui_parse_text(item.displayName);
200 }
201 }
Dees_Troyeead9852013-02-15 14:31:06 -0600202 DataManager::GetValue(mVariable, currentValue);
203 NotifyVarChange(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -0600204 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400205}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100206
207size_t GUIListBox::GetItemCount()
208{
that8ab5c132015-09-13 23:00:54 +0200209 return mVisibleItems.size();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100210}
211
that0af77952015-02-25 08:52:19 +0100212void GUIListBox::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100213{
that0af77952015-02-25 08:52:19 +0100214 // note: the "selected" parameter above is for the currently touched item
215 // don't confuse it with the more persistent "selected" flag per list item used below
that8ab5c132015-09-13 23:00:54 +0200216 ListItem& item = mListItems[mVisibleItems[itemindex]];
217 ImageResource* icon = item.selected ? mIconSelected : mIconUnselected;
218 const std::string& text = item.displayName;
that0af77952015-02-25 08:52:19 +0100219
220 RenderStdItem(yPos, selected, icon, text.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100221}
222
223void GUIListBox::NotifySelect(size_t item_selected)
224{
Ethan Yonker56ce3322015-07-30 15:04:32 -0500225 if (!isCheckList) {
that8ab5c132015-09-13 23:00:54 +0200226 // deselect all items, even invisible ones
227 for (size_t i = 0; i < mListItems.size(); i++) {
228 mListItems[i].selected = 0;
Ethan Yonker56ce3322015-07-30 15:04:32 -0500229 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100230 }
that8ab5c132015-09-13 23:00:54 +0200231
232 ListItem& item = mListItems[mVisibleItems[item_selected]];
233
234 if (isCheckList) {
235 int selected = 1 - item.selected;
236 item.selected = selected;
237 DataManager::SetValue(item.variableName, selected ? "1" : "0");
238 } else {
239 item.selected = 1;
240 string str = item.variableValue; // [check] should this set currentValue instead?
241 DataManager::SetValue(mVariable, str);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100242 }
that8ab5c132015-09-13 23:00:54 +0200243 if (item.action)
244 item.action->doActions();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100245 mUpdate = 1;
246}