summaryrefslogtreecommitdiffstats
path: root/gui/checkbox.cpp
blob: a2958db991ff99934ccff7f0835dfb79084ee1c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
	Copyright 2017 TeamWin
	This file is part of TWRP/TeamWin Recovery Project.

	TWRP is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	TWRP is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
*/

// checkbox.cpp - GUICheckbox object

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>

#include <string>

extern "C" {
#include "../twcommon.h"
}
#include "../minuitwrp/minui.h"

#include "rapidxml.hpp"
#include "objects.hpp"

GUICheckbox::GUICheckbox(xml_node<>* node)
	: GUIObject(node)
{
	xml_attribute<>* attr;
	xml_node<>* child;

	mChecked = NULL;
	mUnchecked = NULL;
	mLabel = NULL;
	mRendered = false;

	mLastState = 0;

	if (!node)
		return;

	// The label can be loaded directly
	mLabel = new GUIText(node);

	// Read the check states
	child = FindNode(node, "image");
	if (child)
	{
		mChecked = LoadAttrImage(child, "checked");
		mUnchecked = LoadAttrImage(child, "unchecked");
	}

	// Get the variable data
	child = FindNode(node, "data");
	if (child)
	{
		attr = child->first_attribute("variable");
		if (attr)
			mVarName = attr->value();
		attr = child->first_attribute("default");
		if (attr)
			DataManager::SetValue(mVarName, attr->value());
	}

	mCheckW = mChecked->GetWidth();
	mCheckH = mChecked->GetHeight();
	if (mCheckW == 0)
	{
		mCheckW = mUnchecked->GetWidth();
		mCheckH = mUnchecked->GetHeight();
	}

	int x, y, w, h;
	mLabel->GetRenderPos(x, y, w, h);
	SetRenderPos(x, y, 0, 0);
}

GUICheckbox::~GUICheckbox()
{
}

int GUICheckbox::Render(void)
{
	if (!isConditionTrue())
	{
		mRendered = false;
		return 0;
	}

	int ret = 0;
	int lastState = 0;
	DataManager::GetValue(mVarName, lastState);

	if (lastState)
	{
		if (mChecked && mChecked->GetResource())
			gr_blit(mChecked->GetResource(), 0, 0, mCheckW, mCheckH, mRenderX, mRenderY);
	}
	else
	{
		if (mUnchecked && mUnchecked->GetResource())
			gr_blit(mUnchecked->GetResource(), 0, 0, mCheckW, mCheckH, mRenderX, mRenderY);
	}
	if (mLabel)
		ret = mLabel->Render();
	mLastState = lastState;
	mRendered = true;
	return ret;
}

int GUICheckbox::Update(void)
{
	if (!isConditionTrue())	return (mRendered ? 2 : 0);
	if (!mRendered)			return 2;

	int lastState = 0;
	DataManager::GetValue(mVarName, lastState);

	if (lastState != mLastState)
		return 2;
	return 0;
}

int GUICheckbox::SetRenderPos(int x, int y, int w, int h)
{
	mRenderX = x;
	mRenderY = y;

	if (w || h)
		return -1;

	int textW, textH;
	mLabel->GetCurrentBounds(textW, textH);

	w = textW + mCheckW + 5;
	mRenderW = w;
	mRenderH = mCheckH;

	mTextX = mRenderX + mCheckW + 5;
	mTextY = mRenderY + (mCheckH / 2);

	mLabel->SetRenderPos(mTextX, mTextY, 0, 0);
	mLabel->SetPlacement(TEXT_ONLY_RIGHT);
	mLabel->SetMaxWidth(gr_fb_width() - mTextX);
	SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
	return 0;
}

int GUICheckbox::NotifyTouch(TOUCH_STATE state, int x __unused, int y __unused)
{
	if (!isConditionTrue())
		return -1;

	if (state == TOUCH_RELEASE)
	{
		int lastState;
		DataManager::GetValue(mVarName, lastState);
		lastState = (lastState == 0) ? 1 : 0;
		DataManager::SetValue(mVarName, lastState);

		DataManager::Vibrate("tw_button_vibrate");
	}
	return 0;
}