]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/art_manager.cpp
skins2: rework image control (scale while preserving aspect ratio)
[vlc] / modules / gui / skins2 / src / art_manager.cpp
1 /*****************************************************************************
2  * art_manager.cpp
3  *****************************************************************************
4  * Copyright (C) 2010 the VideoLAN team
5  * $Id$
6  *
7  * Author: Erwan Tulou      <erwan10@vidoelan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "art_manager.hpp"
29 #include <vlc_image.h>
30
31 #define MAX_ART_CACHED    2
32
33
34 ArtManager *ArtManager::instance( intf_thread_t *pIntf )
35 {
36     if( pIntf->p_sys->p_artManager == NULL )
37     {
38         pIntf->p_sys->p_artManager = new ArtManager( pIntf );
39     }
40
41     return pIntf->p_sys->p_artManager;
42 }
43
44
45 void ArtManager::destroy( intf_thread_t *pIntf )
46 {
47     delete pIntf->p_sys->p_artManager;
48     pIntf->p_sys->p_artManager = NULL;
49 }
50
51
52 ArtManager::ArtManager( intf_thread_t* pIntf ) : SkinObject( pIntf )
53 {
54     // initialize handler
55     m_pImageHandler = image_HandlerCreate( pIntf );
56
57     if( !m_pImageHandler )
58         msg_Err( getIntf(), "initialization of art manager failed" );
59 }
60
61
62 ArtManager::~ArtManager( )
63 {
64     if( m_pImageHandler )
65     {
66         image_HandlerDelete( m_pImageHandler );
67         m_pImageHandler = NULL;
68     }
69
70     list<ArtBitmap*>::const_iterator it;
71     for( it = m_listBitmap.begin(); it != m_listBitmap.end(); ++it )
72         delete *it;
73     m_listBitmap.clear();
74 }
75
76
77 ArtBitmap* ArtManager::getArtBitmap( string uriName )
78 {
79     if( !uriName.size() )
80         return NULL;
81
82     if( !m_pImageHandler )
83         return NULL;
84
85     // check whether art is already loaded
86     list<ArtBitmap*>::const_iterator it;
87     for( it = m_listBitmap.begin(); it != m_listBitmap.end(); ++it )
88     {
89         if( (*it)->getUriName() == uriName )
90             return *it;
91     }
92
93     // create and retain a new ArtBitmap since uri is not yet known
94     ArtBitmap* pArt = new ArtBitmap( getIntf(), m_pImageHandler, uriName );
95     if( pArt && pArt->getWidth() && pArt->getHeight() )
96     {
97         if( m_listBitmap.size() == MAX_ART_CACHED )
98         {
99             ArtBitmap* pOldest = *(m_listBitmap.begin());
100             delete pOldest;
101             m_listBitmap.pop_front();
102         }
103         m_listBitmap.push_back( pArt );
104         return pArt;
105     }
106     else
107     {
108         delete pArt;
109         return NULL;
110     }
111 }