blob: 1eceab1f51a46eee3befe672af1a126743ce66c5 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// 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
20extern "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
29GUIText::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_Troy4d12f962012-10-19 13:13:15 -040041 isHighlighted = false;
42 hasHighlightColor = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040043
44 if (!node) return;
45
46 // Initialize color to solid black
47 memset(&mColor, 0, sizeof(COLOR));
48 mColor.alpha = 255;
Dees_Troy4d12f962012-10-19 13:13:15 -040049 memset(&mHighlightColor, 0, sizeof(COLOR));
50 mHighlightColor.alpha = 255;
Dees_Troy51a0e822012-09-05 15:24:24 -040051
52 attr = node->first_attribute("color");
53 if (attr)
54 {
55 std::string color = attr->value();
56 ConvertStrToColor(color, &mColor);
57 }
Dees_Troy4d12f962012-10-19 13:13:15 -040058 attr = node->first_attribute("highlightcolor");
59 if (attr)
60 {
61 std::string color = attr->value();
62 ConvertStrToColor(color, &mHighlightColor);
63 hasHighlightColor = true;
64 }
Dees_Troy51a0e822012-09-05 15:24:24 -040065
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_Troy4d12f962012-10-19 13:13:15 -040080
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_Troy51a0e822012-09-05 15:24:24 -040088 }
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
104int 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_Troy4d12f962012-10-19 13:13:15 -0400139 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_Troy51a0e822012-09-05 15:24:24 -0400143
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
151int 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();
Dees_Troya13d74f2013-03-24 08:54:55 -0500168 if (mLastValue == newValue)
169 return 0;
170 else
171 mLastValue = newValue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400172 return 2;
173}
174
175int GUIText::GetCurrentBounds(int& w, int& h)
176{
177 void* fontResource = NULL;
178
179 if (mFont) fontResource = mFont->GetResource();
180
181 h = mFontHeight;
Dees_Troya13d74f2013-03-24 08:54:55 -0500182 mLastValue = parseText();
Dees_Troy51a0e822012-09-05 15:24:24 -0400183 w = gr_measureEx(mLastValue.c_str(), fontResource);
184 return 0;
185}
186
187std::string GUIText::parseText(void)
188{
189 static int counter = 0;
190 std::string str = mText;
191 size_t pos = 0;
192 size_t next = 0, end = 0;
193
194 while (1)
195 {
196 next = str.find('%', pos);
197 if (next == std::string::npos) return str;
198 end = str.find('%', next + 1);
199 if (end == std::string::npos) return str;
200
201 // We have a block of data
202 std::string var = str.substr(next + 1, (end - next) - 1);
203 str.erase(next, (end - next) + 1);
204
205 if (next + 1 == end)
206 {
207 str.insert(next, 1, '%');
208 }
209 else
210 {
211 std::string value;
212 if (DataManager::GetValue(var, value) == 0)
213 str.insert(next, value);
214 }
215
216 pos = next + 1;
217 }
218}
219
220int GUIText::NotifyVarChange(std::string varName, std::string value)
221{
222 mVarChanged = 1;
223 return 0;
224}
225
226int GUIText::SetMaxWidth(unsigned width)
227{
228 maxWidth = width;
229 mVarChanged = 1;
230 return 0;
231}
232
233int GUIText::SkipCharCount(unsigned skip)
234{
235 charSkip = skip;
236 mVarChanged = 1;
237 return 0;
238}