blob: 9fc85a052a5ec5990252d459d85c194ee60078c2 [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;
113 if (mButtonImg) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500114 mButtonImg->GetRenderPos(x, y, w, h);
115 } else if (hasFill) {
116 LoadPlacement(node->first_node("placement"), &x, &y, &w, &h);
117 }
118 SetRenderPos(x, y, w, h);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200119 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120}
121
122GUIButton::~GUIButton()
123{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200124 if (mButtonImg) delete mButtonImg;
125 if (mButtonLabel) delete mButtonLabel;
126 if (mAction) delete mAction;
127 if (mButtonIcon) delete mButtonIcon;
Dees_Troy51a0e822012-09-05 15:24:24 -0400128}
129
130int GUIButton::Render(void)
131{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (!isConditionTrue())
133 {
134 mRendered = false;
135 return 0;
136 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400137
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400139
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 if (mButtonImg) ret = mButtonImg->Render();
141 if (ret < 0) return ret;
Dees_Troya13d74f2013-03-24 08:54:55 -0500142 if (hasFill) {
143 gr_color(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha);
144 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
145 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 if (mButtonIcon && mButtonIcon->GetResource())
147 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
148 if (mButtonLabel) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500149 int w, h;
150 mButtonLabel->GetCurrentBounds(w, h);
151 if (w != mTextW) {
152 mTextW = w;
153 // As a special case, we'll allow large text which automatically moves it to the right.
154 if (mTextW > mRenderW)
155 {
156 mTextX = mRenderW + mRenderX + 5;
157 mRenderW += mTextW + 5;
158 }
159 else
160 {
161 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
162 }
163 mButtonLabel->SetRenderPos(mTextX, mTextY);
164 }
165 ret = mButtonLabel->Render();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 if (ret < 0) return ret;
Dees_Troya13d74f2013-03-24 08:54:55 -0500167 }
Dees_Troy1a7a6672013-02-15 09:39:07 -0600168 if (renderHighlight && hasHighlightColor) {
169 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
170 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
171 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200172 mRendered = true;
173 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400174}
175
176int GUIButton::Update(void)
177{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200178 if (!isConditionTrue()) return (mRendered ? 2 : 0);
179 if (!mRendered) return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400180
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200181 int ret = 0, ret2 = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400182
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200183 if (mButtonImg) ret = mButtonImg->Update();
184 if (ret < 0) return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400185
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186 if (ret == 0)
187 {
188 if (mButtonLabel) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500189 ret2 = mButtonLabel->Update();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200190 if (ret2 < 0) return ret2;
191 if (ret2 > ret) ret = ret2;
Dees_Troya13d74f2013-03-24 08:54:55 -0500192 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200193 }
194 else if (ret == 1)
195 {
196 // The button re-rendered, so everyone else is a render
197 if (mButtonIcon && mButtonIcon->GetResource())
198 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
199 if (mButtonLabel) ret = mButtonLabel->Render();
200 if (ret < 0) return ret;
201 ret = 1;
202 }
203 else
204 {
205 // Aparently, the button needs a background update
206 ret = 2;
207 }
208 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400209}
210
211int GUIButton::SetRenderPos(int x, int y, int w, int h)
212{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 mRenderX = x;
214 mRenderY = y;
215 if (w || h)
216 {
217 mRenderW = w;
218 mRenderH = h;
219 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400220
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 mIconW = 0; mIconH = 0;
222 if (mButtonIcon && mButtonIcon->GetResource())
223 {
224 mIconW = gr_get_width(mButtonIcon->GetResource());
225 mIconH = gr_get_height(mButtonIcon->GetResource());
226 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400227
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200228 mTextH = 0;
229 mTextW = 0;
230 mIconX = mRenderX + ((mRenderW - mIconW) / 2);
231 if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH);
232 if (mTextW)
233 {
234 // As a special case, we'll allow large text which automatically moves it to the right.
235 if (mTextW > mRenderW)
236 {
237 mTextX = mRenderW + mRenderX + 5;
238 mRenderW += mTextW + 5;
239 }
240 else
241 {
242 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
243 }
244 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400245
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200246 if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH)
247 {
248 mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2);
249 mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2);
250 }
251 else
252 {
253 int divisor = mRenderH - (mIconH + mTextH);
254 mIconY = mRenderY + (divisor / 3);
255 mTextY = mRenderY + (divisor * 2 / 3) + mIconH;
256 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400257
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200258 if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY);
259 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
260 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
261 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400262}
263
264int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y)
265{
Dees_Troy4d12f962012-10-19 13:13:15 -0400266 static int last_state = 0;
267
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200268 if (!isConditionTrue()) return -1;
Dees_Troy4d12f962012-10-19 13:13:15 -0400269 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH || state == TOUCH_RELEASE) {
270 if (last_state == 1) {
271 last_state = 0;
272 if (mButtonLabel != NULL)
273 mButtonLabel->isHighlighted = false;
274 if (mButtonImg != NULL)
275 mButtonImg->isHighlighted = false;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600276 renderHighlight = false;
Dees_Troy4d12f962012-10-19 13:13:15 -0400277 mRendered = false;
278 }
279 } else {
280 if (last_state == 0) {
281 last_state = 1;
282 if (mButtonLabel != NULL)
283 mButtonLabel->isHighlighted = true;
284 if (mButtonImg != NULL)
285 mButtonImg->isHighlighted = true;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600286 renderHighlight = true;
Dees_Troy4d12f962012-10-19 13:13:15 -0400287 mRendered = false;
288 }
289 }
290 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH)
291 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200292 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400293}