1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "ctrl_image.hpp"
26 #include "../commands/cmd_dialogs.hpp"
27 #include "../events/evt_generic.hpp"
28 #include "../src/os_factory.hpp"
29 #include "../src/os_graphics.hpp"
30 #include "../src/vlcproc.hpp"
31 #include "../src/scaled_bitmap.hpp"
32 #include "../src/art_manager.hpp"
33 #include "../utils/position.hpp"
36 CtrlImage::CtrlImage( intf_thread_t *pIntf, GenericBitmap &rBitmap,
37 CmdGeneric &rCommand, resize_t resizeMethod,
38 const UString &rHelp, VarBool *pVisible, bool art ):
39 CtrlFlat( pIntf, rHelp, pVisible ),
40 m_pBitmap( &rBitmap ), m_pOriginalBitmap( &rBitmap ),
41 m_rCommand( rCommand ), m_resizeMethod( resizeMethod ), m_art( art ),
44 // Create an initial unscaled image in the buffer
45 m_pImage = OSFactory::instance( pIntf )->createOSGraphics(
46 rBitmap.getWidth(), rBitmap.getHeight() );
47 m_pImage->drawBitmap( *m_pBitmap );
49 // Observe the variable
52 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
53 pVlcProc->getStreamArtVar().addObserver( this );
59 CtrlImage::~CtrlImage()
65 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
66 pVlcProc->getStreamArtVar().delObserver( this );
71 void CtrlImage::handleEvent( EvtGeneric &rEvent )
73 // No FSM for this simple transition
74 if( rEvent.getAsString() == "mouse:right:up:none" )
76 CmdDlgShowPopupMenu( getIntf() ).execute();
78 else if( rEvent.getAsString() == "mouse:left:up:none" )
80 CmdDlgHidePopupMenu( getIntf() ).execute();
81 CmdDlgHideVideoPopupMenu( getIntf() ).execute();
82 CmdDlgHideAudioPopupMenu( getIntf() ).execute();
83 CmdDlgHideMiscPopupMenu( getIntf() ).execute();
85 else if( rEvent.getAsString() == "mouse:left:dblclick:none" )
92 bool CtrlImage::mouseOver( int x, int y ) const
94 if( x >= 0 && x < getPosition()->getWidth() &&
95 y >= 0 && y < getPosition()->getHeight() )
97 // convert the coordinates to make them fit to the
98 // size of the original image if needed
99 switch( m_resizeMethod )
102 x %= m_pImage->getWidth();
103 y %= m_pImage->getHeight();
106 case kScaleAndRatioPreserved:
114 return m_pImage->hit( x, y );
121 void CtrlImage::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h )
123 const Position *pPos = getPosition();
127 int width = pPos->getWidth();
128 int height = pPos->getHeight();
129 if( width <= 0 || height <= 0 )
132 rect region( pPos->getLeft(), pPos->getTop(),
133 pPos->getWidth(), pPos->getHeight() );
134 rect clip( xDest, yDest, w, h );
136 if( !rect::intersect( region, clip, &inter ) )
139 if( m_resizeMethod == kScale )
141 // Use scaling method
142 if( width != m_pImage->getWidth() ||
143 height != m_pImage->getHeight() )
145 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
146 // Rescale the image with the actual size of the control
147 ScaledBitmap bmp( getIntf(), *m_pBitmap, width, height );
149 m_pImage = pOsFactory->createOSGraphics( width, height );
150 m_pImage->drawBitmap( bmp, 0, 0 );
152 rImage.drawGraphics( *m_pImage,
153 inter.x - pPos->getLeft(),
154 inter.y - pPos->getTop(),
156 inter.width, inter.height );
158 else if( m_resizeMethod == kMosaic )
160 int xDest0 = pPos->getLeft();
161 int yDest0 = pPos->getTop();
166 int curWidth = __MIN( width, m_pImage->getWidth() );
167 height = pPos->getHeight();
168 int curYDest = yDest0;
171 int curHeight = __MIN( height, m_pImage->getHeight() );
172 rect region1( xDest0, curYDest, curWidth, curHeight );
174 if( rect::intersect( region1, clip, &inter1 ) )
176 rImage.drawGraphics( *m_pImage,
177 inter1.x - region1.x,
178 inter1.y - region1.y,
180 inter1.width, inter1.height );
182 curYDest += curHeight;
183 height -= m_pImage->getHeight();
186 width -= m_pImage->getWidth();
189 else if( m_resizeMethod == kScaleAndRatioPreserved )
191 int w0 = m_pBitmap->getWidth();
192 int h0 = m_pBitmap->getHeight();
194 int scaled_height = width * h0 / w0;
195 int scaled_width = height * w0 / h0;
197 // new image scaled with aspect ratio preserved
198 // and centered inside the control boundaries
200 if( scaled_height > height )
204 m_x = ( width - w ) / 2;
212 m_y = ( height - h ) / 2;
215 // rescale the image if size changed
216 if( w != m_pImage->getWidth() ||
217 h != m_pImage->getHeight() )
219 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
220 ScaledBitmap bmp( getIntf(), *m_pBitmap, w, h );
222 m_pImage = pOsFactory->createOSGraphics( w, h );
223 m_pImage->drawBitmap( bmp, 0, 0 );
226 // draw the scaled image at offset (m_x, m_y) from control origin
227 rect region1( pPos->getLeft() + m_x, pPos->getTop() + m_y, w, h );
229 if( rect::intersect( region1, inter, &inter1 ) )
231 rImage.drawGraphics( *m_pImage,
232 inter1.x - pPos->getLeft() - m_x,
233 inter1.y - pPos->getTop() - m_y,
235 inter1.width, inter1.height );
241 void CtrlImage::onUpdate( Subject<VarString> &rVariable, void* arg )
243 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
245 if( &rVariable == &pVlcProc->getStreamArtVar() )
247 string str = ((VarString&)rVariable).get();
248 ArtManager* pArtManager = ArtManager::instance( getIntf() );
249 GenericBitmap* pArt = (GenericBitmap*) pArtManager->getArtBitmap( str );
251 m_pBitmap = pArt ? pArt : m_pOriginalBitmap;
253 msg_Dbg( getIntf(), "art file %s to be displayed (wxh = %ix%i)",
255 m_pBitmap->getWidth(),
256 m_pBitmap->getHeight() );
259 m_pImage = OSFactory::instance( getIntf() )->createOSGraphics(
260 m_pBitmap->getWidth(),
261 m_pBitmap->getHeight() );
262 m_pImage->drawBitmap( *m_pBitmap );