blob: 9898e283825387dd2d9ba99a681ec38d4769c172 [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;
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");
50 mColor = LoadAttrColor(FindNode(node, "font"), "color", mColor);
51 mHighlightColor = LoadAttrColor(FindNode(node, "font"), "highlightcolor", mColor);
Dees_Troy51a0e822012-09-05 15:24:24 -040052
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020053 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060054 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040055
Ethan Yonker21ff02a2015-02-18 14:35:00 -060056 xml_node<>* child = FindNode(node, "text");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020057 if (child) mText = child->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050059 child = FindNode(node, "noscaling");
60 if (child) {
61 scaleWidth = false;
62 } else {
63 if (mPlacement == TOP_LEFT || mPlacement == BOTTOM_LEFT) {
64 maxWidth = gr_fb_width() - mRenderX;
65 } else if (mPlacement == TOP_RIGHT || mPlacement == BOTTOM_RIGHT) {
66 maxWidth = mRenderX;
67 } else if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) {
68 if (mRenderX < gr_fb_width() / 2) {
69 maxWidth = mRenderX * 2;
70 } else {
71 maxWidth = (gr_fb_width() - mRenderX) * 2;
72 }
73 }
74 }
75
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020076 // Simple way to check for static state
thatb2e8f672015-03-05 20:25:39 +010077 mLastValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 if (mLastValue != mText) mIsStatic = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040079
thatf6ed8fc2015-02-14 20:23:16 +010080 mFontHeight = mFont->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -040081}
82
83int GUIText::Render(void)
84{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 if (!isConditionTrue())
86 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040087
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 void* fontResource = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020089 if (mFont)
90 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040091
thatb2e8f672015-03-05 20:25:39 +010092 mLastValue = gui_parse_text(mText);
thatf6ed8fc2015-02-14 20:23:16 +010093 string displayValue = mLastValue;
Dees_Troy51a0e822012-09-05 15:24:24 -040094
95 if (charSkip)
96 displayValue.erase(0, charSkip);
97
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 mVarChanged = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040099
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200100 int x = mRenderX, y = mRenderY;
101 int width = gr_measureEx(displayValue.c_str(), fontResource);
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
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500108 gr_textEx_scaleW(mRenderX, mRenderY, displayValue.c_str(), fontResource, maxWidth, mPlacement, scaleWidth);
109
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200110 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400111}
112
113int GUIText::Update(void)
114{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 if (!isConditionTrue())
116 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400117
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200118 static int updateCounter = 3;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 // This hack just makes sure we update at least once a minute for things like clock and battery
121 if (updateCounter) updateCounter--;
122 else
123 {
124 mVarChanged = 1;
125 updateCounter = 3;
126 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400127
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200128 if (mIsStatic || !mVarChanged)
129 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400130
thatb2e8f672015-03-05 20:25:39 +0100131 std::string newValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (mLastValue == newValue)
Dees_Troya13d74f2013-03-24 08:54:55 -0500133 return 0;
134 else
135 mLastValue = newValue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400137}
138
139int GUIText::GetCurrentBounds(int& w, int& h)
140{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 void* fontResource = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400142
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200143 if (mFont)
144 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400145
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 h = mFontHeight;
thatb2e8f672015-03-05 20:25:39 +0100147 mLastValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200148 w = gr_measureEx(mLastValue.c_str(), fontResource);
149 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150}
151
Vojtech Bocek07220562014-02-08 02:05:33 +0100152int GUIText::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400153{
Vojtech Bocek07220562014-02-08 02:05:33 +0100154 GUIObject::NotifyVarChange(varName, value);
155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 mVarChanged = 1;
157 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158}
159
160int GUIText::SetMaxWidth(unsigned width)
161{
162 maxWidth = width;
163 mVarChanged = 1;
164 return 0;
165}
166
167int GUIText::SkipCharCount(unsigned skip)
168{
169 charSkip = skip;
170 mVarChanged = 1;
171 return 0;
172}