blob: e52b58942d36c1f1662766500b2ac7a252228b60 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// checkbox.cpp - GUICheckbox object
2
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
8#include <sys/reboot.h>
9#include <sys/stat.h>
10#include <sys/time.h>
11#include <sys/mman.h>
12#include <sys/types.h>
13#include <sys/ioctl.h>
14#include <time.h>
15#include <unistd.h>
16#include <stdlib.h>
17
18#include <string>
19
20extern "C" {
21#include "../common.h"
22#include "../minuitwrp/minui.h"
23#include "../recovery_ui.h"
24}
25
26#include "rapidxml.hpp"
27#include "objects.hpp"
28
29GUICheckbox::GUICheckbox(xml_node<>* node)
30 : Conditional(node)
31{
32 xml_attribute<>* attr;
33 xml_node<>* child;
34
35 mChecked = NULL;
36 mUnchecked = NULL;
37 mLabel = NULL;
38 mRendered = false;
39
40 mLastState = 0;
41
42 if (!node) return;
43
44 // The label can be loaded directly
45 mLabel = new GUIText(node);
46
47 // Read the check states
48 child = node->first_node("image");
49 if (child)
50 {
51 attr = child->first_attribute("checked");
52 if (attr)
53 mChecked = PageManager::FindResource(attr->value());
54 attr = child->first_attribute("unchecked");
55 if (attr)
56 mUnchecked = PageManager::FindResource(attr->value());
57 }
58
59 // Get the variable data
60 child = node->first_node("data");
61 if (child)
62 {
63 attr = child->first_attribute("variable");
64 if (attr)
65 mVarName = attr->value();
66 attr = child->first_attribute("default");
67 if (attr)
68 DataManager::SetValue(mVarName, attr->value());
69 }
70
71 mCheckW = 0; mCheckH = 0;
72 if (mChecked && mChecked->GetResource())
73 {
74 mCheckW = gr_get_width(mChecked->GetResource());
75 mCheckH = gr_get_height(mChecked->GetResource());
76 }
77 else if (mUnchecked && mUnchecked->GetResource())
78 {
79 mCheckW = gr_get_width(mUnchecked->GetResource());
80 mCheckH = gr_get_height(mUnchecked->GetResource());
81 }
82
83 int x, y, w, h;
84 mLabel->GetRenderPos(x, y, w, h);
85 SetRenderPos(x, y, 0, 0);
86 return;
87}
88
89GUICheckbox::~GUICheckbox()
90{
91}
92
93int GUICheckbox::Render(void)
94{
95 if (!isConditionTrue())
96 {
97 mRendered = false;
98 return 0;
99 }
100
101 int ret = 0;
102 int lastState = 0;
103 DataManager::GetValue(mVarName, lastState);
104
105 if (lastState)
106 {
107 if (mChecked && mChecked->GetResource())
108 gr_blit(mChecked->GetResource(), 0, 0, mCheckW, mCheckH, mRenderX, mRenderY);
109 }
110 else
111 {
112 if (mUnchecked && mUnchecked->GetResource())
113 gr_blit(mUnchecked->GetResource(), 0, 0, mCheckW, mCheckH, mRenderX, mRenderY);
114 }
115 if (mLabel) ret = mLabel->Render();
116 mLastState = lastState;
117 mRendered = true;
118 return ret;
119}
120
121int GUICheckbox::Update(void)
122{
123 if (!isConditionTrue()) return (mRendered ? 2 : 0);
124 if (!mRendered) return 2;
125
126 int lastState = 0;
127 DataManager::GetValue(mVarName, lastState);
128
129 if (lastState != mLastState)
130 return 2;
131 return 0;
132}
133
134int GUICheckbox::SetRenderPos(int x, int y, int w, int h)
135{
136 mRenderX = x;
137 mRenderY = y;
138
139 if (w || h) return -1;
140
141 int textW, textH;
142 mLabel->GetCurrentBounds(textW, textH);
143
144 w = textW + mCheckW + 5;
145 mRenderW = w;
146 mRenderH = mCheckH;
147
148 mTextX = mRenderX + mCheckW + 5;
149 mTextY = mRenderY + ((mCheckH / 2) - (textH / 2));
150
151 mLabel->SetRenderPos(mTextX, mTextY, 0, 0);
152 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
153 return 0;
154}
155
156int GUICheckbox::NotifyTouch(TOUCH_STATE state, int x, int y)
157{
158 if (!isConditionTrue()) return -1;
159
160 if (state == TOUCH_RELEASE)
161 {
162 int lastState;
163 DataManager::GetValue(mVarName, lastState);
164 lastState = (lastState == 0) ? 1 : 0;
165 DataManager::SetValue(mVarName, lastState);
166 }
167 return 0;
168}
169