blob: 0a53a7c88fb41804de522fae6634ceb7d36cae2b [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
that8ab5c132015-09-13 23:00:54 +020061 allowSelection = false; // allows using listbox as a read-only list or menu
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) {
that8ab5c132015-09-13 23:00:54 +020067 ListItem item;
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;
that8ab5c132015-09-13 23:00:54 +020072 item.displayName = gui_parse_text(attr->value());
73 item.variableValue = gui_parse_text(child->value());
74 item.selected = (child->value() == currentValue);
75 item.action = NULL;
thatc01391c2015-07-09 00:19:58 +020076 xml_node<>* action = child->first_node("action");
77 if (action) {
that8ab5c132015-09-13 23:00:54 +020078 item.action = new GUIAction(action);
thatc01391c2015-07-09 00:19:58 +020079 allowSelection = true;
80 }
Ethan Yonker56ce3322015-07-30 15:04:32 -050081 xml_node<>* variable_name = child->first_node("data");
82 if (variable_name) {
83 attr = variable_name->first_attribute("variable");
84 if (attr) {
that8ab5c132015-09-13 23:00:54 +020085 item.variableName = attr->value();
86 item.selected = (DataManager::GetIntValue(item.variableName) != 0);
Ethan Yonker56ce3322015-07-30 15:04:32 -050087 allowSelection = true;
88 isCheckList = true;
89 }
90 }
Dees_Troy51a0e822012-09-05 15:24:24 -040091
that8ab5c132015-09-13 23:00:54 +020092 LoadConditions(child, item.mConditions);
93
94 mListItems.push_back(item);
95 mVisibleItems.push_back(mListItems.size()-1);
Dees_Troy51a0e822012-09-05 15:24:24 -040096
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097 child = child->next_sibling("listitem");
98 }
Dees_Troy51a0e822012-09-05 15:24:24 -040099}
100
101GUIListBox::~GUIListBox()
102{
Dees_Troy51a0e822012-09-05 15:24:24 -0400103}
104
105int GUIListBox::Update(void)
106{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100107 if(!isConditionTrue())
108 return 0;
109
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100110 GUIScrollList::Update();
Dees_Troyeead9852013-02-15 14:31:06 -0600111
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100112 if (mUpdate) {
Dees_Troyeead9852013-02-15 14:31:06 -0600113 mUpdate = 0;
114 if (Render() == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400115 return 2;
Dees_Troyeead9852013-02-15 14:31:06 -0600116 }
Dees_Troyeead9852013-02-15 14:31:06 -0600117 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400118}
119
Vojtech Bocek07220562014-02-08 02:05:33 +0100120int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400121{
thatfa30aca2015-02-13 01:22:22 +0100122 GUIScrollList::NotifyVarChange(varName, value);
123
Vojtech Bocekede51c52014-02-07 23:58:09 +0100124 if(!isConditionTrue())
125 return 0;
126
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100127 // Check to see if the variable that we are using to store the list selected value has been updated
128 if (varName == mVariable) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400129 currentValue = value;
Dees_Troy51a0e822012-09-05 15:24:24 -0400130 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200131 }
that8ab5c132015-09-13 23:00:54 +0200132
133 std::vector<size_t> mVisibleItemsOld;
134 std::swap(mVisibleItemsOld, mVisibleItems);
135 for (size_t i = 0; i < mListItems.size(); i++) {
136 ListItem& item = mListItems[i];
137 // update per-item visibility condition
138 bool itemVisible = UpdateConditions(item.mConditions, varName);
139 if (itemVisible)
140 mVisibleItems.push_back(i);
141
142 if (isCheckList)
143 {
144 if (item.variableName == varName) {
145 item.selected = (value != "0");
146 mUpdate = 1;
147 }
148 }
149 else if (varName == mVariable) {
150 if (item.variableValue == currentValue) {
151 item.selected = 1;
152 SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1);
153 } else {
154 item.selected = 0;
155 }
156 }
157 }
158
159 if (mVisibleItemsOld != mVisibleItems) {
160 mUpdate = 1; // some item's visibility has changed
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500161 if (firstDisplayedItem >= (int)mVisibleItems.size()) {
that8ab5c132015-09-13 23:00:54 +0200162 // all items in the view area were removed - make last item visible
163 SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1);
164 }
165 }
166
Dees_Troyeead9852013-02-15 14:31:06 -0600167 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400168}
169
Dees_Troy51a0e822012-09-05 15:24:24 -0400170void GUIListBox::SetPageFocus(int inFocus)
171{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100172 GUIScrollList::SetPageFocus(inFocus);
173 if (inFocus) {
Dees_Troyeead9852013-02-15 14:31:06 -0600174 DataManager::GetValue(mVariable, currentValue);
175 NotifyVarChange(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -0600176 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400177}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100178
179size_t GUIListBox::GetItemCount()
180{
that8ab5c132015-09-13 23:00:54 +0200181 return mVisibleItems.size();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100182}
183
that0af77952015-02-25 08:52:19 +0100184void GUIListBox::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100185{
that0af77952015-02-25 08:52:19 +0100186 // note: the "selected" parameter above is for the currently touched item
187 // don't confuse it with the more persistent "selected" flag per list item used below
that8ab5c132015-09-13 23:00:54 +0200188 ListItem& item = mListItems[mVisibleItems[itemindex]];
189 ImageResource* icon = item.selected ? mIconSelected : mIconUnselected;
190 const std::string& text = item.displayName;
that0af77952015-02-25 08:52:19 +0100191
192 RenderStdItem(yPos, selected, icon, text.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100193}
194
195void GUIListBox::NotifySelect(size_t item_selected)
196{
Ethan Yonker56ce3322015-07-30 15:04:32 -0500197 if (!isCheckList) {
that8ab5c132015-09-13 23:00:54 +0200198 // deselect all items, even invisible ones
199 for (size_t i = 0; i < mListItems.size(); i++) {
200 mListItems[i].selected = 0;
Ethan Yonker56ce3322015-07-30 15:04:32 -0500201 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100202 }
that8ab5c132015-09-13 23:00:54 +0200203
204 ListItem& item = mListItems[mVisibleItems[item_selected]];
205
206 if (isCheckList) {
207 int selected = 1 - item.selected;
208 item.selected = selected;
209 DataManager::SetValue(item.variableName, selected ? "1" : "0");
210 } else {
211 item.selected = 1;
212 string str = item.variableValue; // [check] should this set currentValue instead?
213 DataManager::SetValue(mVariable, str);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100214 }
that8ab5c132015-09-13 23:00:54 +0200215 if (item.action)
216 item.action->doActions();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100217 mUpdate = 1;
218}