blob: 7eb6c18b411e3781a6c36d89983b547172c0b0be [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)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020029 : Conditional(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040030{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020031 xml_attribute<>* attr;
32 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020034 mFont = NULL;
35 mIsStatic = 1;
36 mVarChanged = 0;
37 mFontHeight = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040038 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
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020043 if (!node)
44 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040045
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020046 // 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));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020050 mHighlightColor.alpha = 255;
Dees_Troy51a0e822012-09-05 15:24:24 -040051
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 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");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 if (attr)
60 {
61 std::string color = attr->value();
Dees_Troy4d12f962012-10-19 13:13:15 -040062 ConvertStrToColor(color, &mHighlightColor);
63 hasHighlightColor = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020064 }
Dees_Troy51a0e822012-09-05 15:24:24 -040065
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 // 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());
Dees_Troy51a0e822012-09-05 15:24:24 -040073
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020074 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");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020082 if (attr)
83 {
84 std::string color = attr->value();
Dees_Troy4d12f962012-10-19 13:13:15 -040085 ConvertStrToColor(color, &mHighlightColor);
86 hasHighlightColor = true;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 }
88 }
Dees_Troy51a0e822012-09-05 15:24:24 -040089
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 // Load the placement
91 LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040092
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020093 child = node->first_node("text");
94 if (child) mText = child->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040095
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 // Simple way to check for static state
97 mLastValue = parseText();
98 if (mLastValue != mText) mIsStatic = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040099
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200100 gr_getFontDetails(mFont ? mFont->GetResource() : NULL, (unsigned*) &mFontHeight, NULL);
101 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400102}
103
104int GUIText::Render(void)
105{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200106 if (!isConditionTrue())
107 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400108
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200109 void* fontResource = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400110 string displayValue;
111
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200112 if (mFont)
113 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400114
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 mLastValue = parseText();
Dees_Troy51a0e822012-09-05 15:24:24 -0400116 displayValue = mLastValue;
117
118 if (charSkip)
119 displayValue.erase(0, charSkip);
120
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200121 mVarChanged = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400122
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200123 int x = mRenderX, y = mRenderY;
124 int width = gr_measureEx(displayValue.c_str(), fontResource);
Dees_Troy51a0e822012-09-05 15:24:24 -0400125
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200126 if (mPlacement != TOP_LEFT && mPlacement != BOTTOM_LEFT)
127 {
128 if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY)
129 x -= (width / 2);
130 else
131 x -= width;
132 }
133 if (mPlacement != TOP_LEFT && mPlacement != TOP_RIGHT)
134 {
135 if (mPlacement == CENTER)
136 y -= (mFontHeight / 2);
137 else if (mPlacement == BOTTOM_LEFT || mPlacement == BOTTOM_RIGHT)
138 y -= mFontHeight;
139 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400140
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 if (hasHighlightColor && isHighlighted)
Dees_Troy4d12f962012-10-19 13:13:15 -0400142 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
143 else
144 gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha);
Dees_Troy51a0e822012-09-05 15:24:24 -0400145
146 if (maxWidth)
147 gr_textExW(x, y, displayValue.c_str(), fontResource, maxWidth + x);
148 else
149 gr_textEx(x, y, displayValue.c_str(), fontResource);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400151}
152
153int GUIText::Update(void)
154{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 if (!isConditionTrue())
156 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400157
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 static int updateCounter = 3;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 // This hack just makes sure we update at least once a minute for things like clock and battery
161 if (updateCounter) updateCounter--;
162 else
163 {
164 mVarChanged = 1;
165 updateCounter = 3;
166 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400167
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200168 if (mIsStatic || !mVarChanged)
169 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400170
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200171 std::string newValue = parseText();
172 if (mLastValue == newValue)
Dees_Troya13d74f2013-03-24 08:54:55 -0500173 return 0;
174 else
175 mLastValue = newValue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200176 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400177}
178
179int GUIText::GetCurrentBounds(int& w, int& h)
180{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200181 void* fontResource = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400182
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200183 if (mFont)
184 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400185
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186 h = mFontHeight;
Dees_Troya13d74f2013-03-24 08:54:55 -0500187 mLastValue = parseText();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200188 w = gr_measureEx(mLastValue.c_str(), fontResource);
189 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400190}
191
192std::string GUIText::parseText(void)
193{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200194 static int counter = 0;
195 std::string str = mText;
196 size_t pos = 0;
197 size_t next = 0, end = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400198
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200199 while (1)
200 {
201 next = str.find('%', pos);
202 if (next == std::string::npos) return str;
203 end = str.find('%', next + 1);
204 if (end == std::string::npos) return str;
Dees_Troy51a0e822012-09-05 15:24:24 -0400205
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 // We have a block of data
207 std::string var = str.substr(next + 1, (end - next) - 1);
208 str.erase(next, (end - next) + 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400209
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200210 if (next + 1 == end)
211 {
212 str.insert(next, 1, '%');
213 }
214 else
215 {
216 std::string value;
217 if (DataManager::GetValue(var, value) == 0)
218 str.insert(next, value);
219 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400220
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200221 pos = next + 1;
222 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400223}
224
225int GUIText::NotifyVarChange(std::string varName, std::string value)
226{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200227 mVarChanged = 1;
228 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400229}
230
231int GUIText::SetMaxWidth(unsigned width)
232{
233 maxWidth = width;
234 mVarChanged = 1;
235 return 0;
236}
237
238int GUIText::SkipCharCount(unsigned skip)
239{
240 charSkip = skip;
241 mVarChanged = 1;
242 return 0;
243}