[gui] fix <hinttext> not being shown once unfocused/refocused
[vuplus_xbmc] / xbmc / guilib / GUITextLayout.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://xbmc.org
6  *
7  *  This Program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  This Program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with XBMC; see the file COPYING.  If not, see
19  *  <http://www.gnu.org/licenses/>.
20  *
21  */
22
23 #include "utils/StdString.h"
24
25 #include <vector>
26
27 #ifdef __GNUC__
28 // under gcc, inline will only take place if optimizations are applied (-O). this will force inline even without optimizations.
29 #define XBMC_FORCE_INLINE __attribute__((always_inline))
30 #else
31 #define XBMC_FORCE_INLINE
32 #endif
33
34 class CGUIFont;
35 class CScrollInfo;
36
37 // Process will be:
38
39 // 1.  String is divided up into a "multiinfo" vector via the infomanager.
40 // 2.  The multiinfo vector is then parsed by the infomanager at rendertime and the resultant string is constructed.
41 // 3.  This is saved for comparison perhaps.  If the same, we are done.  If not, go to 4.
42 // 4.  The string is then parsed into a vector<CGUIString>.
43 // 5.  Each item in the vector is length-calculated, and then layout occurs governed by alignment and wrapping rules.
44 // 6.  A new vector<CGUIString> is constructed
45
46 typedef uint32_t character_t;
47 typedef uint32_t color_t;
48 typedef std::vector<character_t> vecText;
49 typedef std::vector<color_t> vecColors;
50
51 class CGUIString
52 {
53 public:
54   typedef vecText::const_iterator iString;
55
56   CGUIString(iString start, iString end, bool carriageReturn);
57
58   CStdString GetAsString() const;
59
60   vecText m_text;
61   bool m_carriageReturn; // true if we have a carriage return here
62 };
63
64 class CGUITextLayout
65 {
66 public:
67   CGUITextLayout(CGUIFont *font, bool wrap, float fHeight=0.0f, CGUIFont *borderFont = NULL);  // this may need changing - we may just use this class to replace CLabelInfo completely
68
69   bool UpdateScrollinfo(CScrollInfo &scrollInfo);
70
71   // main function to render strings
72   void Render(float x, float y, float angle, color_t color, color_t shadowColor, uint32_t alignment, float maxWidth, bool solid = false);
73   void RenderScrolling(float x, float y, float angle, color_t color, color_t shadowColor, uint32_t alignment, float maxWidth, const CScrollInfo &scrollInfo);
74   void RenderOutline(float x, float y, color_t color, color_t outlineColor, uint32_t alignment, float maxWidth);
75
76   /*! \brief Returns the precalculated width and height of the text to be rendered (in constant time).
77    \param width [out] width of text
78    \param height [out] height of text
79    \sa GetTextWidth, CalcTextExtent
80    */
81   void GetTextExtent(float &width, float &height) const;
82   
83   /*! \brief Returns the precalculated width of the text to be rendered (in constant time).
84    \return width of text
85    \sa GetTextExtent, CalcTextExtent
86    */
87   float GetTextWidth() const { return m_textWidth; };
88   
89   float GetTextWidth(const CStdStringW &text) const;
90   bool Update(const CStdString &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
91   bool UpdateW(const CStdStringW &text, float maxWidth = 0, bool forceUpdate = false, bool forceLTRReadingOrder = false);
92
93   /*! \brief Update text from a pre-styled vecText/vecColors combination
94    Allows styled text to be passed directly to the text layout.
95    \param text the styled text to set.
96    \param colors the colors used on the text.
97    \param maxWidth the maximum width for wrapping text, defaults to 0 (no max width).
98    \param forceLTRReadingOrder whether to force left to right reading order, defaults to false.
99    */
100   void UpdateStyled(const vecText &text, const vecColors &colors, float maxWidth = 0, bool forceLTRReadingOrder = false);
101
102   unsigned int GetTextLength() const;
103   void GetFirstText(vecText &text) const;
104   void Reset();
105
106   void SetWrap(bool bWrap=true);
107   void SetMaxHeight(float fHeight);
108
109
110   static void DrawText(CGUIFont *font, float x, float y, color_t color, color_t shadowColor, const CStdString &text, uint32_t align);
111   static void Filter(CStdString &text);
112
113 protected:
114   void LineBreakText(const vecText &text, std::vector<CGUIString> &lines);
115   void WrapText(const vecText &text, float maxWidth);
116   static void BidiTransform(std::vector<CGUIString> &lines, bool forceLTRReadingOrder);
117   static CStdStringW BidiFlip(const CStdStringW &text, bool forceLTRReadingOrder);
118   void CalcTextExtent();
119   void UpdateCommon(const CStdStringW &text, float maxWidth, bool forceLTRReadingOrder);
120
121   // our text to render
122   vecColors m_colors;
123   std::vector<CGUIString> m_lines;
124   typedef std::vector<CGUIString>::iterator iLine;
125
126   // the layout and font details
127   CGUIFont *m_font;        // has style, colour info
128   CGUIFont *m_borderFont;  // only used for outlined text
129
130   bool  m_wrap;            // wrapping (true if justify is enabled!)
131   float m_maxHeight;
132   // the default color (may differ from the font objects defaults)
133   color_t m_textColor;
134
135   std::string m_lastUtf8Text;
136   CStdStringW m_lastText;
137   bool        m_lastUpdateW; ///< true if the last string we updated was the wstring version
138   float m_textWidth;
139   float m_textHeight;
140 private:
141   inline bool IsSpace(character_t letter) const XBMC_FORCE_INLINE
142   {
143     return (letter & 0xffff) == L' ';
144   };
145   inline bool CanWrapAtLetter(character_t letter) const XBMC_FORCE_INLINE
146   {
147     character_t ch = letter & 0xffff;
148     return ch == L' ' || (ch >=0x4e00 && ch <= 0x9fff);
149   };
150   static void AppendToUTF32(const CStdString &utf8, character_t colStyle, vecText &utf32);
151   static void AppendToUTF32(const CStdStringW &utf16, character_t colStyle, vecText &utf32);
152   static void ParseText(const CStdStringW &text, uint32_t defaultStyle, color_t defaultColor, vecColors &colors, vecText &parsedText);
153
154   static void utf8ToW(const CStdString &utf8, CStdStringW &utf16);
155 };
156