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 |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 40 | child = node->first_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 |
| 50 | child = node->first_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 | } |
| 61 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | // Get the data for the list |
| 63 | child = node->first_node("listitem"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 64 | if (!child) return; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 65 | while (child) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 66 | ListData data; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 67 | |
| 68 | attr = child->first_attribute("name"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 69 | if (!attr) return; |
| 70 | data.displayName = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 71 | |
| 72 | data.variableValue = child->value(); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 73 | if (child->value() == currentValue) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 74 | data.selected = 1; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 75 | } else { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 76 | data.selected = 0; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 77 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 78 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 79 | mList.push_back(data); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 80 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 81 | child = child->next_sibling("listitem"); |
| 82 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | GUIListBox::~GUIListBox() |
| 86 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | int GUIListBox::Update(void) |
| 90 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 91 | if(!isConditionTrue()) |
| 92 | return 0; |
| 93 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 94 | GUIScrollList::Update(); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 95 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 96 | if (mUpdate) { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 97 | mUpdate = 0; |
| 98 | if (Render() == 0) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 99 | return 2; |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 100 | } |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 101 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 104 | int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 105 | { |
that | fa30aca | 2015-02-13 01:22:22 +0100 | [diff] [blame] | 106 | GUIScrollList::NotifyVarChange(varName, value); |
| 107 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 108 | if(!isConditionTrue()) |
| 109 | return 0; |
| 110 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 111 | // Check to see if the variable that we are using to store the list selected value has been updated |
| 112 | if (varName == mVariable) { |
| 113 | int i, listSize = mList.size(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 114 | |
| 115 | currentValue = value; |
| 116 | |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 117 | for (i = 0; i < listSize; i++) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 118 | if (mList.at(i).variableValue == currentValue) { |
| 119 | mList.at(i).selected = 1; |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 120 | SetVisibleListLocation(i); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 121 | } else |
| 122 | mList.at(i).selected = 0; |
| 123 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 124 | mUpdate = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 125 | return 0; |
| 126 | } |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 127 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 130 | void GUIListBox::SetPageFocus(int inFocus) |
| 131 | { |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 132 | GUIScrollList::SetPageFocus(inFocus); |
| 133 | if (inFocus) { |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 134 | DataManager::GetValue(mVariable, currentValue); |
| 135 | NotifyVarChange(mVariable, currentValue); |
Dees_Troy | eead985 | 2013-02-15 14:31:06 -0600 | [diff] [blame] | 136 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 137 | } |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 138 | |
| 139 | size_t GUIListBox::GetItemCount() |
| 140 | { |
| 141 | return mList.size(); |
| 142 | } |
| 143 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 144 | int GUIListBox::GetListItem(size_t item_index, ImageResource*& icon, std::string &text) |
Ethan Yonker | 0a3a98f | 2015-02-05 00:48:28 +0100 | [diff] [blame] | 145 | { |
| 146 | text = mList.at(item_index).displayName; |
| 147 | if (mList.at(item_index).selected) |
| 148 | icon = mIconSelected; |
| 149 | else |
| 150 | icon = mIconUnselected; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | void GUIListBox::NotifySelect(size_t item_selected) |
| 155 | { |
| 156 | for (size_t i = 0; i < mList.size(); i++) { |
| 157 | mList.at(i).selected = 0; |
| 158 | } |
| 159 | if (item_selected < mList.size()) { |
| 160 | mList.at(item_selected).selected = 1; |
| 161 | string str = mList.at(item_selected).variableValue; |
| 162 | DataManager::SetValue(mVariable, str); |
| 163 | } |
| 164 | mUpdate = 1; |
| 165 | } |