blob: b6d5e6348a075dbf001fe5b303e5e8bf9fca77f3 [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)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020029 : Conditional(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040030{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020031 xml_attribute<>* attr;
32 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020034 mChecked = NULL;
35 mUnchecked = NULL;
36 mLabel = NULL;
37 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040038
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020039 mLastState = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040040
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020041 if (!node)
42 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040043
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020044 // The label can be loaded directly
45 mLabel = new GUIText(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040046
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020047 // 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 }
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 // 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 }
Dees_Troy51a0e822012-09-05 15:24:24 -040070
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020071 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 }
Dees_Troy51a0e822012-09-05 15:24:24 -040082
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 int x, y, w, h;
84 mLabel->GetRenderPos(x, y, w, h);
85 SetRenderPos(x, y, 0, 0);
86 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040087}
88
89GUICheckbox::~GUICheckbox()
90{
91}
92
93int GUICheckbox::Render(void)
94{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 if (!isConditionTrue())
96 {
97 mRendered = false;
98 return 0;
99 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 int ret = 0;
102 int lastState = 0;
103 DataManager::GetValue(mVarName, lastState);
Dees_Troy51a0e822012-09-05 15:24:24 -0400104
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105 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)
116 ret = mLabel->Render();
117 mLastState = lastState;
118 mRendered = true;
119 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120}
121
122int GUICheckbox::Update(void)
123{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200124 if (!isConditionTrue()) return (mRendered ? 2 : 0);
125 if (!mRendered) return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400126
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200127 int lastState = 0;
128 DataManager::GetValue(mVarName, lastState);
Dees_Troy51a0e822012-09-05 15:24:24 -0400129
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200130 if (lastState != mLastState)
131 return 2;
132 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133}
134
135int GUICheckbox::SetRenderPos(int x, int y, int w, int h)
136{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137 mRenderX = x;
138 mRenderY = y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400139
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 if (w || h)
141 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400142
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200143 int textW, textH;
144 mLabel->GetCurrentBounds(textW, textH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400145
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 w = textW + mCheckW + 5;
147 mRenderW = w;
148 mRenderH = mCheckH;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 mTextX = mRenderX + mCheckW + 5;
151 mTextY = mRenderY + ((mCheckH / 2) - (textH / 2));
Dees_Troy51a0e822012-09-05 15:24:24 -0400152
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 mLabel->SetRenderPos(mTextX, mTextY, 0, 0);
154 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
155 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156}
157
158int GUICheckbox::NotifyTouch(TOUCH_STATE state, int x, int y)
159{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 if (!isConditionTrue())
161 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400162
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 if (state == TOUCH_RELEASE)
164 {
165 int lastState;
166 DataManager::GetValue(mVarName, lastState);
167 lastState = (lastState == 0) ? 1 : 0;
168 DataManager::SetValue(mVarName, lastState);
169 }
170 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400171}
172