]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_image.cpp
3de4fc97ee4d515dead6df3dc69774e40249717a
[vlc] / modules / gui / skins2 / controls / ctrl_image.cpp
1 /*****************************************************************************
2  * ctrl_image.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
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_bitmap.hpp"
33 #include "../utils/position.hpp"
34
35
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     // 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 );
48
49     // Observe the variable
50     if( m_art )
51     {
52         VlcProc *pVlcProc = VlcProc::instance( getIntf() );
53         pVlcProc->getStreamArtVar().addObserver( this );
54
55         ArtBitmap::initArtBitmap( getIntf() );
56     }
57
58 }
59
60
61 CtrlImage::~CtrlImage()
62 {
63     delete m_pImage;
64
65     if( m_art )
66     {
67         VlcProc *pVlcProc = VlcProc::instance( getIntf() );
68         pVlcProc->getStreamArtVar().delObserver( this );
69
70         ArtBitmap::freeArtBitmap( );
71     }
72 }
73
74
75 void CtrlImage::handleEvent( EvtGeneric &rEvent )
76 {
77     // No FSM for this simple transition
78     if( rEvent.getAsString() == "mouse:right:up:none" )
79     {
80         CmdDlgShowPopupMenu( getIntf() ).execute();
81     }
82     else if( rEvent.getAsString() == "mouse:left:up:none" )
83     {
84         CmdDlgHidePopupMenu( getIntf() ).execute();
85         CmdDlgHideVideoPopupMenu( getIntf() ).execute();
86         CmdDlgHideAudioPopupMenu( getIntf() ).execute();
87         CmdDlgHideMiscPopupMenu( getIntf() ).execute();
88     }
89     else if( rEvent.getAsString() == "mouse:left:dblclick:none" )
90     {
91         m_rCommand.execute();
92     }
93 }
94
95
96 bool CtrlImage::mouseOver( int x, int y ) const
97 {
98     if( x >= 0 && x < getPosition()->getWidth() &&
99         y >= 0 && y < getPosition()->getHeight() )
100     {
101         // convert the coordinates to make them fit to the
102         // size of the original image if needed
103         switch( m_resizeMethod )
104         {
105         case kMosaic:
106             x %= m_pImage->getWidth();
107             y %= m_pImage->getHeight();
108             break;
109
110         case kScaleAndRatioPreserved:
111             x -= m_x;
112             y -= m_y;
113             break;
114
115         case kScale:
116             break;
117         }
118         return m_pImage->hit( x, y );
119     }
120
121     return false;
122 }
123
124
125 void CtrlImage::draw( OSGraphics &rImage, int xDest, int yDest )
126 {
127     const Position *pPos = getPosition();
128     if( !pPos )
129         return;
130
131     int width = pPos->getWidth();
132     int height = pPos->getHeight();
133     if( width <= 0 || height <= 0 )
134         return;
135
136     if( m_resizeMethod == kScale )
137     {
138         // Use scaling method
139         if( width != m_pImage->getWidth() ||
140             height != m_pImage->getHeight() )
141         {
142             OSFactory *pOsFactory = OSFactory::instance( getIntf() );
143             // Rescale the image with the actual size of the control
144             ScaledBitmap bmp( getIntf(), *m_pBitmap, width, height );
145             delete m_pImage;
146             m_pImage = pOsFactory->createOSGraphics( width, height );
147             m_pImage->drawBitmap( bmp, 0, 0 );
148         }
149         rImage.drawGraphics( *m_pImage, 0, 0, xDest, yDest );
150     }
151     else if( m_resizeMethod == kMosaic )
152     {
153         // Use mosaic method
154         while( width > 0 )
155         {
156             int curWidth = __MIN( width, m_pImage->getWidth() );
157             height = pPos->getHeight();
158             int curYDest = yDest;
159             while( height > 0 )
160             {
161                 int curHeight = __MIN( height, m_pImage->getHeight() );
162                 rImage.drawGraphics( *m_pImage, 0, 0, xDest, curYDest,
163                                      curWidth, curHeight );
164                 curYDest += curHeight;
165                 height -= m_pImage->getHeight();
166             }
167             xDest += curWidth;
168             width -= m_pImage->getWidth();
169         }
170     }
171     else if( m_resizeMethod == kScaleAndRatioPreserved )
172     {
173         int w0 = m_pBitmap->getWidth();
174         int h0 = m_pBitmap->getHeight();
175
176         int scaled_height = width * h0 / w0;
177         int scaled_width  = height * w0 / h0;
178
179         // new image scaled with aspect ratio preserved
180         // and centered inside the control boundaries
181         int w, h;
182         if( scaled_height > height )
183         {
184             w = scaled_width;
185             h = height;
186             m_x = ( width - w ) / 2;
187             m_y = 0;
188         }
189         else
190         {
191             w = width;
192             h = scaled_height;
193             m_x = 0;
194             m_y = ( height - h ) / 2;
195         }
196
197         // rescale the image if size changed
198         if( w != m_pImage->getWidth() ||
199             h != m_pImage->getHeight() )
200         {
201             OSFactory *pOsFactory = OSFactory::instance( getIntf() );
202             ScaledBitmap bmp( getIntf(), *m_pBitmap, w, h );
203             delete m_pImage;
204             m_pImage = pOsFactory->createOSGraphics( w, h );
205             m_pImage->drawBitmap( bmp, 0, 0 );
206         }
207
208         // draw the scaled image at offset (m_x, m_y) from control origin
209         rImage.drawGraphics( *m_pImage, 0, 0, xDest + m_x, yDest + m_y );
210     }
211 }
212
213
214 void CtrlImage::onUpdate( Subject<VarString> &rVariable, void* arg )
215 {
216     VlcProc *pVlcProc = VlcProc::instance( getIntf() );
217
218     if( &rVariable == &pVlcProc->getStreamArtVar() )
219     {
220         string str = ((VarString&)rVariable).get();
221         GenericBitmap* pArt = (GenericBitmap*) ArtBitmap::getArtBitmap( str );
222
223         m_pBitmap = pArt ? pArt : m_pOriginalBitmap;
224
225         msg_Dbg( getIntf(), "art file %s to be displayed (wxh = %ix%i)",
226                             str.c_str(),
227                             m_pBitmap->getWidth(),
228                             m_pBitmap->getHeight() );
229
230         delete m_pImage;
231         m_pImage = OSFactory::instance( getIntf() )->createOSGraphics(
232                                         m_pBitmap->getWidth(),
233                                         m_pBitmap->getHeight() );
234         m_pImage->drawBitmap( *m_pBitmap );
235
236         notifyLayout();
237     }
238 }
239
240