]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_video.cpp
skins2: improve refresh of layouts
[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     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->move( pPos->getLeft(), pPos->getTop() );
105         m_pVoutWindow->resize( pPos->getWidth(), pPos->getHeight() );
106         m_pVoutWindow->show();
107     }
108 }
109
110
111 void CtrlVideo::setLayout( GenericLayout *pLayout,
112                            const Position &rPosition )
113 {
114     CtrlGeneric::setLayout( pLayout, rPosition );
115     m_pLayout->getActiveVar().addObserver( this );
116
117     m_bIsUseable = isVisible() && m_pLayout->getActiveVar().get();
118
119     // register Video Control
120     VoutManager::instance( getIntf() )->registerCtrlVideo( this );
121
122     msg_Dbg( getIntf(),"New VideoControl detected(%p), useability=%s",
123                            this, m_bIsUseable ? "true" : "false" );
124 }
125
126
127 void CtrlVideo::unsetLayout()
128 {
129     m_pLayout->getActiveVar().delObserver( this );
130     CtrlGeneric::unsetLayout();
131 }
132
133
134 void CtrlVideo::resizeControl( int width, int height )
135 {
136     WindowManager &rWindowManager =
137         getIntf()->p_sys->p_theme->getWindowManager();
138
139     const Position *pPos = getPosition();
140
141     if( width != pPos->getWidth() || height != pPos->getHeight() )
142     {
143         // new layout dimensions
144         int newWidth = width + m_xShift;
145         int newHeight = height + m_yShift;
146
147         // Resize the layout
148         rWindowManager.startResize( m_rLayout, WindowManager::kResizeSE );
149         rWindowManager.resize( m_rLayout, newWidth, newHeight );
150         rWindowManager.stopResize();
151
152         if( m_pVoutWindow )
153         {
154             m_pVoutWindow->resize( pPos->getWidth(), pPos->getHeight() );
155             m_pVoutWindow->move( pPos->getLeft(), pPos->getTop() );
156         }
157     }
158 }
159
160
161 void CtrlVideo::onUpdate( Subject<VarBool> &rVariable, void *arg  )
162 {
163     // Visibility changed
164     if( &rVariable == m_pVisible )
165     {
166         msg_Dbg( getIntf(), "VideoCtrl : Visibility changed (visible=%d)",
167                                   isVisible() );
168         notifyLayout();
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     VarBool &rFullscreen = VlcProc::instance( getIntf() )->getFullscreenVar();
179     if( &rVariable == &rFullscreen )
180     {
181         msg_Dbg( getIntf(), "VideoCtrl : fullscreen toggled (fullscreen = %d)",
182                       rFullscreen.get() );
183     }
184
185     m_bIsUseable = isVisible() &&
186                    m_pLayout->getActiveVar().get() &&
187                    !rFullscreen.get();
188
189     if( m_bIsUseable && !isUsed() )
190     {
191         VoutManager::instance( getIntf() )->requestVout( this );
192     }
193     else if( !m_bIsUseable && isUsed() )
194     {
195         VoutManager::instance( getIntf() )->discardVout( this );
196     }
197 }
198
199 void CtrlVideo::attachVoutWindow( VoutWindow* pVoutWindow, int width, int height )
200 {
201     width = ( width < 0 ) ? pVoutWindow->getOriginalWidth() : width;
202     height = ( height < 0 ) ? pVoutWindow->getOriginalHeight() : height;
203
204     WindowManager &rWindowManager =
205         getIntf()->p_sys->p_theme->getWindowManager();
206     TopWindow* pWin = getWindow();
207     rWindowManager.show( *pWin );
208
209     if( m_bAutoResize && width && height )
210     {
211         int newWidth = width + m_xShift;
212         int newHeight = height + m_yShift;
213
214         rWindowManager.startResize( m_rLayout, WindowManager::kResizeSE );
215         rWindowManager.resize( m_rLayout, newWidth, newHeight );
216         rWindowManager.stopResize();
217     }
218
219     pVoutWindow->setCtrlVideo( this );
220
221     m_pVoutWindow = pVoutWindow;
222 }
223
224
225 void CtrlVideo::detachVoutWindow( )
226 {
227     m_pVoutWindow->setCtrlVideo( NULL );
228     m_pVoutWindow = NULL;
229 }
230