/**************************** ExampleFLB ******************************/
#include "EZ.h"

void callback(EZ_Widget *w, void *d);
void wcallback(EZ_Widget *w, void *d);

main(int ac, char **av)
{
  char       str[256];
  FILE       *fp;
  EZ_Item    *items[10];
  EZ_Widget  *flistbox;
  int i, cnt=1;

  srand(getpid());
  EZ_Initialize(ac, av, 0);
  
  flistbox = EZ_CreateWidget(EZ_WIDGET_FANCY_LIST_BOX, NULL,
			     EZ_FANCY_LIST_BOX_COLUMNS, 6,
			     EZ_WIDTH, 400, EZ_HEIGHT, 500,
			     EZ_IPADY, 2,
			     EZ_BORDER_WIDTH, 1,
			     EZ_CALLBACK, callback, NULL,
			     EZ_FANCY_LIST_BOX_TYPE,1, 
                             EZ_IMAGE_FILE, "marble",
			     EZ_SELECTION_FOREGROUND, "black",
			     0);			     
  fp = popen("ls -alg | awk '{print $9 \" \" $3 \" \" $4 \" \" $5 \" \" $1}'", "r");
  if(fp)
    {
      char fields[6][32];
      while(fgets(str, 255, fp) != NULL)
        {
          if(sscanf(str, "%s %s %s %s %s",fields[0],fields[1],fields[2],fields[3],fields[4]) == 5)
            {
	      for(i = 0; i < 5; i++)
                {
		  /*
		  if(i == 0)
		    {
		      EZ_Widget *tw = EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, NULL,
						      EZ_LABEL_STRING, fields[0],
						      EZ_PADX, 0, EZ_PADY, 0,
						      NULL);
		      items[0] = EZ_CreateWidgetItem(tw);
		      EZ_AddWidgetCallBack(tw, EZ_CALLBACK, wcallback, items[0], 0);
		    }
		  else */
		    {
		      items[i] = EZ_CreateLabelItem(fields[i], NULL);
		      EZ_ConfigureItem(items[i], EZ_BORDER_TYPE, EZ_BORDER_RAISED, EZ_BORDER_WIDTH, 1,0);
		    }

                  switch(i)
                    {
                    case 0:  EZ_ConfigureItem(items[i], EZ_FOREGROUND, "red",EZ_PADX, 4,0); 
                      if(fields[4][0] == 'd') EZ_ConfigureItem(items[i], EZ_PIXMAP_FILE, "folder.xpm", 0);
                      else  EZ_ConfigureItem(items[i], EZ_PIXMAP_FILE, "file.xpm",0);
                      break;
                    case 1:  EZ_ConfigureItem(items[i], EZ_FOREGROUND, "blue",0); break;
                    case 2:  EZ_ConfigureItem(items[i], EZ_FOREGROUND, "yellow",0); break;
                    case 3:  EZ_ConfigureItem(items[i], EZ_FOREGROUND, "cyan",0); break;
                    case 4:  EZ_ConfigureItem(items[i], EZ_FOREGROUND, "magenta",0); break;
                    default: break;
                    }
                }
	      {
		char tstr[16]; sprintf(tstr, "%d", cnt++);
		items[5] = EZ_CreateLabelItem(tstr, NULL); 
	      }
	      if( (rand()>> 5)%3 == 0) items[0] = NULL;
	      if( (rand()>> 5)%3 == 0) items[4] = NULL;
	      if( (rand()>> 5)%3 == 0) items[2] = NULL;
	      printf("(%d)\n",EZ_GetFancyListBoxNumberOfRows(flistbox));
              EZ_FancyListBoxInsertRow(flistbox, items, 6, 
				       EZ_GetFancyListBoxNumberOfRows(flistbox)
				       ); /* at the end */
            }
        }
      /* a header */
      items[0] = EZ_CreateLabelItem("File/Dir Name", NULL);
      items[1] = EZ_CreateLabelItem("User Name", NULL);
      items[2] = EZ_CreateLabelItem("Group Name", NULL);
      items[3] = EZ_CreateLabelItem("File Size ", NULL);
      items[4] = EZ_CreateLabelItem("Permissions", NULL);
      items[5] = EZ_CreateLabelItem("Count", NULL);
      for(i = 0; i < 5; i++)
        {
          switch(i)
            {
            case 0:  EZ_ConfigureItem(items[i], EZ_FOREGROUND, "red",0); break;
            case 1:  EZ_ConfigureItem(items[i], EZ_FOREGROUND, "blue",0); break;
            case 2:  EZ_ConfigureItem(items[i], EZ_FOREGROUND, "yellow",0); break;
            case 3:  EZ_ConfigureItem(items[i], EZ_FOREGROUND, "cyan",0); break;
            case 4:  EZ_ConfigureItem(items[i], EZ_FOREGROUND, "magenta",0); break;
            default: break;
            }
          EZ_ConfigureItem(items[i], EZ_BORDER_WIDTH,2,EZ_BORDER_TYPE, EZ_BORDER_RAISED,
                           EZ_BACKGROUND, "bisque2", EZ_PADY, 2,
                           EZ_LABEL_POSITION, EZ_LEFT, 0);
         }
      EZ_SetFancyListBoxHeader(flistbox, items, 6);
    }
  pclose(fp);
  EZ_DisplayWidget(flistbox);
  printf("%d %d\n",
	 EZ_GetFancyListBoxNumberOfRows(flistbox),
	 EZ_GetFancyListBoxNumberOfColumns(flistbox));
  EZ_EventMainLoop();
}
void callback(EZ_Widget *flb, void *data)
{
  EZ_Item *newitems[10];
  static int iii = 0;
  char str[16];
  EZ_Item **items;
  /*
  {
    sprintf(str, "Count=%d", iii++);
    newitems[0] = EZ_CreateLabelItem(str, NULL);
    newitems[1] = EZ_CreateLabelItem("User Name", NULL);
    newitems[2] = EZ_CreateLabelItem("Group Name", NULL);
    newitems[3] = EZ_CreateLabelItem("File Size ", NULL);
    newitems[4] = EZ_CreateLabelItem("Permissions", NULL);
    EZ_ConfigureItem(newitems[0], EZ_BORDER_WIDTH,2,EZ_BORDER_TYPE, EZ_BORDER_RAISED, NULL);
    
    EZ_FancyListBoxInsertRow(flb, newitems, 5, 1); 

    EZ_FreezeWidget(flb);
    printf("[%d]\n", EZ_GetFancyListBoxNumberOfRows(flb));
    while ((EZ_GetFancyListBoxNumberOfRows(flb) - 1) > 10)
      {
	EZ_FancyListBoxDeleteRow(flb, \
                             (EZ_GetFancyListBoxNumberOfRows(flb) - 1));
      }
    EZ_UnFreezeWidget(flb);
  }
  */

  items = EZ_GetFancyListBoxSelection(flb);
  if(items)
    {
      EZ_Item *item = items[0];
      if(item)
	{
	  EZ_ConfigureItem(item, EZ_FONT_NAME,
			   "-Adobe-Helvetica-Medium-R-Normal--*-240-*-*-*-*-*-*",
			   NULL);
	}
    }
  {
    int i,test[3];
    for(i = 0; i < 3; i++) test[i] = (rand()<<5)%100;
    printf("deleting row %d\n",test[0]);
    EZ_FancyListBoxDeleteSomeRows(flb, test, 1);
  }
}




void wcallback(EZ_Widget *w, void *d)
{
  if(EZ_GetWidgetIntData(w) == 0)
    { 
      EZ_ConfigureWidget(w, EZ_FONT_NAME,
			 "-Adobe-Helvetica-Medium-R-Normal--*-240-*-*-*-*-*-*",
			 NULL);  
      EZ_SetWidgetIntData(w, 1);

    }
  else
    {
      EZ_ConfigureWidget(w, EZ_FONT_NAME,
			 "-Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*",
			 NULL);  
      EZ_SetWidgetIntData(w, 0);
    }
  EZ_UpdateDisplayItem(d);
}
