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