blob: 3f9c2df1fded2114b371e2d1c5b68a3eae23d0e4 [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
2 Copyright 2012 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
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/reboot.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29#include <sys/ioctl.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
33
34#include <string>
35
36extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039}
40
41#include "rapidxml.hpp"
42#include "objects.hpp"
43
44GUIButton::GUIButton(xml_node<>* node)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020045 : Conditional(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040046{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020047 xml_attribute<>* attr;
48 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040049
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020050 mButtonImg = NULL;
51 mButtonIcon = NULL;
52 mButtonLabel = NULL;
53 mAction = NULL;
54 mRendered = false;
Dees_Troy1a7a6672013-02-15 09:39:07 -060055 hasHighlightColor = false;
56 renderHighlight = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050057 hasFill = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040060
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020061 // Three of the four can be loaded directly from the node
62 mButtonImg = new GUIImage(node);
63 mButtonLabel = new GUIText(node);
64 mAction = new GUIAction(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040065
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 if (mButtonImg->Render() < 0)
67 {
68 delete mButtonImg;
69 mButtonImg = NULL;
70 }
71 if (mButtonLabel->Render() < 0)
72 {
73 delete mButtonLabel;
74 mButtonLabel = NULL;
75 }
Dees_Troya13d74f2013-03-24 08:54:55 -050076 // Load fill if it exists
77 memset(&mFillColor, 0, sizeof(COLOR));
78 child = node->first_node("fill");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020079 if (child)
80 {
Dees_Troya13d74f2013-03-24 08:54:55 -050081 attr = child->first_attribute("color");
82 if (attr) {
83 hasFill = true;
84 std::string color = attr->value();
85 ConvertStrToColor(color, &mFillColor);
86 }
87 }
88 if (!hasFill && mButtonImg == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000089 LOGERR("No image resource or fill specified for button.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -050090 }
Dees_Troy51a0e822012-09-05 15:24:24 -040091
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 // The icon is a special case
93 child = node->first_node("icon");
94 if (child)
95 {
96 attr = child->first_attribute("resource");
97 if (attr)
98 mButtonIcon = PageManager::FindResource(attr->value());
99 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Dees_Troy1a7a6672013-02-15 09:39:07 -0600101 memset(&mHighlightColor, 0, sizeof(COLOR));
102 child = node->first_node("highlight");
103 if (child) {
104 attr = child->first_attribute("color");
105 if (attr) {
106 hasHighlightColor = true;
107 std::string color = attr->value();
108 ConvertStrToColor(color, &mHighlightColor);
109 }
110 }
111
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200112 int x, y, w, h;
Dees Troyb21cc642013-09-10 17:36:41 +0000113 TextPlacement = TOP_LEFT;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114 if (mButtonImg) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500115 mButtonImg->GetRenderPos(x, y, w, h);
116 } else if (hasFill) {
Dees Troyb21cc642013-09-10 17:36:41 +0000117 LoadPlacement(node->first_node("placement"), &x, &y, &w, &h, &TextPlacement);
Dees_Troya13d74f2013-03-24 08:54:55 -0500118 }
119 SetRenderPos(x, y, w, h);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400121}
122
123GUIButton::~GUIButton()
124{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200125 if (mButtonImg) delete mButtonImg;
126 if (mButtonLabel) delete mButtonLabel;
127 if (mAction) delete mAction;
128 if (mButtonIcon) delete mButtonIcon;
Dees_Troy51a0e822012-09-05 15:24:24 -0400129}
130
131int GUIButton::Render(void)
132{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 if (!isConditionTrue())
134 {
135 mRendered = false;
136 return 0;
137 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400138
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200139 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400140
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 if (mButtonImg) ret = mButtonImg->Render();
142 if (ret < 0) return ret;
Dees_Troya13d74f2013-03-24 08:54:55 -0500143 if (hasFill) {
144 gr_color(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha);
145 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
146 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 if (mButtonIcon && mButtonIcon->GetResource())
148 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
149 if (mButtonLabel) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500150 int w, h;
151 mButtonLabel->GetCurrentBounds(w, h);
152 if (w != mTextW) {
153 mTextW = w;
Dees Troyb21cc642013-09-10 17:36:41 +0000154 if (TextPlacement == CENTER_X_ONLY) {
155 mTextX = ((mRenderW - mRenderX) / 2);
156 } else if (mTextW > mRenderW) { // As a special case, we'll allow large text which automatically moves it to the right.
Dees_Troya13d74f2013-03-24 08:54:55 -0500157 mTextX = mRenderW + mRenderX + 5;
158 mRenderW += mTextW + 5;
Dees Troyb21cc642013-09-10 17:36:41 +0000159 } else {
Dees_Troya13d74f2013-03-24 08:54:55 -0500160 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
161 }
162 mButtonLabel->SetRenderPos(mTextX, mTextY);
163 }
164 ret = mButtonLabel->Render();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200165 if (ret < 0) return ret;
Dees_Troya13d74f2013-03-24 08:54:55 -0500166 }
Dees_Troy1a7a6672013-02-15 09:39:07 -0600167 if (renderHighlight && hasHighlightColor) {
168 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
169 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
170 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200171 mRendered = true;
172 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400173}
174
175int GUIButton::Update(void)
176{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200177 if (!isConditionTrue()) return (mRendered ? 2 : 0);
178 if (!mRendered) return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400179
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200180 int ret = 0, ret2 = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400181
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200182 if (mButtonImg) ret = mButtonImg->Update();
183 if (ret < 0) return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400184
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200185 if (ret == 0)
186 {
187 if (mButtonLabel) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500188 ret2 = mButtonLabel->Update();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200189 if (ret2 < 0) return ret2;
190 if (ret2 > ret) ret = ret2;
Dees_Troya13d74f2013-03-24 08:54:55 -0500191 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200192 }
193 else if (ret == 1)
194 {
195 // The button re-rendered, so everyone else is a render
196 if (mButtonIcon && mButtonIcon->GetResource())
197 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
198 if (mButtonLabel) ret = mButtonLabel->Render();
199 if (ret < 0) return ret;
200 ret = 1;
201 }
202 else
203 {
204 // Aparently, the button needs a background update
205 ret = 2;
206 }
207 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400208}
209
210int GUIButton::SetRenderPos(int x, int y, int w, int h)
211{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 mRenderX = x;
213 mRenderY = y;
214 if (w || h)
215 {
216 mRenderW = w;
217 mRenderH = h;
218 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400219
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200220 mIconW = 0; mIconH = 0;
221 if (mButtonIcon && mButtonIcon->GetResource())
222 {
223 mIconW = gr_get_width(mButtonIcon->GetResource());
224 mIconH = gr_get_height(mButtonIcon->GetResource());
225 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400226
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200227 mTextH = 0;
228 mTextW = 0;
229 mIconX = mRenderX + ((mRenderW - mIconW) / 2);
230 if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH);
231 if (mTextW)
232 {
Dees Troyb21cc642013-09-10 17:36:41 +0000233 if (TextPlacement == CENTER_X_ONLY) {
234 mTextX = ((mRenderW - mRenderX) / 2);
235 } else if (mTextW > mRenderW) { // As a special case, we'll allow large text which automatically moves it to the right.
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 mTextX = mRenderW + mRenderX + 5;
237 mRenderW += mTextW + 5;
Dees Troyb21cc642013-09-10 17:36:41 +0000238 } else {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
240 }
241 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400242
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH)
244 {
245 mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2);
246 mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2);
247 }
248 else
249 {
250 int divisor = mRenderH - (mIconH + mTextH);
251 mIconY = mRenderY + (divisor / 3);
252 mTextY = mRenderY + (divisor * 2 / 3) + mIconH;
253 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400254
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200255 if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY);
256 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
257 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
258 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400259}
260
261int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y)
262{
Dees_Troy4d12f962012-10-19 13:13:15 -0400263 static int last_state = 0;
264
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200265 if (!isConditionTrue()) return -1;
Dees_Troy4d12f962012-10-19 13:13:15 -0400266 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH || state == TOUCH_RELEASE) {
267 if (last_state == 1) {
268 last_state = 0;
269 if (mButtonLabel != NULL)
270 mButtonLabel->isHighlighted = false;
271 if (mButtonImg != NULL)
272 mButtonImg->isHighlighted = false;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600273 renderHighlight = false;
Dees_Troy4d12f962012-10-19 13:13:15 -0400274 mRendered = false;
275 }
276 } else {
277 if (last_state == 0) {
278 last_state = 1;
279 if (mButtonLabel != NULL)
280 mButtonLabel->isHighlighted = true;
281 if (mButtonImg != NULL)
282 mButtonImg->isHighlighted = true;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600283 renderHighlight = true;
Dees_Troy4d12f962012-10-19 13:13:15 -0400284 mRendered = false;
285 }
286 }
287 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH)
288 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400290}