/xbmc/guilib/GUILabel.h

http://github.com/xbmc/xbmc · C Header · 238 lines · 95 code · 34 blank · 109 comment · 0 complexity · b554abad56a33c90cdd48704864dd048 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #pragma once
  9. /*!
  10. \file GUILabel.h
  11. \brief
  12. */
  13. #include "GUIFont.h"
  14. #include "GUITextLayout.h"
  15. #include "guiinfo/GUIInfoColor.h"
  16. #include "utils/Color.h"
  17. #include "utils/Geometry.h"
  18. class CLabelInfo
  19. {
  20. public:
  21. CLabelInfo():
  22. scrollSuffix(" | ")
  23. {
  24. font = NULL;
  25. align = XBFONT_LEFT;
  26. offsetX = offsetY = 0;
  27. width = 0;
  28. angle = 0;
  29. scrollSpeed = CScrollInfo::defaultSpeed;
  30. };
  31. bool UpdateColors()
  32. {
  33. bool changed = false;
  34. changed |= textColor.Update();
  35. changed |= shadowColor.Update();
  36. changed |= selectedColor.Update();
  37. changed |= disabledColor.Update();
  38. changed |= focusedColor.Update();
  39. changed |= invalidColor.Update();
  40. return changed;
  41. };
  42. KODI::GUILIB::GUIINFO::CGUIInfoColor textColor;
  43. KODI::GUILIB::GUIINFO::CGUIInfoColor shadowColor;
  44. KODI::GUILIB::GUIINFO::CGUIInfoColor selectedColor;
  45. KODI::GUILIB::GUIINFO::CGUIInfoColor disabledColor;
  46. KODI::GUILIB::GUIINFO::CGUIInfoColor focusedColor;
  47. KODI::GUILIB::GUIINFO::CGUIInfoColor invalidColor;
  48. uint32_t align;
  49. float offsetX;
  50. float offsetY;
  51. float width;
  52. float angle;
  53. CGUIFont *font;
  54. int scrollSpeed;
  55. std::string scrollSuffix;
  56. };
  57. /*!
  58. \ingroup controls, labels
  59. \brief Class for rendering text labels. Handles alignment and rendering of text within a control.
  60. */
  61. class CGUILabel
  62. {
  63. public:
  64. /*! \brief allowed color categories for labels, as defined by the skin
  65. */
  66. enum COLOR { COLOR_TEXT = 0,
  67. COLOR_SELECTED,
  68. COLOR_FOCUSED,
  69. COLOR_DISABLED,
  70. COLOR_INVALID };
  71. /*! \brief allowed overflow handling techniques for labels, as defined by the skin
  72. */
  73. enum OVER_FLOW { OVER_FLOW_TRUNCATE = 0,
  74. OVER_FLOW_SCROLL,
  75. OVER_FLOW_WRAP,
  76. OVER_FLOW_CLIP };
  77. CGUILabel(float posX, float posY, float width, float height, const CLabelInfo& labelInfo, OVER_FLOW overflow = OVER_FLOW_TRUNCATE);
  78. virtual ~CGUILabel(void);
  79. /*! \brief Process the label
  80. \return bool stating if process caused control to change
  81. */
  82. bool Process(unsigned int currentTime);
  83. /*! \brief Render the label on screen
  84. */
  85. void Render();
  86. /*! \brief Set the maximal extent of the label
  87. Sets the maximal size and positioning that the label may render in. Note that `textwidth>` can override
  88. this, and `<textoffsetx>` and `<textoffsety>` may also allow the label to be moved outside this rectangle.
  89. */
  90. bool SetMaxRect(float x, float y, float w, float h);
  91. bool SetAlign(uint32_t align);
  92. /*! \brief Set the text to be displayed in the label
  93. Updates the label control and recomputes final position and size
  94. \param text std::string to set as this labels text
  95. \sa SetTextW, SetStyledText
  96. */
  97. bool SetText(const std::string &label);
  98. /*! \brief Set the text to be displayed in the label
  99. Updates the label control and recomputes final position and size
  100. \param text std::wstring to set as this labels text
  101. \sa SetText, SetStyledText
  102. */
  103. bool SetTextW(const std::wstring &label);
  104. /*! \brief Set styled text to be displayed in the label
  105. Updates the label control and recomputes final position and size
  106. \param text styled text to set.
  107. \param colors colors referenced in the styled text.
  108. \sa SetText, SetTextW
  109. */
  110. bool SetStyledText(const vecText &text, const std::vector<UTILS::Color> &colors);
  111. /*! \brief Set the color to use for the label
  112. Sets the color to be used for this label. Takes effect at the next render
  113. \param color color to be used for the label
  114. */
  115. bool SetColor(COLOR color);
  116. /*! \brief Set the final layout of the current text
  117. Overrides the calculated layout of the current text, forcing a particular size and position
  118. \param rect CRect containing the extents of the current text
  119. \sa GetRenderRect, UpdateRenderRect
  120. */
  121. void SetRenderRect(const CRect &rect) { m_renderRect = rect; };
  122. /*! \brief Set whether or not this label control should scroll
  123. \param scrolling true if this label should scroll.
  124. */
  125. bool SetScrolling(bool scrolling);
  126. /*! \brief Set max. text scroll count
  127. */
  128. void SetScrollLoopCount(unsigned int loopCount) { m_maxScrollLoops = loopCount; };
  129. /*! \brief Set how this label should handle overflowing text.
  130. \param overflow the overflow type
  131. \sa OVER_FLOW
  132. */
  133. bool SetOverflow(OVER_FLOW overflow);
  134. /*! \brief Set this label invalid. Forces an update of the control
  135. */
  136. void SetInvalid();
  137. /*! \brief Update this labels colors
  138. */
  139. bool UpdateColors();
  140. /*! \brief Returns the precalculated final layout of the current text
  141. \return CRect containing the extents of the current text
  142. \sa SetRenderRect, UpdateRenderRect
  143. */
  144. const CRect &GetRenderRect() const { return m_renderRect; };
  145. /*! \brief Returns the precalculated full width of the current text, regardless of layout
  146. \return full width of the current text
  147. \sa CalcTextWidth
  148. */
  149. float GetTextWidth() const { return m_textLayout.GetTextWidth(); };
  150. /*! \brief Returns the maximal width that this label can render into
  151. \return Maximal width that this label can render into. Note that this may differ from the
  152. amount given in SetMaxRect as offsets and text width overrides have been taken into account.
  153. \sa SetMaxRect
  154. */
  155. float GetMaxWidth() const;
  156. /*! \brief Calculates the width of some text
  157. \param text std::wstring of text whose width we want
  158. \return width of the given text
  159. \sa GetTextWidth
  160. */
  161. float CalcTextWidth(const std::wstring &text) const { return m_textLayout.GetTextWidth(text); };
  162. const CLabelInfo& GetLabelInfo() const { return m_label; };
  163. CLabelInfo &GetLabelInfo() { return m_label; };
  164. /*! \brief Check a left aligned and right aligned label for overlap and cut the labels off so that no overlap occurs
  165. If a left-aligned label occupies some of the same space on screen as a right-aligned label, then we may be able to
  166. correct for this by restricting the width of one or both of them. This routine checks two labels to see whether they
  167. satisfy this assumption and, if so, adjusts the render rect of both labels so that they no longer do so. The order
  168. of the two labels is not important, but we do assume that the left-aligned label is also the left-most on screen, and
  169. that the right-aligned label is the right most on-screen, so that they overlap due to the fact that one or both of
  170. the labels are longer than anticipated. In the following diagram, [R...[R R] refers to the maximal allowed and
  171. actual space occupied by the right label. Similarly, [L L]...L] refers to the maximal and actual space occupied
  172. by the left label. | refers to the central cutting point, i.e. the point that would divide the maximal allowed
  173. overlap perfectly in two. There are 3 scenarios to consider:
  174. cut
  175. [L [R...[R L].|..........L] R] left label ends to the left of the cut -> just crop the left label.
  176. [L [R.....[R | L]..L] R] both left and right labels occupy more than the cut allows, so crop both.
  177. [L [R..........|.[R L]..L] R] right label ends to the right of the cut -> just crop the right label.
  178. \param label1 First label to check
  179. \param label2 Second label to check
  180. */
  181. static bool CheckAndCorrectOverlap(CGUILabel &label1, CGUILabel &label2);
  182. protected:
  183. UTILS::Color GetColor() const;
  184. /*! \brief Computes the final layout of the text
  185. Uses the maximal position and width of the text, as well as the text length
  186. and alignment to compute the final render rect of the text.
  187. \sa GetRenderRect, SetRenderRect
  188. */
  189. void UpdateRenderRect();
  190. private:
  191. CLabelInfo m_label;
  192. CGUITextLayout m_textLayout;
  193. bool m_scrolling;
  194. OVER_FLOW m_overflowType;
  195. CScrollInfo m_scrollInfo;
  196. CRect m_renderRect; ///< actual sizing of text
  197. CRect m_maxRect; ///< maximum sizing of text
  198. bool m_invalid; ///< if true, the label needs recomputing
  199. COLOR m_color; ///< color to render text \sa SetColor, GetColor
  200. unsigned int m_maxScrollLoops = ~0U;
  201. };