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