blob: cc18b170fe93f83eee9e8b5132ab04735ee27c22 [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 Bocekede51c52014-02-07 23:58:09 +010029 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040030{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020031 mFont = NULL;
32 mIsStatic = 1;
33 mVarChanged = 0;
34 mFontHeight = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040035 maxWidth = 0;
36 charSkip = 0;
Dees_Troy4d12f962012-10-19 13:13:15 -040037 isHighlighted = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040038
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020039 if (!node)
40 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040041
thatf6ed8fc2015-02-14 20:23:16 +010042 // Load colors
43 mColor = LoadAttrColor(node, "color", COLOR(0,0,0,255));
44 mHighlightColor = LoadAttrColor(node, "highlightcolor", mColor);
Dees_Troy51a0e822012-09-05 15:24:24 -040045
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020046 // Load the font, and possibly override the color
thatf6ed8fc2015-02-14 20:23:16 +010047 xml_node<>* child = node->first_node("font");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020048 if (child)
49 {
thatf6ed8fc2015-02-14 20:23:16 +010050 mFont = LoadAttrFont(child, "resource");
51 mColor = LoadAttrColor(child, "color", mColor);
52 mHighlightColor = LoadAttrColor(child, "highlightcolor", mColor);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020053 }
Dees_Troy51a0e822012-09-05 15:24:24 -040054
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020055 // Load the placement
56 LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040057
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020058 child = node->first_node("text");
59 if (child) mText = child->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040060
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020061 // Simple way to check for static state
62 mLastValue = parseText();
63 if (mLastValue != mText) mIsStatic = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040064
thatf6ed8fc2015-02-14 20:23:16 +010065 mFontHeight = mFont->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -040066}
67
68int GUIText::Render(void)
69{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020070 if (!isConditionTrue())
71 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040072
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020073 void* fontResource = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020074 if (mFont)
75 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040076
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 mLastValue = parseText();
thatf6ed8fc2015-02-14 20:23:16 +010078 string displayValue = mLastValue;
Dees_Troy51a0e822012-09-05 15:24:24 -040079
80 if (charSkip)
81 displayValue.erase(0, charSkip);
82
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 mVarChanged = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 int x = mRenderX, y = mRenderY;
86 int width = gr_measureEx(displayValue.c_str(), fontResource);
Dees_Troy51a0e822012-09-05 15:24:24 -040087
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 if (mPlacement != TOP_LEFT && mPlacement != BOTTOM_LEFT)
89 {
90 if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY)
91 x -= (width / 2);
92 else
93 x -= width;
94 }
95 if (mPlacement != TOP_LEFT && mPlacement != TOP_RIGHT)
96 {
97 if (mPlacement == CENTER)
98 y -= (mFontHeight / 2);
99 else if (mPlacement == BOTTOM_LEFT || mPlacement == BOTTOM_RIGHT)
100 y -= mFontHeight;
101 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400102
thatf6ed8fc2015-02-14 20:23:16 +0100103 if (isHighlighted)
Dees_Troy4d12f962012-10-19 13:13:15 -0400104 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
105 else
106 gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha);
Dees_Troy51a0e822012-09-05 15:24:24 -0400107
108 if (maxWidth)
109 gr_textExW(x, y, displayValue.c_str(), fontResource, maxWidth + x);
110 else
111 gr_textEx(x, y, displayValue.c_str(), fontResource);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200112 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400113}
114
115int GUIText::Update(void)
116{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 if (!isConditionTrue())
118 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 static int updateCounter = 3;
Dees_Troy51a0e822012-09-05 15:24:24 -0400121
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200122 // This hack just makes sure we update at least once a minute for things like clock and battery
123 if (updateCounter) updateCounter--;
124 else
125 {
126 mVarChanged = 1;
127 updateCounter = 3;
128 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400129
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200130 if (mIsStatic || !mVarChanged)
131 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400132
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 std::string newValue = parseText();
134 if (mLastValue == newValue)
Dees_Troya13d74f2013-03-24 08:54:55 -0500135 return 0;
136 else
137 mLastValue = newValue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400139}
140
141int GUIText::GetCurrentBounds(int& w, int& h)
142{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200143 void* fontResource = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400144
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 if (mFont)
146 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400147
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200148 h = mFontHeight;
Dees_Troya13d74f2013-03-24 08:54:55 -0500149 mLastValue = parseText();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 w = gr_measureEx(mLastValue.c_str(), fontResource);
151 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400152}
153
154std::string GUIText::parseText(void)
155{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 static int counter = 0;
157 std::string str = mText;
158 size_t pos = 0;
159 size_t next = 0, end = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400160
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200161 while (1)
162 {
163 next = str.find('%', pos);
164 if (next == std::string::npos) return str;
165 end = str.find('%', next + 1);
166 if (end == std::string::npos) return str;
Dees_Troy51a0e822012-09-05 15:24:24 -0400167
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200168 // We have a block of data
169 std::string var = str.substr(next + 1, (end - next) - 1);
170 str.erase(next, (end - next) + 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400171
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200172 if (next + 1 == end)
173 {
174 str.insert(next, 1, '%');
175 }
176 else
177 {
178 std::string value;
179 if (DataManager::GetValue(var, value) == 0)
180 str.insert(next, value);
181 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400182
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200183 pos = next + 1;
184 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400185}
186
Vojtech Bocek07220562014-02-08 02:05:33 +0100187int GUIText::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400188{
Vojtech Bocek07220562014-02-08 02:05:33 +0100189 GUIObject::NotifyVarChange(varName, value);
190
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 mVarChanged = 1;
192 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400193}
194
195int GUIText::SetMaxWidth(unsigned width)
196{
197 maxWidth = width;
198 mVarChanged = 1;
199 return 0;
200}
201
202int GUIText::SkipCharCount(unsigned skip)
203{
204 charSkip = skip;
205 mVarChanged = 1;
206 return 0;
207}