]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_video.cpp
skins2: fix warnings about initialization order.
[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_bAutoResize( autoResize), m_xShift( 0 ), m_yShift( 0 ),
40     m_bIsUseable( false), m_pVoutWindow( NULL )
41 {
42     VarBool &rFullscreen = VlcProc::instance( getIntf() )->getFullscreenVar();
43     rFullscreen.addObserver( this );
44
45     // if global parameter set to no resize, override skins behavior
46     if( !var_InheritBool( pIntf, "qt-video-autoresize" ) )
47         m_bAutoResize = false;
48 }
49
50
51 CtrlVideo::~CtrlVideo()
52 {
53     VarBool &rFullscreen = VlcProc::instance( getIntf() )->getFullscreenVar();
54     rFullscreen.delObserver( this );
55 }
56
57
58 void CtrlVideo::handleEvent( EvtGeneric &rEvent )
59 {
60 }
61
62
63 bool CtrlVideo::mouseOver( int x, int y ) const
64 {
65     return false;
66 }
67
68
69 void CtrlVideo::onResize()
70 {
71     const Position *pPos = getPosition();
72     if( pPos && m_pVoutWindow )
73     {
74         m_pVoutWindow->move( pPos->getLeft(), pPos->getTop() );
75         m_pVoutWindow->resize( pPos->getWidth(), pPos->getHeight() );
76     }
77 }
78
79
80 void CtrlVideo::onPositionChange()
81 {
82     // Compute the difference between layout size and video size
83     m_xShift = m_rLayout.getWidth() - getPosition()->getWidth();
84     m_yShift = m_rLayout.getHeight() - getPosition()->getHeight();
85 }
86
87
88 void CtrlVideo::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h)
89 {
90     const Position *pPos = getPosition();
91     rect region( pPos->getLeft(), pPos->getTop(),
92                  pPos->getWidth(), pPos->getHeight() );
93     rect clip( xDest, yDest, w, h );
94     rect inter;
95
96     if( rect::intersect( region, clip, &inter ) )
97     {
98         // Draw a black rectangle under the video to avoid transparency
99         rImage.fillRect( inter.x, inter.y, inter.width, inter.height, 0 );
100     }
101
102     if( m_pVoutWindow )
103     {
104         m_pVoutWindow->show();
105     }
106 }
107
108
109 void CtrlVideo::setLayout( GenericLayout *pLayout,
110                            const Position &rPosition )
111 {
112     CtrlGeneric::setLayout( pLayout, rPosition );
113     m_pLayout->getActiveVar().addObserver( this );
114
115     m_bIsUseable = isVisible() && m_pLayout->getActiveVar().get();
116
117     // register Video Control
118     VoutManager::instance( getIntf() )->registerCtrlVideo( this );
119
120     msg_Dbg( getIntf(),"New VideoControl detected(%p), useability=%s",
121                            this, m_bIsUseable ? "true" : "false" );
122 }
123
124
125 void CtrlVideo::unsetLayout()
126 {
127     m_pLayout->getActiveVar().delObserver( this );
128     CtrlGeneric::unsetLayout();
129 }
130
131
132 void CtrlVideo::resizeControl( int width, int height )
133 {
134     if( !m_bAutoResize )
135         return;
136
137     WindowManager &rWindowManager =
138         getIntf()->p_sys->p_theme->getWindowManager();
139
140     const Position *pPos = getPosition();
141
142     if( width != pPos->getWidth() || height != pPos->getHeight() )
143     {
144         // new layout dimensions
145         int newWidth = width + m_xShift;
146         int newHeight = height + m_yShift;
147
148         // Resize the layout
149         rWindowManager.startResize( m_rLayout, WindowManager::kResizeSE );
150         rWindowManager.resize( m_rLayout, newWidth, newHeight );
151         rWindowManager.stopResize();
152
153         if( m_pVoutWindow )
154         {
155             m_pVoutWindow->resize( pPos->getWidth(), pPos->getHeight() );
156             m_pVoutWindow->move( pPos->getLeft(), pPos->getTop() );
157         }
158     }
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         notifyLayout();
170     }
171
172     // Active Layout changed
173     if( &rVariable == &m_pLayout->getActiveVar() )
174     {
175         msg_Dbg( getIntf(), "VideoCtrl : Active Layout changed (isActive=%d)",
176                       m_pLayout->getActiveVar().get() );
177     }
178
179     VarBool &rFullscreen = VlcProc::instance( getIntf() )->getFullscreenVar();
180     if( &rVariable == &rFullscreen )
181     {
182         msg_Dbg( getIntf(), "VideoCtrl : fullscreen toggled (fullscreen = %d)",
183                       rFullscreen.get() );
184     }
185
186     m_bIsUseable = isVisible() &&
187                    m_pLayout->getActiveVar().get() &&
188                    !rFullscreen.get();
189
190     if( m_bIsUseable && !isUsed() )
191     {
192         VoutManager::instance( getIntf() )->requestVout( this );
193     }
194     else if( !m_bIsUseable && isUsed() )
195     {
196         VoutManager::instance( getIntf() )->discardVout( this );
197     }
198 }
199
200 void CtrlVideo::attachVoutWindow( VoutWindow* pVoutWindow, int width, int height )
201 {
202     width = ( width < 0 ) ? pVoutWindow->getOriginalWidth() : width;
203     height = ( height < 0 ) ? pVoutWindow->getOriginalHeight() : height;
204
205     WindowManager &rWindowManager =
206         getIntf()->p_sys->p_theme->getWindowManager();
207     TopWindow* pWin = getWindow();
208     rWindowManager.show( *pWin );
209
210     if( m_bAutoResize && width && height )
211     {
212         int newWidth = width + m_xShift;
213         int newHeight = height + m_yShift;
214
215         rWindowManager.startResize( m_rLayout, WindowManager::kResizeSE );
216         rWindowManager.resize( m_rLayout, newWidth, newHeight );
217         rWindowManager.stopResize();
218     }
219
220     pVoutWindow->setCtrlVideo( this );
221
222     m_pVoutWindow = pVoutWindow;
223 }
224
225
226 void CtrlVideo::detachVoutWindow( )
227 {
228     m_pVoutWindow->setCtrlVideo( NULL );
229     m_pVoutWindow = NULL;
230 }
231