blob: 4c9a68ac8c1dd3b1be54d3636024c1066e69bd08 [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;
Dees_Troyeead9852013-02-15 14:31:06 -060037 mUpdate = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040038
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010039 // Get the icons, if any
Ethan Yonker21ff02a2015-02-18 14:35:00 -060040 child = FindNode(node, "icon");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010041 if (child) {
thatf6ed8fc2015-02-14 20:23:16 +010042 mIconSelected = LoadAttrImage(child, "selected");
43 mIconUnselected = LoadAttrImage(child, "unselected");
Dees_Troyeead9852013-02-15 14:31:06 -060044 }
thatf6ed8fc2015-02-14 20:23:16 +010045 int iconWidth = std::max(mIconSelected->GetWidth(), mIconUnselected->GetWidth());
46 int iconHeight = std::max(mIconSelected->GetHeight(), mIconUnselected->GetHeight());
47 SetMaxIconSize(iconWidth, iconHeight);
Dees_Troyeead9852013-02-15 14:31:06 -060048
49 // Handle the result variable
Ethan Yonker21ff02a2015-02-18 14:35:00 -060050 child = FindNode(node, "data");
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010051 if (child) {
Dees_Troyeead9852013-02-15 14:31:06 -060052 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 Yonker0a3a98f2015-02-05 00:48:28 +010058 // Get the currently selected value for the list
59 DataManager::GetValue(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -060060 }
thatc01391c2015-07-09 00:19:58 +020061 else
62 allowSelection = false; // allows using listbox as a read-only list
Dees_Troyeead9852013-02-15 14:31:06 -060063
Dees_Troy51a0e822012-09-05 15:24:24 -040064 // Get the data for the list
Ethan Yonker21ff02a2015-02-18 14:35:00 -060065 child = FindNode(node, "listitem");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 if (!child) return;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +010067 while (child) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020068 ListData data;
Dees_Troy51a0e822012-09-05 15:24:24 -040069
70 attr = child->first_attribute("name");
thatc01391c2015-07-09 00:19:58 +020071 if (!attr)
72 continue;
73 data.displayName = gui_parse_text(attr->value());
74 data.variableValue = gui_parse_text(child->value());
Dees_Troyeead9852013-02-15 14:31:06 -060075 if (child->value() == currentValue) {
Dees_Troy51a0e822012-09-05 15:24:24 -040076 data.selected = 1;
Dees_Troyeead9852013-02-15 14:31:06 -060077 } else {
Dees_Troy51a0e822012-09-05 15:24:24 -040078 data.selected = 0;
Dees_Troyeead9852013-02-15 14:31:06 -060079 }
thatc01391c2015-07-09 00:19:58 +020080 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_Troy51a0e822012-09-05 15:24:24 -040086
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 mList.push_back(data);
Dees_Troy51a0e822012-09-05 15:24:24 -040088
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020089 child = child->next_sibling("listitem");
90 }
Dees_Troy51a0e822012-09-05 15:24:24 -040091}
92
93GUIListBox::~GUIListBox()
94{
Dees_Troy51a0e822012-09-05 15:24:24 -040095}
96
97int GUIListBox::Update(void)
98{
Vojtech Bocekede51c52014-02-07 23:58:09 +010099 if(!isConditionTrue())
100 return 0;
101
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100102 GUIScrollList::Update();
Dees_Troyeead9852013-02-15 14:31:06 -0600103
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100104 if (mUpdate) {
Dees_Troyeead9852013-02-15 14:31:06 -0600105 mUpdate = 0;
106 if (Render() == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400107 return 2;
Dees_Troyeead9852013-02-15 14:31:06 -0600108 }
Dees_Troyeead9852013-02-15 14:31:06 -0600109 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400110}
111
Vojtech Bocek07220562014-02-08 02:05:33 +0100112int GUIListBox::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400113{
thatfa30aca2015-02-13 01:22:22 +0100114 GUIScrollList::NotifyVarChange(varName, value);
115
Vojtech Bocekede51c52014-02-07 23:58:09 +0100116 if(!isConditionTrue())
117 return 0;
118
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100119 // 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_Troy51a0e822012-09-05 15:24:24 -0400122
123 currentValue = value;
124
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100125 for (i = 0; i < listSize; i++) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400126 if (mList.at(i).variableValue == currentValue) {
127 mList.at(i).selected = 1;
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100128 SetVisibleListLocation(i);
Dees_Troy51a0e822012-09-05 15:24:24 -0400129 } else
130 mList.at(i).selected = 0;
131 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400132 mUpdate = 1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 return 0;
134 }
Dees_Troyeead9852013-02-15 14:31:06 -0600135 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136}
137
Dees_Troy51a0e822012-09-05 15:24:24 -0400138void GUIListBox::SetPageFocus(int inFocus)
139{
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100140 GUIScrollList::SetPageFocus(inFocus);
141 if (inFocus) {
Dees_Troyeead9852013-02-15 14:31:06 -0600142 DataManager::GetValue(mVariable, currentValue);
143 NotifyVarChange(mVariable, currentValue);
Dees_Troyeead9852013-02-15 14:31:06 -0600144 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400145}
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100146
147size_t GUIListBox::GetItemCount()
148{
149 return mList.size();
150}
151
that0af77952015-02-25 08:52:19 +0100152void GUIListBox::RenderItem(size_t itemindex, int yPos, bool selected)
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100153{
that0af77952015-02-25 08:52:19 +0100154 // 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 Yonker0a3a98f2015-02-05 00:48:28 +0100160}
161
162void 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()) {
thatc01391c2015-07-09 00:19:58 +0200168 ListData& data = mList.at(item_selected);
169 data.selected = 1;
170 string str = data.variableValue; // [check] should this set currentValue instead?
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100171 DataManager::SetValue(mVariable, str);
thatc01391c2015-07-09 00:19:58 +0200172 if (data.action)
173 data.action->doActions();
Ethan Yonker0a3a98f2015-02-05 00:48:28 +0100174 }
175 mUpdate = 1;
176}