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 ),
46 // art file if any will overwrite the original image
47 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
48 ArtManager* pArtManager = ArtManager::instance( getIntf() );
51 pVlcProc->getStreamArtVar().addObserver( this );
53 // retrieve initial state of art file
54 string str = pVlcProc->getStreamArtVar().get();
55 GenericBitmap* pArt = (GenericBitmap*) pArtManager->getArtBitmap( str );
59 msg_Dbg( getIntf(), "art file %s to be displayed (wxh = %ix%i)",
60 str.c_str(), m_pBitmap->getWidth(), m_pBitmap->getHeight() );
64 // Create the initial image
65 m_pImage = OSFactory::instance( getIntf() )->createOSGraphics(
66 m_pBitmap->getWidth(),
67 m_pBitmap->getHeight() );
68 m_pImage->drawBitmap( *m_pBitmap );
72 CtrlImage::~CtrlImage()
78 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
79 pVlcProc->getStreamArtVar().delObserver( this );
84 void CtrlImage::handleEvent( EvtGeneric &rEvent )
86 // No FSM for this simple transition
87 if( rEvent.getAsString() == "mouse:right:up:none" )
89 CmdDlgShowPopupMenu( getIntf() ).execute();
91 else if( rEvent.getAsString() == "mouse:left:up:none" )
93 CmdDlgHidePopupMenu( getIntf() ).execute();
94 CmdDlgHideVideoPopupMenu( getIntf() ).execute();
95 CmdDlgHideAudioPopupMenu( getIntf() ).execute();
96 CmdDlgHideMiscPopupMenu( getIntf() ).execute();
98 else if( rEvent.getAsString() == "mouse:left:dblclick:none" )
100 m_rCommand.execute();
105 bool CtrlImage::mouseOver( int x, int y ) const
107 if( x >= 0 && x < getPosition()->getWidth() &&
108 y >= 0 && y < getPosition()->getHeight() )
110 // convert the coordinates to make them fit to the
111 // size of the original image if needed
112 switch( m_resizeMethod )
115 x %= m_pImage->getWidth();
116 y %= m_pImage->getHeight();
119 case kScaleAndRatioPreserved:
127 return m_pImage->hit( x, y );
134 void CtrlImage::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h )
136 const Position *pPos = getPosition();
140 int width = pPos->getWidth();
141 int height = pPos->getHeight();
142 if( width <= 0 || height <= 0 )
145 rect region( pPos->getLeft(), pPos->getTop(),
146 pPos->getWidth(), pPos->getHeight() );
147 rect clip( xDest, yDest, w, h );
149 if( !rect::intersect( region, clip, &inter ) )
152 if( m_resizeMethod == kScale )
154 // Use scaling method
155 if( width != m_pImage->getWidth() ||
156 height != m_pImage->getHeight() )
158 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
159 // Rescale the image with the actual size of the control
160 ScaledBitmap bmp( getIntf(), *m_pBitmap, width, height );
162 m_pImage = pOsFactory->createOSGraphics( width, height );
163 m_pImage->drawBitmap( bmp, 0, 0 );
165 rImage.drawGraphics( *m_pImage,
166 inter.x - pPos->getLeft(),
167 inter.y - pPos->getTop(),
169 inter.width, inter.height );
171 else if( m_resizeMethod == kMosaic )
173 int xDest0 = pPos->getLeft();
174 int yDest0 = pPos->getTop();
179 int curWidth = __MIN( width, m_pImage->getWidth() );
180 height = pPos->getHeight();
181 int curYDest = yDest0;
184 int curHeight = __MIN( height, m_pImage->getHeight() );
185 rect region1( xDest0, curYDest, curWidth, curHeight );
187 if( rect::intersect( region1, clip, &inter1 ) )
189 rImage.drawGraphics( *m_pImage,
190 inter1.x - region1.x,
191 inter1.y - region1.y,
193 inter1.width, inter1.height );
195 curYDest += curHeight;
196 height -= m_pImage->getHeight();
199 width -= m_pImage->getWidth();
202 else if( m_resizeMethod == kScaleAndRatioPreserved )
204 int w0 = m_pBitmap->getWidth();
205 int h0 = m_pBitmap->getHeight();
207 int scaled_height = width * h0 / w0;
208 int scaled_width = height * w0 / h0;
210 // new image scaled with aspect ratio preserved
211 // and centered inside the control boundaries
213 if( scaled_height > height )
217 m_x = ( width - w ) / 2;
225 m_y = ( height - h ) / 2;
228 // rescale the image if size changed
229 if( w != m_pImage->getWidth() ||
230 h != m_pImage->getHeight() )
232 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
233 ScaledBitmap bmp( getIntf(), *m_pBitmap, w, h );
235 m_pImage = pOsFactory->createOSGraphics( w, h );
236 m_pImage->drawBitmap( bmp, 0, 0 );
239 // draw the scaled image at offset (m_x, m_y) from control origin
240 rect region1( pPos->getLeft() + m_x, pPos->getTop() + m_y, w, h );
242 if( rect::intersect( region1, inter, &inter1 ) )
244 rImage.drawGraphics( *m_pImage,
245 inter1.x - pPos->getLeft() - m_x,
246 inter1.y - pPos->getTop() - m_y,
248 inter1.width, inter1.height );
254 void CtrlImage::onUpdate( Subject<VarString> &rVariable, void* arg )
257 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
259 if( &rVariable == &pVlcProc->getStreamArtVar() )
261 string str = ((VarString&)rVariable).get();
262 ArtManager* pArtManager = ArtManager::instance( getIntf() );
263 GenericBitmap* pArt = (GenericBitmap*) pArtManager->getArtBitmap( str );
265 m_pBitmap = pArt ? pArt : m_pOriginalBitmap;
266 msg_Dbg( getIntf(), "art file %s to be displayed (wxh = %ix%i)",
268 m_pBitmap->getWidth(),
269 m_pBitmap->getHeight() );
272 m_pImage = OSFactory::instance( getIntf() )->createOSGraphics(
273 m_pBitmap->getWidth(),
274 m_pBitmap->getHeight() );
275 m_pImage->drawBitmap( *m_pBitmap );