blob: 37ba958c1aafb00b011b37ed78099355cffa0dc8 [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#include <sys/stat.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040021#include <dirent.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040022
23extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000024#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040026}
27
28#include "rapidxml.hpp"
29#include "objects.hpp"
30#include "../data.hpp"
Dees_Troyeead9852013-02-15 14:31:06 -060031
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010032GUIListBox::GUIListBox(xml_node<>* node) : GUIScrollList(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040033{
Dees_Troyeead9852013-02-15 14:31:06 -060034 xml_attribute<>* attr;
35 xml_node<>* child;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010036 mIconSelected = mIconUnselected = NULL;
37 int mSelectedIconWidth = 0, mSelectedIconHeight = 0, mUnselectedIconWidth = 0, mUnselectedIconHeight = 0, mIconWidth = 0, mIconHeight = 0;
Dees_Troyeead9852013-02-15 14:31:06 -060038 mUpdate = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040039
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010040 // Get the icons, if any
Dees_Troyeead9852013-02-15 14:31:06 -060041 child = node->first_node("icon");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010042 if (child) {
Dees_Troyeead9852013-02-15 14:31:06 -060043 attr = child->first_attribute("selected");
44 if (attr)
45 mIconSelected = PageManager::FindResource(attr->value());
46 attr = child->first_attribute("unselected");
47 if (attr)
48 mIconUnselected = PageManager::FindResource(attr->value());
49 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010050 if (mIconSelected && mIconSelected->GetResource()) {
51 mSelectedIconWidth = gr_get_width(mIconSelected->GetResource());
52 mSelectedIconHeight = gr_get_height(mIconSelected->GetResource());
53 if (mSelectedIconHeight > mIconHeight)
54 mIconHeight = mSelectedIconHeight;
55 mIconWidth = mSelectedIconWidth;
Dees_Troyeead9852013-02-15 14:31:06 -060056 }
57
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010058 if (mIconUnselected && mIconUnselected->GetResource()) {
59 mUnselectedIconWidth = gr_get_width(mIconUnselected->GetResource());
60 mUnselectedIconHeight = gr_get_height(mIconUnselected->GetResource());
61 if (mUnselectedIconHeight > mIconHeight)
62 mIconHeight = mUnselectedIconHeight;
63 if (mUnselectedIconWidth > mIconWidth)
64 mIconWidth = mUnselectedIconWidth;
Dees_Troyeead9852013-02-15 14:31:06 -060065 }
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010066 SetMaxIconSize(mIconWidth, mIconHeight);
Dees_Troyeead9852013-02-15 14:31:06 -060067
68 // Handle the result variable
69 child = node->first_node("data");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010070 if (child) {
Dees_Troyeead9852013-02-15 14:31:06 -060071 attr = child->first_attribute("name");
72 if (attr)
73 mVariable = attr->value();
74 attr = child->first_attribute("default");
75 if (attr)
76 DataManager::SetValue(mVariable, attr->value());
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010077 // Get the currently selected value for the list
78 DataManager::GetValue(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -060079 }
80
Dees_Troy51a0e822012-09-05 15:24:24 -040081 // Get the data for the list
82 child = node->first_node("listitem");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 if (!child) return;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010084 while (child) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 ListData data;
Dees_Troy51a0e822012-09-05 15:24:24 -040086
87 attr = child->first_attribute("name");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 if (!attr) return;
89 data.displayName = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040090
91 data.variableValue = child->value();
Dees_Troyeead9852013-02-15 14:31:06 -060092 if (child->value() == currentValue) {
Dees_Troy51a0e822012-09-05 15:24:24 -040093 data.selected = 1;
Dees_Troyeead9852013-02-15 14:31:06 -060094 } else {
Dees_Troy51a0e822012-09-05 15:24:24 -040095 data.selected = 0;
Dees_Troyeead9852013-02-15 14:31:06 -060096 }
Dees_Troy51a0e822012-09-05 15:24:24 -040097
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 mList.push_back(data);
Dees_Troy51a0e822012-09-05 15:24:24 -040099
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200100 child = child->next_sibling("listitem");
101 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400102}
103
104GUIListBox::~GUIListBox()
105{
Dees_Troy51a0e822012-09-05 15:24:24 -0400106}
107
108int GUIListBox::Update(void)
109{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100110 if(!isConditionTrue())
111 return 0;
112
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100113 GUIScrollList::Update();
Dees_Troyeead9852013-02-15 14:31:06 -0600114
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100115 if (mUpdate) {
Dees_Troyeead9852013-02-15 14:31:06 -0600116 mUpdate = 0;
117 if (Render() == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400118 return 2;
Dees_Troyeead9852013-02-15 14:31:06 -0600119 }
Dees_Troyeead9852013-02-15 14:31:06 -0600120 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400121}
122
Vojtech Bocek07220562014-02-08 02:05:33 +0100123int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400124{
thatfa30aca2015-02-13 01:22:22 +0100125 GUIScrollList::NotifyVarChange(varName, value);
126
Vojtech Bocekede51c52014-02-07 23:58:09 +0100127 if(!isConditionTrue())
128 return 0;
129
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100130 // Check to see if the variable that we are using to store the list selected value has been updated
131 if (varName == mVariable) {
132 int i, listSize = mList.size();
Dees_Troy51a0e822012-09-05 15:24:24 -0400133
134 currentValue = value;
135
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100136 for (i = 0; i < listSize; i++) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400137 if (mList.at(i).variableValue == currentValue) {
138 mList.at(i).selected = 1;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100139 SetVisibleListLocation(i);
Dees_Troy51a0e822012-09-05 15:24:24 -0400140 } else
141 mList.at(i).selected = 0;
142 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400143 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200144 return 0;
145 }
Dees_Troyeead9852013-02-15 14:31:06 -0600146 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400147}
148
Dees_Troy51a0e822012-09-05 15:24:24 -0400149void GUIListBox::SetPageFocus(int inFocus)
150{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100151 GUIScrollList::SetPageFocus(inFocus);
152 if (inFocus) {
Dees_Troyeead9852013-02-15 14:31:06 -0600153 DataManager::GetValue(mVariable, currentValue);
154 NotifyVarChange(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -0600155 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400156}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100157
158size_t GUIListBox::GetItemCount()
159{
160 return mList.size();
161}
162
163int GUIListBox::GetListItem(size_t item_index, Resource*& icon, std::string &text)
164{
165 text = mList.at(item_index).displayName;
166 if (mList.at(item_index).selected)
167 icon = mIconSelected;
168 else
169 icon = mIconUnselected;
170 return 0;
171}
172
173void GUIListBox::NotifySelect(size_t item_selected)
174{
175 for (size_t i = 0; i < mList.size(); i++) {
176 mList.at(i).selected = 0;
177 }
178 if (item_selected < mList.size()) {
179 mList.at(item_selected).selected = 1;
180 string str = mList.at(item_selected).variableValue;
181 DataManager::SetValue(mVariable, str);
182 }
183 mUpdate = 1;
184}