/*********************************************************************
 *
 *            EZFM---EZWGL based file manager for X11
 *
 *             Copyright (C) 1998  Maorong Zou
 *
 *
 *  This file is part of EZFM.
 *
 *  EZFM 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, or (at your option)
 * any later version.
 *
 * EZFM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EZFM; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *************************************************************************/



/*********************************************************************
 *
 *  Save/restore the state info when exists/starts
 */
#define _STATE_C_
#include "ezfm.h"
/*********************************************************************/
void ezfmSaveState MY_ANSIARGS((EZ_Widget *widget, void *data));
int  ezfmReadState MY_ANSIARGS((void));
/*********************************************************************/
static Toplevel *makeFolders   MY_ANSIARGS((char *buf));
static void makeSelection MY_ANSIARGS((Folder *folder, char *item, int x, int y));
static EZ_TreeNode *matchTreeNode MY_ANSIARGS((EZ_TreeNode *node, void *data));
/*********************************************************************/
EXTERN EZ_Widget *getToolBoxPage MY_ANSIARGS((int idx));
/*********************************************************************/
void ezfmSaveState(wwt, ddta) EZ_Widget *wwt; void *ddta;
{
  char stateFile[256], *envHome;
  FILE *fp;

  /*  open ~/.ezfmState */
  envHome = getenv("HOME");
  sprintf(stateFile, "%s/.ezfmState", envHome);
  fp = fopen(stateFile, "w");
  if(fp == NULL) return; 
  fprintf(fp, "#\n# ezfm saved state, DO NOT EDIT !!!\n#\n");

  /* global options */
  fprintf(fp, "Section \"Options\"\n");  
  fprintf(fp, "  %d %d %d %d %s\n", ezfmFlags, EZ_Btn1, EZ_Btn2, EZ_Btn3,
	  ezfmFolderBgPixmap[0] == '\0' ? "nil": ezfmFolderBgPixmap);
  fprintf(fp, "EndSection\n");

  fprintf(fp, "Section \"Transparency\"\n");  
  fprintf(fp, "  %d %d %d %d %d\n", (int)trgbtd[0], (int)trgbtd[1],
          (int)trgbtd[2], (int)trgbtd[3], (int)trgbtd[4]);
  fprintf(fp, "EndSection\n");
  /* folders */
  {
    Toplevel   *toplevels[100], *tp;
    int        i, ntoplevels = 0;
    Folder     *folder, *folders = getFolderList();
    int        ox=0, oy=0, ix=0, iy=0, xr,yr,wr,hr,xf,yf,wf=0,hf=0;
    char       *pathname;
    EZ_Widget  *widget;
    fprintf(fp, "Section \"Folders\"\n");

    /* folders are under toplevels and one toplevel may
     * contain two folders. So folders need to be saved
     * based on toplevels
     */
    while(folders) /* find all toplevels */
      {
	folder = folders->next;
	tp = folders->toplevel;
	for(i = 0; i < ntoplevels; i++)  if(toplevels[i] == tp) break;
	if(i == ntoplevels)
	  {
	    toplevels[i] = tp;
	    ntoplevels++;
	  }
	folders = folder;
	if(ntoplevels >= 100) break;
      }

    /* save state info, one toplevel a time */
    for(i = 0; i < ntoplevels; i++)
      {
	int      viewable, type, selection, oflag, tbv;
	EZ_Item  *item = NULL;
	char     *ipath, *bgi, *tptr;

	/* save geometry of the toplevel frame */
	oflag =  toplevels[i]->flags;
	widget = toplevels[i]->toplevel;
	viewable = EZ_WidgetIsViewable(widget);
	tbv =  EZ_WidgetIsViewable(toplevels[i]->toolbar);
	if(viewable) EZ_GetWidgetAbsoluteGeometry(widget, &xr, &yr, &wr, &hr);
	else EZ_GetWidgetGeometry(widget, &xr, &yr, &wr, &hr);
	
	fprintf(fp, "  [%d %d %d %d %d %d %d] ", viewable, xr,yr,wr,hr,oflag, tbv);
	
	/* the first folder */
	folder = toplevels[i]->folder[0];
	type = folder->type;
	pathname = folder->finfo->pathname;
	EZ_GetWidgetAbsoluteGeometry(folder->frame, &xf, &yf, &wf, &hf);
	if(type == ICON_FOLDER)
	  {
	    item = EZ_GetWorkAreaSelection(folder->workarea);
	    EZ_GetWorkAreaXYOffset(folder->workarea, &ox, &oy);
	  }
	else if(type == LIST_FOLDER)
	  {
	    EZ_Item **selRow = EZ_GetFancyListBoxSelection(folder->flistbox);
	    if(selRow) item = selRow[0];
	    EZ_GetFancyListBoxXYOffset(folder->flistbox, &ox, &oy);
	  }
	else
	  {
	    EZ_TreeNode *node = EZ_GetListTreeWidgetSelection(folder->listtree);
	    if(node) item = EZ_TreeNodeGetItem(node);
	    EZ_GetListTreeWidgetXYOffset(folder->listtree, &ox, &oy);
	  }
	selection = (item != NULL);
	if(selection)
	  {
	    FileInfo *finfo = (FileInfo *)EZ_GetItemPtrData(item);
	    ipath = finfo->pathname;
	    EZ_GetItemPosition(item, &xr, &yr);
	  }
	else  { xr = ox, yr = oy; ipath = "nil";}

	bgi = folder->bgpixmap;
	if(bgi == NULL)  bgi = "nil";
	else  if( (tptr = strrchr(bgi,'/')) != NULL) bgi = tptr+1;
	fprintf(fp, "[%d %s %d %d %s %d %d %s] ", type, pathname, wf, hf, ipath, xr,yr, bgi);
		
	
	/* the second folder */
	folder = toplevels[i]->folder[1];
	if(folder)
	  {
	    item = NULL;
	    type = folder->type;
	    pathname = folder->finfo->pathname;
	    EZ_GetWidgetAbsoluteGeometry(folder->frame, &xf, &yf, &wf, &hf);
	    if(type == ICON_FOLDER)
	      {
		item = EZ_GetWorkAreaSelection(folder->workarea);
		EZ_GetWorkAreaXYOffset(folder->workarea, &ox, &oy);
	      }
	    else if(type == LIST_FOLDER)
	      {
		EZ_Item **selRow = EZ_GetFancyListBoxSelection(folder->flistbox);
		if(selRow) item = selRow[0];
		EZ_GetFancyListBoxXYOffset(folder->flistbox, &ox, &oy);
	      }
	    else
	      {
		EZ_TreeNode *node = EZ_GetListTreeWidgetSelection(folder->listtree);
		if(node) item = EZ_TreeNodeGetItem(node);
		EZ_GetListTreeWidgetXYOffset(folder->listtree, &ox, &oy);
	      }
	    selection = (item != NULL);
	    if(selection)
	      {
		FileInfo *finfo = (FileInfo *)EZ_GetItemPtrData(item);
		ipath = finfo->pathname;
		EZ_GetItemPosition(item, &xr, &yr);
	      }
	    else { xr = ox; yr = oy; ipath = "nil";}
	    bgi = folder->bgpixmap;
	    if(bgi == NULL)  bgi = "nil";
	    else  if( (tptr = strrchr(bgi,'/')) != NULL) bgi = tptr+1;
	    fprintf(fp, "[%d %s %d %d %s %d %d %s] ", type, pathname, wf, hf, ipath, xr,yr,bgi);
	  }
	fprintf(fp, "\n");

	/* save histry list */
	{
	  EZ_ListBoxEntry   *data;
	  EZ_Widget   *oentry, *jnk, *lbox;
	  int         j,cnt;
	  folder = toplevels[i]->folder[0];
	  oentry = folder->optentry;
	  EZ_GetOptionalEntryWidgetComponents(oentry, &jnk, &lbox, &jnk);
	  EZ_GetListBoxData(lbox, &data, &cnt);	  
	  fprintf(fp,"\tHistory\n");
	  for(j = 0; j < cnt && j < 16; j++)
	    fprintf(fp, "\t  %s\n", data[j].data);
	  fprintf(fp,"\tEndHistory\n");
	  /* the second folder */
	  fprintf(fp,"\tHistory\n");
	  if((folder = toplevels[i]->folder[1]) != NULL)
	    {
	      oentry = folder->optentry;
	      EZ_GetOptionalEntryWidgetComponents(oentry, &jnk, &lbox, &jnk);
	      EZ_GetListBoxData(lbox, &data, &cnt);	  
	      for(j = 0; j < cnt && j < 16; j++)
		fprintf(fp, "\t  %s\n", data[j].data);
	    }
	  fprintf(fp,"\tEndHistory\n");
	}
      }
    fprintf(fp, "EndSection\n");
  }
  /* toolBox */
  if(EZ_WidgetExist(ezfmToolBox))
    {
      char *bgi, *tptr;
      int  xr,yr,wr,hr,viewable = EZ_WidgetIsViewable(ezfmToolBox);
      int  i, activePage = (int)EZ_GetWidgetIntData(ezfmToolBox);
      fprintf(fp, "Section \"ToolBox\"\n");
      
      if(viewable) 
	EZ_GetWidgetAbsoluteGeometry(ezfmToolBox, &xr, &yr, &wr, &hr);
      else if(EZ_GetWidgetWindow(ezfmToolBox) != None)
	EZ_GetWidgetGeometry(ezfmToolBox, &xr, &yr, &wr, &hr); 
      else
	{
	  EZ_ComputeWidgetWindowSize(ezfmToolBox, &wr, &hr, NULL);
	  EZ_GetWidgetGeometry(ezfmToolBox, &xr, &yr, &wr, &hr); 
	}
      fprintf(fp, "  %d %d %d %d %d %d\n", viewable, xr,yr,wr,hr, activePage);      
      
      for(i = 0; i <30; i++)
	{
	  EZ_Widget *thePage =  getToolBoxPage(i);
	  if(thePage)
	    {
	      EZ_Widget *workarea = (EZ_Widget *)EZ_GetWidgetPtrData(thePage);
	      char *tptr, *bgi = EZ_GetWidgetPtrData(workarea);
	      if(bgi == NULL) bgi = "nil";
	      else if( (tptr = strrchr(bgi,'/')) != NULL) bgi = tptr+1;
	      fprintf(fp, "  Page %d %s\n", i, bgi);
	    }
	}
      fprintf(fp, "EndSection\n");
    }
  /* toolBar */
  if(EZ_WidgetIsViewable(ezfmToolBar))
    {
      int xr,yr,wr,hr;
      fprintf(fp, "Section \"ToolBar\"\n");
      EZ_GetWidgetAbsoluteGeometry(ezfmToolBar, &xr, &yr, &wr, &hr);
      fprintf(fp, "  %d %d %d %d\n", xr,yr,wr,hr);      
      fprintf(fp, "EndSection\n");
    }
  /* icons */
  {
    Icon *next, *icons = getIconList();
    EZ_Widget *widget;
    int       xr,yr,wr,hr, isTool;
    char      *pathname;
    FileInfo  *finfo;

    fprintf(fp, "Section \"Icons\"\n");
    while(icons)
      {
	next = icons->next;
	widget = icons->icon;
	finfo  = icons->finfo;
	isTool = icons->isTool;
	pathname = finfo->pathname;
	EZ_GetWidgetAbsoluteGeometry(widget, &xr, &yr, &wr, &hr);
	fprintf(fp,"  %s %d %d %d\n", pathname, xr,yr,isTool);
	icons = next;
      }
    fprintf(fp, "EndSection\n");
  }
  fprintf(fp, "# End \n");
  fclose(fp);
}
/*********************************************************************/
int ezfmReadState()
{
  char stateFile[256], *envHome;
  char line[BUFSIZ], copy[BUFSIZ];
  char *buf, *ptr;
  int  i, status, lineno=1;
  FILE *fp;

  envHome = getenv("HOME");
  sprintf(stateFile, "%s/.ezfmState", envHome);
  
  fp = fopen(stateFile, "r");
  if(fp == NULL) return(0);
  
  while(1)
    {
      while((status = seekWord(line, &buf, &lineno, fp, "section")) == 0)
	/* null statement */ ; 
      if(status > 0)
	{
	  ptr = skipSpaces(buf);
	  if(!strncmp(ptr, "\"Icons\"", 7))
	    {
	      while(1)
		{
		  if( (status = nextLine(line, &lineno, fp)) > 0)
		    {
		      if(matchWord(line,&buf, "endsection"))
			break;	      
		      else 
			{
			  char *p = skipSpaces(buf);
			  char pathname[256], *epathname;
			  int  x,y,isTool;
			  if(sscanf(p, "%s %d %d %d", pathname, &x, &y, &isTool) == 4)
			    {
                              epathname = EZ_TildeExpand(pathname);
			      if(access(epathname, F_OK) == 0)
				{
				  if(isTool == 0)
				    {
				      EZ_Widget *icon = getCreateIcon(epathname, x, y);
				      EZ_DisplayWidget(icon);
				    }
				  else
				    {
				      char *tspec = getToolSpec(epathname);
				      if(tspec)
					{
					  EZ_Widget *icon = createToolItemIcon(epathname, tspec, x, y);
					  EZ_DisplayWidget(icon);
					}
				    }
				}
                              if(epathname) free(epathname);
			    }
			}
		    }
		  else break;
		}
	    }
	  else if(!strncmp(ptr, "\"Folders\"", 6))
	    {
	      while(1)
		{
		  if( (status = nextLine(line, &lineno, fp)) > 0)
		    {
		      if(matchWord(line,&buf, "endsection"))
			break;	      
		      else
			{
			  char *history[16], storage[16*256], *tptr, *epath;
			  Toplevel  *tp = makeFolders(buf);
			  Folder    *folder;
			  EZ_Widget *oentry;
			  int  i;

			  for(i = 0; i < 16; i++) history[i] = storage + 256 * i;
			  i = 0;
			  status = nextLine(line, &lineno, fp);
			  while(1)
			    {
			      if( (status = nextLine(line, &lineno, fp)) < 0) break;
			      if(matchWord(line,&buf, "endhistory")) break;
			      if(i < 16)
                                {
                                  epath = EZ_TildeExpand(buf);
                                  strcpy(history[i++], epath);
                                  free(epath);
                                }
			    }
			  if(tp && (folder = tp->folder[0]) != NULL && i > 0)
			    {
			      oentry = folder->optentry;
			      EZ_SetOptionalEntryOptions(oentry,history, i);
			    }
			  i = 0;
			  status = nextLine(line, &lineno, fp);
			  while(1)
			    {
			      if( (status = nextLine(line, &lineno, fp)) < 0) break;
			      if(matchWord(line,&buf, "endhistory")) break;
			      if(i < 16)
                                {
                                  epath = EZ_TildeExpand(buf);
                                  strcpy(history[i++], epath);
                                  free(epath);
                                }
			    }
			  if(tp && (folder = tp->folder[1]) != NULL && i > 0)
			    {
			      oentry = folder->optentry;
			      EZ_SetOptionalEntryOptions(oentry,history, i);
			    }
                          if(tp && (folder = tp->folder[0]))
                            {
                              int sorter = (folder->flags & SORT_BY_BITS);
                              folder->flags &= ~SORT_BY_BITS;
                              ezfmSetFolderSorter(folder, sorter);
                            }
			}
		    }
		  if(status < 0) break;
		}
	    }
	  else  if(!strncmp(ptr, "\"ToolBox\"", 8))
	    {
	      if( (status = nextLine(line, &lineno, fp)) > 0)
		{
		  int viewable, x,y,w,h,actPage;
		  if(sscanf(line, "%d %d %d %d %d %d", &viewable, &x, &y, &w, &h, &actPage) == 6)
		    {
		      EZ_ConfigureWidget(ezfmToolBox,
					 EZ_GEOMETRY,        x,y,w,h,
					 EZ_FORGET_GEOMETRY, True,
					 EZ_WMHINTS,         USPosition|PPosition,
					 EZ_MARK_CLEAN,      viewable==0?-1:0, /* */
					 0);
		      while(1)
			{
			  if( (status = nextLine(line, &lineno, fp)) > 0)
			    {
			      if(matchWord(line,&buf, "endsection"))
				break;	      
			      else
				{
				  char bgxpm[128], junk[32];
				  int  idx;
				  if(sscanf(line, "%s %d %s", junk, &idx, bgxpm) == 3)
				    if(strcmp(bgxpm, "nil"))
				      {
					EZ_Widget *thePage = getToolBoxPage(idx);
					if(thePage)
					  {
					    EZ_Widget *wa =(EZ_Widget *)EZ_GetWidgetPtrData(thePage);
					    setObjBgPixmap(wa, bgxpm, NULL);
					  }
				      }
				}
			    }
			  else break;
			}
		      /* restore last active page */
		      {
			EZ_Widget *thePage =  getToolBoxPage(actPage);
			if(thePage) EZ_ActivateNoteBookPage(thePage);
		      }	      
		      if(viewable)
                        {
                          EZ_DisplayWidget(ezfmToolBox);	
                          EZ_MoveWidgetWindow(ezfmToolBox, x, y);
                        }
		    }
		}
	    }
	  else  if(!strncmp(ptr, "\"ToolBar\"", 8))
	    {
	      if( (status = nextLine(line, &lineno, fp)) > 0)
		{
		  int  x,y,w,h;
		  if(sscanf(line, "%d %d %d %d", &x, &y, &w, &h) == 4)
		    {
		      ezfmToolBar = makeToolBar(NULL);
		      EZ_ConfigureWidget(ezfmToolBar,
					 EZ_GEOMETRY,        x,y,w,h,
					 EZ_FORGET_GEOMETRY, True,
					 EZ_WMHINTS,         USPosition|PPosition,
					 0);
		      EZ_DisplayWidget(ezfmToolBar);
		    }
		}
	    }
	  else  if(!strncmp(ptr, "\"Options\"", 8))
	    {
	      if( (status = nextLine(line, &lineno, fp)) > 0)
		{
		  int flags, btns[3];   char xpm[128];
		  if(sscanf(line, "%d %d %d %d %s", &flags,btns, btns+1, btns+2,
			    xpm) == 5)
		    {
		      ezfmFlags = flags;
		      EZ_Btn1 = btns[0]; EZ_Btn2 = btns[1]; EZ_Btn3 = btns[2];
		      if(strcmp(xpm, "nil")) strcpy(ezfmFolderBgPixmap, xpm);
		    }
		}
	    }
	  else
	    {
	      /*fprintf(stderr,"%s: Line %d, unknown section name %s.\n",stateFile,lineno, ptr);*/
	      while((status = seekWord(line, &buf, &lineno, fp,"endsection")) == 0)
		;
	    }
	}
      else break;
    }
  fclose(fp); return(1);
}
/****************************************************************************/
int ezfmReadStateA()
{
  char stateFile[256], *envHome;
  char line[BUFSIZ], copy[BUFSIZ];
  char *buf, *ptr;
  int  i, status, lineno=1;
  FILE *fp;

  envHome = getenv("HOME");
  sprintf(stateFile, "%s/.ezfmState", envHome);
  
  fp = fopen(stateFile, "r");
  if(fp == NULL) return(0);
  
  while(1)
    {
      while((status = seekWord(line, &buf, &lineno, fp, "section")) == 0)
	/* null statement */ ; 
      if(status > 0)
	{
	  ptr = skipSpaces(buf);
	  if(!strncmp(ptr, "\"Transparency\"", 10))
	    {
	      if( (status = nextLine(line, &lineno, fp)) > 0)
                {
                  int i, rgbpp[6];
                  if(sscanf(line, "%d %d %d %d %d", rgbpp,rgbpp+1,rgbpp+2,rgbpp+3,rgbpp+4)==5)
                    {for(i = 0; i < 5; i++) { trgbtd[i] = (float)rgbpp[i];} }
                }
              fclose(fp);
              return(1);
            }
          else
            {
	      while((status = nextLine(line, &lineno, fp)) > 0 &&
                    matchWord(line,&buf, "endsection") == 0)
                /* null statement */ ; 
            }
	}
      else break;
    }
  fclose(fp); 
  return(0);
}
/***********************************************************************************
 *
 * create Folders based on saved state info
 */
static Toplevel *makeFolders(buf) char *buf;
{
  char *p = skipSpaces(buf);
  char *geo, *fdrs[3];
  int  i, cnt = 0, oflag, tbv;
  int  viewable, x,y,w,h;
  Toplevel *tp = NULL;

  if(*p != '[') return(NULL);
  geo = ++p;
  fdrs[0] = fdrs[1] = NULL;
  while(*p != '\0' && cnt < 3)
    {
      if(*p == ']') *p = '\0';
      else if(*p == '[') {fdrs[cnt++] = p+1;}
      p++;
    }
  if(cnt == 0) return(NULL); 
  if(cnt > 2) cnt = 2;

  /* read in the geometry of the toplevel frame */
  if(sscanf(geo, "%d %d %d %d %d %d %d", &viewable, &x, &y, &w, &h, &oflag, &tbv) == 7)
    {
      char     pathname[256], ipath1[256], ipath2[256], bgxpm[128];
      int      type1, type2, sfg, xy1[2], xy2[2], wh1[2], wh2[2];
      Folder   *folder1, *folder2 = NULL;
      char     *epathname;

      sfg = ezfmFlags;
      ezfmFlags = oflag | (sfg & (DONT_CONFIRM_ACT_BIT|DONT_CONFIRM_DND_BIT));
      if(sscanf(fdrs[0], "%d %s %d %d %s %d %d %s", 
		&type1, pathname, wh1, wh1+1, ipath1, xy1, xy1+1, bgxpm) == 8)
	{
          epathname = EZ_TildeExpand(pathname);
	  OpenFolder(epathname,NULL, 0, type1);
	  folder1 = findFolder(epathname);
	  if(folder1) 
	    {
	      tp = folder1->toplevel;
	      if(strcmp(bgxpm, "nil"))
		{
		  EZ_LabelPixmap *pxm = EZ_CreateLabelPixmapFromImageFile(bgxpm);
		  setObjBgPixmap(folder1, bgxpm, pxm);
		}
	      if(tbv == 0) EZ_DeActivateWidget(tp->toolbar);
	    }
          free(epathname);
	}
      if(tp && fdrs[1] && sscanf(fdrs[1], "%d %s %d %d %s %d %d %s",
				 &type2, pathname, wh2, wh2+1,ipath2, xy2, xy2+1, bgxpm) == 8)
	{
          epathname = EZ_TildeExpand(pathname);
	  folder2 = tp->folder[1] = createNewFolder(epathname, tp, 1, type2);
	  updateFolder(tp->folder[1], 0);	  
	  if(tp->folder[1])
	    {
	      if(strcmp(bgxpm, "nil"))
		{
		  EZ_LabelPixmap *pxm = EZ_CreateLabelPixmapFromImageFile(bgxpm);
		  setObjBgPixmap(folder2,   bgxpm, pxm);
		}
	    }
          free(epathname);
	}
      ezfmFlags = sfg;
      if(tp)
	{
	  EZ_Widget *toplevel = tp->toplevel;
	  if(folder2 != NULL)
	    EZ_ConfigureWidget(folder1->frame,
			       EZ_WIDTH,       wh1[0],
			       EZ_FORGET_SIZE, False,
			       0);

	  EZ_ConfigureWidget(toplevel,
			     EZ_GEOMETRY,        x,y,w,h,
			     EZ_WMHINTS,         USPosition,
			     EZ_WM_INITIAL_STATE,(viewable==0 ? IconicState: NormalState),
			     0);	    
	  EZ_DisplayWidget(toplevel);
          EZ_MoveWidgetWindow(toplevel, x, y);

	  /* highlight selection */
	  makeSelection(folder1, ipath1, xy1[0], xy1[1]);	      
	  if(folder2 != NULL) makeSelection(folder2, ipath2, xy2[0], xy2[1]);	      
	}
    }
  return(tp);
}
/***********************************************************************************/
static void makeSelection(folder, path, x, y) Folder *folder; char *path; int x, y;
{
  int type = folder->type;
  FileInfo  *finfo;
  switch(type)
    {
    case ICON_FOLDER:
      {
	if(path[0] != 'n')
	  {
	    int      i, nitems = EZ_GetWorkAreaNumberOfItems(folder->workarea);
	    EZ_Item  *item, **items = EZ_GetWorkAreaData(folder->workarea);
	    for(i = 0; i < nitems; i++)
	      {
		item = items[i];
		finfo = (FileInfo *)EZ_GetItemPtrData(item);
		if(!strcmp(finfo->pathname, path))
		  {
		    int xy[2]; xy[0] = x; xy[1] = y;
		    EZ_WorkAreaSelectItem(folder->workarea, item, xy);
		    break;
		  }
	      }
	  }
	else  EZ_SetWorkAreaXYOffset(folder->workarea, x,y);
      }
    break;
    case LIST_FOLDER:
      {
	if(path[0] != 'n')
	  {
	    int       i, nrows   = EZ_GetFancyListBoxNumberOfRows(folder->flistbox);
	    EZ_Item   *item, **row, ***rows = EZ_GetFancyListBoxData(folder->flistbox);
	    for(i = 1; i < nrows; i++) /* stupid header row */
	      {
		row = rows[i];
		item = row[0];
		finfo = (FileInfo *)EZ_GetItemPtrData(item);
		if(!strcmp(finfo->pathname, path))
		  {
		    int xy[2]; xy[0] = x; xy[1] = y;
		    EZ_FancyListBoxSelectItem(folder->flistbox, item, xy);
		    break;
		  }
	      }
	  }
	else  EZ_SetFancyListBoxXYOffset(folder->flistbox, x, y);
      }
    break;
    case TREE_FOLDER:
      {
	if(path[0] != 'n')
	  {
	    EZ_TreeNode *node, *root = EZ_GetListTreeWidgetTree(folder->listtree);
	    node = EZ_TreeFindNodeUsingProcedure(root,matchTreeNode, path);
	    if(node)
	      {
		int xy[2]; 
		xy[0] = x; xy[1] = y;
		EZ_ListTreeWidgetSelectNode(folder->listtree, node, xy);
	      }
	  }
	else EZ_SetListTreeWidgetXYOffset(folder->listtree, x,  y);
      }
    break;
    default:
      break;
    }
}
/****************************************************************************/
static EZ_TreeNode *matchTreeNode(node, data)
     EZ_TreeNode *node; void *data;
{
  if(node)
    {
      EZ_Item *item = EZ_TreeNodeGetItem(node);
      FileInfo *finfo = (FileInfo *)EZ_GetItemPtrData(item);
      char *path = (char *)data;
      if(!strcmp(path, finfo->pathname)) return(node);
    }
  return(NULL);
}
/****************************************************************************/
#undef _STATE_C_
