/******************************************************************************
 *
 *  EZ Demo.  
 */
#include "EZ.h"
#include <stdio.h>

static EZ_Widget *CreateAnIconItem();
static void      exec_command();

/******************************************************************************/
typedef struct {
  char label_string[128];
  char xpm_file[128];
  char command[128];
  int  x, y;
  EZ_Widget *widget;
} AnIcon;
/******************************************************************************/

AnIcon  tools[128];

main(ac, av)
     int ac; char **av;
{
  EZ_Widget  *item;
  FILE *fp, *fopen();

  if((fp = fopen("commands","r")))
     {
       int i = 0, lastY=2, last_Width=0, last_Height=0;
       char line[512];
       EZ_Initialize(ac,av,0);
  
       while(i < 20 && fgets(line, 512, fp))
	 {
	   /* stupid parser */
	   {
	     int j = 0, k;
	     char *tmpstr;
	     /* label_string */
	     while(line[j] != '"' && j < 500) j++;
	     if(j < 500)
	       {
		 j++; k = 0;
		 tmpstr = tools[i].label_string;
		 while(line[j] != '"' && j < 500) tmpstr[k++] = line[j++];
		 tmpstr[k]=0;
		 j++;
	       }
	     /* xpm_file */
	     if(j < 500)
	       {		 
		 while(line[j] !='"' && j < 500) j++;
		 if(j < 500)
		   {
		     j++; k = 0;
		     tmpstr = tools[i].xpm_file;
		     while(line[j] != '"' && j < 500) tmpstr[k++] = line[j++];
		     tmpstr[k]=0;
		     j++;
		   }
	       }
	     /* command */
	     if(j < 500)
	       {		 
		 while(line[j] !='"' && j < 500) j++;
		 if(j < 500)
		   {
		     j++; k = 0;
		     tmpstr = tools[i].command;
		     while(line[j] != '"' && j < 500) tmpstr[k++] = line[j++];
		     tmpstr[k]=0;
		   }
	       }
	   if(j < 500)
	     {
	       tools[i].widget = CreateAnIconItem(tools[i].label_string, tools[i].xpm_file, tools[i].command);
	       tools[i].x = 2;
	       tools[i].y = lastY;
	       EZ_SetWidgetPosition(tools[i].widget, tools[i].x, tools[i].y);
	       EZ_DisplayWidget(tools[i].widget);
	       EZ_GetWidgetDimension(tools[i].widget, &last_Width, &last_Height);
	       lastY += last_Height + 2;
	       i++;
	     }
	   }
	 }
       EZ_EventMainLoop();
     }
}

/************************************************************************************/
EZ_Widget *CreateAnIconItem(label_string, xpm_file, command)
     char *label_string, *xpm_file, *command;
{
  EZ_Widget  *item, *icon, *label;

  item = EZ_CreateIcon();
  EZ_ConfigureWidget(item, 
                     EZ_ORIENTATION, EZ_VERTICAL_CENTER, 
		     EZ_BORDER_WIDTH, 2, EZ_BORDER_TYPE, EZ_BORDER_UP,
		     EZ_CALL_BACK, exec_command, NULL, EZ_WIDTH_HINT, 100,
		     EZ_IPADX, 8, EZ_IPADY, 6, 0);
  icon = EZ_CreateNWLabel(item, NULL);
  EZ_ConfigureWidget(icon, EZ_BITMAP_FILE,xpm_file, 0);
  
  label= EZ_CreateNWLabel(item, label_string);
  EZ_ConfigureWidget(label, EZ_FONT_NAME, "-Adobe-Helvetica-Bold-R-Normal--*-140-*-*-*-*-*-*", 0);
  EZ_SetWidgetPtrData(item, command);
  
  return(item);
}

/************************************************************************************/
static void exec_command(widget)
     EZ_Widget *widget;
{
  char command[1024];
  if(widget)
    {
      (void)strcpy(command, (char *)EZ_GetWidgetPtrData(widget));
      (void)strcat(command, " &");
      (void)system(command);
    }
}
/************************************************************************************/

