]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/theme.cpp
skins2: first proposal for a skinnable fullscreen controller (fsc)
[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 along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "theme.hpp"
26 #include "top_window.hpp"
27 #include <sstream>
28
29
30 Theme::~Theme()
31 {
32     // Be sure things are destroyed in the right order (XXX check)
33     m_layouts.clear();
34     m_controls.clear();
35     m_windows.clear();
36     m_bitmaps.clear();
37     m_fonts.clear();
38     m_commands.clear();
39     m_vars.clear();
40     m_curves.clear();
41 }
42
43
44 void Theme::loadConfig()
45 {
46     msg_Dbg( getIntf(), "loading theme configuration");
47
48     // Get config from vlcrc file
49     char *save = config_GetPsz( getIntf(), "skins2-config" );
50     if( !save ) return;
51
52     // Is there an existing config?
53     if( !strcmp( save, "" ) )
54     {
55         // Show the windows as indicated by the XML file
56         m_windowManager.showAll( true );
57         return;
58     }
59
60     istringstream inStream(save);
61     free( save );
62
63     char sep;
64     string winId, layId;
65     int x, y, width, height, visible;
66     bool somethingVisible = false;
67     while( !inStream.eof() )
68     {
69         inStream >> sep;
70         if( sep != '[' ) goto invalid;
71         inStream >> winId >> layId >> x >> y >> width >> height >> visible >> sep >> ws;
72         if( sep != ']' ) goto invalid;
73
74         // Try to find the window and the layout
75         map<string, TopWindowPtr>::const_iterator itWin;
76         map<string, GenericLayoutPtr>::const_iterator itLay;
77         itWin = m_windows.find( winId );
78         itLay = m_layouts.find( layId );
79         if( itWin == m_windows.end() || itLay == m_layouts.end() )
80         {
81             goto invalid;
82         }
83         TopWindow *pWin = itWin->second.get();
84         GenericLayout *pLayout = itLay->second.get();
85
86         // Restore the layout
87         m_windowManager.setActiveLayout( *pWin, *pLayout );
88         if( pLayout->getWidth() != width ||
89             pLayout->getHeight() != height )
90         {
91             m_windowManager.startResize( *pLayout, WindowManager::kResizeSE );
92             m_windowManager.resize( *pLayout, width, height );
93             m_windowManager.stopResize();
94         }
95         // Move the window (which incidentally takes care of the anchoring)
96         m_windowManager.startMove( *pWin );
97         m_windowManager.move( *pWin, x, y );
98         m_windowManager.stopMove();
99         if( visible )
100         {
101             somethingVisible = true;
102             m_windowManager.show( *pWin );
103         }
104     }
105
106     if( !somethingVisible )
107     {
108         goto invalid;
109     }
110     return;
111
112 invalid:
113     msg_Warn( getIntf(), "invalid config: %s", inStream.str().c_str() );
114     // Restore the visibility defined in the theme
115     m_windowManager.showAll( true );
116 }
117
118
119 void Theme::saveConfig()
120 {
121     msg_Dbg( getIntf(), "saving theme configuration");
122
123     map<string, TopWindowPtr>::const_iterator itWin;
124     map<string, GenericLayoutPtr>::const_iterator itLay;
125     ostringstream outStream;
126     for( itWin = m_windows.begin(); itWin != m_windows.end(); itWin++ )
127     {
128         TopWindow *pWin = itWin->second.get();
129
130         // Find the layout id for this window
131         string layoutId;
132         const GenericLayout *pLayout = &pWin->getActiveLayout();
133         for( itLay = m_layouts.begin(); itLay != m_layouts.end(); itLay++ )
134         {
135             if( itLay->second.get() == pLayout )
136             {
137                 layoutId = itLay->first;
138             }
139         }
140
141         outStream << '[' << itWin->first << ' ' << layoutId << ' '
142             << pWin->getLeft() << ' ' << pWin->getTop() << ' '
143             << pLayout->getWidth() << ' ' << pLayout->getHeight() << ' '
144             << (pWin->getVisibleVar().get() ? 1 : 0) << ']';
145     }
146
147     // Save config to file
148     config_PutPsz( getIntf(), "skins2-config", outStream.str().c_str() );
149 }
150
151
152 // Takes an ID of the form "id1;id2;id3", and returns the object
153 // corresponding to the first valid ID. If no ID is valid, it returns NULL.
154 // XXX The string handling here probably could be improved.
155 template<class T> typename T::pointer
156 Theme::IDmap<T>::find_first_object( const string &id ) const
157 {
158     string rightPart = id;
159     string::size_type pos;
160     do
161     {
162         pos = rightPart.find( ";" );
163         string leftPart = rightPart.substr( 0, pos );
164
165         typename T::pointer p = find_object( leftPart );
166         if( p ) return p;
167
168         if( pos != string::npos )
169         {
170             rightPart = rightPart.substr( pos, rightPart.size() );
171             rightPart =
172                 rightPart.substr( rightPart.find_first_not_of( " \t;" ),
173                                   rightPart.size() );
174         }
175     }
176     while( pos != string::npos );
177     return NULL;
178 }
179
180 GenericBitmap *Theme::getBitmapById( const string &id ) const
181 {
182     return m_bitmaps.find_first_object( id );
183 }
184
185 GenericFont *Theme::getFontById( const string &id ) const
186 {
187     return m_fonts.find_first_object( id );
188 }
189