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 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | #include "rapidxml.hpp" |
| 27 | #include "objects.hpp" |
| 28 | #include "../data.hpp" |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 29 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 30 | GUIListBox::GUIListBox(xml_node<>* node) : GUIScrollList(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 31 | { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 32 | xml_attribute<>* attr; |
| 33 | xml_node<>* child; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 34 | mIconSelected = mIconUnselected = NULL; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 35 | mUpdate = 0; |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 36 | isCheckList = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 38 | // Get the icons, if any |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 39 | child = FindNode(node, "icon"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 40 | if (child) { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 41 | mIconSelected = LoadAttrImage(child, "selected"); |
| 42 | mIconUnselected = LoadAttrImage(child, "unselected"); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 43 | } |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 44 | int iconWidth = std::max(mIconSelected->GetWidth(), mIconUnselected->GetWidth()); |
| 45 | int iconHeight = std::max(mIconSelected->GetHeight(), mIconUnselected->GetHeight()); |
| 46 | SetMaxIconSize(iconWidth, iconHeight); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 47 | |
| 48 | // Handle the result variable |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 49 | child = FindNode(node, "data"); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 50 | if (child) { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 51 | 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 Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 57 | // Get the currently selected value for the list |
| 58 | DataManager::GetValue(mVariable, currentValue); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 59 | } |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 60 | else |
| 61 | allowSelection = false; // allows using listbox as a read-only list |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 62 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 63 | // Get the data for the list |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 64 | child = FindNode(node, "listitem"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 65 | if (!child) return; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 66 | while (child) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 67 | ListData data; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | |
| 69 | attr = child->first_attribute("name"); |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 70 | if (!attr) |
| 71 | continue; |
| 72 | data.displayName = gui_parse_text(attr->value()); |
| 73 | data.variableValue = gui_parse_text(child->value()); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 74 | if (child->value() == currentValue) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | data.selected = 1; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 76 | } else { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 77 | data.selected = 0; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 78 | } |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 79 | data.action = NULL; |
| 80 | xml_node<>* action = child->first_node("action"); |
| 81 | if (action) { |
| 82 | data.action = new GUIAction(action); |
| 83 | allowSelection = true; |
| 84 | } |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 85 | xml_node<>* variable_name = child->first_node("data"); |
| 86 | if (variable_name) { |
| 87 | attr = variable_name->first_attribute("variable"); |
| 88 | if (attr) { |
| 89 | data.variableName = attr->value(); |
| 90 | if (DataManager::GetIntValue(data.variableName) == 0) |
| 91 | data.selected = 0; |
| 92 | else |
| 93 | data.selected = 1; |
| 94 | allowSelection = true; |
| 95 | isCheckList = true; |
| 96 | } |
| 97 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 98 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 99 | mList.push_back(data); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 100 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 101 | child = child->next_sibling("listitem"); |
| 102 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | GUIListBox::~GUIListBox() |
| 106 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | int GUIListBox::Update(void) |
| 110 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 111 | if(!isConditionTrue()) |
| 112 | return 0; |
| 113 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 114 | GUIScrollList::Update(); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 115 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 116 | if (mUpdate) { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 117 | mUpdate = 0; |
| 118 | if (Render() == 0) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | return 2; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 120 | } |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 121 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 124 | int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 125 | { |
that | fa30aca | 2015-02-13 01:22:22 +0100 | [diff] [blame] | 126 | GUIScrollList::NotifyVarChange(varName, value); |
| 127 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 128 | if(!isConditionTrue()) |
| 129 | return 0; |
| 130 | |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 131 | if (isCheckList) { |
| 132 | int i, listSize = mList.size(); |
| 133 | |
| 134 | for (i = 0; i < listSize; i++) { |
| 135 | if (mList.at(i).variableName == varName) { |
| 136 | if (value == "0") { |
| 137 | mList.at(i).selected = 0; |
| 138 | } else { |
| 139 | mList.at(i).selected = 1; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | mUpdate = 1; |
| 144 | return 0; |
| 145 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 146 | // Check to see if the variable that we are using to store the list selected value has been updated |
| 147 | if (varName == mVariable) { |
| 148 | int i, listSize = mList.size(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 149 | |
| 150 | currentValue = value; |
| 151 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 152 | for (i = 0; i < listSize; i++) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 153 | if (mList.at(i).variableValue == currentValue) { |
| 154 | mList.at(i).selected = 1; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 155 | SetVisibleListLocation(i); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 156 | } else |
| 157 | mList.at(i).selected = 0; |
| 158 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 159 | mUpdate = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 160 | return 0; |
| 161 | } |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 162 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 165 | void GUIListBox::SetPageFocus(int inFocus) |
| 166 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 167 | GUIScrollList::SetPageFocus(inFocus); |
| 168 | if (inFocus) { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 169 | DataManager::GetValue(mVariable, currentValue); |
| 170 | NotifyVarChange(mVariable, currentValue); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 171 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 172 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 173 | |
| 174 | size_t GUIListBox::GetItemCount() |
| 175 | { |
| 176 | return mList.size(); |
| 177 | } |
| 178 | |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 179 | void GUIListBox::RenderItem(size_t itemindex, int yPos, bool selected) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 180 | { |
that | 0af7795 | 2015-02-25 08:52:19 +0100 | [diff] [blame] | 181 | // note: the "selected" parameter above is for the currently touched item |
| 182 | // don't confuse it with the more persistent "selected" flag per list item used below |
| 183 | ImageResource* icon = mList.at(itemindex).selected ? mIconSelected : mIconUnselected; |
| 184 | const std::string& text = mList.at(itemindex).displayName; |
| 185 | |
| 186 | RenderStdItem(yPos, selected, icon, text.c_str()); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void GUIListBox::NotifySelect(size_t item_selected) |
| 190 | { |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 191 | if (!isCheckList) { |
| 192 | for (size_t i = 0; i < mList.size(); i++) { |
| 193 | mList.at(i).selected = 0; |
| 194 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 195 | } |
| 196 | if (item_selected < mList.size()) { |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 197 | ListData& data = mList.at(item_selected); |
Ethan Yonker | 56ce332 | 2015-07-30 15:04:32 -0500 | [diff] [blame] | 198 | if (isCheckList) { |
| 199 | if (data.selected) { |
| 200 | data.selected = 0; |
| 201 | DataManager::SetValue(data.variableName, "0"); |
| 202 | } else { |
| 203 | data.selected = 1; |
| 204 | DataManager::SetValue(data.variableName, "1"); |
| 205 | } |
| 206 | } else { |
| 207 | data.selected = 1; |
| 208 | string str = data.variableValue; // [check] should this set currentValue instead? |
| 209 | DataManager::SetValue(mVariable, str); |
| 210 | } |
that | c01391c | 2015-07-09 00:19:58 +0200 | [diff] [blame] | 211 | if (data.action) |
| 212 | data.action->doActions(); |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 213 | } |
| 214 | mUpdate = 1; |
| 215 | } |