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