/* attribs.c - get an xforms attribute given a name
 * distributed as part of _ s u l a _
 * 
 * Author: Tano Fotang, 1998
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 2 of the License , or (at your option) 
 * any later version.
 * see the file COPYING for more info.
 * 
 */


#include <forms.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "attribs.h"


typedef struct TAttrib {
	char *name;
	 int value;
 } Attrib;

 static int find_attrib(Attrib * p, const char *var, int n) {
	 register int low, high, mid, cond;
	 low = 0; high = n - 1;
	 while (low <= high) {
	 mid = (low + high) / 2;
	 if ((cond = strcasecmp(var,(p + mid)->name)) < 0)
	 high = mid - 1;
	 else
	 if (cond > 0)
	 low = mid + 1;
	 else
	 return mid;
	 }
	 return -1;

 }

 
int str2fontsize(const char *str) {
Attrib fontsizes[]={
	{"huge",FL_HUGE_SIZE},
	{"large", FL_LARGE_SIZE},
	{"medium",FL_MEDIUM_SIZE},
	{"normal",FL_NORMAL_SIZE},
	{"small",FL_SMALL_SIZE},
	{"tiny",FL_TINY_SIZE}
};
Attrib *p=fontsizes;
int i;
if(!str) return (-1);
i=find_attrib(p, str, sizeof(fontsizes) / sizeof(Attrib));
return i>-1?fontsizes[i].value :-1 /*FL_NORMAL_SIZE*/;
  }

int str2fontstyle(const char *str) {

	Attrib fontstyles[] = {
                 { "bold", FL_BOLD_STYLE },
                 {"bolditalic", FL_BOLDITALIC_STYLE },
                 { "embossed", FL_EMBOSSED_STYLE },
                 { "engraved", FL_ENGRAVED_STYLE },
                 { "fixed", FL_FIXED_STYLE },
                 { "fixedbold", FL_FIXEDBOLD_STYLE },
                 { "fixedbolditalic", FL_FIXEDBOLDITALIC_STYLE },
                 { "fixeditalic", FL_FIXEDITALIC_STYLE },
                 { "italic", FL_ITALIC_STYLE },
                 { "normal", FL_NORMAL_STYLE },
                 { "shadow", FL_SHADOW_STYLE },
                 { "times", FL_TIMES_STYLE },
                 { "timesbold", FL_TIMESBOLD_STYLE },
                 { "timesbolditalic", FL_TIMESBOLDITALIC_STYLE },
                 { "timesitalic", FL_TIMESITALIC_STYLE }
         };

	 int fstyle = 0;
	 char *pp, *p;
	 char tmp[256];
	 Attrib * fs = fontstyles;
	 int entries = sizeof(fontstyles) / sizeof(Attrib);

	 if(!str) return (-1);
	 pp = tmp;
	 p = (char *) str;
	 while (*p) {
		 int i;
		 while ((*pp = *p++) && (*pp != '+'))
		 	if(*pp==' ' || *pp=='\t') continue; 
			else pp++;
		 if (*pp == '+') * pp = '\0';
		 i = find_attrib(fs, tmp, entries);
		 if (i > -1) fstyle |= fontstyles[i].value;
		 pp = tmp;
		 }
	 return (fstyle ? fstyle : -1/*FL_BOLD_STYLE*/);
 }

 int str2color(const char *str) {

  	Attrib wfl_color[] = {
  		{"bcol", FL_TOP_BCOL},
		 {  "black", FL_BLACK },
		 { "blue", FL_BLUE },
		 { "bottom_bcol", FL_BOTTOM_BCOL },
		 {  "cyan", FL_CYAN },
		 { "darkcyan", FL_DARKCYAN },
		 { "darkgold", FL_DARKGOLD  },
		 {  "darktomato", FL_DARKTOMATO },
		 { "gray", FL_COL1 },
		 { "green", FL_GREEN },
		 { "grey", FL_COL1 },
		 { "inactive_col", FL_INACTIVE_COL },
		 { "indianred", FL_INDIANRED },
		 { "left_bcol", FL_LEFT_BCOL },
		 { "magenta", FL_MAGENTA },
		 { "mcol", FL_MCOL },
		 { "orchid", FL_ORCHID },
		 { "palegreen", FL_PALEGREEN },
		 { "red", FL_RED },
		 { "right_bcol", FL_RIGHT_BCOL },
		 { "slateblue", FL_SLATEBLUE },
		 { "tomato", FL_TOMATO	 },
		 { "wheat", FL_WHEAT },
		 { "white", FL_WHITE },
		 { "yellow", FL_YELLOW }
 
		 };

		 Attrib * p = wfl_color;

		 int i;
	    i = find_attrib(p, str, sizeof(wfl_color) / sizeof(Attrib));
		 if (i < 0) {
		 return (-1);
		 }
		 else
		 return wfl_color[i].value;

}

int new_str2color(Display *disp, Colormap cmap, FL_COLOR index, char *name)
{
	XColor color;
	int result;
	result=XParseColor(disp, cmap,name,&color);
	if(result)
		fl_mapcolor(index, color.red,color.green, color.blue);
	return result;
}


