Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // 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 | |
| 20 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 21 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | #include "rapidxml.hpp" |
| 26 | #include "objects.hpp" |
| 27 | |
| 28 | GUICheckbox::GUICheckbox(xml_node<>* node) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 29 | : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 30 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 31 | xml_attribute<>* attr; |
| 32 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 33 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 34 | mChecked = NULL; |
| 35 | mUnchecked = NULL; |
| 36 | mLabel = NULL; |
| 37 | mRendered = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 38 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 39 | mLastState = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 41 | if (!node) |
| 42 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 44 | // The label can be loaded directly |
| 45 | mLabel = new GUIText(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 46 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 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 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 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 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 70 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 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 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 82 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 83 | int x, y, w, h; |
| 84 | mLabel->GetRenderPos(x, y, w, h); |
| 85 | SetRenderPos(x, y, 0, 0); |
| 86 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | GUICheckbox::~GUICheckbox() |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | int GUICheckbox::Render(void) |
| 94 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 95 | if (!isConditionTrue()) |
| 96 | { |
| 97 | mRendered = false; |
| 98 | return 0; |
| 99 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 100 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 101 | int ret = 0; |
| 102 | int lastState = 0; |
| 103 | DataManager::GetValue(mVarName, lastState); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 104 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 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) |
| 116 | ret = mLabel->Render(); |
| 117 | mLastState = lastState; |
| 118 | mRendered = true; |
| 119 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | int GUICheckbox::Update(void) |
| 123 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 124 | if (!isConditionTrue()) return (mRendered ? 2 : 0); |
| 125 | if (!mRendered) return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 126 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 127 | int lastState = 0; |
| 128 | DataManager::GetValue(mVarName, lastState); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 129 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 130 | if (lastState != mLastState) |
| 131 | return 2; |
| 132 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | int GUICheckbox::SetRenderPos(int x, int y, int w, int h) |
| 136 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 137 | mRenderX = x; |
| 138 | mRenderY = y; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 139 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 140 | if (w || h) |
| 141 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 142 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 143 | int textW, textH; |
| 144 | mLabel->GetCurrentBounds(textW, textH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 146 | w = textW + mCheckW + 5; |
| 147 | mRenderW = w; |
| 148 | mRenderH = mCheckH; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 149 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 150 | mTextX = mRenderX + mCheckW + 5; |
| 151 | mTextY = mRenderY + ((mCheckH / 2) - (textH / 2)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 152 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 153 | mLabel->SetRenderPos(mTextX, mTextY, 0, 0); |
| 154 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 155 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | int GUICheckbox::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 159 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 160 | if (!isConditionTrue()) |
| 161 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 162 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 163 | if (state == TOUCH_RELEASE) |
| 164 | { |
| 165 | int lastState; |
| 166 | DataManager::GetValue(mVarName, lastState); |
| 167 | lastState = (lastState == 0) ? 1 : 0; |
| 168 | DataManager::SetValue(mVarName, lastState); |
Vojtech Bocek | 5af8f3f | 2014-02-08 02:21:23 +0100 | [diff] [blame] | 169 | |
| 170 | DataManager::Vibrate("tw_button_vibrate"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 171 | } |
| 172 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 173 | } |
| 174 | |