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