]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_video.cpp
That's safe to delete NULL.
[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
50
51 CtrlVideo::~CtrlVideo()
52 {
53     VarBox &rVoutSize = VlcProc::instance( getIntf() )->getVoutSizeVar();
54     rVoutSize.delObserver( this );
55
56     //m_pLayout->getActiveVar().delObserver( this );
57 }
58
59
60 void CtrlVideo::handleEvent( EvtGeneric &rEvent )
61 {
62 }
63
64
65 bool CtrlVideo::mouseOver( int x, int y ) const
66 {
67     return false;
68 }
69
70
71 void CtrlVideo::onResize()
72 {
73     const Position *pPos = getPosition();
74     if( pPos && m_pVoutWindow )
75     {
76         m_pVoutWindow->move( pPos->getLeft(), pPos->getTop() );
77         m_pVoutWindow->resize( pPos->getWidth(), pPos->getHeight() );
78     }
79 }
80
81
82 void CtrlVideo::onPositionChange()
83 {
84     // Compute the difference between layout size and video size
85     m_xShift = m_rLayout.getWidth() - getPosition()->getWidth();
86     m_yShift = m_rLayout.getHeight() - getPosition()->getHeight();
87 }
88
89
90 void CtrlVideo::draw( OSGraphics &rImage, int xDest, int yDest )
91 {
92     GenericWindow *pParent = getWindow();
93     const Position *pPos = getPosition();
94     if( pParent && pPos )
95     {
96         // Draw a black rectangle under the video to avoid transparency
97         rImage.fillRect( pPos->getLeft(), pPos->getTop(), pPos->getWidth(),
98                          pPos->getHeight(), 0 );
99     }
100 }
101
102
103 void CtrlVideo::setLayout( GenericLayout *pLayout,
104                            const Position &rPosition )
105 {
106     CtrlGeneric::setLayout( pLayout, rPosition );
107     m_pLayout->getActiveVar().addObserver( this );
108
109     m_bIsUseable = isVisible() && m_pLayout->getActiveVar().get();
110
111     // register Video Control
112     VoutManager::instance( getIntf() )->registerCtrlVideo( this );
113
114     msg_Dbg( getIntf(),"New VideoControl detected(%p), useability=%s",
115                            this, m_bIsUseable ? "true" : "false" );
116 }
117
118
119 void CtrlVideo::resizeControl( int width, int height )
120 {
121     int newWidth = width + m_xShift;
122     int newHeight = height + m_yShift;
123
124     // Create a resize command
125     // FIXME: this way of getting the window manager kind of sucks
126     WindowManager &rWindowManager =
127         getIntf()->p_sys->p_theme->getWindowManager();
128     rWindowManager.startResize( m_rLayout, WindowManager::kResizeSE );
129     CmdGeneric *pCmd = new CmdResize( getIntf(), rWindowManager,
130                                       m_rLayout, newWidth, newHeight );
131     // Push the command in the asynchronous command queue
132     AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
133     pQueue->push( CmdGenericPtr( pCmd ), false );
134
135     // FIXME: this should be a command too
136     rWindowManager.stopResize();
137
138     pCmd = new CmdResizeInnerVout( getIntf(), this );
139     pQueue->push( CmdGenericPtr( pCmd ), false );
140
141     TopWindow* pWin = getWindow();
142     rWindowManager.show( *pWin );
143 }
144
145
146 void CtrlVideo::onUpdate( Subject<VarBox> &rVoutSize, void *arg )
147 {
148     int newWidth = ((VarBox&)rVoutSize).getWidth() + m_xShift;
149     int newHeight = ((VarBox&)rVoutSize).getHeight() + m_yShift;
150
151     resizeControl( newWidth, newHeight );
152 }
153
154
155 void CtrlVideo::onUpdate( Subject<VarBool> &rVariable, void *arg  )
156 {
157     // Visibility changed
158     if( &rVariable == m_pVisible )
159     {
160         msg_Dbg( getIntf(), "VideoCtrl : Visibility changed (visible=%d)",
161                                   isVisible() );
162     }
163
164     // Active Layout changed
165     if( &rVariable == &m_pLayout->getActiveVar() )
166     {
167         msg_Dbg( getIntf(), "VideoCtrl : Active Layout changed (isActive=%d)",
168                       m_pLayout->getActiveVar().get() );
169     }
170
171     m_bIsUseable = isVisible() && m_pLayout->getActiveVar().get();
172
173     if( m_bIsUseable && !isUsed() )
174     {
175         VoutManager::instance( getIntf() )->requestVout( this );
176     }
177     else if( !m_bIsUseable && isUsed() )
178     {
179         VoutManager::instance( getIntf() )->discardVout( this );
180     }
181 }
182
183 void CtrlVideo::attachVoutWindow( VoutWindow* pVoutWindow, int width, int height )
184 {
185     width = ( width < 0 ) ? pVoutWindow->getOriginalWidth() : width;
186     height = ( height < 0 ) ? pVoutWindow->getOriginalHeight() : height;
187
188     WindowManager &rWindowManager =
189         getIntf()->p_sys->p_theme->getWindowManager();
190     TopWindow* pWin = getWindow();
191     rWindowManager.show( *pWin );
192
193     if( m_bAutoResize && width && height )
194     {
195         int newWidth = width + m_xShift;
196         int newHeight = height + m_yShift;
197
198         rWindowManager.startResize( m_rLayout, WindowManager::kResizeSE );
199         rWindowManager.resize( m_rLayout, newWidth, newHeight );
200         rWindowManager.stopResize();
201     }
202
203     pVoutWindow->setCtrlVideo( this );
204
205     m_pVoutWindow = pVoutWindow;
206 }
207
208
209 void CtrlVideo::detachVoutWindow( )
210 {
211     m_pVoutWindow->setCtrlVideo( NULL );
212     m_pVoutWindow = NULL;
213 }
214
215
216 void CtrlVideo::resizeInnerVout( )
217 {
218     if( m_pVoutWindow )
219     {
220         WindowManager &rWindowManager =
221              getIntf()->p_sys->p_theme->getWindowManager();
222         TopWindow* pWin = getWindow();
223
224         const Position *pPos = getPosition();
225
226         m_pVoutWindow->resize( pPos->getWidth(), pPos->getHeight() );
227         m_pVoutWindow->move( pPos->getLeft(), pPos->getTop() );
228
229         rWindowManager.show( *pWin );
230         m_pVoutWindow->show();
231     }
232 }
233