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