]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/theme.cpp
Skins strings (Refs:#438)
[vlc] / modules / gui / skins2 / src / theme.cpp
1 /*****************************************************************************
2  * theme.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 "theme.hpp"
26
27
28 Theme::~Theme()
29 {
30     // Be sure things are destroyed in the right order (XXX check)
31     m_layouts.clear();
32     m_controls.clear();
33     m_windows.clear();
34     m_bitmaps.clear();
35     m_fonts.clear();
36     m_commands.clear();
37     m_vars.clear();
38     m_curves.clear();
39 }
40
41
42 void Theme::loadConfig()
43 {
44     msg_Dbg( getIntf(), "loading theme configuration");
45
46     // Get config from vlcrc file
47     char *save = config_GetPsz( getIntf(), "skins2-config" );
48     if( !save ) return;
49
50     // Is there an existing config?
51     if( !strcmp( save, "" ) )
52     {
53         // Show the windows
54         m_windowManager.showAll( true );
55         return;
56     }
57
58     // Initialization
59     map<string, TopWindowPtr>::const_iterator it;
60     int i = 0;
61     int x, y, visible, scan;
62
63     // Get config for each window
64     for( it = m_windows.begin(); it != m_windows.end(); it++ )
65     {
66         TopWindow *pWin = (*it).second.get();
67         // Get config
68         scan = sscanf( &save[i * 13], "(%4d,%4d,%1d)", &x, &y, &visible );
69
70         // If config has the correct number of arguments
71         if( scan > 2 )
72         {
73             m_windowManager.startMove( *pWin );
74             m_windowManager.move( *pWin, x, y );
75             m_windowManager.stopMove();
76             if( visible )
77             {
78                 m_windowManager.show( *pWin );
79             }
80         }
81
82         // Next window
83         i++;
84     }
85     free( save );
86 }
87
88
89 void Theme::saveConfig()
90 {
91     msg_Dbg( getIntf(), "saving theme configuration");
92
93     // Initialize char where config is stored
94     char *save  = new char[400];
95     map<string, TopWindowPtr>::const_iterator it;
96     int i = 0;
97     int x, y;
98
99     // Save config of every window
100     for( it = m_windows.begin(); it != m_windows.end(); it++ )
101     {
102         TopWindow *pWin = (*it).second.get();
103         // Print config
104         x = pWin->getLeft();
105         y = pWin->getTop();
106         sprintf( &save[i * 13], "(%4d,%4d,%1d)", x, y,
107             pWin->getVisibleVar().get() );
108         i++;
109     }
110
111     // Save config to file
112     config_PutPsz( getIntf(), "skins2-config", save );
113
114     // Free memory
115     delete[] save;
116 }
117
118
119 // Useful macro
120 #define FIND_OBJECT( mapData, mapName ) \
121     map<string, mapData>::const_iterator it; \
122     it = mapName.find( id ); \
123     if( it == mapName.end() ) \
124     { \
125         return NULL; \
126     } \
127     return (*it).second.get();
128
129 // This macro takes an ID of the form "id1;id2;id3", and returns the object
130 // corresponding to the first valid ID. If no ID is valid, it returns NULL.
131 // XXX: should we use a template method instead?
132 #define FIND_FIRST_OBJECT( mapDataPtr, mapName ) \
133     string rightPart = id; \
134     string::size_type pos; \
135     do \
136     { \
137         pos = rightPart.find( ";" ); \
138         string leftPart = rightPart.substr( 0, pos ); \
139         map<string, mapDataPtr>::const_iterator it = mapName.find( leftPart ); \
140         if( it != mapName.end() ) \
141         { \
142             return (*it).second.get(); \
143             break; \
144         } \
145  \
146         if( pos != string::npos ) \
147         { \
148             rightPart = rightPart.substr( pos, rightPart.size() ); \
149             rightPart = \
150                 rightPart.substr( rightPart.find_first_not_of( " \t;" ), \
151                                   rightPart.size() ); \
152         } \
153     } \
154     while( pos != string::npos ); \
155     return NULL;
156
157 GenericBitmap *Theme::getBitmapById( const string &id )
158 {
159     FIND_FIRST_OBJECT( GenericBitmapPtr, m_bitmaps );
160 }
161
162 GenericFont *Theme::getFontById( const string &id )
163 {
164     FIND_FIRST_OBJECT( GenericFontPtr, m_fonts );
165 }
166
167 Popup *Theme::getPopupById( const string &id )
168 {
169     FIND_OBJECT( PopupPtr, m_popups );
170 }
171
172 TopWindow *Theme::getWindowById( const string &id )
173 {
174     FIND_OBJECT( TopWindowPtr, m_windows );
175 }
176
177 GenericLayout *Theme::getLayoutById( const string &id )
178 {
179     FIND_OBJECT( GenericLayoutPtr, m_layouts );
180 }
181
182 CtrlGeneric *Theme::getControlById( const string &id )
183 {
184     FIND_OBJECT( CtrlGenericPtr, m_controls );
185 }
186
187
188