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 |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 48 | child = FindNode(node, "image"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 49 | if (child) |
| 50 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 51 | mChecked = LoadAttrImage(child, "checked"); |
| 52 | mUnchecked = LoadAttrImage(child, "unchecked"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 53 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 54 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 55 | // Get the variable data |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 56 | child = FindNode(node, "data"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 66 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 67 | mCheckW = mChecked->GetWidth(); |
| 68 | mCheckH = mChecked->GetHeight(); |
| 69 | if (mCheckW == 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 70 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 71 | mCheckW = mUnchecked->GetWidth(); |
| 72 | mCheckH = mUnchecked->GetHeight(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 73 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 74 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 75 | int x, y, w, h; |
| 76 | mLabel->GetRenderPos(x, y, w, h); |
| 77 | SetRenderPos(x, y, 0, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | GUICheckbox::~GUICheckbox() |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | int GUICheckbox::Render(void) |
| 85 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 86 | if (!isConditionTrue()) |
| 87 | { |
| 88 | mRendered = false; |
| 89 | return 0; |
| 90 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 91 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 92 | int ret = 0; |
| 93 | int lastState = 0; |
| 94 | DataManager::GetValue(mVarName, lastState); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 95 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 96 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | int GUICheckbox::Update(void) |
| 114 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 115 | if (!isConditionTrue()) return (mRendered ? 2 : 0); |
| 116 | if (!mRendered) return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 117 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 118 | int lastState = 0; |
| 119 | DataManager::GetValue(mVarName, lastState); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 120 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 121 | if (lastState != mLastState) |
| 122 | return 2; |
| 123 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | int GUICheckbox::SetRenderPos(int x, int y, int w, int h) |
| 127 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 128 | mRenderX = x; |
| 129 | mRenderY = y; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 130 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 131 | if (w || h) |
| 132 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 133 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 134 | int textW, textH; |
| 135 | mLabel->GetCurrentBounds(textW, textH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 136 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 137 | w = textW + mCheckW + 5; |
| 138 | mRenderW = w; |
| 139 | mRenderH = mCheckH; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 140 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 141 | mTextX = mRenderX + mCheckW + 5; |
| 142 | mTextY = mRenderY + ((mCheckH / 2) - (textH / 2)); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 144 | mLabel->SetRenderPos(mTextX, mTextY, 0, 0); |
| 145 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 146 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int GUICheckbox::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 150 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 151 | if (!isConditionTrue()) |
| 152 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 153 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 154 | 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 Bocek | 5af8f3f | 2014-02-08 02:21:23 +0100 | [diff] [blame] | 160 | |
| 161 | DataManager::Vibrate("tw_button_vibrate"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 162 | } |
| 163 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 164 | } |
| 165 | |