]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_button.cpp
0cfe884cbc35bbd483436602f021e10cbfdc0fd1
[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., 59 Temple Place - Suite 330, Boston, MA  02111, 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_cmdUpOverDownOver( this ), m_cmdDownOverUpOver( this ),
41     m_cmdDownOverDown( this ), m_cmdDownDownOver( this ),
42     m_cmdUpOverUp( this ), m_cmdUpUpOver( this ),
43     m_cmdDownUp( this ), m_cmdUpHidden( this ),
44     m_cmdHiddenUp( this )
45 {
46     // Build the images of the button
47     OSFactory *pOsFactory = OSFactory::instance( pIntf );
48     m_pImgUp = pOsFactory->createOSGraphics( rBmpUp.getWidth(),
49                                              rBmpUp.getHeight() );
50     m_pImgUp->drawBitmap( rBmpUp, 0, 0 );
51     m_pImgDown = pOsFactory->createOSGraphics( rBmpDown.getWidth(),
52                                                rBmpDown.getHeight() );
53     m_pImgDown->drawBitmap( rBmpDown, 0, 0 );
54     m_pImgOver = pOsFactory->createOSGraphics( rBmpOver.getWidth(),
55                                                rBmpOver.getHeight() );
56     m_pImgOver->drawBitmap( rBmpOver, 0, 0 );
57
58     // States
59     m_fsm.addState( "up" );
60     m_fsm.addState( "down" );
61     m_fsm.addState( "upOver" );
62     m_fsm.addState( "downOver" );
63     m_fsm.addState( "hidden" );
64
65     // Transitions
66     m_fsm.addTransition( "upOver", "mouse:left:down", "downOver",
67                          &m_cmdUpOverDownOver );
68     m_fsm.addTransition( "upOver", "mouse:left:dblclick", "downOver",
69                          &m_cmdUpOverDownOver );
70     m_fsm.addTransition( "downOver", "mouse:left:up", "upOver",
71                          &m_cmdDownOverUpOver );
72     m_fsm.addTransition( "downOver", "leave", "down", &m_cmdDownOverDown );
73     m_fsm.addTransition( "down", "enter", "downOver", &m_cmdDownDownOver );
74     m_fsm.addTransition( "upOver", "leave", "up", &m_cmdUpOverUp );
75     m_fsm.addTransition( "up", "enter", "upOver", &m_cmdUpUpOver );
76     m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
77     // XXX: It would be easy to use a "ANY" initial state to handle these
78     // four lines in only one. But till now it isn't worthwhile...
79     m_fsm.addTransition( "up", "special:hide", "hidden", &m_cmdUpHidden );
80     m_fsm.addTransition( "down", "special:hide", "hidden", &m_cmdUpHidden );
81     m_fsm.addTransition( "upOver", "special:hide", "hidden", &m_cmdUpHidden );
82     m_fsm.addTransition( "downOver", "special:hide", "hidden", &m_cmdUpHidden );
83     m_fsm.addTransition( "hidden", "special:show", "up", &m_cmdHiddenUp );
84
85     // Initial state
86     m_fsm.setState( "up" );
87     m_pImg = m_pImgUp;
88 }
89
90
91 CtrlButton::~CtrlButton()
92 {
93     SKINS_DELETE( m_pImgUp );
94     SKINS_DELETE( m_pImgDown );
95     SKINS_DELETE( m_pImgOver );
96 }
97
98
99 void CtrlButton::handleEvent( EvtGeneric &rEvent )
100 {
101     m_fsm.handleTransition( rEvent.getAsString() );
102 }
103
104
105 bool CtrlButton::mouseOver( int x, int y ) const
106 {
107     if( m_pImg )
108     {
109         return m_pImg->hit( x, y );
110     }
111     else
112     {
113         return false;
114     }
115 }
116
117
118 void CtrlButton::draw( OSGraphics &rImage, int xDest, int yDest )
119 {
120     if( m_pImg )
121     {
122         // Draw the current image
123         rImage.drawGraphics( *m_pImg, 0, 0, xDest, yDest );
124     }
125 }
126
127
128 void CtrlButton::CmdUpOverDownOver::execute()
129 {
130     m_pParent->captureMouse();
131     const OSGraphics *pOldImg = m_pParent->m_pImg;
132     m_pParent->m_pImg = m_pParent->m_pImgDown;
133     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
134 }
135
136
137 void CtrlButton::CmdDownOverUpOver::execute()
138 {
139     m_pParent->releaseMouse();
140     const OSGraphics *pOldImg = m_pParent->m_pImg;
141     m_pParent->m_pImg = m_pParent->m_pImgUp;
142     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
143     // Execute the command associated to this button
144     m_pParent->m_rCommand.execute();
145 }
146
147
148 void CtrlButton::CmdDownOverDown::execute()
149 {
150     const OSGraphics *pOldImg = m_pParent->m_pImg;
151     m_pParent->m_pImg = m_pParent->m_pImgUp;
152     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
153 }
154
155
156 void CtrlButton::CmdDownDownOver::execute()
157 {
158     const OSGraphics *pOldImg = m_pParent->m_pImg;
159     m_pParent->m_pImg = m_pParent->m_pImgDown;
160     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
161 }
162
163
164 void CtrlButton::CmdUpUpOver::execute()
165 {
166     const OSGraphics *pOldImg = m_pParent->m_pImg;
167     m_pParent->m_pImg = m_pParent->m_pImgOver;
168     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
169 }
170
171
172 void CtrlButton::CmdUpOverUp::execute()
173 {
174     const OSGraphics *pOldImg = m_pParent->m_pImg;
175     m_pParent->m_pImg = m_pParent->m_pImgUp;
176     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
177 }
178
179
180 void CtrlButton::CmdDownUp::execute()
181 {
182     m_pParent->releaseMouse();
183 }
184
185
186 void CtrlButton::CmdUpHidden::execute()
187 {
188     const OSGraphics *pOldImg = m_pParent->m_pImg;
189     m_pParent->m_pImg = NULL;
190     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
191 }
192
193
194 void CtrlButton::CmdHiddenUp::execute()
195 {
196     const OSGraphics *pOldImg = m_pParent->m_pImg;
197     m_pParent->m_pImg = m_pParent->m_pImgUp;
198     m_pParent->notifyLayoutMaxSize( pOldImg, m_pParent->m_pImg );
199 }
200