blob: f4900ba2c375359e4f08b92b8b324fcbfb58fd62 [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 Bocekede51c52014-02-07 23:58:09 +010029 : GUIObject(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 {
thatf6ed8fc2015-02-14 20:23:16 +010051 mChecked = LoadAttrImage(child, "checked");
52 mUnchecked = LoadAttrImage(child, "unchecked");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020053 }
Dees_Troy51a0e822012-09-05 15:24:24 -040054
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020055 // Get the variable data
56 child = node->first_node("data");
57 if (child)
58 {
59 attr = child->first_attribute("variable");
60 if (attr)
61 mVarName = attr->value();
62 attr = child->first_attribute("default");
63 if (attr)
64 DataManager::SetValue(mVarName, attr->value());
65 }
Dees_Troy51a0e822012-09-05 15:24:24 -040066
thatf6ed8fc2015-02-14 20:23:16 +010067 mCheckW = mChecked->GetWidth();
68 mCheckH = mChecked->GetHeight();
69 if (mCheckW == 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020070 {
thatf6ed8fc2015-02-14 20:23:16 +010071 mCheckW = mUnchecked->GetWidth();
72 mCheckH = mUnchecked->GetHeight();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020073 }
Dees_Troy51a0e822012-09-05 15:24:24 -040074
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020075 int x, y, w, h;
76 mLabel->GetRenderPos(x, y, w, h);
77 SetRenderPos(x, y, 0, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -040078}
79
80GUICheckbox::~GUICheckbox()
81{
82}
83
84int GUICheckbox::Render(void)
85{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020086 if (!isConditionTrue())
87 {
88 mRendered = false;
89 return 0;
90 }
Dees_Troy51a0e822012-09-05 15:24:24 -040091
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 int ret = 0;
93 int lastState = 0;
94 DataManager::GetValue(mVarName, lastState);
Dees_Troy51a0e822012-09-05 15:24:24 -040095
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 if (lastState)
97 {
98 if (mChecked && mChecked->GetResource())
99 gr_blit(mChecked->GetResource(), 0, 0, mCheckW, mCheckH, mRenderX, mRenderY);
100 }
101 else
102 {
103 if (mUnchecked && mUnchecked->GetResource())
104 gr_blit(mUnchecked->GetResource(), 0, 0, mCheckW, mCheckH, mRenderX, mRenderY);
105 }
106 if (mLabel)
107 ret = mLabel->Render();
108 mLastState = lastState;
109 mRendered = true;
110 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400111}
112
113int GUICheckbox::Update(void)
114{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 if (!isConditionTrue()) return (mRendered ? 2 : 0);
116 if (!mRendered) return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400117
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200118 int lastState = 0;
119 DataManager::GetValue(mVarName, lastState);
Dees_Troy51a0e822012-09-05 15:24:24 -0400120
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200121 if (lastState != mLastState)
122 return 2;
123 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400124}
125
126int GUICheckbox::SetRenderPos(int x, int y, int w, int h)
127{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200128 mRenderX = x;
129 mRenderY = y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400130
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200131 if (w || h)
132 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200134 int textW, textH;
135 mLabel->GetCurrentBounds(textW, textH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137 w = textW + mCheckW + 5;
138 mRenderW = w;
139 mRenderH = mCheckH;
Dees_Troy51a0e822012-09-05 15:24:24 -0400140
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 mTextX = mRenderX + mCheckW + 5;
142 mTextY = mRenderY + ((mCheckH / 2) - (textH / 2));
Dees_Troy51a0e822012-09-05 15:24:24 -0400143
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200144 mLabel->SetRenderPos(mTextX, mTextY, 0, 0);
145 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
146 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400147}
148
149int GUICheckbox::NotifyTouch(TOUCH_STATE state, int x, int y)
150{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 if (!isConditionTrue())
152 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 if (state == TOUCH_RELEASE)
155 {
156 int lastState;
157 DataManager::GetValue(mVarName, lastState);
158 lastState = (lastState == 0) ? 1 : 0;
159 DataManager::SetValue(mVarName, lastState);
Vojtech Bocek5af8f3f2014-02-08 02:21:23 +0100160
161 DataManager::Vibrate("tw_button_vibrate");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200162 }
163 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400164}
165