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