1 /*****************************************************************************
2 * ctrl_image.cpp
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
5 * $Id: 70d9648e9236494d723326095ef553fb6b4f78bb $
6 *
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
9 *
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.
14 *
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.
19 *
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 *****************************************************************************/
24
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"
34
35
CtrlImage(intf_thread_t * pIntf,GenericBitmap & rBitmap,CmdGeneric & rCommand,resize_t resizeMethod,const UString & rHelp,VarBool * pVisible,bool art)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 ),
42 m_x( 0 ), m_y( 0 )
43 {
44 if( m_art )
45 {
46 // art file if any will overwrite the original image
47 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
48 ArtManager* pArtManager = ArtManager::instance( getIntf() );
49
50 // add observer
51 pVlcProc->getStreamArtVar().addObserver( this );
52
53 // retrieve initial state of art file
54 std::string str = pVlcProc->getStreamArtVar().get();
55 GenericBitmap* pArt = (GenericBitmap*) pArtManager->getArtBitmap( str );
56 if( pArt )
57 {
58 m_pBitmap = pArt;
59 msg_Dbg( getIntf(), "art file %s to be displayed (wxh = %ix%i)",
60 str.c_str(), m_pBitmap->getWidth(), m_pBitmap->getHeight() );
61 }
62 }
63
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 );
69 }
70
71
~CtrlImage()72 CtrlImage::~CtrlImage()
73 {
74 delete m_pImage;
75
76 if( m_art )
77 {
78 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
79 pVlcProc->getStreamArtVar().delObserver( this );
80 }
81 }
82
83
handleEvent(EvtGeneric & rEvent)84 void CtrlImage::handleEvent( EvtGeneric &rEvent )
85 {
86 // No FSM for this simple transition
87 if( rEvent.getAsString() == "mouse:right:up:none" )
88 {
89 CmdDlgShowPopupMenu( getIntf() ).execute();
90 }
91 else if( rEvent.getAsString() == "mouse:left:up:none" )
92 {
93 CmdDlgHidePopupMenu( getIntf() ).execute();
94 CmdDlgHideVideoPopupMenu( getIntf() ).execute();
95 CmdDlgHideAudioPopupMenu( getIntf() ).execute();
96 CmdDlgHideMiscPopupMenu( getIntf() ).execute();
97 }
98 else if( rEvent.getAsString() == "mouse:left:dblclick:none" )
99 {
100 m_rCommand.execute();
101 }
102 }
103
104
mouseOver(int x,int y) const105 bool CtrlImage::mouseOver( int x, int y ) const
106 {
107 if( x >= 0 && x < getPosition()->getWidth() &&
108 y >= 0 && y < getPosition()->getHeight() )
109 {
110 // convert the coordinates to make them fit to the
111 // size of the original image if needed
112 switch( m_resizeMethod )
113 {
114 case kMosaic:
115 x %= m_pImage->getWidth();
116 y %= m_pImage->getHeight();
117 break;
118
119 case kScaleAndRatioPreserved:
120 x -= m_x;
121 y -= m_y;
122 break;
123
124 case kScale:
125 break;
126 }
127 return m_pImage->hit( x, y );
128 }
129
130 return false;
131 }
132
133
draw(OSGraphics & rImage,int xDest,int yDest,int w,int h)134 void CtrlImage::draw( OSGraphics &rImage, int xDest, int yDest, int w, int h )
135 {
136 const Position *pPos = getPosition();
137 if( !pPos )
138 return;
139
140 int width = pPos->getWidth();
141 int height = pPos->getHeight();
142 if( width <= 0 || height <= 0 )
143 return;
144
145 rect region( pPos->getLeft(), pPos->getTop(),
146 pPos->getWidth(), pPos->getHeight() );
147 rect clip( xDest, yDest, w, h );
148 rect inter;
149 if( !rect::intersect( region, clip, &inter ) )
150 return;
151
152 if( m_resizeMethod == kScale )
153 {
154 // Use scaling method
155 if( width != m_pImage->getWidth() ||
156 height != m_pImage->getHeight() )
157 {
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 );
161 delete m_pImage;
162 m_pImage = pOsFactory->createOSGraphics( width, height );
163 m_pImage->drawBitmap( bmp, 0, 0 );
164 }
165 rImage.drawGraphics( *m_pImage,
166 inter.x - pPos->getLeft(),
167 inter.y - pPos->getTop(),
168 inter.x, inter.y,
169 inter.width, inter.height );
170 }
171 else if( m_resizeMethod == kMosaic )
172 {
173 int xDest0 = pPos->getLeft();
174 int yDest0 = pPos->getTop();
175
176 // Use mosaic method
177 while( width > 0 )
178 {
179 int curWidth = __MIN( width, m_pImage->getWidth() );
180 height = pPos->getHeight();
181 int curYDest = yDest0;
182 while( height > 0 )
183 {
184 int curHeight = __MIN( height, m_pImage->getHeight() );
185 rect region1( xDest0, curYDest, curWidth, curHeight );
186 rect inter1;
187 if( rect::intersect( region1, clip, &inter1 ) )
188 {
189 rImage.drawGraphics( *m_pImage,
190 inter1.x - region1.x,
191 inter1.y - region1.y,
192 inter1.x, inter1.y,
193 inter1.width, inter1.height );
194 }
195 curYDest += curHeight;
196 height -= m_pImage->getHeight();
197 }
198 xDest0 += curWidth;
199 width -= m_pImage->getWidth();
200 }
201 }
202 else if( m_resizeMethod == kScaleAndRatioPreserved )
203 {
204 int w0 = m_pBitmap->getWidth();
205 int h0 = m_pBitmap->getHeight();
206
207 int scaled_height = width * h0 / w0;
208 int scaled_width = height * w0 / h0;
209
210 // new image scaled with aspect ratio preserved
211 // and centered inside the control boundaries
212 int w, h;
213 if( scaled_height > height )
214 {
215 w = scaled_width;
216 h = height;
217 m_x = ( width - w ) / 2;
218 m_y = 0;
219 }
220 else
221 {
222 w = width;
223 h = scaled_height;
224 m_x = 0;
225 m_y = ( height - h ) / 2;
226 }
227
228 // rescale the image if size changed
229 if( w != m_pImage->getWidth() ||
230 h != m_pImage->getHeight() )
231 {
232 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
233 ScaledBitmap bmp( getIntf(), *m_pBitmap, w, h );
234 delete m_pImage;
235 m_pImage = pOsFactory->createOSGraphics( w, h );
236 m_pImage->drawBitmap( bmp, 0, 0 );
237 }
238
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 );
241 rect inter1;
242 if( rect::intersect( region1, inter, &inter1 ) )
243 {
244 rImage.drawGraphics( *m_pImage,
245 inter1.x - pPos->getLeft() - m_x,
246 inter1.y - pPos->getTop() - m_y,
247 inter1.x, inter1.y,
248 inter1.width, inter1.height );
249 }
250 }
251 }
252
253
onUpdate(Subject<VarString> & rVariable,void * arg)254 void CtrlImage::onUpdate( Subject<VarString> &rVariable, void* arg )
255 {
256 (void)arg;
257 VlcProc *pVlcProc = VlcProc::instance( getIntf() );
258
259 if( &rVariable == &pVlcProc->getStreamArtVar() )
260 {
261 std::string str = ((VarString&)rVariable).get();
262 ArtManager* pArtManager = ArtManager::instance( getIntf() );
263 GenericBitmap* pArt = (GenericBitmap*) pArtManager->getArtBitmap( str );
264
265 m_pBitmap = pArt ? pArt : m_pOriginalBitmap;
266 msg_Dbg( getIntf(), "art file %s to be displayed (wxh = %ix%i)",
267 str.c_str(),
268 m_pBitmap->getWidth(),
269 m_pBitmap->getHeight() );
270
271 delete m_pImage;
272 m_pImage = OSFactory::instance( getIntf() )->createOSGraphics(
273 m_pBitmap->getWidth(),
274 m_pBitmap->getHeight() );
275 m_pImage->drawBitmap( *m_pBitmap );
276
277 notifyLayout();
278 }
279 }
280
281
282