5 #include <lib/gui/eskin.h>
6 #include <lib/gui/ewidget.h>
7 #include <lib/gdi/gfbdc.h>
8 #include <lib/gdi/glcddc.h>
9 #include <lib/gdi/epng.h>
10 #include <lib/base/eerror.h>
11 #include <lib/gdi/font.h>
12 #include <lib/base/eptrlist.h>
14 std::map< eString,tWidgetCreator > eSkin::widget_creator;
18 eNamedColor *eSkin::searchColor(const eString &name)
20 for (std::list<eNamedColor>::iterator i(colors.begin()); i != colors.end(); ++i)
22 if (!i->name.compare(name))
32 void eSkin::addWidgetCreator(const eString &name, tWidgetCreator creator)
34 widget_creator[name] = creator; // add this tWidgetCreator to map... if exist.. overwrite
37 void eSkin::removeWidgetCreator(const eString &name, tWidgetCreator creator)
39 widget_creator.erase(name);
42 int eSkin::parseColor(const eString &name, const char* color, gRGB &col)
47 if (sscanf(color+1, "%lx", &vcol)!=1)
49 eDebug("invalid color named \"%s\" (value: %s)", name.c_str(), color+1);
52 col.r=(vcol>>16)&0xFF;
55 col.a=(vcol>>24)&0xFF;
58 eNamedColor *n=searchColor(color);
61 eDebug("invalid color named \"%s\" (alias to: \"%s\")", name.c_str(), color);
69 int eSkin::parseColors(XMLTreeNode *xcolors)
73 std::list<eNamedColor>::iterator newcolors=colors.end();
75 for (node=xcolors->GetChild(); node; node=node->GetNext())
77 if (strcmp(node->GetType(), "color"))
79 eDebug("junk found in colorsection (%s)", node->GetType());
83 const char *name=node->GetAttributeValue("name"), *color=node->GetAttributeValue("color"), *end=node->GetAttributeValue("end");
87 eDebug("no color/name specified");
94 const char *size=node->GetAttributeValue("size");
104 if ((col.size>1) && (!end))
106 eDebug("no end specified in \"%s\" but is gradient", name);
110 if (parseColor(name, color, col.value))
113 if (end && parseColor(name, end, col.end))
116 colors.push_back(col);
117 if (newcolors == colors.end())
121 for (std::list<eNamedColor>::iterator i(newcolors); i != colors.end(); ++i)
125 for (d=0; d<maxcolors; d+=col.size)
128 for (s=0; s<col.size; s++)
129 if ((d+s>maxcolors) || colorused[d+s])
137 for (int s=0; s<col.size; s++, d++)
142 int rdiff=-col.value.r+col.end.r;
143 int gdiff=-col.value.g+col.end.g;
144 int bdiff=-col.value.b+col.end.b;
145 int adiff=-col.value.a+col.end.a;
146 rdiff*=s; rdiff/=(col.size-1);
147 gdiff*=s; gdiff/=(col.size-1);
148 bdiff*=s; bdiff/=(col.size-1);
149 adiff*=s; adiff/=(col.size-1);
150 palette[d].r=col.value.r+rdiff;
151 palette[d].g=col.value.g+gdiff;
152 palette[d].b=col.value.b+bdiff;
153 palette[d].a=col.value.a+adiff;
155 palette[d]=col.value;
161 int eSkin::parseScheme(XMLTreeNode *xscheme)
164 for (node=xscheme->GetChild(); node; node=node->GetNext())
166 if (strcmp(node->GetType(), "map"))
168 eDebug("illegal scheme entry found: %s", node->GetType());
171 char *name=node->GetAttributeValue("name"), *color=node->GetAttributeValue("color");
174 eDebug("no name or color specified in colorscheme");
179 if ((p=base.find('+'))!=-1)
181 offset=atoi(base.mid(p).c_str());
184 eNamedColor *n=searchColor(base);
187 eDebug("illegal color \"%s\" specified", base.c_str());
190 scheme[name] = gColor(n->index+offset);
195 int eSkin::parseFontAlias(XMLTreeNode *xscheme)
198 for (node=xscheme->GetChild(); node; node=node->GetNext())
200 if (strcmp(node->GetType(), "map"))
202 eDebug("illegal fontalias entry found: %s", node->GetType());
205 char *font=node->GetAttributeValue("font"),
206 *name=node->GetAttributeValue("name"),
207 *size=node->GetAttributeValue("size");
209 if (!name || !font || !size)
211 eDebug("no name, alias or size spezified in fontaliase");
215 std::map<eString, gFont>::iterator it = fontAlias.find(name);
216 if (it != fontAlias.end())
219 std::map<eString, eString>::iterator i = fonts.find(font);
220 if (i == fonts.end())
222 eDebug("font %s not found, skip make alias %s", font, name);
225 fontAlias[name]=gFont(i->second, atoi(size));
230 int eSkin::parseImages(XMLTreeNode *inode)
232 char *abasepath=inode->GetAttributeValue("basepath");
235 eString basepath=eString("/enigma/pictures/");
236 if (abasepath[0] == '/') // allow absolute paths
239 if (basepath[basepath.length()-1]!='/')
242 for (XMLTreeNode *node=inode->GetChild(); node; node=node->GetNext())
244 if (strcmp(node->GetType(), "img"))
246 eDebug("illegal image entry found: %s", node->GetType());
249 const char *name=node->GetAttributeValue("name");
252 eDebug("illegal <img> entry: no name");
255 const char *src=node->GetAttributeValue("src");
258 eDebug("image/img=\"%s\" no src given", name);
261 std::map<eString, ePtr<gPixmap> >::iterator it = images.find(name);
262 if (it != images.end())
264 // eDebug("Image with name %s already loaded, skip %s", name, src);
267 ePtr<gPixmap> image=0;
268 eString filename=basepath + eString(src);
269 if (abasepath[0] != '/')
271 // search first in CONFIGDIR
272 image=loadPNG((eString(CONFIGDIR)+filename).c_str());
274 image=loadPNG((eString(DATADIR)+filename).c_str());
277 image=loadPNG(filename.c_str());
281 eDebug("image/img=\"%s\" - %s: file not found", name, filename.c_str());
285 if (paldummy && !node->GetAttributeValue("nomerge"))
287 gPixmapDC mydc(image);
289 p.mergePalette(paldummy);
291 images[name] = image;
296 int eSkin::parseImageAlias(XMLTreeNode *xvalues)
298 for (XMLTreeNode *node=xvalues->GetChild(); node; node=node->GetNext())
300 if (strcmp(node->GetType(), "map"))
302 eDebug("illegal values entry %s", node->GetType());
305 const char *name=node->GetAttributeValue("name"),
306 *img=node->GetAttributeValue("img");
309 eDebug("map entry has no name or img");
312 std::map<eString, eString>::iterator it = imageAlias.find(name);
313 if (it != imageAlias.end())
315 eDebug("imagealias %s does exist, skip make alias for image %s", name, img);
318 std::map<eString, ePtr<gPixmap> >::iterator i = images.find(img);
319 if (i == images.end())
321 eDebug("image %s not found, skip make alias %s", img , name);
324 imageAlias[name]=img;
329 int eSkin::parseFonts(XMLTreeNode *xfonts)
331 const char *abasepath=xfonts->GetAttributeValue("basepath");
332 eString basepath=abasepath?abasepath:FONTDIR;
334 if (basepath.length())
335 if (basepath[basepath.length()-1]!='/')
338 for (XMLTreeNode *node=xfonts->GetChild(); node; node=node->GetNext())
340 if (strcmp(node->GetType(), "font"))
342 eDebug("illegal fonts entry %s", node->GetType());
345 const char *file=node->GetAttributeValue("file");
348 eDebug("fonts entry has no file");
351 const char *name=node->GetAttributeValue("name");
354 eDebug("fonts entry has no name use filename %s as name", file);
357 std::map<eString, eString>::iterator it = fonts.find(name);
358 const char *ascale=node->GetAttributeValue("scale");
364 if (it != fonts.end())
366 eDebug("Font with name %s already loaded, skip %s", name, file);
369 fonts[name]=fontRenderClass::getInstance()->AddFont(basepath+eString(file), name, scale);
370 if (node->GetAttributeValue("replacement"))
371 eTextPara::setReplacementFont(name);
376 int eSkin::parseValues(XMLTreeNode *xvalues)
378 for (XMLTreeNode *node=xvalues->GetChild(); node; node=node->GetNext())
380 if (strcmp(node->GetType(), "value"))
382 eDebug("illegal values entry %s", node->GetType());
385 const char *name=node->GetAttributeValue("name");
388 eDebug("values entry has no name");
391 const char *value=node->GetAttributeValue("value");
394 eDebug("values entry has no value");
397 std::map<eString, int>::iterator it = values.find(name);
398 if (it != values.end())
400 eDebug("value %s does exist, skip make value %s=%i", name, value);
403 values[name]=atoi(value);
408 gDC *eSkin::getDCbyName(const char *name)
411 if (!strcmp(name, "fb"))
412 dc=gFBDC::getInstance();
414 else if (!strcmp(name, "lcd"))
415 dc=gLCDDC::getInstance();
420 int eSkin::build(eWidget *widget, XMLTreeNode *node)
422 // eDebug("building a %s", node->GetType());
423 /* if (widget->getType() != node->GetType())
426 for (XMLAttribute *attrib=node->GetAttributes(); attrib; attrib=attrib->GetNext())
428 // eDebug("setting %s := %s", attrib->GetName(), attrib->GetValue());
429 if (widget->setProperty(attrib->GetName(), attrib->GetValue()))
435 for (XMLTreeNode *c=node->GetChild(); c; c=c->GetNext())
439 const char *name=c->GetAttributeValue("name");
442 w=widget->search(name);
446 std::map< eString, tWidgetCreator >::iterator it = widget_creator.find(c->GetType());
448 if ( it == widget_creator.end() )
450 eWarning("widget class %s does not exist", c->GetType());
453 w = (it->second)(widget);
457 // eDebug("failed.");
462 if ((err=build(w, c)))
474 palette=new gRGB[maxcolors];
476 memset(palette, 0, sizeof(gRGB)*maxcolors);
477 paldummy=new gImage(eSize(1, 1), 8);
478 paldummy->clut.data=palette;
479 paldummy->clut.colors=maxcolors;
481 colorused=new int[maxcolors];
482 memset(colorused, 0, maxcolors*sizeof(int));
494 for (std::map<eString, ePtr<gPixmap> >::iterator it(images.begin()); it != images.end(); it++)
501 int eSkin::load(const char *filename)
503 eDebug("loading skin: %s", filename);
504 FILE *in=fopen(filename, "rt");
508 parsers.push_front(new XMLTreeParser("ISO-8859-1"));
509 XMLTreeParser &parser=*parsers.first();
515 unsigned int len=fread(buf, 1, sizeof(buf), in);
516 done=len<sizeof(buf);
517 if (!parser.Parse(buf, len, done))
519 eDebug("parse error: %s at line %d",
520 parser.ErrorString(parser.GetErrorCode()),
521 parser.GetCurrentLineNumber());
529 XMLTreeNode *root=parser.RootNode();
532 if (strcmp(root->GetType(), "eskin"))
534 eDebug("not an eskin");
541 void eSkin::parseSkins()
543 for (ePtrList<XMLTreeParser>::reverse_iterator it(parsers); it != parsers.rend(); it++)
545 XMLTreeNode *node=it->RootNode();
547 for (node=node->GetChild(); node; node=node->GetNext())
548 if (!strcmp(node->GetType(), "colors"))
552 for (ePtrList<XMLTreeParser>::reverse_iterator it(parsers); it != parsers.rend(); it++)
554 XMLTreeNode *node=it->RootNode();
556 for (node=node->GetChild(); node; node=node->GetNext())
557 if (!strcmp(node->GetType(), "colorscheme"))
561 for (ePtrList<XMLTreeParser>::iterator it(parsers); it != parsers.end(); it++)
563 XMLTreeNode *node=it->RootNode();
565 for (node=node->GetChild(); node; node=node->GetNext())
566 if (!strcmp(node->GetType(), "fonts"))
570 for (ePtrList<XMLTreeParser>::iterator it(parsers); it != parsers.end(); it++)
572 XMLTreeNode *node=it->RootNode();
574 for (node=node->GetChild(); node; node=node->GetNext())
575 if (!strcmp(node->GetType(), "fontalias"))
576 parseFontAlias(node);
579 for (ePtrList<XMLTreeParser>::iterator it(parsers); it != parsers.end(); it++)
581 XMLTreeNode *node=it->RootNode();
583 for (node=node->GetChild(); node; node=node->GetNext())
584 if (!strcmp(node->GetType(), "images"))
589 for (ePtrList<XMLTreeParser>::iterator it(parsers); it != parsers.end(); it++)
591 XMLTreeNode *node=it->RootNode();
593 for (node=node->GetChild(); node; node=node->GetNext())
594 if (!strcmp(node->GetType(), "imagealias"))
595 parseImageAlias(node);
599 for (ePtrList<XMLTreeParser>::iterator it(parsers); it != parsers.end(); it++)
601 XMLTreeNode *node=it->RootNode();
603 for (node=node->GetChild(); node; node=node->GetNext())
604 if (!strcmp(node->GetType(), "values"))
610 int eSkin::build(eWidget *widget, const char *name)
612 for (parserList::iterator i(parsers.begin()); i!=parsers.end(); ++i)
614 XMLTreeNode *node=i->RootNode();
615 node=node->GetChild();
618 if (!strcmp(node->GetType(), "object"))
620 const char *oname=node->GetAttributeValue("name");
621 if (!std::strcmp(name, oname))
623 node=node->GetChild();
624 return build(widget, node);
627 node=node->GetNext();
630 eDebug("didn't found it");
634 void eSkin::setPalette(gPixmapDC *pal)
639 p.setPalette(palette, 0, 256);
643 eSkin *eSkin::getActive()
646 eFatal("no active skin");
650 void eSkin::makeActive()
655 gColor eSkin::queryScheme(const eString& name) const
659 if ((p=base.find('+'))!=-1)
661 offset=atoi(base.mid(p).c_str());
665 std::map<eString, gColor>::const_iterator it = scheme.find(base);
667 if (it != scheme.end())
668 return it->second + offset;
670 // eWarning("%s does not exist", name.c_str());
675 RESULT eSkin::queryImage(ePtr<gPixmap> &ptr, const eString& name) const
679 std::map<eString, eString>::const_iterator i = imageAlias.find(name);
681 if (i != imageAlias.end())
686 std::map<eString, ePtr<gPixmap> >::const_iterator it = images.find(img);
688 if (it != images.end())
694 int eSkin::queryValue(const eString& name, int d) const
696 std::map<eString, int>::const_iterator it = values.find(name);
698 if (it != values.end())
704 gColor eSkin::queryColor(const eString& name)
708 int numcol=strtol(name.c_str(), &end, 10);
711 return gColor(numcol);
715 if ((p=base.find('+'))!=-1)
717 offset=atoi(base.mid(p).c_str());
721 eNamedColor *col=searchColor(base);
725 return queryScheme(name);
727 return col->index + offset;
730 gFont eSkin::queryFont(const eString& name)
732 std::map<eString, gFont>::iterator it = fontAlias.find(name); // check if name is a font alias
734 if ( it != fontAlias.end() ) // font alias found
740 unsigned int sem = name.rfind(';'); // check if exist ';' in name
741 if (sem != eString::npos) // then exist
743 family=name.left(sem);
744 size = atoi( name.mid(sem+1).c_str() );
749 std::map<eString, eString>::iterator i = fonts.find(family); // check if family is a font name
750 if ( i != fonts.end() ) // font exist
751 return gFont(i->second, size);
753 for (i = fonts.begin() ; i != fonts.end(); i++) // as last check if family name is a complete font Face
754 if ( i->second == family)
755 return gFont(i->second, size);
757 eFatal("Font %s does not exist", name.c_str() ); // halt Programm now... Font does not exist