blob: 545b7a294b14e5d9bc433e4baa89a93de0762072 [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{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100106 delete mIconSelected;
107 delete mIconUnselected;
Dees_Troy51a0e822012-09-05 15:24:24 -0400108}
109
110int GUIListBox::Update(void)
111{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100112 if(!isConditionTrue())
113 return 0;
114
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100115 GUIScrollList::Update();
Dees_Troyeead9852013-02-15 14:31:06 -0600116
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100117 if (mUpdate) {
Dees_Troyeead9852013-02-15 14:31:06 -0600118 mUpdate = 0;
119 if (Render() == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400120 return 2;
Dees_Troyeead9852013-02-15 14:31:06 -0600121 }
Dees_Troyeead9852013-02-15 14:31:06 -0600122 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400123}
124
Vojtech Bocek07220562014-02-08 02:05:33 +0100125int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400126{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100127 if(!isConditionTrue())
128 return 0;
129
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100130 GUIScrollList::NotifyVarChange(varName, value);
131
132 // Check to see if the variable that we are using to store the list selected value has been updated
133 if (varName == mVariable) {
134 int i, listSize = mList.size();
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
136 currentValue = value;
137
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100138 for (i = 0; i < listSize; i++) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400139 if (mList.at(i).variableValue == currentValue) {
140 mList.at(i).selected = 1;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100141 SetVisibleListLocation(i);
Dees_Troy51a0e822012-09-05 15:24:24 -0400142 } else
143 mList.at(i).selected = 0;
144 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 return 0;
147 }
Dees_Troyeead9852013-02-15 14:31:06 -0600148 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149}
150
Dees_Troy51a0e822012-09-05 15:24:24 -0400151void GUIListBox::SetPageFocus(int inFocus)
152{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100153 GUIScrollList::SetPageFocus(inFocus);
154 if (inFocus) {
Dees_Troyeead9852013-02-15 14:31:06 -0600155 DataManager::GetValue(mVariable, currentValue);
156 NotifyVarChange(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -0600157 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400158}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100159
160size_t GUIListBox::GetItemCount()
161{
162 return mList.size();
163}
164
165int GUIListBox::GetListItem(size_t item_index, Resource*& icon, std::string &text)
166{
167 text = mList.at(item_index).displayName;
168 if (mList.at(item_index).selected)
169 icon = mIconSelected;
170 else
171 icon = mIconUnselected;
172 return 0;
173}
174
175void GUIListBox::NotifySelect(size_t item_selected)
176{
177 for (size_t i = 0; i < mList.size(); i++) {
178 mList.at(i).selected = 0;
179 }
180 if (item_selected < mList.size()) {
181 mList.at(item_selected).selected = 1;
182 string str = mList.at(item_selected).variableValue;
183 DataManager::SetValue(mVariable, str);
184 }
185 mUpdate = 1;
186}