blob: 2e2512c50ffd7c7e45c343c95ef2c72355df01c5 [file] [log] [blame]
Ethan Yonker44925ad2015-07-22 12:33:59 -05001/*
2 Copyright 2015 bigbiff/Dees_Troy/_that 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*/
18
19// textbox.cpp - GUITextBox object
20
21#include <string>
22
23extern "C" {
24#include "../twcommon.h"
Ethan Yonker44925ad2015-07-22 12:33:59 -050025}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010026#include "../minuitwrp/minui.h"
Ethan Yonker44925ad2015-07-22 12:33:59 -050027
28#include "rapidxml.hpp"
29#include "objects.hpp"
30
31GUITextBox::GUITextBox(xml_node<>* node) : GUIScrollList(node)
32{
33 xml_node<>* child;
34
35 mLastCount = 0;
36 mIsStatic = true;
37
38 allowSelection = false; // textbox doesn't support list item selections
39
40 child = FindNode(node, "color");
41 if (child)
42 {
43 mFontColor = LoadAttrColor(child, "foreground", mFontColor);
44 mBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor);
45 //mScrollColor = LoadAttrColor(child, "scroll", mScrollColor);
46 }
47 child = FindNode(node, "text");
48 while (child) {
49 string txt = child->value();
50 mText.push_back(txt);
51 string lookup = gui_parse_text(txt);
52 if (lookup != txt)
53 mIsStatic = false;
54 mLastValue.push_back(lookup);
55 child = child->next_sibling("text");
56 }
57}
58
59int GUITextBox::Update(void)
60{
61 if (AddLines(&mLastValue, NULL, &mLastCount, &rText, NULL)) {
62 // someone added new text
63 // at least the scrollbar must be updated, even if the new lines are currently not visible
64 mUpdate = 1;
65 }
66
67 GUIScrollList::Update();
68
69 if (mUpdate) {
70 mUpdate = 0;
71 if (Render() == 0)
72 return 2;
73 }
74 return 0;
75}
76
77size_t GUITextBox::GetItemCount()
78{
79 return rText.size();
80}
81
82void GUITextBox::RenderItem(size_t itemindex, int yPos, bool selected __unused)
83{
84 // Set the color for the font
85 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
86
87 // render text
88 const char* text = rText[itemindex].c_str();
Ethan Yonkerfbb43532015-12-28 21:54:50 +010089 gr_textEx_scaleW(mRenderX, yPos, text, mFont->GetResource(), mRenderW, TOP_LEFT, 0);
Ethan Yonker44925ad2015-07-22 12:33:59 -050090}
91
92void GUITextBox::NotifySelect(size_t item_selected __unused)
93{
94 // do nothing - textbox ignores selections
95}
96
97int GUITextBox::NotifyVarChange(const std::string& varName, const std::string& value)
98{
99 GUIScrollList::NotifyVarChange(varName, value);
100
101 if(!isConditionTrue() || mIsStatic)
102 return 0;
103
104 // Check to see if the variable exists in mText
105 for (size_t i = 0; i < mText.size(); i++) {
106 string lookup = gui_parse_text(mText.at(i));
107 if (lookup != mText.at(i)) {
108 mLastValue.at(i) = lookup;
109 mUpdate = 1;
110 // There are ways to improve efficiency here, but I am not
111 // sure if we will even use this feature in the stock theme
112 // at all except for language translation. If we start using
113 // variables in textboxes in the stock theme, we can circle
114 // back and make improvements here.
115 mLastCount = 0;
116 rText.clear();
117 }
118 }
119 return 0;
120}