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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
|
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <lib/gui/eskin.h>
#include <lib/gui/ewidget.h>
#include <lib/gdi/gfbdc.h>
#include <lib/gdi/glcddc.h>
#include <lib/gdi/epng.h>
#include <lib/base/eerror.h>
#include <lib/gdi/font.h>
#include <lib/base/eptrlist.h>
std::map< eString,tWidgetCreator > eSkin::widget_creator;
eSkin *eSkin::active;
eNamedColor *eSkin::searchColor(const eString &name)
{
for (std::list<eNamedColor>::iterator i(colors.begin()); i != colors.end(); ++i)
{
if (!i->name.compare(name))
return &*i;
}
return 0;
}
void eSkin::clear()
{
}
void eSkin::addWidgetCreator(const eString &name, tWidgetCreator creator)
{
widget_creator[name] = creator; // add this tWidgetCreator to map... if exist.. overwrite
}
void eSkin::removeWidgetCreator(const eString &name, tWidgetCreator creator)
{
widget_creator.erase(name);
}
int eSkin::parseColor(const eString &name, const char* color, gRGB &col)
{
if (color[0]=='#')
{
unsigned long vcol=0;
if (sscanf(color+1, "%lx", &vcol)!=1)
{
eDebug("invalid color named \"%s\" (value: %s)", name.c_str(), color+1);
return -1;
}
col.r=(vcol>>16)&0xFF;
col.g=(vcol>>8)&0xFF;
col.b=vcol&0xFF;
col.a=(vcol>>24)&0xFF;
} else
{
eNamedColor *n=searchColor(color);
if (!n)
{
eDebug("invalid color named \"%s\" (alias to: \"%s\")", name.c_str(), color);
return -1;
}
col=n->value;
}
return 0;
}
int eSkin::parseColors(XMLTreeNode *xcolors)
{
XMLTreeNode *node;
std::list<eNamedColor>::iterator newcolors=colors.end();
for (node=xcolors->GetChild(); node; node=node->GetNext())
{
if (strcmp(node->GetType(), "color"))
{
eDebug("junk found in colorsection (%s)", node->GetType());
continue;
}
const char *name=node->GetAttributeValue("name"), *color=node->GetAttributeValue("color"), *end=node->GetAttributeValue("end");
if (!color || !name)
{
eDebug("no color/name specified");
continue;
}
eNamedColor col;
col.name=name;
const char *size=node->GetAttributeValue("size");
if (size)
col.size=atoi(size);
else
col.size=0;
if (!col.size)
col.size=1;
if ((col.size>1) && (!end))
{
eDebug("no end specified in \"%s\" but is gradient", name);
continue;
}
if (parseColor(name, color, col.value))
continue;
if (end && parseColor(name, end, col.end))
continue;
colors.push_back(col);
if (newcolors == colors.end())
--newcolors;
}
for (std::list<eNamedColor>::iterator i(newcolors); i != colors.end(); ++i)
{
eNamedColor &col=*i;
int d;
for (d=0; d<maxcolors; d+=col.size)
{
int s;
for (s=0; s<col.size; s++)
if ((d+s>maxcolors) || colorused[d+s])
break;
if (s==col.size)
break;
}
if (d==maxcolors)
continue;
col.index=gColor(d);
for (int s=0; s<col.size; s++, d++)
{
colorused[d]=1;
if (s)
{
int rdiff=-col.value.r+col.end.r;
int gdiff=-col.value.g+col.end.g;
int bdiff=-col.value.b+col.end.b;
int adiff=-col.value.a+col.end.a;
rdiff*=s; rdiff/=(col.size-1);
gdiff*=s; gdiff/=(col.size-1);
bdiff*=s; bdiff/=(col.size-1);
adiff*=s; adiff/=(col.size-1);
palette[d].r=col.value.r+rdiff;
palette[d].g=col.value.g+gdiff;
palette[d].b=col.value.b+bdiff;
palette[d].a=col.value.a+adiff;
} else
palette[d]=col.value;
}
}
return 0;
}
int eSkin::parseScheme(XMLTreeNode *xscheme)
{
XMLTreeNode *node;
for (node=xscheme->GetChild(); node; node=node->GetNext())
{
if (strcmp(node->GetType(), "map"))
{
eDebug("illegal scheme entry found: %s", node->GetType());
continue;
}
char *name=node->GetAttributeValue("name"), *color=node->GetAttributeValue("color");
if (!name || !color)
{
eDebug("no name or color specified in colorscheme");
continue;
}
eString base=color;
int offset=0, p;
if ((p=base.find('+'))!=-1)
{
offset=atoi(base.mid(p).c_str());
base=base.left(p);
}
eNamedColor *n=searchColor(base);
if (!n)
{
eDebug("illegal color \"%s\" specified", base.c_str());
continue;
}
scheme[name] = gColor(n->index+offset);
}
return 0;
}
int eSkin::parseFontAlias(XMLTreeNode *xscheme)
{
XMLTreeNode *node;
for (node=xscheme->GetChild(); node; node=node->GetNext())
{
if (strcmp(node->GetType(), "map"))
{
eDebug("illegal fontalias entry found: %s", node->GetType());
continue;
}
char *font=node->GetAttributeValue("font"),
*name=node->GetAttributeValue("name"),
*size=node->GetAttributeValue("size");
if (!name || !font || !size)
{
eDebug("no name, alias or size spezified in fontaliase");
continue;
}
std::map<eString, gFont>::iterator it = fontAlias.find(name);
if (it != fontAlias.end())
continue;
std::map<eString, eString>::iterator i = fonts.find(font);
if (i == fonts.end())
{
eDebug("font %s not found, skip make alias %s", font, name);
continue;
}
fontAlias[name]=gFont(i->second, atoi(size));
}
return 0;
}
int eSkin::parseImages(XMLTreeNode *inode)
{
char *abasepath=inode->GetAttributeValue("basepath");
if (!abasepath)
abasepath="";
eString basepath=eString("/enigma/pictures/");
if (abasepath[0] == '/') // allow absolute paths
basepath="";
basepath+=abasepath;
if (basepath[basepath.length()-1]!='/')
basepath+="/";
for (XMLTreeNode *node=inode->GetChild(); node; node=node->GetNext())
{
if (strcmp(node->GetType(), "img"))
{
eDebug("illegal image entry found: %s", node->GetType());
continue;
}
const char *name=node->GetAttributeValue("name");
if (!name)
{
eDebug("illegal <img> entry: no name");
continue;
}
const char *src=node->GetAttributeValue("src");
if (!src)
{
eDebug("image/img=\"%s\" no src given", name);
continue;
}
std::map<eString, ePtr<gPixmap> >::iterator it = images.find(name);
if (it != images.end())
{
// eDebug("Image with name %s already loaded, skip %s", name, src);
continue;
}
ePtr<gPixmap> image=0;
eString filename=basepath + eString(src);
if (abasepath[0] != '/')
{
// search first in CONFIGDIR
image=loadPNG((eString(CONFIGDIR)+filename).c_str());
if (!image)
image=loadPNG((eString(DATADIR)+filename).c_str());
}
else // abs path
image=loadPNG(filename.c_str());
if (!image)
{
eDebug("image/img=\"%s\" - %s: file not found", name, filename.c_str());
continue;
}
if (paldummy && !node->GetAttributeValue("nomerge"))
{
gPixmapDC mydc(image);
gPainter p(mydc);
p.mergePalette(paldummy);
}
images[name] = image;
}
return 0;
}
int eSkin::parseImageAlias(XMLTreeNode *xvalues)
{
for (XMLTreeNode *node=xvalues->GetChild(); node; node=node->GetNext())
{
if (strcmp(node->GetType(), "map"))
{
eDebug("illegal values entry %s", node->GetType());
continue;
}
const char *name=node->GetAttributeValue("name"),
*img=node->GetAttributeValue("img");
if (!name || !img)
{
eDebug("map entry has no name or img");
continue;
}
std::map<eString, eString>::iterator it = imageAlias.find(name);
if (it != imageAlias.end())
{
eDebug("imagealias %s does exist, skip make alias for image %s", name, img);
continue;
}
std::map<eString, ePtr<gPixmap> >::iterator i = images.find(img);
if (i == images.end())
{
eDebug("image %s not found, skip make alias %s", img , name);
continue;
}
imageAlias[name]=img;
}
return 0;
}
int eSkin::parseFonts(XMLTreeNode *xfonts)
{
const char *abasepath=xfonts->GetAttributeValue("basepath");
eString basepath=abasepath?abasepath:FONTDIR;
if (basepath.length())
if (basepath[basepath.length()-1]!='/')
basepath+="/";
for (XMLTreeNode *node=xfonts->GetChild(); node; node=node->GetNext())
{
if (strcmp(node->GetType(), "font"))
{
eDebug("illegal fonts entry %s", node->GetType());
continue;
}
const char *file=node->GetAttributeValue("file");
if (!file)
{
eDebug("fonts entry has no file");
continue;
}
const char *name=node->GetAttributeValue("name");
if (!name)
{
eDebug("fonts entry has no name use filename %s as name", file);
name = file;
}
std::map<eString, eString>::iterator it = fonts.find(name);
const char *ascale=node->GetAttributeValue("scale");
int scale=0;
if (ascale)
scale=atoi(ascale);
if (!scale)
scale=100;
if (it != fonts.end())
{
eDebug("Font with name %s already loaded, skip %s", name, file);
continue;
}
fonts[name]=fontRenderClass::getInstance()->AddFont(basepath+eString(file), name, scale);
if (node->GetAttributeValue("replacement"))
eTextPara::setReplacementFont(name);
}
return 0;
}
int eSkin::parseValues(XMLTreeNode *xvalues)
{
for (XMLTreeNode *node=xvalues->GetChild(); node; node=node->GetNext())
{
if (strcmp(node->GetType(), "value"))
{
eDebug("illegal values entry %s", node->GetType());
continue;
}
const char *name=node->GetAttributeValue("name");
if (!name)
{
eDebug("values entry has no name");
continue;
}
const char *value=node->GetAttributeValue("value");
if (!value)
{
eDebug("values entry has no value");
continue;
}
std::map<eString, int>::iterator it = values.find(name);
if (it != values.end())
{
eDebug("value %s does exist, skip make value %s=%i", name, value);
continue;
}
values[name]=atoi(value);
}
return 0;
}
gDC *eSkin::getDCbyName(const char *name)
{
gPixmapDC *dc=0;
if (!strcmp(name, "fb"))
dc=gFBDC::getInstance();
#ifndef DISABLE_LCD
else if (!strcmp(name, "lcd"))
dc=gLCDDC::getInstance();
#endif
return dc;
}
int eSkin::build(eWidget *widget, XMLTreeNode *node)
{
// eDebug("building a %s", node->GetType());
/* if (widget->getType() != node->GetType())
return -1;*/
for (XMLAttribute *attrib=node->GetAttributes(); attrib; attrib=attrib->GetNext())
{
// eDebug("setting %s := %s", attrib->GetName(), attrib->GetValue());
if (widget->setProperty(attrib->GetName(), attrib->GetValue()))
{
eDebug("failed");
return -1;
}
}
for (XMLTreeNode *c=node->GetChild(); c; c=c->GetNext())
{
eWidget *w=0;
const char *name=c->GetAttributeValue("name");
if (name)
w=widget->search(name);
if (!w)
{
std::map< eString, tWidgetCreator >::iterator it = widget_creator.find(c->GetType());
if ( it == widget_creator.end() )
{
eWarning("widget class %s does not exist", c->GetType());
return -ENOENT;
}
w = (it->second)(widget);
}
if (!w)
{
// eDebug("failed.");
return -EINVAL;
}
w->zOrderRaise();
int err;
if ((err=build(w, c)))
{
return err;
}
}
return 0;
}
eSkin::eSkin()
{
maxcolors=256;
palette=new gRGB[maxcolors];
memset(palette, 0, sizeof(gRGB)*maxcolors);
paldummy=new gImage(eSize(1, 1), 8);
paldummy->clut.data=palette;
paldummy->clut.colors=maxcolors;
colorused=new int[maxcolors];
memset(colorused, 0, maxcolors*sizeof(int));
}
eSkin::~eSkin()
{
if (active==this)
active=0;
clear();
delete colorused;
for (std::map<eString, ePtr<gPixmap> >::iterator it(images.begin()); it != images.end(); it++)
delete it->second;
if (paldummy)
delete paldummy;
}
int eSkin::load(const char *filename)
{
eDebug("loading skin: %s", filename);
FILE *in=fopen(filename, "rt");
if (!in)
return -1;
parsers.push_front(new XMLTreeParser("ISO-8859-1"));
XMLTreeParser &parser=*parsers.first();
char buf[2048];
int done;
do
{
unsigned int len=fread(buf, 1, sizeof(buf), in);
done=len<sizeof(buf);
if (!parser.Parse(buf, len, done))
{
eDebug("parse error: %s at line %d",
parser.ErrorString(parser.GetErrorCode()),
parser.GetCurrentLineNumber());
parsers.pop_front();
fclose(in);
return -1;
}
} while (!done);
fclose(in);
XMLTreeNode *root=parser.RootNode();
if (!root)
return -1;
if (strcmp(root->GetType(), "eskin"))
{
eDebug("not an eskin");
return -1;
}
return 0;
}
void eSkin::parseSkins()
{
for (ePtrList<XMLTreeParser>::reverse_iterator it(parsers); it != parsers.rend(); it++)
{
XMLTreeNode *node=it->RootNode();
for (node=node->GetChild(); node; node=node->GetNext())
if (!strcmp(node->GetType(), "colors"))
parseColors(node);
}
for (ePtrList<XMLTreeParser>::reverse_iterator it(parsers); it != parsers.rend(); it++)
{
XMLTreeNode *node=it->RootNode();
for (node=node->GetChild(); node; node=node->GetNext())
if (!strcmp(node->GetType(), "colorscheme"))
parseScheme(node);
}
for (ePtrList<XMLTreeParser>::iterator it(parsers); it != parsers.end(); it++)
{
XMLTreeNode *node=it->RootNode();
for (node=node->GetChild(); node; node=node->GetNext())
if (!strcmp(node->GetType(), "fonts"))
parseFonts(node);
}
for (ePtrList<XMLTreeParser>::iterator it(parsers); it != parsers.end(); it++)
{
XMLTreeNode *node=it->RootNode();
for (node=node->GetChild(); node; node=node->GetNext())
if (!strcmp(node->GetType(), "fontalias"))
parseFontAlias(node);
}
for (ePtrList<XMLTreeParser>::iterator it(parsers); it != parsers.end(); it++)
{
XMLTreeNode *node=it->RootNode();
for (node=node->GetChild(); node; node=node->GetNext())
if (!strcmp(node->GetType(), "images"))
parseImages(node);
}
for (ePtrList<XMLTreeParser>::iterator it(parsers); it != parsers.end(); it++)
{
XMLTreeNode *node=it->RootNode();
for (node=node->GetChild(); node; node=node->GetNext())
if (!strcmp(node->GetType(), "imagealias"))
parseImageAlias(node);
}
for (ePtrList<XMLTreeParser>::iterator it(parsers); it != parsers.end(); it++)
{
XMLTreeNode *node=it->RootNode();
for (node=node->GetChild(); node; node=node->GetNext())
if (!strcmp(node->GetType(), "values"))
parseValues(node);
}
}
int eSkin::build(eWidget *widget, const char *name)
{
for (parserList::iterator i(parsers.begin()); i!=parsers.end(); ++i)
{
XMLTreeNode *node=i->RootNode();
node=node->GetChild();
while (node)
{
if (!strcmp(node->GetType(), "object"))
{
const char *oname=node->GetAttributeValue("name");
if (!std::strcmp(name, oname))
{
node=node->GetChild();
return build(widget, node);
}
}
node=node->GetNext();
}
}
eDebug("didn't found it");
return -ENOENT;
}
void eSkin::setPalette(gPixmapDC *pal)
{
if (palette)
{
gPainter p(*pal);
p.setPalette(palette, 0, 256);
}
}
eSkin *eSkin::getActive()
{
if (!active)
eFatal("no active skin");
return active;
}
void eSkin::makeActive()
{
active=this;
}
gColor eSkin::queryScheme(const eString& name) const
{
eString base=name;
int offset=0, p;
if ((p=base.find('+'))!=-1)
{
offset=atoi(base.mid(p).c_str());
base=base.left(p);
}
std::map<eString, gColor>::const_iterator it = scheme.find(base);
if (it != scheme.end())
return it->second + offset;
// eWarning("%s does not exist", name.c_str());
return gColor(0);
}
RESULT eSkin::queryImage(ePtr<gPixmap> &ptr, const eString& name) const
{
eString img;
std::map<eString, eString>::const_iterator i = imageAlias.find(name);
if (i != imageAlias.end())
img = i->second;
else
img = name;
std::map<eString, ePtr<gPixmap> >::const_iterator it = images.find(img);
if (it != images.end())
ptr = it->second;
return 0;
}
int eSkin::queryValue(const eString& name, int d) const
{
std::map<eString, int>::const_iterator it = values.find(name);
if (it != values.end())
return it->second;
return d;
}
gColor eSkin::queryColor(const eString& name)
{
char *end;
int numcol=strtol(name.c_str(), &end, 10);
if (!*end)
return gColor(numcol);
eString base=name;
int offset=0, p;
if ((p=base.find('+'))!=-1)
{
offset=atoi(base.mid(p).c_str());
base=base.left(p);
}
eNamedColor *col=searchColor(base);
if (!col)
{
return queryScheme(name);
} else
return col->index + offset;
}
gFont eSkin::queryFont(const eString& name)
{
std::map<eString, gFont>::iterator it = fontAlias.find(name); // check if name is a font alias
if ( it != fontAlias.end() ) // font alias found
return it->second;
eString family;
int size=0;
unsigned int sem = name.rfind(';'); // check if exist ';' in name
if (sem != eString::npos) // then exist
{
family=name.left(sem);
size = atoi( name.mid(sem+1).c_str() );
if (size<=0)
size=16;
}
std::map<eString, eString>::iterator i = fonts.find(family); // check if family is a font name
if ( i != fonts.end() ) // font exist
return gFont(i->second, size);
for (i = fonts.begin() ; i != fonts.end(); i++) // as last check if family name is a complete font Face
if ( i->second == family)
return gFont(i->second, size);
eFatal("Font %s does not exist", name.c_str() ); // halt Programm now... Font does not exist
return gFont();
}
|