Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // text.cpp - GUIText 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" { |
| 21 | #include "../common.h" |
| 22 | #include "../minuitwrp/minui.h" |
| 23 | #include "../recovery_ui.h" |
| 24 | } |
| 25 | |
| 26 | #include "rapidxml.hpp" |
| 27 | #include "objects.hpp" |
| 28 | |
| 29 | GUIText::GUIText(xml_node<>* node) |
| 30 | : Conditional(node) |
| 31 | { |
| 32 | xml_attribute<>* attr; |
| 33 | xml_node<>* child; |
| 34 | |
| 35 | mFont = NULL; |
| 36 | mIsStatic = 1; |
| 37 | mVarChanged = 0; |
| 38 | mFontHeight = 0; |
| 39 | maxWidth = 0; |
| 40 | charSkip = 0; |
| 41 | |
| 42 | if (!node) return; |
| 43 | |
| 44 | // Initialize color to solid black |
| 45 | memset(&mColor, 0, sizeof(COLOR)); |
| 46 | mColor.alpha = 255; |
| 47 | |
| 48 | attr = node->first_attribute("color"); |
| 49 | if (attr) |
| 50 | { |
| 51 | std::string color = attr->value(); |
| 52 | ConvertStrToColor(color, &mColor); |
| 53 | } |
| 54 | |
| 55 | // Load the font, and possibly override the color |
| 56 | child = node->first_node("font"); |
| 57 | if (child) |
| 58 | { |
| 59 | attr = child->first_attribute("resource"); |
| 60 | if (attr) |
| 61 | mFont = PageManager::FindResource(attr->value()); |
| 62 | |
| 63 | attr = child->first_attribute("color"); |
| 64 | if (attr) |
| 65 | { |
| 66 | std::string color = attr->value(); |
| 67 | ConvertStrToColor(color, &mColor); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Load the placement |
| 72 | LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement); |
| 73 | |
| 74 | child = node->first_node("text"); |
| 75 | if (child) mText = child->value(); |
| 76 | |
| 77 | // Simple way to check for static state |
| 78 | mLastValue = parseText(); |
| 79 | if (mLastValue != mText) mIsStatic = 0; |
| 80 | |
| 81 | gr_getFontDetails(mFont ? mFont->GetResource() : NULL, (unsigned*) &mFontHeight, NULL); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | int GUIText::Render(void) |
| 86 | { |
| 87 | if (!isConditionTrue()) return 0; |
| 88 | |
| 89 | void* fontResource = NULL; |
| 90 | string displayValue; |
| 91 | |
| 92 | if (mFont) fontResource = mFont->GetResource(); |
| 93 | |
| 94 | mLastValue = parseText(); |
| 95 | displayValue = mLastValue; |
| 96 | |
| 97 | if (charSkip) |
| 98 | displayValue.erase(0, charSkip); |
| 99 | |
| 100 | mVarChanged = 0; |
| 101 | |
| 102 | int x = mRenderX, y = mRenderY; |
| 103 | int width = gr_measureEx(displayValue.c_str(), fontResource); |
| 104 | |
| 105 | if (mPlacement != TOP_LEFT && mPlacement != BOTTOM_LEFT) |
| 106 | { |
| 107 | if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) |
| 108 | x -= (width / 2); |
| 109 | else |
| 110 | x -= width; |
| 111 | } |
| 112 | if (mPlacement != TOP_LEFT && mPlacement != TOP_RIGHT) |
| 113 | { |
| 114 | if (mPlacement == CENTER) |
| 115 | y -= (mFontHeight / 2); |
| 116 | else if (mPlacement == BOTTOM_LEFT || mPlacement == BOTTOM_RIGHT) |
| 117 | y -= mFontHeight; |
| 118 | } |
| 119 | |
| 120 | gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha); |
| 121 | |
| 122 | if (maxWidth) |
| 123 | gr_textExW(x, y, displayValue.c_str(), fontResource, maxWidth + x); |
| 124 | else |
| 125 | gr_textEx(x, y, displayValue.c_str(), fontResource); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | int GUIText::Update(void) |
| 130 | { |
| 131 | if (!isConditionTrue()) return 0; |
| 132 | |
| 133 | static int updateCounter = 3; |
| 134 | |
| 135 | // This hack just makes sure we update at least once a minute for things like clock and battery |
| 136 | if (updateCounter) updateCounter--; |
| 137 | else |
| 138 | { |
| 139 | mVarChanged = 1; |
| 140 | updateCounter = 3; |
| 141 | } |
| 142 | |
| 143 | if (mIsStatic || !mVarChanged) return 0; |
| 144 | |
| 145 | std::string newValue = parseText(); |
| 146 | if (mLastValue == newValue) return 0; |
| 147 | return 2; |
| 148 | } |
| 149 | |
| 150 | int GUIText::GetCurrentBounds(int& w, int& h) |
| 151 | { |
| 152 | void* fontResource = NULL; |
| 153 | |
| 154 | if (mFont) fontResource = mFont->GetResource(); |
| 155 | |
| 156 | h = mFontHeight; |
| 157 | w = gr_measureEx(mLastValue.c_str(), fontResource); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | std::string GUIText::parseText(void) |
| 162 | { |
| 163 | static int counter = 0; |
| 164 | std::string str = mText; |
| 165 | size_t pos = 0; |
| 166 | size_t next = 0, end = 0; |
| 167 | |
| 168 | while (1) |
| 169 | { |
| 170 | next = str.find('%', pos); |
| 171 | if (next == std::string::npos) return str; |
| 172 | end = str.find('%', next + 1); |
| 173 | if (end == std::string::npos) return str; |
| 174 | |
| 175 | // We have a block of data |
| 176 | std::string var = str.substr(next + 1, (end - next) - 1); |
| 177 | str.erase(next, (end - next) + 1); |
| 178 | |
| 179 | if (next + 1 == end) |
| 180 | { |
| 181 | str.insert(next, 1, '%'); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | std::string value; |
| 186 | if (DataManager::GetValue(var, value) == 0) |
| 187 | str.insert(next, value); |
| 188 | } |
| 189 | |
| 190 | pos = next + 1; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | int GUIText::NotifyVarChange(std::string varName, std::string value) |
| 195 | { |
| 196 | mVarChanged = 1; |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int GUIText::SetMaxWidth(unsigned width) |
| 201 | { |
| 202 | maxWidth = width; |
| 203 | mVarChanged = 1; |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | int GUIText::SkipCharCount(unsigned skip) |
| 208 | { |
| 209 | charSkip = skip; |
| 210 | mVarChanged = 1; |
| 211 | return 0; |
| 212 | } |