9 * Copyright (C) 2005-2013 Team XBMC
12 * This Program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This Program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with XBMC; see the file COPYING. If not, see
24 * <http://www.gnu.org/licenses/>.
28 #include "GUITextLayout.h"
29 #include "GUIInfoTypes.h"
40 offsetX = offsetY = 0;
43 scrollSpeed = CScrollInfo::defaultSpeed;
50 changed |= textColor.Update();
51 changed |= shadowColor.Update();
52 changed |= selectedColor.Update();
53 changed |= disabledColor.Update();
54 changed |= focusedColor.Update();
55 changed |= invalidColor.Update();
60 CGUIInfoColor textColor;
61 CGUIInfoColor shadowColor;
62 CGUIInfoColor selectedColor;
63 CGUIInfoColor disabledColor;
64 CGUIInfoColor focusedColor;
65 CGUIInfoColor invalidColor;
73 CStdString scrollSuffix;
77 \ingroup controls, labels
78 \brief Class for rendering text labels. Handles alignment and rendering of text within a control.
83 /*! \brief allowed color categories for labels, as defined by the skin
85 enum COLOR { COLOR_TEXT = 0,
91 /*! \brief allowed overflow handling techniques for labels, as defined by the skin
93 enum OVER_FLOW { OVER_FLOW_TRUNCATE = 0,
98 CGUILabel(float posX, float posY, float width, float height, const CLabelInfo& labelInfo, OVER_FLOW overflow = OVER_FLOW_TRUNCATE);
99 virtual ~CGUILabel(void);
101 /*! \brief Process the label
102 \return bool stating if process caused control to change
104 bool Process(unsigned int currentTime);
106 /*! \brief Render the label on screen
110 /*! \brief Set the maximal extent of the label
111 Sets the maximal size and positioning that the label may render in. Note that <textwidth> can override
112 this, and <textoffsetx> and <textoffsety> may also allow the label to be moved outside this rectangle.
114 bool SetMaxRect(float x, float y, float w, float h);
116 bool SetAlign(uint32_t align);
118 /*! \brief Set the text to be displayed in the label
119 Updates the label control and recomputes final position and size
120 \param text CStdString to set as this labels text
121 \sa SetTextW, SetStyledText
123 bool SetText(const CStdString &label);
125 /*! \brief Set the text to be displayed in the label
126 Updates the label control and recomputes final position and size
127 \param text CStdStringW to set as this labels text
128 \sa SetText, SetStyledText
130 bool SetTextW(const CStdStringW &label);
132 /*! \brief Set styled text to be displayed in the label
133 Updates the label control and recomputes final position and size
134 \param text styled text to set.
135 \param colors colors referenced in the styled text.
136 \sa SetText, SetTextW
138 bool SetStyledText(const vecText &text, const vecColors &colors);
140 /*! \brief Set the color to use for the label
141 Sets the color to be used for this label. Takes effect at the next render
142 \param color color to be used for the label
144 bool SetColor(COLOR color);
146 /*! \brief Set the final layout of the current text
147 Overrides the calculated layout of the current text, forcing a particular size and position
148 \param rect CRect containing the extents of the current text
149 \sa GetRenderRect, UpdateRenderRect
151 void SetRenderRect(const CRect &rect) { m_renderRect = rect; };
153 /*! \brief Set whether or not this label control should scroll
154 \param scrolling true if this label should scroll.
156 bool SetScrolling(bool scrolling);
158 /*! \brief Set how this label should handle overflowing text.
159 \param overflow the overflow type
162 bool SetOverflow(OVER_FLOW overflow);
164 /*! \brief Set this label invalid. Forces an update of the control
168 /*! \brief Update this labels colors
172 /*! \brief Returns the precalculated final layout of the current text
173 \return CRect containing the extents of the current text
174 \sa SetRenderRect, UpdateRenderRect
176 const CRect &GetRenderRect() const { return m_renderRect; };
178 /*! \brief Returns the precalculated full width of the current text, regardless of layout
179 \return full width of the current text
182 float GetTextWidth() const { return m_textLayout.GetTextWidth(); };
184 /*! \brief Returns the maximal width that this label can render into
185 \return Maximal width that this label can render into. Note that this may differ from the
186 amount given in SetMaxRect as offsets and text width overrides have been taken into account.
189 float GetMaxWidth() const;
191 /*! \brief Calculates the width of some text
192 \param text CStdStringW of text whose width we want
193 \return width of the given text
196 float CalcTextWidth(const CStdStringW &text) const { return m_textLayout.GetTextWidth(text); };
198 const CLabelInfo& GetLabelInfo() const { return m_label; };
199 CLabelInfo &GetLabelInfo() { return m_label; };
201 /*! \brief Check a left aligned and right aligned label for overlap and cut the labels off so that no overlap occurs
203 If a left-aligned label occupies some of the same space on screen as a right-aligned label, then we may be able to
204 correct for this by restricting the width of one or both of them. This routine checks two labels to see whether they
205 satisfy this assumption and, if so, adjusts the render rect of both labels so that they no longer do so. The order
206 of the two labels is not important, but we do assume that the left-aligned label is also the left-most on screen, and
207 that the right-aligned label is the right most on-screen, so that they overlap due to the fact that one or both of
208 the labels are longer than anticipated. In the following diagram, [R...[R R] refers to the maximal allowed and
209 actual space occupied by the right label. Similarly, [L L]...L] refers to the maximal and actual space occupied
210 by the left label. | refers to the central cutting point, i.e. the point that would divide the maximal allowed
211 overlap perfectly in two. There are 3 scenarios to consider:
214 [L [R...[R L].|..........L] R] left label ends to the left of the cut -> just crop the left label.
215 [L [R.....[R | L]..L] R] both left and right labels occupy more than the cut allows, so crop both.
216 [L [R..........|.[R L]..L] R] right label ends to the right of the cut -> just crop the right label.
218 \param label1 First label to check
219 \param label2 Second label to check
221 static bool CheckAndCorrectOverlap(CGUILabel &label1, CGUILabel &label2);
224 color_t GetColor() const;
226 /*! \brief Computes the final layout of the text
227 Uses the maximal position and width of the text, as well as the text length
228 and alignment to compute the final render rect of the text.
229 \sa GetRenderRect, SetRenderRect
231 void UpdateRenderRect();
235 CGUITextLayout m_textLayout;
238 OVER_FLOW m_overflowType;
240 CScrollInfo m_scrollInfo;
241 CRect m_renderRect; ///< actual sizing of text
242 CRect m_maxRect; ///< maximum sizing of text
243 bool m_invalid; ///< if true, the label needs recomputing
244 COLOR m_color; ///< color to render text \sa SetColor, GetColor