blob: 6498af62c5470850c13c8b2b728d8c3027149e0a [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}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010023#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040024
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;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050037 scaleWidth = true;
Dees_Troy4d12f962012-10-19 13:13:15 -040038 isHighlighted = false;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050039 mText = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040040
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020041 if (!node)
42 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040043
thatf6ed8fc2015-02-14 20:23:16 +010044 // Load colors
45 mColor = LoadAttrColor(node, "color", COLOR(0,0,0,255));
46 mHighlightColor = LoadAttrColor(node, "highlightcolor", mColor);
Dees_Troy51a0e822012-09-05 15:24:24 -040047
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020048 // Load the font, and possibly override the color
Ethan Yonker21ff02a2015-02-18 14:35:00 -060049 mFont = LoadAttrFont(FindNode(node, "font"), "resource");
Ethan Yonkerfbb43532015-12-28 21:54:50 +010050 if (!mFont)
51 return;
Ethan Yonker21ff02a2015-02-18 14:35:00 -060052 mColor = LoadAttrColor(FindNode(node, "font"), "color", mColor);
53 mHighlightColor = LoadAttrColor(FindNode(node, "font"), "highlightcolor", mColor);
Dees_Troy51a0e822012-09-05 15:24:24 -040054
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020055 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060056 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040057
Ethan Yonker21ff02a2015-02-18 14:35:00 -060058 xml_node<>* child = FindNode(node, "text");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 if (child) mText = child->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040060
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050061 child = FindNode(node, "noscaling");
62 if (child) {
63 scaleWidth = false;
64 } else {
65 if (mPlacement == TOP_LEFT || mPlacement == BOTTOM_LEFT) {
66 maxWidth = gr_fb_width() - mRenderX;
67 } else if (mPlacement == TOP_RIGHT || mPlacement == BOTTOM_RIGHT) {
68 maxWidth = mRenderX;
69 } else if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) {
70 if (mRenderX < gr_fb_width() / 2) {
71 maxWidth = mRenderX * 2;
72 } else {
73 maxWidth = (gr_fb_width() - mRenderX) * 2;
74 }
75 }
76 }
77
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 // Simple way to check for static state
thatb2e8f672015-03-05 20:25:39 +010079 mLastValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 if (mLastValue != mText) mIsStatic = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040081
thatf6ed8fc2015-02-14 20:23:16 +010082 mFontHeight = mFont->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -040083}
84
85int GUIText::Render(void)
86{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020087 if (!isConditionTrue())
88 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040089
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 void* fontResource = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 if (mFont)
92 fontResource = mFont->GetResource();
Ethan Yonkerfbb43532015-12-28 21:54:50 +010093 else
94 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040095
thatb2e8f672015-03-05 20:25:39 +010096 mLastValue = gui_parse_text(mText);
thatf6ed8fc2015-02-14 20:23:16 +010097 string displayValue = mLastValue;
Dees_Troy51a0e822012-09-05 15:24:24 -040098
99 if (charSkip)
100 displayValue.erase(0, charSkip);
101
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200102 mVarChanged = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400103
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200104 int x = mRenderX, y = mRenderY;
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100105 int width = gr_ttf_measureEx(displayValue.c_str(), fontResource);
Dees_Troy51a0e822012-09-05 15:24:24 -0400106
thatf6ed8fc2015-02-14 20:23:16 +0100107 if (isHighlighted)
Dees_Troy4d12f962012-10-19 13:13:15 -0400108 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
109 else
110 gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha);
Dees_Troy51a0e822012-09-05 15:24:24 -0400111
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500112 gr_textEx_scaleW(mRenderX, mRenderY, displayValue.c_str(), fontResource, maxWidth, mPlacement, scaleWidth);
113
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400115}
116
117int GUIText::Update(void)
118{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200119 if (!isConditionTrue())
120 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400121
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200122 static int updateCounter = 3;
Dees_Troy51a0e822012-09-05 15:24:24 -0400123
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200124 // This hack just makes sure we update at least once a minute for things like clock and battery
125 if (updateCounter) updateCounter--;
126 else
127 {
128 mVarChanged = 1;
129 updateCounter = 3;
130 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400131
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (mIsStatic || !mVarChanged)
133 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
thatb2e8f672015-03-05 20:25:39 +0100135 std::string newValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 if (mLastValue == newValue)
Dees_Troya13d74f2013-03-24 08:54:55 -0500137 return 0;
138 else
139 mLastValue = newValue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400141}
142
143int GUIText::GetCurrentBounds(int& w, int& h)
144{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 void* fontResource = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 if (mFont)
148 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400149
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 h = mFontHeight;
thatb2e8f672015-03-05 20:25:39 +0100151 mLastValue = gui_parse_text(mText);
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100152 w = gr_ttf_measureEx(mLastValue.c_str(), fontResource);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200153 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400154}
155
Vojtech Bocek07220562014-02-08 02:05:33 +0100156int GUIText::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400157{
Vojtech Bocek07220562014-02-08 02:05:33 +0100158 GUIObject::NotifyVarChange(varName, value);
159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 mVarChanged = 1;
161 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400162}
163
164int GUIText::SetMaxWidth(unsigned width)
165{
166 maxWidth = width;
167 mVarChanged = 1;
168 return 0;
169}
170
171int GUIText::SkipCharCount(unsigned skip)
172{
173 charSkip = skip;
174 mVarChanged = 1;
175 return 0;
176}