blob: effb9359bfadde31d792b6c82bbde13f21ac6a9b [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
Ethan Yonker21ff02a2015-02-18 14:35:00 -060047 mFont = LoadAttrFont(FindNode(node, "font"), "resource");
48 mColor = LoadAttrColor(FindNode(node, "font"), "color", mColor);
49 mHighlightColor = LoadAttrColor(FindNode(node, "font"), "highlightcolor", mColor);
Dees_Troy51a0e822012-09-05 15:24:24 -040050
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020051 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060052 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040053
Ethan Yonker21ff02a2015-02-18 14:35:00 -060054 xml_node<>* child = FindNode(node, "text");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020055 if (child) mText = child->value();
Dees_Troy51a0e822012-09-05 15:24:24 -040056
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020057 // Simple way to check for static state
thatb2e8f672015-03-05 20:25:39 +010058 mLastValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 if (mLastValue != mText) mIsStatic = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040060
thatf6ed8fc2015-02-14 20:23:16 +010061 mFontHeight = mFont->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -040062}
63
64int GUIText::Render(void)
65{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 if (!isConditionTrue())
67 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040068
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020069 void* fontResource = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020070 if (mFont)
71 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -040072
thatb2e8f672015-03-05 20:25:39 +010073 mLastValue = gui_parse_text(mText);
thatf6ed8fc2015-02-14 20:23:16 +010074 string displayValue = mLastValue;
Dees_Troy51a0e822012-09-05 15:24:24 -040075
76 if (charSkip)
77 displayValue.erase(0, charSkip);
78
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020079 mVarChanged = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040080
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020081 int x = mRenderX, y = mRenderY;
82 int width = gr_measureEx(displayValue.c_str(), fontResource);
Dees_Troy51a0e822012-09-05 15:24:24 -040083
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084 if (mPlacement != TOP_LEFT && mPlacement != BOTTOM_LEFT)
85 {
86 if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY)
87 x -= (width / 2);
88 else
89 x -= width;
90 }
91 if (mPlacement != TOP_LEFT && mPlacement != TOP_RIGHT)
92 {
93 if (mPlacement == CENTER)
94 y -= (mFontHeight / 2);
95 else if (mPlacement == BOTTOM_LEFT || mPlacement == BOTTOM_RIGHT)
96 y -= mFontHeight;
97 }
Dees_Troy51a0e822012-09-05 15:24:24 -040098
thatf6ed8fc2015-02-14 20:23:16 +010099 if (isHighlighted)
Dees_Troy4d12f962012-10-19 13:13:15 -0400100 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
101 else
102 gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha);
Dees_Troy51a0e822012-09-05 15:24:24 -0400103
104 if (maxWidth)
105 gr_textExW(x, y, displayValue.c_str(), fontResource, maxWidth + x);
106 else
107 gr_textEx(x, y, displayValue.c_str(), fontResource);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200108 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400109}
110
111int GUIText::Update(void)
112{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200113 if (!isConditionTrue())
114 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400115
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200116 static int updateCounter = 3;
Dees_Troy51a0e822012-09-05 15:24:24 -0400117
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200118 // This hack just makes sure we update at least once a minute for things like clock and battery
119 if (updateCounter) updateCounter--;
120 else
121 {
122 mVarChanged = 1;
123 updateCounter = 3;
124 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400125
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200126 if (mIsStatic || !mVarChanged)
127 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400128
thatb2e8f672015-03-05 20:25:39 +0100129 std::string newValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200130 if (mLastValue == newValue)
Dees_Troya13d74f2013-03-24 08:54:55 -0500131 return 0;
132 else
133 mLastValue = newValue;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200134 return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400135}
136
137int GUIText::GetCurrentBounds(int& w, int& h)
138{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200139 void* fontResource = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400140
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 if (mFont)
142 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400143
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200144 h = mFontHeight;
thatb2e8f672015-03-05 20:25:39 +0100145 mLastValue = gui_parse_text(mText);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200146 w = gr_measureEx(mLastValue.c_str(), fontResource);
147 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400148}
149
Vojtech Bocek07220562014-02-08 02:05:33 +0100150int GUIText::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400151{
Vojtech Bocek07220562014-02-08 02:05:33 +0100152 GUIObject::NotifyVarChange(varName, value);
153
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 mVarChanged = 1;
155 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156}
157
158int GUIText::SetMaxWidth(unsigned width)
159{
160 maxWidth = width;
161 mVarChanged = 1;
162 return 0;
163}
164
165int GUIText::SkipCharCount(unsigned skip)
166{
167 charSkip = skip;
168 mVarChanged = 1;
169 return 0;
170}