blob: de63cba368d4fa69d258c47ec584d8a008fe8c84 [file] [log] [blame]
Matt Mower37a7ab62017-01-18 20:06:08 -06001/*
2 Copyright 2017 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
Dees_Troy51a0e822012-09-05 15:24:24 -040019// checkbox.cpp - GUICheckbox object
20
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
26#include <sys/reboot.h>
27#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/mman.h>
30#include <sys/types.h>
31#include <sys/ioctl.h>
32#include <time.h>
33#include <unistd.h>
34#include <stdlib.h>
35
36#include <string>
37
38extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000039#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010041#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042
43#include "rapidxml.hpp"
44#include "objects.hpp"
45
46GUICheckbox::GUICheckbox(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010047 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040048{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020049 xml_attribute<>* attr;
50 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040051
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 mChecked = NULL;
53 mUnchecked = NULL;
54 mLabel = NULL;
55 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040056
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020057 mLastState = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 if (!node)
60 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040061
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020062 // The label can be loaded directly
63 mLabel = new GUIText(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040064
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020065 // Read the check states
Ethan Yonker21ff02a2015-02-18 14:35:00 -060066 child = FindNode(node, "image");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020067 if (child)
68 {
thatf6ed8fc2015-02-14 20:23:16 +010069 mChecked = LoadAttrImage(child, "checked");
70 mUnchecked = LoadAttrImage(child, "unchecked");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020071 }
Dees_Troy51a0e822012-09-05 15:24:24 -040072
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020073 // Get the variable data
Ethan Yonker21ff02a2015-02-18 14:35:00 -060074 child = FindNode(node, "data");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020075 if (child)
76 {
77 attr = child->first_attribute("variable");
78 if (attr)
79 mVarName = attr->value();
80 attr = child->first_attribute("default");
81 if (attr)
82 DataManager::SetValue(mVarName, attr->value());
83 }
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Ethan Yonker58f21322018-08-24 11:17:36 -050085 mCheckW = mCheckH = 0;
86 if (mChecked && mChecked->GetResource()) {
87 mCheckW = mChecked->GetWidth();
88 mCheckH = mChecked->GetHeight();
89 } else if (mUnchecked && mUnchecked->GetResource()) {
thatf6ed8fc2015-02-14 20:23:16 +010090 mCheckW = mUnchecked->GetWidth();
91 mCheckH = mUnchecked->GetHeight();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 }
Dees_Troy51a0e822012-09-05 15:24:24 -040093
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 int x, y, w, h;
95 mLabel->GetRenderPos(x, y, w, h);
96 SetRenderPos(x, y, 0, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -040097}
98
99GUICheckbox::~GUICheckbox()
100{
101}
102
103int GUICheckbox::Render(void)
104{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105 if (!isConditionTrue())
106 {
107 mRendered = false;
108 return 0;
109 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400110
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200111 int ret = 0;
112 int lastState = 0;
113 DataManager::GetValue(mVarName, lastState);
Dees_Troy51a0e822012-09-05 15:24:24 -0400114
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 if (lastState)
116 {
117 if (mChecked && mChecked->GetResource())
118 gr_blit(mChecked->GetResource(), 0, 0, mCheckW, mCheckH, mRenderX, mRenderY);
119 }
120 else
121 {
122 if (mUnchecked && mUnchecked->GetResource())
123 gr_blit(mUnchecked->GetResource(), 0, 0, mCheckW, mCheckH, mRenderX, mRenderY);
124 }
125 if (mLabel)
126 ret = mLabel->Render();
127 mLastState = lastState;
128 mRendered = true;
129 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400130}
131
132int GUICheckbox::Update(void)
133{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200134 if (!isConditionTrue()) return (mRendered ? 2 : 0);
135 if (!mRendered) return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137 int lastState = 0;
138 DataManager::GetValue(mVarName, lastState);
Dees_Troy51a0e822012-09-05 15:24:24 -0400139
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 if (lastState != mLastState)
141 return 2;
142 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400143}
144
145int GUICheckbox::SetRenderPos(int x, int y, int w, int h)
146{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 mRenderX = x;
148 mRenderY = y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 if (w || h)
151 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400152
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 int textW, textH;
154 mLabel->GetCurrentBounds(textW, textH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 w = textW + mCheckW + 5;
157 mRenderW = w;
158 mRenderH = mCheckH;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 mTextX = mRenderX + mCheckW + 5;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500161 mTextY = mRenderY + (mCheckH / 2);
Dees_Troy51a0e822012-09-05 15:24:24 -0400162
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 mLabel->SetRenderPos(mTextX, mTextY, 0, 0);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500164 mLabel->SetPlacement(TEXT_ONLY_RIGHT);
165 mLabel->SetMaxWidth(gr_fb_width() - mTextX);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
167 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400168}
169
Ethan Yonkerd0514ba2015-10-22 14:17:47 -0500170int GUICheckbox::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400171{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200172 if (!isConditionTrue())
173 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400174
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200175 if (state == TOUCH_RELEASE)
176 {
177 int lastState;
178 DataManager::GetValue(mVarName, lastState);
179 lastState = (lastState == 0) ? 1 : 0;
180 DataManager::SetValue(mVarName, lastState);
Vojtech Bocek5af8f3f2014-02-08 02:21:23 +0100181
182 DataManager::Vibrate("tw_button_vibrate");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200183 }
184 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400185}
186