]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_button.cpp
skins2: improve deallocation of ressources for layouts and controls
[vlc] / modules / gui / skins2 / controls / ctrl_button.cpp
1 /*****************************************************************************
2  * ctrl_button.cpp
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
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "ctrl_button.hpp"
26 #include "../events/evt_generic.hpp"
27 #include "../src/generic_bitmap.hpp"
28 #include "../src/generic_layout.hpp"
29 #include "../src/os_factory.hpp"
30 #include "../src/os_graphics.hpp"
31 #include "../commands/cmd_generic.hpp"
32
33
34 CtrlButton::CtrlButton( intf_thread_t *pIntf, const GenericBitmap &rBmpUp,
35                         const GenericBitmap &rBmpOver,
36                         const GenericBitmap &rBmpDown, CmdGeneric &rCommand,
37                         const UString &rTooltip, const UString &rHelp,
38                         VarBool *pVisible ):
39     CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
40     m_rCommand( rCommand ), m_tooltip( rTooltip ),
41     m_imgUp( pIntf, rBmpUp ), m_imgOver( pIntf, rBmpOver ),
42     m_imgDown( pIntf, rBmpDown ), m_pImg( NULL ), m_cmdUpOverDownOver( this ),
43     m_cmdDownOverUpOver( this ), m_cmdDownOverDown( this ),
44     m_cmdDownDownOver( this ), m_cmdUpOverUp( this ), m_cmdUpUpOver( this ),
45     m_cmdDownUp( this ), m_cmdUpHidden( this ), m_cmdHiddenUp( this )
46 {
47     // States
48     m_fsm.addState( "up" );
49     m_fsm.addState( "down" );
50     m_fsm.addState( "upOver" );
51     m_fsm.addState( "downOver" );
52     m_fsm.addState( "hidden" );
53
54     // Transitions
55     m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
56                          &m_cmdUpOverDownOver );
57     m_fsm.addTransition( "upOver", "mouse:left:dblclick", "downOver",
58                          &m_cmdUpOverDownOver );
59     m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
60                          &m_cmdDownOverUpOver );
61     m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
62     m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
63     m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
64     m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
65     m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
66     // XXX: It would be easy to use a "ANY" initial state to handle these
67     // four lines in only one. But till now it isn't worthwhile...
68     m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
69     m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
70     m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
71     m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
72     m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );
73
74     // Initial state
75     m_fsm.setState( "up" );
76     setImage( &m_imgUp );
77 }
78
79
80 CtrlButton::~CtrlButton()
81 {
82 }
83
84 void CtrlButton::setLayout( GenericLayout *pLayout,
85                            const Position &rPosition )
86 {
87     CtrlGeneric::setLayout( pLayout, rPosition );
88     m_pLayout->getActiveVar().addObserver( this );
89 }
90
91
92 void CtrlButton::unsetLayout()
93 {
94     m_pLayout->getActiveVar().delObserver( this );
95     CtrlGeneric::unsetLayout();
96 }
97
98 void CtrlButton::handleEvent( EvtGeneric &rEvent )
99 {
100     m_fsm.handleTransition( rEvent.getAsString() );
101 }
102
103
104 bool CtrlButton::mouseOver( int x, int y ) const
105 {
106     if( m_pImg )
107     {
108         return m_pImg->hit( x, y );
109     }
110     else
111     {
112         return false;
113     }
114 }
115
116
117 void CtrlButton::draw( OSGraphics &rImage, int xDest, int yDest )
118 {
119     if( m_pImg )
120     {
121         // Draw the current image
122         m_pImg->draw( rImage, xDest, yDest );
123     }
124 }
125
126
127 void CtrlButton::setImage( AnimBitmap *pImg )
128 {
129     AnimBitmap *pOldImg = m_pImg;
130     m_pImg = pImg;
131
132     if( pOldImg )
133     {
134         pOldImg->stopAnim();
135         pOldImg->delObserver( this );
136     }
137
138     if( pImg )
139     {
140         pImg->startAnim();
141         pImg->addObserver( this );
142     }
143
144     notifyLayoutMaxSize( pOldImg, pImg );
145 }
146
147
148 void CtrlButton::onUpdate( Subject<AnimBitmap> &rBitmap, void *arg )
149 {
150     notifyLayout();
151 }
152
153
154 void CtrlButton::CmdUpOverDownOver::execute()
155 {
156     m_pParent->captureMouse();
157     m_pParent->setImage( &m_pParent->m_imgDown );
158 }
159
160
161 void CtrlButton::CmdDownOverUpOver::execute()
162 {
163     m_pParent->releaseMouse();
164     m_pParent->setImage( &m_pParent->m_imgUp );
165     // Execute the command associated to this button
166     m_pParent->m_rCommand.execute();
167 }
168
169
170 void CtrlButton::CmdDownOverDown::execute()
171 {
172     m_pParent->setImage( &m_pParent->m_imgUp );
173 }
174
175
176 void CtrlButton::CmdDownDownOver::execute()
177 {
178     m_pParent->setImage( &m_pParent->m_imgDown );
179 }
180
181
182 void CtrlButton::CmdUpUpOver::execute()
183 {
184     m_pParent->setImage( &m_pParent->m_imgOver );
185 }
186
187
188 void CtrlButton::CmdUpOverUp::execute()
189 {
190     m_pParent->setImage( &m_pParent->m_imgUp );
191 }
192
193
194 void CtrlButton::CmdDownUp::execute()
195 {
196     m_pParent->releaseMouse();
197 }
198
199
200 void CtrlButton::CmdUpHidden::execute()
201 {
202     m_pParent->setImage( NULL );
203 }
204
205
206 void CtrlButton::CmdHiddenUp::execute()
207 {
208     m_pParent->setImage( &m_pParent->m_imgUp );
209 }
210
211 void CtrlButton::onUpdate( Subject<VarBool> &rVariable, void *arg  )
212 {
213     // restart animation
214     if(     &rVariable == m_pVisible
215         ||  &rVariable == &m_pLayout->getActiveVar()
216       )
217     {
218         if( m_pImg )
219         {
220             m_pImg->stopAnim();
221             m_pImg->startAnim();
222         }
223     }
224     CtrlGeneric::onUpdate( rVariable, arg );
225 }
226