Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // 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 | |
| 20 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 21 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | #include "rapidxml.hpp" |
| 26 | #include "objects.hpp" |
| 27 | |
| 28 | GUIText::GUIText(xml_node<>* node) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 29 | : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 30 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 31 | mFont = NULL; |
| 32 | mIsStatic = 1; |
| 33 | mVarChanged = 0; |
| 34 | mFontHeight = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 35 | maxWidth = 0; |
| 36 | charSkip = 0; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 37 | scaleWidth = true; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 38 | isHighlighted = false; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 39 | mText = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 41 | if (!node) |
| 42 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 44 | // Load colors |
| 45 | mColor = LoadAttrColor(node, "color", COLOR(0,0,0,255)); |
| 46 | mHighlightColor = LoadAttrColor(node, "highlightcolor", mColor); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 48 | // Load the font, and possibly override the color |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 49 | mFont = LoadAttrFont(FindNode(node, "font"), "resource"); |
| 50 | mColor = LoadAttrColor(FindNode(node, "font"), "color", mColor); |
| 51 | mHighlightColor = LoadAttrColor(FindNode(node, "font"), "highlightcolor", mColor); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 52 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 53 | // Load the placement |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 54 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 55 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 56 | xml_node<>* child = FindNode(node, "text"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 57 | if (child) mText = child->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 59 | 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 Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 76 | // Simple way to check for static state |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 77 | mLastValue = gui_parse_text(mText); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 78 | if (mLastValue != mText) mIsStatic = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 79 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 80 | mFontHeight = mFont->GetHeight(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | int GUIText::Render(void) |
| 84 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 85 | if (!isConditionTrue()) |
| 86 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 87 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 88 | void* fontResource = NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 89 | if (mFont) |
| 90 | fontResource = mFont->GetResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 91 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 92 | mLastValue = gui_parse_text(mText); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 93 | string displayValue = mLastValue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 94 | |
| 95 | if (charSkip) |
| 96 | displayValue.erase(0, charSkip); |
| 97 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 98 | mVarChanged = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 99 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 100 | int x = mRenderX, y = mRenderY; |
| 101 | int width = gr_measureEx(displayValue.c_str(), fontResource); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 102 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 103 | if (isHighlighted) |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 104 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
| 105 | else |
| 106 | gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 108 | gr_textEx_scaleW(mRenderX, mRenderY, displayValue.c_str(), fontResource, maxWidth, mPlacement, scaleWidth); |
| 109 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 110 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | int GUIText::Update(void) |
| 114 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 115 | if (!isConditionTrue()) |
| 116 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 117 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 118 | static int updateCounter = 3; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 120 | // 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 127 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 128 | if (mIsStatic || !mVarChanged) |
| 129 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 130 | |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 131 | std::string newValue = gui_parse_text(mText); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 132 | if (mLastValue == newValue) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 133 | return 0; |
| 134 | else |
| 135 | mLastValue = newValue; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 136 | return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int GUIText::GetCurrentBounds(int& w, int& h) |
| 140 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 141 | void* fontResource = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 142 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 143 | if (mFont) |
| 144 | fontResource = mFont->GetResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 146 | h = mFontHeight; |
that | b2e8f67 | 2015-03-05 20:25:39 +0100 | [diff] [blame] | 147 | mLastValue = gui_parse_text(mText); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 148 | w = gr_measureEx(mLastValue.c_str(), fontResource); |
| 149 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 152 | int GUIText::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 153 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 154 | GUIObject::NotifyVarChange(varName, value); |
| 155 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 156 | mVarChanged = 1; |
| 157 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | int GUIText::SetMaxWidth(unsigned width) |
| 161 | { |
| 162 | maxWidth = width; |
| 163 | mVarChanged = 1; |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | int GUIText::SkipCharCount(unsigned skip) |
| 168 | { |
| 169 | charSkip = skip; |
| 170 | mVarChanged = 1; |
| 171 | return 0; |
| 172 | } |