]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_video.cpp
skins2 vout manager
[vlc] / modules / gui / skins2 / controls / ctrl_video.cpp
1 /*****************************************************************************
2  * ctrl_video.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "ctrl_video.hpp"
25 #include "../src/theme.hpp"
26 #include "../src/vout_window.hpp"
27 #include "../src/os_graphics.hpp"
28 #include "../src/vlcproc.hpp"
29 #include "../src/vout_manager.hpp"
30 #include "../src/window_manager.hpp"
31 #include "../commands/async_queue.hpp"
32 #include "../commands/cmd_resize.hpp"
33
34
35 CtrlVideo::CtrlVideo( intf_thread_t *pIntf, GenericLayout &rLayout,
36                       bool autoResize, const UString &rHelp,
37                       VarBool *pVisible ):
38     CtrlGeneric( pIntf, rHelp, pVisible ), m_rLayout( rLayout ),
39     m_xShift( 0 ), m_yShift( 0 ), m_bAutoResize( autoResize ),
40     m_pVoutWindow( NULL ), m_bIsUseable( false )
41 {
42     // Observe the vout size variable if the control is auto-resizable
43     if( m_bAutoResize )
44     {
45         VarBox &rVoutSize = VlcProc::instance( pIntf )->getVoutSizeVar();
46         rVoutSize.addObserver( this );
47     }
48
49     // observe visibility variable
50     if( m_pVisible )
51         m_pVisible->addObserver( this );
52 }
53
54
55 CtrlVideo::~CtrlVideo()
56 {
57     VarBox &rVoutSize = VlcProc::instance( getIntf() )->getVoutSizeVar();
58     rVoutSize.delObserver( this );
59
60     //m_pLayout->getActiveVar().delObserver( this );
61
62     if( m_pVisible )
63         m_pVisible->delObserver( this );
64 }
65
66
67 void CtrlVideo::handleEvent( EvtGeneric &rEvent )
68 {
69 }
70
71
72 bool CtrlVideo::mouseOver( int x, int y ) const
73 {
74     return false;
75 }
76
77
78 void CtrlVideo::onResize()
79 {
80     const Position *pPos = getPosition();
81     if( pPos && m_pVoutWindow )
82     {
83         m_pVoutWindow->move( pPos->getLeft(), pPos->getTop() );
84         m_pVoutWindow->resize( pPos->getWidth(), pPos->getHeight() );
85     }
86 }
87
88
89 void CtrlVideo::onPositionChange()
90 {
91     // Compute the difference between layout size and video size
92     m_xShift = m_rLayout.getWidth() - getPosition()->getWidth();
93     m_yShift = m_rLayout.getHeight() - getPosition()->getHeight();
94 }
95
96
97 void CtrlVideo::draw( OSGraphics &rImage, int xDest, int yDest )
98 {
99     GenericWindow *pParent = getWindow();
100     const Position *pPos = getPosition();
101     if( pParent && pPos )
102     {
103         // Draw a black rectangle under the video to avoid transparency
104         rImage.fillRect( pPos->getLeft(), pPos->getTop(), pPos->getWidth(),
105                          pPos->getHeight(), 0 );
106     }
107 }
108
109
110 void CtrlVideo::setLayout( GenericLayout *pLayout,
111                            const Position &rPosition )
112 {
113     CtrlGeneric::setLayout( pLayout, rPosition );
114     m_pLayout->getActiveVar().addObserver( this );
115
116     m_bIsUseable = isVisible() && m_pLayout->getActiveVar().get();
117
118     // register Video Control
119     VoutManager::instance( getIntf() )->registerCtrlVideo( this );
120
121     msg_Dbg( getIntf(),"New VideoControl detected(%x), useability=%s",
122                            this, m_bIsUseable ? "true" : "false" );
123 }
124
125
126 void CtrlVideo::resizeControl( int width, int height )
127 {
128     int newWidth = width + m_xShift;
129     int newHeight = height + m_yShift;
130
131     // Create a resize command
132     // FIXME: this way of getting the window manager kind of sucks
133     WindowManager &rWindowManager =
134         getIntf()->p_sys->p_theme->getWindowManager();
135     rWindowManager.startResize( m_rLayout, WindowManager::kResizeSE );
136     CmdGeneric *pCmd = new CmdResize( getIntf(), rWindowManager,
137                                       m_rLayout, newWidth, newHeight );
138     // Push the command in the asynchronous command queue
139     AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
140     pQueue->push( CmdGenericPtr( pCmd ), false );
141
142     // FIXME: this should be a command too
143     rWindowManager.stopResize();
144
145     pCmd = new CmdResizeInnerVout( getIntf(), this );
146     pQueue->push( CmdGenericPtr( pCmd ), false );
147
148     TopWindow* pWin = getWindow();
149     rWindowManager.show( *pWin );
150 }
151
152
153 void CtrlVideo::onUpdate( Subject<VarBox> &rVoutSize, void *arg )
154 {
155     int newWidth = ((VarBox&)rVoutSize).getWidth() + m_xShift;
156     int newHeight = ((VarBox&)rVoutSize).getHeight() + m_yShift;
157
158     resizeControl( newWidth, newHeight );
159 }
160
161
162 void CtrlVideo::onUpdate( Subject<VarBool> &rVariable, void *arg  )
163 {
164     // Visibility changed
165     if( &rVariable == m_pVisible )
166     {
167         msg_Dbg( getIntf(), "VideoCtrl : Visibility changed (visible=%d)",
168                                   isVisible() );
169     }
170
171     // Active Layout changed
172     if( &rVariable == &m_pLayout->getActiveVar() )
173     {
174         msg_Dbg( getIntf(), "VideoCtrl : Active Layout changed (isActive=%d)",
175                       m_pLayout->getActiveVar().get() );
176     }
177
178     m_bIsUseable = isVisible() && m_pLayout->getActiveVar().get();
179
180     if( m_bIsUseable && !isUsed() )
181     {
182         VoutManager::instance( getIntf() )->requestVout( this );
183     }
184     else if( !m_bIsUseable && isUsed() )
185     {
186         VoutManager::instance( getIntf() )->discardVout( this );
187     }
188 }
189
190 void CtrlVideo::attachVoutWindow( VoutWindow* pVoutWindow )
191 {
192     int width = pVoutWindow->getOriginalWidth();
193     int height = pVoutWindow->getOriginalHeight();
194
195     WindowManager &rWindowManager =
196         getIntf()->p_sys->p_theme->getWindowManager();
197     TopWindow* pWin = getWindow();
198     rWindowManager.show( *pWin );
199
200     if( m_bAutoResize && width && height )
201     {
202         int newWidth = width + m_xShift;
203         int newHeight = height + m_yShift;
204
205         rWindowManager.startResize( m_rLayout, WindowManager::kResizeSE );
206         rWindowManager.resize( m_rLayout, newWidth, newHeight );
207         rWindowManager.stopResize();
208     }
209
210     pVoutWindow->setCtrlVideo( this );
211
212     m_pVoutWindow = pVoutWindow;
213 }
214
215
216 void CtrlVideo::detachVoutWindow( )
217 {
218     m_pVoutWindow->setCtrlVideo( NULL );
219     m_pVoutWindow = NULL;
220 }
221
222
223 void CtrlVideo::resizeInnerVout( )
224 {
225     WindowManager &rWindowManager =
226          getIntf()->p_sys->p_theme->getWindowManager();
227     TopWindow* pWin = getWindow();
228
229     const Position *pPos = getPosition();
230
231     m_pVoutWindow->resize( pPos->getWidth(), pPos->getHeight() );
232     m_pVoutWindow->move( pPos->getLeft(), pPos->getTop() );
233
234     rWindowManager.show( *pWin );
235     m_pVoutWindow->show();
236 }
237