Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | extern "C" { |
| 24 | #include "../twcommon.h" |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 25 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 26 | #include "../minuitwrp/minui.h" |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 27 | |
| 28 | #include "rapidxml.hpp" |
| 29 | #include "objects.hpp" |
| 30 | |
| 31 | GUITextBox::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 | |
| 59 | int 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 | |
| 77 | size_t GUITextBox::GetItemCount() |
| 78 | { |
| 79 | return rText.size(); |
| 80 | } |
| 81 | |
| 82 | void GUITextBox::RenderItem(size_t itemindex, int yPos, bool selected __unused) |
| 83 | { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 84 | if (!mFont || !mFont->GetResource()) |
| 85 | return; |
| 86 | |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 87 | // Set the color for the font |
| 88 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 89 | |
| 90 | // render text |
| 91 | const char* text = rText[itemindex].c_str(); |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 92 | gr_textEx_scaleW(mRenderX, yPos, text, mFont->GetResource(), mRenderW, TOP_LEFT, 0); |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void GUITextBox::NotifySelect(size_t item_selected __unused) |
| 96 | { |
| 97 | // do nothing - textbox ignores selections |
| 98 | } |
| 99 | |
| 100 | int GUITextBox::NotifyVarChange(const std::string& varName, const std::string& value) |
| 101 | { |
| 102 | GUIScrollList::NotifyVarChange(varName, value); |
| 103 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 104 | if (!isConditionTrue() || mIsStatic) |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 105 | return 0; |
| 106 | |
| 107 | // Check to see if the variable exists in mText |
| 108 | for (size_t i = 0; i < mText.size(); i++) { |
| 109 | string lookup = gui_parse_text(mText.at(i)); |
| 110 | if (lookup != mText.at(i)) { |
| 111 | mLastValue.at(i) = lookup; |
| 112 | mUpdate = 1; |
| 113 | // There are ways to improve efficiency here, but I am not |
| 114 | // sure if we will even use this feature in the stock theme |
| 115 | // at all except for language translation. If we start using |
| 116 | // variables in textboxes in the stock theme, we can circle |
| 117 | // back and make improvements here. |
| 118 | mLastCount = 0; |
| 119 | rText.clear(); |
| 120 | } |
| 121 | } |
| 122 | return 0; |
| 123 | } |