home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Enlightenment / enl14.tgz / enl14.tar / enl14 / imageclass.c < prev    next >
C/C++ Source or Header  |  1997-11-06  |  5KB  |  195 lines

  1. #include "enl.h"
  2.  
  3. Image *LoadImage(char *file)
  4. {
  5.    if (!file) return NULL;
  6.    if (!exists(file))
  7.      {
  8.     Alert("The Image file:\n"
  9.           "%s\n"
  10.           "does not exist.The image cannot be loaded.\n",file);
  11.     return NULL;
  12.      }
  13.    return ImlibLoadImage(imd,file,NULL);
  14. }
  15.  
  16. void FreeImageClass(ImageClass *item)
  17. {
  18.    if (!item) return;
  19.    if (item->name) Efree(item->name);
  20.    FreeImageEntry(item->normal);
  21.    if (item->hilited!=item->normal) FreeImageEntry(item->hilited);
  22.    if ((item->clicked!=item->hilited)&&
  23.        (item->clicked!=item->normal)) FreeImageEntry(item->clicked);
  24.    if (item->disabled!=item->normal) FreeImageEntry(item->disabled);
  25. }
  26.  
  27. void GetPixmaps(ImageClass *ic, char state, int width, int height, Pixmap *pmap, Pixmap *mask)
  28. {
  29.    ImageEntry *ie;
  30.  
  31.    if ((!ic)||(!pmap)||(!mask)) return;
  32.    if      (state==STATE_NORMAL)   ie=ic->normal;
  33.    else if (state==STATE_HILITED)  ie=ic->hilited;
  34.    else if (state==STATE_CLICKED)  ie=ic->clicked;
  35.    else if (state==STATE_DISABLED) ie=ic->disabled;
  36.    else                            ie=ic->normal;
  37.    if (!ie) return;
  38.    if (ic->unloadable)
  39.      {
  40.     if (!ie->im) 
  41.       {
  42.          if ((ie->width==width)&&(ie->height==height))
  43.            {
  44.           *pmap=ie->pmap;*mask=ie->mask;
  45.            }
  46.          else ie->im=LoadImage(ie->name);
  47.       }
  48.     if (ie->im)
  49.       {
  50.          ImlibRender(imd,ie->im,width,height);
  51.          ie->pmap=ImlibMoveImageToPixmap(imd,ie->im);
  52.          ie->mask=ImlibMoveMaskToPixmap(imd,ie->im);
  53.          ie->width=width;ie->height=height;
  54.          *pmap=ie->pmap;*mask=ie->mask;
  55.          ImlibDestroyImage(imd,ie->im);
  56.          ie->im=NULL;
  57.       }
  58.      }
  59.    else
  60.      {
  61.     if (ie->im)
  62.       {
  63.          ImlibRender(imd,ie->im,width,height);
  64.          *pmap=ImlibMoveImageToPixmap(imd,ie->im);
  65.          *mask=ImlibMoveMaskToPixmap(imd,ie->im);
  66.       }
  67.      }
  68. }
  69.  
  70. void FreeImageEntry(ImageEntry *item)
  71. {
  72.    if (!item) return;
  73.    
  74.    if (item->name) Efree(item->name);
  75.    if (item->im) ImlibDestroyImage(imd,item->im);
  76.    if (item->icl) Efree(item->icl);
  77.    if (item->bd) Efree(item->bd);
  78.    if (!item->im)
  79.      {
  80.     if (item->pmap) ImlibFreePixmap(imd,item->pmap);
  81.     if (item->mask) ImlibFreePixmap(imd,item->mask);
  82.      }
  83.    Efree(item);
  84. }
  85.  
  86. ImageEntry *CfgReadImageEntry(char *type)
  87. {
  88.    ImageEntry *ie;
  89.    char s[256];
  90.    int wd;
  91.    
  92.    ie=(ImageEntry *)Emalloc(sizeof(ImageEntry));
  93.    if (!ie) return NULL;
  94.    ie->name=NULL;ie->im=NULL;ie->icl=NULL;ie->bd=NULL;
  95.    ie->width=0;ie->height=0;ie->pmap=0;ie->mask=0;
  96.    if (!CfgGetLine(NULL))
  97.      {
  98.     Efree(ie);
  99.     strcpy(type,"eof");
  100.     return NULL;
  101.      }
  102.    wd=1;
  103.    word(line,wd++,s);
  104.    if (!strcmp(s,"end")) 
  105.      {
  106.     Efree(ie);
  107.     strcpy(type,"end");
  108.     return NULL;
  109.      }
  110.    word(line,wd++,s);strcpy(type,s);
  111.    word(line,wd++,s);ie->name=duplicate(s);
  112.    word(line,wd++,s);
  113.    if (!strcmp(s,"shape"))
  114.      {
  115.     ie->icl=(ImColor *)Emalloc(sizeof(ImColor));
  116.     word(line,wd++,s);ie->icl->r=atoi(s);
  117.     word(line,wd++,s);ie->icl->g=atoi(s);
  118.     word(line,wd++,s);ie->icl->b=atoi(s);
  119.      }
  120.    word(line,wd++,s);
  121.    if (!strcmp(s,"border"))
  122.      {
  123.     ie->bd=(Border *)Emalloc(sizeof(Border));
  124.     word(line,wd++,s);ie->bd->left=atoi(s);
  125.     word(line,wd++,s);ie->bd->right=atoi(s);
  126.     word(line,wd++,s);ie->bd->top=atoi(s);
  127.     word(line,wd++,s);ie->bd->bottom=atoi(s);
  128.      }
  129.    ie->im=LoadImage(ie->name);
  130.    if (!ie->im) 
  131.      {
  132.     if (ie->name) Efree(ie->name);
  133.     if (ie->icl) Efree(ie->icl);
  134.     if (ie->bd) Efree(ie->bd);
  135.     Efree(ie);
  136.     return NULL;
  137.      }
  138.    if (ie->icl) ImlibSetImageTransparentColor(imd,ie->im,ie->icl);
  139.    if (ie->bd) ImlibSetImageBorder(imd,ie->im,ie->bd->left,ie->bd->right,
  140.                    ie->bd->top,ie->bd->bottom);
  141.    return ie;
  142. }
  143.  
  144. void CfgLoadImageClass()
  145. {
  146.    ImageClass *ic;
  147.    ImageEntry *ie;
  148.    char s[1024];
  149.    
  150.    if (!CfgGetLine(NULL)) return;
  151.    ic=Emalloc(sizeof(ImageClass));
  152.    if (!ic) return;
  153.    ic->name=NULL;ic->normal=NULL;ic->hilited=NULL;ic->clicked=NULL;
  154.    ic->disabled=NULL;ic->unloadable=0;
  155.    word(line,1,s);
  156.    if (!strcmp("name",s))
  157.      {
  158.     word(line,2,s);ic->name=duplicate(s);
  159.      }
  160.    if (!CfgGetLine(NULL)) return;
  161.    word(line,1,s);
  162.    if (!strcmp("unloadable",s))
  163.      {
  164.     word(line,2,s);
  165.     if (!strcmp(s,"yes")) ic->unloadable=1;
  166.      }
  167.    for (;;)
  168.      {
  169.     ie=CfgReadImageEntry(s);
  170.     if ((!strcmp(s,"end"))||(!strcmp(s,"eof")))
  171.       {
  172.          if (!ic->hilited) ic->hilited=ic->normal;
  173.          if (!ic->clicked) ic->clicked=ic->hilited;
  174.          if (!ic->disabled) ic->disabled=ic->normal;
  175.          AddItem((void *)ic,ic->name,LIST_TYPE_IMAGECLASS);
  176.          return;
  177.       }
  178.     else if (!strcmp(s,"normal"))
  179.       {
  180.          if (!ie)
  181.            {
  182.           Alert("Unable to load the normal base image of the %s image class\n"
  183.             "This will result in this class being unavailable.\n");
  184.           Efree(ic->name);
  185.           Efree(ic);
  186.           return;
  187.            }
  188.          ic->normal=ie;
  189.       }
  190.     else if (!strcmp(s,"hilited")) ic->hilited=ie;
  191.     else if (!strcmp(s,"clicked")) ic->clicked=ie;
  192.     else if (!strcmp(s,"disabled")) ic->disabled=ie;
  193.      }
  194. }
  195.