Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1 | /* |
| 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | #include <string.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 20 | |
| 21 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 22 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 24 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | |
| 26 | #include "rapidxml.hpp" |
| 27 | #include "objects.hpp" |
| 28 | #include "../data.hpp" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 29 | #include "pages.hpp" |
| 30 | |
| 31 | extern std::vector<language_struct> Language_List; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 32 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 33 | GUIListBox::GUIListBox(xml_node<>* node) : GUIScrollList(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 35 | xml_attribute<>* attr; |
| 36 | xml_node<>* child; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 37 | mIconSelected = mIconUnselected = NULL; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 38 | mUpdate = 0; |
Ethan Yonker | dedbb7f | 2016-01-17 18:17:38 -0600 | [diff] [blame] | 39 | isCheckList = isTextParsed = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 41 | // Get the icons, if any |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 42 | child = FindNode(node, "icon"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 43 | if (child) { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 44 | mIconSelected = LoadAttrImage(child, "selected"); |
| 45 | mIconUnselected = LoadAttrImage(child, "unselected"); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 46 | } |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 47 | 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 | } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 58 | SetMaxIconSize(iconWidth, iconHeight); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 59 | |
| 60 | // Handle the result variable |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 61 | child = FindNode(node, "data"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 62 | if (child) { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 63 | 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 Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 69 | // Get the currently selected value for the list |
| 70 | DataManager::GetValue(mVariable, currentValue); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 71 | 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_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 86 | } |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 87 | else |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 88 | allowSelection = false; // allows using listbox as a read-only list or menu |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 89 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 90 | // Get the data for the list |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 91 | child = FindNode(node, "listitem"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 92 | if (!child) return; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 93 | while (child) { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 94 | ListItem item; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 95 | |
| 96 | attr = child->first_attribute("name"); |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 97 | if (!attr) |
| 98 | continue; |
Ethan Yonker | dedbb7f | 2016-01-17 18:17:38 -0600 | [diff] [blame] | 99 | // We will parse display names when we get page focus to ensure that translating takes place |
| 100 | item.displayName = attr->value(); |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 101 | item.variableValue = gui_parse_text(child->value()); |
| 102 | item.selected = (child->value() == currentValue); |
| 103 | item.action = NULL; |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 104 | xml_node<>* action = child->first_node("action"); |
| 105 | if (action) { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 106 | item.action = new GUIAction(action); |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 107 | allowSelection = true; |
| 108 | } |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 109 | xml_node<>* variable_name = child->first_node("data"); |
| 110 | if (variable_name) { |
| 111 | attr = variable_name->first_attribute("variable"); |
| 112 | if (attr) { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 113 | item.variableName = attr->value(); |
| 114 | item.selected = (DataManager::GetIntValue(item.variableName) != 0); |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 115 | allowSelection = true; |
| 116 | isCheckList = true; |
| 117 | } |
| 118 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 120 | LoadConditions(child, item.mConditions); |
| 121 | |
| 122 | mListItems.push_back(item); |
| 123 | mVisibleItems.push_back(mListItems.size()-1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 124 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 125 | child = child->next_sibling("listitem"); |
| 126 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | GUIListBox::~GUIListBox() |
| 130 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | int GUIListBox::Update(void) |
| 134 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 135 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 136 | return 0; |
| 137 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 138 | GUIScrollList::Update(); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 139 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 140 | if (mUpdate) { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 141 | mUpdate = 0; |
| 142 | if (Render() == 0) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | return 2; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 144 | } |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 145 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 148 | int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 149 | { |
that | fa30aca | 2015-02-13 01:22:22 +0100 | [diff] [blame] | 150 | GUIScrollList::NotifyVarChange(varName, value); |
| 151 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 152 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 153 | return 0; |
| 154 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 155 | // Check to see if the variable that we are using to store the list selected value has been updated |
| 156 | if (varName == mVariable) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 157 | currentValue = value; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 158 | mUpdate = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 159 | } |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 160 | |
| 161 | std::vector<size_t> mVisibleItemsOld; |
| 162 | std::swap(mVisibleItemsOld, mVisibleItems); |
| 163 | for (size_t i = 0; i < mListItems.size(); i++) { |
| 164 | ListItem& item = mListItems[i]; |
| 165 | // update per-item visibility condition |
| 166 | bool itemVisible = UpdateConditions(item.mConditions, varName); |
| 167 | if (itemVisible) |
| 168 | mVisibleItems.push_back(i); |
| 169 | |
| 170 | if (isCheckList) |
| 171 | { |
that | 35f1740 | 2015-11-02 20:15:07 +0100 | [diff] [blame] | 172 | if (item.variableName == varName || varName.empty()) { |
| 173 | std::string val; |
| 174 | DataManager::GetValue(item.variableName, val); |
| 175 | item.selected = (val != "0"); |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 176 | mUpdate = 1; |
| 177 | } |
| 178 | } |
| 179 | else if (varName == mVariable) { |
| 180 | if (item.variableValue == currentValue) { |
| 181 | item.selected = 1; |
| 182 | SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1); |
| 183 | } else { |
| 184 | item.selected = 0; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (mVisibleItemsOld != mVisibleItems) { |
| 190 | mUpdate = 1; // some item's visibility has changed |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 191 | if (firstDisplayedItem >= (int)mVisibleItems.size()) { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 192 | // all items in the view area were removed - make last item visible |
| 193 | SetVisibleListLocation(mVisibleItems.empty() ? 0 : mVisibleItems.size()-1); |
| 194 | } |
| 195 | } |
| 196 | |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 197 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 200 | void GUIListBox::SetPageFocus(int inFocus) |
| 201 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 202 | GUIScrollList::SetPageFocus(inFocus); |
| 203 | if (inFocus) { |
Ethan Yonker | dedbb7f | 2016-01-17 18:17:38 -0600 | [diff] [blame] | 204 | if (!isTextParsed) { |
| 205 | isTextParsed = true; |
| 206 | for (size_t i = 0; i < mListItems.size(); i++) { |
| 207 | ListItem& item = mListItems[i]; |
| 208 | item.displayName = gui_parse_text(item.displayName); |
| 209 | } |
| 210 | } |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 211 | DataManager::GetValue(mVariable, currentValue); |
| 212 | NotifyVarChange(mVariable, currentValue); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 213 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 214 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 215 | |
| 216 | size_t GUIListBox::GetItemCount() |
| 217 | { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 218 | return mVisibleItems.size(); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 219 | } |
| 220 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 221 | void GUIListBox::RenderItem(size_t itemindex, int yPos, bool selected) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 222 | { |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 223 | // note: the "selected" parameter above is for the currently touched item |
| 224 | // don't confuse it with the more persistent "selected" flag per list item used below |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 225 | ListItem& item = mListItems[mVisibleItems[itemindex]]; |
| 226 | ImageResource* icon = item.selected ? mIconSelected : mIconUnselected; |
| 227 | const std::string& text = item.displayName; |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 228 | |
| 229 | RenderStdItem(yPos, selected, icon, text.c_str()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void GUIListBox::NotifySelect(size_t item_selected) |
| 233 | { |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 234 | if (!isCheckList) { |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 235 | // deselect all items, even invisible ones |
| 236 | for (size_t i = 0; i < mListItems.size(); i++) { |
| 237 | mListItems[i].selected = 0; |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 238 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 239 | } |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 240 | |
| 241 | ListItem& item = mListItems[mVisibleItems[item_selected]]; |
| 242 | |
| 243 | if (isCheckList) { |
| 244 | int selected = 1 - item.selected; |
| 245 | item.selected = selected; |
| 246 | DataManager::SetValue(item.variableName, selected ? "1" : "0"); |
| 247 | } else { |
| 248 | item.selected = 1; |
| 249 | string str = item.variableValue; // [check] should this set currentValue instead? |
| 250 | DataManager::SetValue(mVariable, str); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 251 | } |
that | 8ab5c13 | 2015-09-13 23:00:54 +0200 | [diff] [blame] | 252 | if (item.action) |
| 253 | item.action->doActions(); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 254 | mUpdate = 1; |
| 255 | } |