blob: d2973bb6e256952ef2429206ed1af58ed65a6824 [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 {
that35f17402015-11-02 20:15:07 +0100144 if (item.variableName == varName || varName.empty()) {
145 std::string val;
146 DataManager::GetValue(item.variableName, val);
147 item.selected = (val != "0");
that8ab5c132015-09-13 23:00:54 +0200148 mUpdate = 1;
149 }
150 }
151 else if (varName == mVariable) {
152 if (item.variableValue == currentValue) {
153 item.selected = 1;
154 SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1);
155 } else {
156 item.selected = 0;
157 }
158 }
159 }
160
161 if (mVisibleItemsOld != mVisibleItems) {
162 mUpdate = 1; // some item's visibility has changed
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500163 if (firstDisplayedItem >= (int)mVisibleItems.size()) {
that8ab5c132015-09-13 23:00:54 +0200164 // all items in the view area were removed - make last item visible
165 SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1);
166 }
167 }
168
Dees_Troyeead9852013-02-15 14:31:06 -0600169 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400170}
171
Dees_Troy51a0e822012-09-05 15:24:24 -0400172void GUIListBox::SetPageFocus(int inFocus)
173{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100174 GUIScrollList::SetPageFocus(inFocus);
175 if (inFocus) {
Dees_Troyeead9852013-02-15 14:31:06 -0600176 DataManager::GetValue(mVariable, currentValue);
177 NotifyVarChange(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -0600178 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400179}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100180
181size_t GUIListBox::GetItemCount()
182{
that8ab5c132015-09-13 23:00:54 +0200183 return mVisibleItems.size();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100184}
185
that0af77952015-02-25 08:52:19 +0100186void GUIListBox::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100187{
that0af77952015-02-25 08:52:19 +0100188 // note: the "selected" parameter above is for the currently touched item
189 // don't confuse it with the more persistent "selected" flag per list item used below
that8ab5c132015-09-13 23:00:54 +0200190 ListItem& item = mListItems[mVisibleItems[itemindex]];
191 ImageResource* icon = item.selected ? mIconSelected : mIconUnselected;
192 const std::string& text = item.displayName;
that0af77952015-02-25 08:52:19 +0100193
194 RenderStdItem(yPos, selected, icon, text.c_str());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100195}
196
197void GUIListBox::NotifySelect(size_t item_selected)
198{
Ethan Yonker56ce3322015-07-30 15:04:32 -0500199 if (!isCheckList) {
that8ab5c132015-09-13 23:00:54 +0200200 // deselect all items, even invisible ones
201 for (size_t i = 0; i < mListItems.size(); i++) {
202 mListItems[i].selected = 0;
Ethan Yonker56ce3322015-07-30 15:04:32 -0500203 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100204 }
that8ab5c132015-09-13 23:00:54 +0200205
206 ListItem& item = mListItems[mVisibleItems[item_selected]];
207
208 if (isCheckList) {
209 int selected = 1 - item.selected;
210 item.selected = selected;
211 DataManager::SetValue(item.variableName, selected ? "1" : "0");
212 } else {
213 item.selected = 1;
214 string str = item.variableValue; // [check] should this set currentValue instead?
215 DataManager::SetValue(mVariable, str);
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100216 }
that8ab5c132015-09-13 23:00:54 +0200217 if (item.action)
218 item.action->doActions();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100219 mUpdate = 1;
220}