]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_slider.hpp
skins2: improve refresh of layouts
[vlc] / modules / gui / skins2 / controls / ctrl_slider.hpp
1 /*****************************************************************************
2  * ctrl_slider.hpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifndef CTRL_SLIDER_HPP
26 #define CTRL_SLIDER_HPP
27
28 #include "ctrl_generic.hpp"
29 #include "../utils/bezier.hpp"
30 #include "../utils/fsm.hpp"
31 #include "../utils/observer.hpp"
32
33
34 class GenericBitmap;
35 class ScaledBitmap;
36 class OSGraphics;
37 class VarPercent;
38
39
40 /// Cursor of a slider
41 class CtrlSliderCursor: public CtrlGeneric, public Observer<VarPercent>
42 {
43 public:
44     /// Create a cursor with 3 images (which are NOT copied, be careful)
45     /// If pVisible is NULL, the control is always visible
46     CtrlSliderCursor( intf_thread_t *pIntf, const GenericBitmap &rBmpUp,
47                       const GenericBitmap &rBmpOver,
48                       const GenericBitmap &rBmpDown,
49                       const Bezier &rCurve, VarPercent &rVariable,
50                       VarBool *pVisible, const UString &rTooltip,
51                       const UString &rHelp );
52
53     virtual ~CtrlSliderCursor();
54
55     /// Handle an event
56     virtual void handleEvent( EvtGeneric &rEvent );
57
58     /// Check whether coordinates are inside the control
59     virtual bool mouseOver( int x, int y ) const;
60
61     /// Draw the control on the given graphics
62     virtual void draw( OSGraphics &rImage, int xDest, int yDest, int w, int h );
63
64     /// Called when the position is set
65     virtual void onPositionChange();
66
67     /// Method called when the control is resized
68     virtual void onResize();
69
70     /// Method called to notify are to be updated
71     virtual void notifyLayout( int witdh = -1, int height = -1,
72                                int xOffSet = 0, int yOffSet = 0 );
73
74
75     /// Get the text of the tooltip
76     virtual UString getTooltipText() const { return m_tooltip; }
77
78     /// Get the type of control (custom RTTI)
79     virtual string getType() const { return "slider_cursor"; }
80
81 private:
82     /// Finite state machine of the control
83     FSM m_fsm;
84     /// Variable associated to the cursor
85     VarPercent &m_rVariable;
86     /// Tooltip text
87     const UString m_tooltip;
88     /// Initial size of the control
89     int m_width, m_height;
90     /// Position of the cursor
91     int m_xPosition, m_yPosition;
92     rect m_currentCursorRect;
93     /// Callback objects
94     DEFINE_CALLBACK( CtrlSliderCursor, OverDown )
95     DEFINE_CALLBACK( CtrlSliderCursor, DownOver )
96     DEFINE_CALLBACK( CtrlSliderCursor, OverUp )
97     DEFINE_CALLBACK( CtrlSliderCursor, UpOver )
98     DEFINE_CALLBACK( CtrlSliderCursor, Move )
99     DEFINE_CALLBACK( CtrlSliderCursor, Scroll )
100     /// Last saved position of the cursor (stored as a percentage)
101     float m_lastPercentage;
102     /// Offset between the mouse pointer and the center of the cursor
103     int m_xOffset, m_yOffset;
104     /// The last received event
105     EvtGeneric *m_pEvt;
106     /// Images of the cursor in the differents states
107     OSGraphics *m_pImgUp, *m_pImgOver, *m_pImgDown;
108     /// Current image
109     OSGraphics *m_pImg;
110     /// Bezier curve of the slider
111     const Bezier &m_rCurve;
112
113     /// Method called when the position variable is modified
114     virtual void onUpdate( Subject<VarPercent> &rVariable, void * );
115
116     /// Method to compute the resize factors
117     void getResizeFactors( float &rFactorX, float &rFactorY ) const;
118
119     /// Call notifyLayout
120     void refreshLayout( bool force = true );
121 };
122
123
124 /// Slider background
125 class CtrlSliderBg: public CtrlGeneric, public Observer<VarPercent>
126 {
127 public:
128     CtrlSliderBg( intf_thread_t *pIntf,
129                   const Bezier &rCurve, VarPercent &rVariable,
130                   int thickness, GenericBitmap *pBackground, int nbHoriz,
131                   int nbVert, int padHoriz, int padVert, VarBool *pVisible,
132                   const UString &rHelp );
133     virtual ~CtrlSliderBg();
134
135     /// Tell whether the mouse is over the control
136     virtual bool mouseOver( int x, int y ) const;
137
138     /// Draw the control on the given graphics
139     virtual void draw( OSGraphics &rImage, int xDest, int yDest, int w, int h );
140
141     /// Handle an event
142     virtual void handleEvent( EvtGeneric &rEvent );
143
144     /// Method called when the control is resized
145     virtual void onResize();
146
147     /// Get the type of control (custom RTTI)
148     virtual string getType() const { return "slider_bg"; }
149
150     /// Associate a cursor to this background
151     void associateCursor( CtrlSliderCursor &rCursor );
152
153 private:
154     /// Cursor of the slider
155     CtrlSliderCursor *m_pCursor;
156     /// Variable associated to the slider
157     VarPercent &m_rVariable;
158     /// Thickness of the curve
159     int m_thickness;
160     /// Bezier curve of the slider
161     const Bezier &m_rCurve;
162     /// Initial size of the control
163     int m_width, m_height;
164     /// Background image sequence (optional)
165     GenericBitmap *m_pImgSeq;
166     /// Scaled bitmap if needed
167     ScaledBitmap *m_pScaledBmp;
168     /// Number of images in the background bitmap
169     int m_nbHoriz, m_nbVert;
170     /// Number of pixels between two images
171     int m_padHoriz, m_padVert;
172     /// Size of a background image
173     int m_bgWidth, m_bgHeight;
174     /// Index of the current background image
175     int m_position;
176
177     /// Method called when the observed variable is modified
178     virtual void onUpdate( Subject<VarPercent> &rVariable, void* );
179
180     /// Method to compute the resize factors
181     void getResizeFactors( float &rFactorX, float &rFactorY ) const;
182 };
183
184
185 #endif