blob: 46a6622e26f8494670b29c5f8a8e68607f2c388a [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" {
Dees_Troy2673cec2013-04-02 20:22:16 +000021#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040022#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040023}
24
25#include "rapidxml.hpp"
26#include "objects.hpp"
27
28GUIText::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_Troy4d12f962012-10-19 13:13:15 -040040 isHighlighted = false;
41 hasHighlightColor = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040042
43 if (!node) return;
44
45 // Initialize color to solid black
46 memset(&mColor, 0, sizeof(COLOR));
47 mColor.alpha = 255;
Dees_Troy4d12f962012-10-19 13:13:15 -040048 memset(&mHighlightColor, 0, sizeof(COLOR));
49 mHighlightColor.alpha = 255;
Dees_Troy51a0e822012-09-05 15:24:24 -040050
51 attr = node->first_attribute("color");
52 if (attr)
53 {
54 std::string color = attr->value();
55 ConvertStrToColor(color, &mColor);
56 }
Dees_Troy4d12f962012-10-19 13:13:15 -040057 attr = node->first_attribute("highlightcolor");
58 if (attr)
59 {
60 std::string color = attr->value();
61 ConvertStrToColor(color, &mHighlightColor);
62 hasHighlightColor = true;
63 }
Dees_Troy51a0e822012-09-05 15:24:24 -040064
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_Troy4d12f962012-10-19 13:13:15 -040079
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_Troy51a0e822012-09-05 15:24:24 -040087 }
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
103int 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_Troy4d12f962012-10-19 13:13:15 -0400138 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_Troy51a0e822012-09-05 15:24:24 -0400142
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
150int 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_Troya13d74f2013-03-24 08:54:55 -0500167 if (mLastValue == newValue)
168 return 0;
169 else
170 mLastValue = newValue;
Dees_Troy51a0e822012-09-05 15:24:24 -0400171 return 2;
172}
173
174int GUIText::GetCurrentBounds(int& w, int& h)
175{
176 void* fontResource = NULL;
177
178 if (mFont) fontResource = mFont->GetResource();
179
180 h = mFontHeight;
Dees_Troya13d74f2013-03-24 08:54:55 -0500181 mLastValue = parseText();
Dees_Troy51a0e822012-09-05 15:24:24 -0400182 w = gr_measureEx(mLastValue.c_str(), fontResource);
183 return 0;
184}
185
186std::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
219int GUIText::NotifyVarChange(std::string varName, std::string value)
220{
221 mVarChanged = 1;
222 return 0;
223}
224
225int GUIText::SetMaxWidth(unsigned width)
226{
227 maxWidth = width;
228 mVarChanged = 1;
229 return 0;
230}
231
232int GUIText::SkipCharCount(unsigned skip)
233{
234 charSkip = skip;
235 mVarChanged = 1;
236 return 0;
237}