/
themeloader.cpp
490 lines (398 loc) · 11.9 KB
/
themeloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
ParaGUI - crossplatform widgetset
Copyright (C) 2000 - 2009 Alexander Pipelka
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexander Pipelka
pipelka@teleweb.at
Last Update: $Author: braindead $
Update Date: $Date: 2009/06/04 10:25:14 $
Source File: $Source: /sources/paragui/paragui/src/themes/themeloader.cpp,v $
CVS/RCS Revision: $Revision: 1.3.6.5.2.12 $
Status: $State: Exp $
*/
#include "paragui.h"
#include "pgcolor.h"
#include "theme_priv.h"
#include "pglog.h"
#include "pgfilearchive.h"
#include "pgdraw.h"
#include <string>
#include <expat.h>
#include <iostream>
// Expat UNICODE workaround - copied from xmltchar.h (Expat dist) - Inserted by Ales Teska
#ifdef XML_UNICODE
#ifndef XML_UNICODE_WCHAR_T
#error UNICODE version requires a 16-bit Unicode-compatible wchar_t=20
#endif
#define T(x) L ## x
#define tcscmp wcscmp
#else /* not XML_UNICODE */
#define T(x) x
#define tcscmp strcmp
#endif
typedef struct _PARSE_INFO {
int depth;
int mode;
THEME_THEME* theme;
std::string str_currentWidget;
std::string str_currentObject;
THEME_WIDGET* p_currentWidget;
THEME_OBJECT* p_currentObject;
std::string themename;
}
PARSE_INFO;
#define THEMEMODE_NONE 0
#define THEMEMODE_THEME 1
#define THEMEMODE_WIDGET 2
#define THEMEMODE_OBJECT 3
#define THEME_SUFFIX ".theme"
#define BUFFSIZE 8192
static char* buff;
void parseGlobProps(PARSE_INFO* info, const XML_Char* name, const XML_Char** atts) {
if(strcmp(name, "theme") == 0) {
// create new theme template
info->theme = new THEME_THEME;
} else {
std::cerr << "UNKNOWN PROP: " << name << std::endl;
}
}
void parseThemeProps(PARSE_INFO* info, const XML_Char* prop, const XML_Char** atts) {
const XML_Char* val = atts[1];
int i=0;
if(tcscmp(T(prop), T("title")) == 0) {
info->theme->title = val;
} else if (tcscmp(T(prop), T("description")) == 0) {
info->theme->description = val;
} else if (tcscmp(T(prop), T("author")) == 0) {
info->theme->author = val;
} else if (tcscmp(T(prop), T("email")) == 0) {
info->theme->email = val;
} else if (tcscmp(T(prop), T("widget")) == 0) {
THEME_WIDGET* widget = new THEME_WIDGET;
widget->type = "";
info->p_currentWidget = widget;
info->mode = THEMEMODE_WIDGET;
} else if(tcscmp(T(prop), T("font")) == 0) {
THEME_FONT* font = new THEME_FONT;
font->size = 14;
for(i=0; atts[i]; i += 2) {
if(tcscmp(T(atts[i]), T("name")) == 0) {
font->name = atts[i+1];
} else if(tcscmp(T(atts[i]), T("value")) == 0) {
font->value = atts[i+1];
} else if(tcscmp(T(atts[i]), T("size")) == 0) {
font->size = atoi(atts[i+1]);
} else {
std::cerr << "UNKNOWN FONT ATTRIBUTE: " << atts[i] << std::endl;
}
}
info->theme->defaultfont = font;
} else {
std::cerr << "UNKNOWN THEME ATTRIBUTE: " << prop << std::endl;
}
}
void parseWidgetProps(PARSE_INFO* info, const XML_Char* prop, const XML_Char** atts) {
std::string val=atts[1];
if(tcscmp(T(prop), T("type")) == 0) {
info->p_currentWidget->type = val;
info->theme->widget[val] = info->p_currentWidget;
} else if(tcscmp(T(prop), T("object")) == 0) {
THEME_OBJECT* object = new THEME_OBJECT;
object->type = "";
object->name = "";
info->p_currentObject = object;
info->mode = THEMEMODE_OBJECT;
} else {
std::cerr << "UNKNOWN WIDGET ATTRIBUTE: " << prop << std::endl;
}
}
void parseObjectProps(PARSE_INFO* info, const XML_Char* prop, const XML_Char** atts) {
std::string val=atts[1];
int i;
THEME_OBJECT* object = info->p_currentObject;
//
// TYPE
//
if(tcscmp(T(prop), T("type")) == 0) {
object->type = val;
}
//
// NAME
//
else if(tcscmp(T(prop), T("name")) == 0) {
object->name = val;
info->p_currentWidget->object[object->name] = object;
}
//
// FILENAME
//
else if(tcscmp(T(prop), T("filename")) == 0) {
THEME_FILENAME* filename = new THEME_FILENAME;
filename->hasColorKey = false;
for(i=0; atts[i]; i += 2) {
if(tcscmp(T(atts[i]), T("name")) == 0) {
filename->name = atts[i+1];
} else if(tcscmp(T(atts[i]), T("value")) == 0) {
filename->value = atts[i+1];
} else if(tcscmp(T(atts[i]), T("colorkey")) == 0) {
sscanf(atts[i+1], "0x%08x", (unsigned int*)(&filename->colorkey));
filename->hasColorKey = true;
} else {
std::cerr << "UNKNOWN FILENAME ATTRIBUTE: " << atts[i] << std::endl;
}
}
// load the image file
filename->surface = PG_FileArchive::LoadSurface(filename->value, true);
if(filename->surface == NULL) {
delete filename;
return;
}
// set the colorkey (if there is any)
if(filename->hasColorKey && filename->surface) {
PG_Color c = filename->colorkey;
Uint32 key = c.MapRGB(filename->surface->format);
SDL_SetColorKey(filename->surface, SDL_SRCCOLORKEY, key);
}
object->filename[filename->name] = filename;
}
//
// FONT
//
else if(tcscmp(T(prop), T("font")) == 0) {
THEME_FONT* font = new THEME_FONT;
font->size = 14;
for(i=0; atts[i]; i += 2) {
if(tcscmp(T(atts[i]), T("name")) == 0) {
font->name = atts[i+1];
} else if(tcscmp(T(atts[i]), T("value")) == 0) {
font->name = atts[i+1];
font->value = atts[i+1];
} else if(tcscmp(T(atts[i]), T("size")) == 0) {
font->size = atoi(atts[i+1]);
} else {
std::cerr << "UNKNOWN FONT ATTRIBUTE: " << atts[i] << std::endl;
}
}
object->font = font;
}
//
// PROPERTY
//
else if(tcscmp(T(prop), T("property")) == 0) {
THEME_PROPERTY* property = new THEME_PROPERTY;
for(i=0; atts[i]; i += 2) {
if(tcscmp(T(atts[i]), T("name")) == 0) {
property->name = atts[i+1];
} else if(tcscmp(T(atts[i]), T("value")) == 0) {
if(tcscmp(T(atts[i+1]), T("TILE")) == 0) {
property->value = PG_Draw::TILE;
} else if(tcscmp(T(atts[i+1]), T("STRETCH")) == 0) {
property->value = PG_Draw::STRETCH;
} else if(tcscmp(T(atts[i+1]), T("3TILEH")) == 0) {
property->value = PG_Draw::TILE3H;
} else if(tcscmp(T(atts[i+1]), T("3TILEV")) == 0) {
property->value = PG_Draw::TILE3V;
} else if(tcscmp(T(atts[i+1]), T("9TILE")) == 0) {
property->value = PG_Draw::TILE9;
} else {
property->value = atoi(atts[i+1]);
}
} else {
std::cerr << "UNKNOWN PROPERTY ATTRIBUTE: " << atts[i] << std::endl;
}
}
object->property[property->name] = property;
}
//
// COLOR
//
else if(tcscmp(T(prop), T("color")) == 0) {
THEME_PROPERTY* property = new THEME_PROPERTY;
for(i=0; atts[i]; i += 2) {
if(tcscmp(T(atts[i]), T("name")) == 0) {
property->name = atts[i+1];
} else if(tcscmp(T(atts[i]), T("value")) == 0) {
unsigned int value;
sscanf(atts[i+1], "0x%08x", &value);
property->value = value;
} else {
std::cerr << "UNKNOWN COLOR ATTRIBUTE: " << atts[i] << std::endl;
}
}
object->property[property->name] = property;
}
//
// GRADIENT
//
else if(tcscmp(T(prop), T("gradient")) == 0) {
THEME_GRADIENT* gradient = new THEME_GRADIENT;
Uint32 c;
std::string val;
for(i=0; atts[i]; i += 2) {
if(tcscmp(T(atts[i]), T("name")) == 0) {
gradient->name = atts[i+1];
} else if(tcscmp(T(atts[i]), T("color0")) == 0) {
val = atts[i+1];
sscanf(val.c_str(), "0x%08x", &c);
gradient->colors[0] = c;
} else if(tcscmp(T(atts[i]), T("color1")) == 0) {
val = atts[i+1];
sscanf(val.c_str(), "0x%08x", &c);
gradient->colors[1] = c;
} else if(tcscmp(T(atts[i]), T("color2")) == 0) {
val = atts[i+1];
sscanf(val.c_str(), "0x%08x", &c);
gradient->colors[2] = c;
} else if(tcscmp(T(atts[i]), T("color3")) == 0) {
val = atts[i+1];
sscanf(val.c_str(), "0x%08x", &c);
gradient->colors[3] = c;
} else {
std::cerr << "UNKNOWN PROPERTY ATTRIBUTE: " << atts[i] << std::endl;
}
}
/*for(i=0; i<4; i++) {
gradient->gradient.colors[i] = gradient->color[i];
}*/
object->gradient[gradient->name] = gradient;
}
//
// STRING
//
else if(tcscmp(T(prop), T("string")) == 0) {
THEME_STRING* str = new THEME_STRING;
for(i=0; atts[i]; i += 2) {
if(tcscmp(T(atts[i]), T("name")) == 0) {
str->name = atts[i+1];
} else if(tcscmp(T(atts[i]), T("value")) == 0) {
str->value = atts[i+1];
} else {
std::cerr << "UNKNOWN STRING ATTRIBUTE: " << atts[i] << std::endl;
}
}
object->strings.insert(info->p_currentObject->strings.end(), str);
}
//
// UNKNOWN OBJECT ATTRIBUTE
//
else {
std::cerr << "UNKNOWN OBJECT ATTRIBUTE: " << prop << std::endl;
}
}
void handlerStart(void* userData, const XML_Char *name, const XML_Char** atts) {
PARSE_INFO* info = (PARSE_INFO*)userData;
info->depth++;
switch(info->mode) {
case THEMEMODE_NONE:
parseGlobProps(info, name, atts);
info->mode = THEMEMODE_THEME;
break;
case THEMEMODE_THEME:
parseThemeProps(info, name, atts);
break;
case THEMEMODE_WIDGET:
parseWidgetProps(info, name, atts);
break;
case THEMEMODE_OBJECT:
parseObjectProps(info, name, atts);
break;
}
}
void handlerEnd(void* userData, const XML_Char* name) {
PARSE_INFO* info = (PARSE_INFO*)userData;
if(tcscmp(T(name), T("object")) == 0) {
info->mode = THEMEMODE_WIDGET;
} else if(tcscmp(T(name), T("widget")) == 0) {
info->mode = THEMEMODE_THEME;
} else if(tcscmp(T(name), T("theme")) == 0) {
info->mode = THEMEMODE_NONE;
}
info->depth--;
}
PG_Theme* PG_Theme::Load(const std::string& xmltheme) {
std::string filename;
buff = new char[BUFFSIZE];
// create new parse info
PARSE_INFO info;
// init parseinfo
info.theme = NULL;
info.p_currentWidget = NULL;
info.p_currentObject = NULL;
info.depth = 0;
info.mode = THEMEMODE_NONE;
filename = xmltheme;
// check if we have a compressed themefile somewhere
filename = xmltheme + ".zip";
// and add it to the searchpath
if(PG_FileArchive::Exists(filename)) {
const char* path = PG_FileArchive::GetRealDir(filename);
char sep = PG_FileArchive::GetDirSeparator()[0];
std::string fullpath = (std::string)path;
if(fullpath[fullpath.size()-1] != sep) {
fullpath += sep;
}
fullpath += filename;
bool rc = PG_FileArchive::AddArchive(fullpath);
if(rc) {
PG_LogMSG("added '%s' to the searchpath", fullpath.c_str());
} else {
PG_LogWRN("failed to add '%s'", fullpath.c_str());
PG_LogWRN("%s", PG_FileArchive::GetLastError());
}
}
// try to open the theme
filename = xmltheme + THEME_SUFFIX;
if(!PG_FileArchive::Exists(filename)) {
PG_LogERR("theme '%s' not found !", filename.c_str());
return NULL;
}
info.themename = xmltheme;
// create a parser
XML_Parser p = XML_ParserCreate(NULL);
// set userdata (parseinfo)
XML_SetUserData(p, (void*)&info);
XML_SetElementHandler(p, handlerStart, handlerEnd);
// create an input-stream
PG_File* file = PG_FileArchive::OpenFile(filename);
if(!file) {
XML_ParserFree(p);
delete[] buff;
return NULL;
}
// do the parsing
while (!file->eof()) {
int done;
int n = file->read(buff, BUFFSIZE);
done = file->eof();
if (! XML_Parse(p, buff, n, done)) {
PG_LogERR("Parse error at line %i:", XML_GetCurrentLineNumber(p));
PG_LogERR("%s", XML_ErrorString(XML_GetErrorCode(p)));
XML_ParserFree(p);
delete[] buff;
return NULL;
}
if(done) {
break;
}
}
// free the parser
XML_ParserFree(p);
// close the file
delete file;
delete[] buff;
PG_LogMSG("theme '%s' loaded sucessfully", filename.c_str());
return info.theme;
}
void PG_Theme::Unload(PG_Theme* theme) {
delete theme;
}