[guilib] fix labelcontrols with auto width always being marked as dirty if they speci...
[vuplus_xbmc] / xbmc / guilib / GUIControl.h
1 /*!
2 \file GUIControl.h
3 \brief
4 */
5
6 #ifndef GUILIB_GUICONTROL_H
7 #define GUILIB_GUICONTROL_H
8 #pragma once
9
10 /*
11  *      Copyright (C) 2005-2013 Team XBMC
12  *      http://xbmc.org
13  *
14  *  This Program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2, or (at your option)
17  *  any later version.
18  *
19  *  This Program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  *  GNU General Public License for more details.
23  *
24  *  You should have received a copy of the GNU General Public License
25  *  along with XBMC; see the file COPYING.  If not, see
26  *  <http://www.gnu.org/licenses/>.
27  *
28  */
29
30 #include "GraphicContext.h" // needed by any rendering operation (all controls)
31 #include "GUIMessage.h"     // needed by practically all controls
32 #include "VisibleEffect.h"  // needed for the CAnimation members
33 #include "GUIInfoTypes.h"   // needed for CGUIInfoColor to handle infolabel'ed colors
34 #include "DirtyRegion.h"
35 #include "GUIAction.h"
36
37 class CGUIListItem; // forward
38 class CAction;
39 class CMouseEvent;
40
41 enum ORIENTATION { HORIZONTAL = 0, VERTICAL };
42
43 class CControlState
44 {
45 public:
46   CControlState(int id, int data)
47   {
48     m_id = id;
49     m_data = data;
50   }
51   int m_id;
52   int m_data;
53 };
54
55 /*!
56  \brief Results of OnMouseEvent()
57  Any value not equal to EVENT_RESULT_UNHANDLED indicates that the event was handled.
58  */
59 enum EVENT_RESULT { EVENT_RESULT_UNHANDLED                      = 0x00,
60                     EVENT_RESULT_HANDLED                        = 0x01,
61                     EVENT_RESULT_PAN_HORIZONTAL                 = 0x02,
62                     EVENT_RESULT_PAN_VERTICAL                   = 0x04,
63                     EVENT_RESULT_PAN_VERTICAL_WITHOUT_INERTIA   = 0x08,
64                     EVENT_RESULT_PAN_HORIZONTAL_WITHOUT_INERTIA = 0x10,
65                     EVENT_RESULT_ROTATE                         = 0x20,
66                     EVENT_RESULT_ZOOM                           = 0x40,
67                     EVENT_RESULT_SWIPE                          = 0x80
68 };
69
70 /*!
71  \ingroup controls
72  \brief Base class for controls
73  */
74 class CGUIControl
75 {
76 public:
77   CGUIControl();
78   CGUIControl(int parentID, int controlID, float posX, float posY, float width, float height);
79   virtual ~CGUIControl(void);
80   virtual CGUIControl *Clone() const=0;
81
82   virtual void DoProcess(unsigned int currentTime, CDirtyRegionList &dirtyregions);
83   virtual void Process(unsigned int currentTime, CDirtyRegionList &dirtyregions);
84   virtual void DoRender();
85   virtual void Render() {};
86
87   /*! \brief Returns whether or not we have processed */
88   bool HasProcessed() const { return m_hasProcessed; };
89
90   // OnAction() is called by our window when we are the focused control.
91   // We should process any control-specific actions in the derived classes,
92   // and return true if we have taken care of the action.  Returning false
93   // indicates that the message may be handed down to the window or application
94   // levels.  This base class implementation handles basic movement, and should
95   // be called from the derived classes when the action has not been handled.
96   // Return true to indicate that the action has been dealt with.
97   virtual bool OnAction(const CAction &action);
98
99   // Common actions to make the code easier to read (no ugly switch statements in derived controls)
100   virtual void OnUp();
101   virtual void OnDown();
102   virtual void OnLeft();
103   virtual void OnRight();
104   virtual bool OnBack();
105   virtual void OnNextControl();
106   virtual void OnPrevControl();
107   virtual void OnFocus() {};
108   virtual void OnUnFocus() {};
109
110   /*! \brief React to a mouse event
111
112    Mouse events are sent from the window to all controls, and each control can react based on the event
113    and location of the event.
114
115    \param point the location in transformed skin coordinates from the upper left corner of the parent control.
116    \param event the mouse event to perform
117    \return EVENT_RESULT corresponding to whether the control handles this event
118    \sa HitTest, CanFocusFromPoint, CMouseEvent, EVENT_RESULT
119    */
120   virtual EVENT_RESULT SendMouseEvent(const CPoint &point, const CMouseEvent &event);
121
122   /*! \brief Perform a mouse action
123
124    Mouse actions are sent from the window to all controls, and each control can react based on the event
125    and location of the actions.
126
127    \param point the location in transformed skin coordinates from the upper left corner of the parent control.
128    \param event the mouse event to perform
129    \return EVENT_RESULT corresponding to whether the control handles this event
130    \sa SendMouseEvent, HitTest, CanFocusFromPoint, CMouseEvent
131    */
132   virtual EVENT_RESULT OnMouseEvent(const CPoint &point, const CMouseEvent &event) { return EVENT_RESULT_UNHANDLED; };
133
134   /*! \brief Unfocus the control if the given point on screen is not within it's boundary
135    \param point the location in transformed skin coordinates from the upper left corner of the parent control.
136    \sa CanFocusFromPoint
137    */
138   virtual void UnfocusFromPoint(const CPoint &point);
139
140   /*! \brief Used to test whether the point is inside a control.
141    \param point location to test
142    \return true if the point is inside the bounds of this control.
143    \sa SetHitRect
144    */
145   virtual bool HitTest(const CPoint &point) const;
146
147   virtual bool OnMessage(CGUIMessage& message);
148   virtual int GetID(void) const;
149   virtual void SetID(int id) { m_controlID = id; };
150   virtual bool HasID(int id) const;
151   virtual bool HasVisibleID(int id) const;
152   int GetParentID() const;
153   virtual bool HasFocus() const;
154   virtual void AllocResources();
155   virtual void FreeResources(bool immediately = false);
156   virtual void DynamicResourceAlloc(bool bOnOff);
157   virtual bool IsDynamicallyAllocated() { return false; };
158   virtual bool CanFocus() const;
159   virtual bool IsVisible() const;
160   bool IsVisibleFromSkin() const { return m_visibleFromSkinCondition; };
161   virtual bool IsDisabled() const;
162   virtual void SetPosition(float posX, float posY);
163   virtual void SetHitRect(const CRect &rect);
164   virtual void SetCamera(const CPoint &camera);
165   bool SetColorDiffuse(const CGUIInfoColor &color);
166   CPoint GetRenderPosition() const;
167   virtual float GetXPosition() const;
168   virtual float GetYPosition() const;
169   virtual float GetWidth() const;
170   virtual float GetHeight() const;
171
172   void MarkDirtyRegion();
173
174   /*! \brief return the render region in screen coordinates of this control
175    */
176   const CRect &GetRenderRegion() const { return m_renderRegion; };
177   /*! \brief calculate the render region in parentcontrol coordinates of this control
178    Called during process to update m_renderRegion
179    */
180   virtual CRect CalcRenderRegion() const;
181
182   virtual void SetNavigation(int up, int down, int left, int right, int back = 0);
183   virtual void SetTabNavigation(int next, int prev);
184
185   /*! \brief Set actions to perform on navigation
186    Navigations are set if replace is true or if there is no previously set action
187    \param up CGUIAction to execute on up
188    \param down CGUIAction to execute on down
189    \param left CGUIAction to execute on left
190    \param right CGUIAction to execute on right
191    \param back CGUIAction to execute on back
192    \param replace Actions are set only if replace is true or there is no previously set action.  Defaults to true
193    \sa SetNavigation
194    */
195   virtual void SetNavigationActions(const CGUIAction &up, const CGUIAction &down,
196                                     const CGUIAction &left, const CGUIAction &right,
197                                     const CGUIAction &back, bool replace = true);
198   void SetNavigationAction(int direction, const CGUIAction &action, bool replace = true);
199   int GetControlIdUp() const { return m_actionUp.GetNavigation(); };
200   int GetControlIdDown() const { return  m_actionDown.GetNavigation(); };
201   int GetControlIdLeft() const { return m_actionLeft.GetNavigation(); };
202   int GetControlIdRight() const { return m_actionRight.GetNavigation(); };
203   int GetControlIdBack() const { return m_actionBack.GetNavigation(); };
204   bool GetNavigationAction(int direction, CGUIAction& action) const;
205   /*! \brief  Start navigating in given direction.
206    */
207   bool Navigate(int direction) const;
208   virtual void SetFocus(bool focus);
209   virtual void SetWidth(float width);
210   virtual void SetHeight(float height);
211   virtual void SetVisible(bool bVisible, bool setVisState = false);
212   void SetVisibleCondition(const CStdString &expression, const CStdString &allowHiddenFocus = "");
213   bool HasVisibleCondition() const { return m_visibleCondition; };
214   void SetEnableCondition(const CStdString &expression);
215   virtual void UpdateVisibility(const CGUIListItem *item = NULL);
216   virtual void SetInitialVisibility();
217   virtual void SetEnabled(bool bEnable);
218   virtual void SetInvalid() { m_bInvalidated = true; };
219   virtual void SetPulseOnSelect(bool pulse) { m_pulseOnSelect = pulse; };
220   virtual CStdString GetDescription() const { return ""; };
221
222   void SetAnimations(const std::vector<CAnimation> &animations);
223   const std::vector<CAnimation> &GetAnimations() const { return m_animations; };
224
225   virtual void QueueAnimation(ANIMATION_TYPE anim);
226   virtual bool IsAnimating(ANIMATION_TYPE anim);
227   virtual bool HasAnimation(ANIMATION_TYPE anim);
228   CAnimation *GetAnimation(ANIMATION_TYPE type, bool checkConditions = true);
229   virtual void ResetAnimation(ANIMATION_TYPE type);
230   virtual void ResetAnimations();
231
232   // push information updates
233   virtual void UpdateInfo(const CGUIListItem *item = NULL) {};
234   virtual void SetPushUpdates(bool pushUpdates) { m_pushedUpdates = pushUpdates; };
235
236   virtual bool IsGroup() const { return false; };
237   virtual bool IsContainer() const { return false; };
238   virtual bool GetCondition(int condition, int data) const { return false; };
239
240   void SetParentControl(CGUIControl *control) { m_parentControl = control; };
241   CGUIControl *GetParentControl(void) const { return m_parentControl; };
242   virtual void SaveStates(std::vector<CControlState> &states);
243
244   enum GUICONTROLTYPES {
245     GUICONTROL_UNKNOWN,
246     GUICONTROL_BUTTON,
247     GUICONTROL_CHECKMARK,
248     GUICONTROL_FADELABEL,
249     GUICONTROL_IMAGE,
250     GUICONTROL_BORDEREDIMAGE,
251     GUICONTROL_LARGE_IMAGE,
252     GUICONTROL_LABEL,
253     GUICONTROL_LISTGROUP,
254     GUICONTROL_PROGRESS,
255     GUICONTROL_RADIO,
256     GUICONTROL_RSS,
257     GUICONTROL_SELECTBUTTON,
258     GUICONTROL_SLIDER,
259     GUICONTROL_SETTINGS_SLIDER,
260     GUICONTROL_SPIN,
261     GUICONTROL_SPINEX,
262     GUICONTROL_TEXTBOX,
263     GUICONTROL_TOGGLEBUTTON,
264     GUICONTROL_VIDEO,
265     GUICONTROL_MOVER,
266     GUICONTROL_RESIZE,
267     GUICONTROL_EDIT,
268     GUICONTROL_VISUALISATION,
269     GUICONTROL_RENDERADDON,
270     GUICONTROL_MULTI_IMAGE,
271     GUICONTROL_GROUP,
272     GUICONTROL_GROUPLIST,
273     GUICONTROL_SCROLLBAR,
274     GUICONTROL_LISTLABEL,
275     GUICONTROL_MULTISELECT,
276     GUICONTAINER_LIST,
277     GUICONTAINER_WRAPLIST,
278     GUICONTAINER_FIXEDLIST,
279     GUICONTAINER_EPGGRID,
280     GUICONTAINER_PANEL
281   };
282   GUICONTROLTYPES GetControlType() const { return ControlType; }
283
284   enum GUIVISIBLE { HIDDEN = 0, DELAYED, VISIBLE };
285
286 #ifdef _DEBUG
287   virtual void DumpTextureUse() {};
288 #endif
289 protected:
290   /*!
291    \brief Return the coordinates of the top left of the control, in the control's parent coordinates
292    \return The top left coordinates of the control
293    */
294   virtual CPoint GetPosition() const { return CPoint(GetXPosition(), GetYPosition()); };
295
296   /*! \brief Called when the mouse is over the control.
297    Default implementation selects the control.
298    \param point location of the mouse in transformed skin coordinates
299    \return true if handled, false otherwise.
300    */
301   virtual bool OnMouseOver(const CPoint &point);
302
303   /*! \brief Test whether we can focus a control from a point on screen
304    \param point the location in vanilla skin coordinates from the upper left corner of the parent control.
305    \return true if the control can be focused from this location
306    \sa UnfocusFromPoint, HitRect
307    */
308   virtual bool CanFocusFromPoint(const CPoint &point) const;
309
310   virtual bool UpdateColors();
311   virtual bool Animate(unsigned int currentTime);
312   virtual bool CheckAnimation(ANIMATION_TYPE animType);
313   void UpdateStates(ANIMATION_TYPE type, ANIMATION_PROCESS currentProcess, ANIMATION_STATE currentState);
314   bool SendWindowMessage(CGUIMessage &message) const;
315
316   // navigation and actions
317   CGUIAction m_actionLeft;
318   CGUIAction m_actionRight;
319   CGUIAction m_actionUp;
320   CGUIAction m_actionDown;
321   CGUIAction m_actionBack;
322   CGUIAction m_actionNext;
323   CGUIAction m_actionPrev;
324
325   float m_posX;
326   float m_posY;
327   float m_height;
328   float m_width;
329   CRect m_hitRect;
330   CGUIInfoColor m_diffuseColor;
331   int m_controlID;
332   int m_parentID;
333   bool m_bHasFocus;
334   bool m_bInvalidated;
335   bool m_bAllocated;
336   bool m_pulseOnSelect;
337   GUICONTROLTYPES ControlType;
338
339   CGUIControl *m_parentControl;   // our parent control if we're part of a group
340
341   // visibility condition/state
342   INFO::InfoPtr m_visibleCondition;
343   GUIVISIBLE m_visible;
344   bool m_visibleFromSkinCondition;
345   bool m_forceHidden;       // set from the code when a hidden operation is given - overrides m_visible
346   CGUIInfoBool m_allowHiddenFocus;
347   bool m_hasProcessed;
348   // enable/disable state
349   INFO::InfoPtr m_enableCondition;
350   bool m_enabled;
351
352   bool m_pushedUpdates;
353
354   // animation effects
355   std::vector<CAnimation> m_animations;
356   CPoint m_camera;
357   bool m_hasCamera;
358   TransformMatrix m_transform;
359   TransformMatrix m_cachedTransform; // Contains the absolute transform the control
360
361   bool  m_controlIsDirty;
362   CRect m_renderRegion;         // In screen coordinates
363 };
364
365 #endif