Fix keymap.
[vuplus_xbmc] / xbmc / guilib / GUILabel.h
1 /*!
2 \file GUILabel.h
3 \brief
4 */
5
6 #pragma once
7
8 /*
9  *      Copyright (C) 2005-2013 Team XBMC
10  *      http://xbmc.org
11  *
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)
15  *  any later version.
16  *
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.
21  *
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/>.
25  *
26  */
27
28 #include "GUITextLayout.h"
29 #include "GUIInfoTypes.h"
30 #include "GUIFont.h"
31 #include "Geometry.h"
32
33 class CLabelInfo
34 {
35 public:
36   CLabelInfo()
37   {
38     font = NULL;
39     align = XBFONT_LEFT;
40     offsetX = offsetY = 0;
41     width = 0;
42     angle = 0;
43     scrollSpeed = CScrollInfo::defaultSpeed;
44     scrollSuffix = " | ";
45   };
46   bool UpdateColors()
47   {
48     bool changed = false;
49
50     changed |= textColor.Update();
51     changed |= shadowColor.Update();
52     changed |= selectedColor.Update();
53     changed |= disabledColor.Update();
54     changed |= focusedColor.Update();
55     changed |= invalidColor.Update();
56
57     return changed;
58   };
59   
60   CGUIInfoColor textColor;
61   CGUIInfoColor shadowColor;
62   CGUIInfoColor selectedColor;
63   CGUIInfoColor disabledColor;
64   CGUIInfoColor focusedColor;
65   CGUIInfoColor invalidColor;
66   uint32_t align;
67   float offsetX;
68   float offsetY;
69   float width;
70   float angle;
71   CGUIFont *font;
72   int scrollSpeed; 
73   CStdString scrollSuffix;
74 };
75
76 /*!
77  \ingroup controls, labels
78  \brief Class for rendering text labels.  Handles alignment and rendering of text within a control.
79  */
80 class CGUILabel
81 {
82 public:
83   /*! \brief allowed color categories for labels, as defined by the skin
84    */
85   enum COLOR { COLOR_TEXT = 0,
86                COLOR_SELECTED,
87                COLOR_FOCUSED,
88                COLOR_DISABLED,
89                COLOR_INVALID };
90   
91   /*! \brief allowed overflow handling techniques for labels, as defined by the skin
92    */
93   enum OVER_FLOW { OVER_FLOW_TRUNCATE = 0,
94                    OVER_FLOW_SCROLL,
95                    OVER_FLOW_WRAP,
96                    OVER_FLOW_CLIP };
97
98   CGUILabel(float posX, float posY, float width, float height, const CLabelInfo& labelInfo, OVER_FLOW overflow = OVER_FLOW_TRUNCATE);
99   virtual ~CGUILabel(void);
100
101   /*! \brief Process the label
102    \return bool stating if process caused control to change
103    */
104   bool Process(unsigned int currentTime);
105
106   /*! \brief Render the label on screen
107    */
108   void Render();
109   
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.
113    */
114   bool SetMaxRect(float x, float y, float w, float h);
115
116   bool SetAlign(uint32_t align);
117   
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
122    */
123   bool SetText(const CStdString &label);
124
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
129    */
130   bool SetTextW(const CStdStringW &label);
131
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
137    */
138   bool SetStyledText(const vecText &text, const vecColors &colors);
139
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
143    */
144   bool SetColor(COLOR color);
145
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
150    */
151   void SetRenderRect(const CRect &rect) { m_renderRect = rect; };
152   
153   /*! \brief Set whether or not this label control should scroll
154    \param scrolling true if this label should scroll.
155    */
156   bool SetScrolling(bool scrolling);
157
158   /*! \brief Set how this label should handle overflowing text.
159    \param overflow the overflow type
160    \sa OVER_FLOW
161    */
162   bool SetOverflow(OVER_FLOW overflow);
163
164   /*! \brief Set this label invalid.  Forces an update of the control
165    */
166   void SetInvalid();
167   
168   /*! \brief Update this labels colors
169    */
170   bool UpdateColors();
171   
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
175    */
176   const CRect &GetRenderRect() const { return m_renderRect; };
177   
178   /*! \brief Returns the precalculated full width of the current text, regardless of layout
179    \return full width of the current text
180    \sa CalcTextWidth
181    */
182   float GetTextWidth() const { return m_textLayout.GetTextWidth(); };
183   
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.
187    \sa SetMaxRect
188    */
189   float GetMaxWidth() const;
190   
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
194    \sa GetTextWidth
195    */
196   float CalcTextWidth(const CStdStringW &text) const { return m_textLayout.GetTextWidth(text); };
197
198   const CLabelInfo& GetLabelInfo() const { return m_label; };
199   CLabelInfo &GetLabelInfo() { return m_label; };
200
201   /*! \brief Check a left aligned and right aligned label for overlap and cut the labels off so that no overlap occurs
202    
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:
212    
213    cut
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.
217    
218    \param label1 First label to check
219    \param label2 Second label to check
220    */
221   static bool CheckAndCorrectOverlap(CGUILabel &label1, CGUILabel &label2);
222   
223 protected:
224   color_t GetColor() const;
225   
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
230    */
231   void UpdateRenderRect();
232
233 private:
234   CLabelInfo     m_label;
235   CGUITextLayout m_textLayout;
236
237   bool           m_scrolling;
238   OVER_FLOW      m_overflowType;
239   bool           m_selected;
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
245 };