/*********************************************************************
 *
 *            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. 
 *
 *************************************************************************/



/***********************************************************************
 *
 * Set the background pixmap for folders
 */
#define _BGN_C_
#include "ezfm.h"
/***********************************************************************/
void setObjBgPixmap    MY_ANSIARGS((void *obj, char *cptr, void *data));
char **listBgPatterns  MY_ANSIARGS((int *cnt));
void setBackdropCB     MY_ANSIARGS((void *obj, void *data));
/***********************************************************************/
static EZ_Widget *toplevel;
static void createToplevel();
/***********************************************************************/
/*
 *  Pops out a dialogue with BGN patterns listed.
 */
void setBackdropCB(widget,data)
     EZ_Widget *widget; void *data;
{
  int i, x0,y0,w0,h0, dw,dh, itmp, w, h, hh;
  Window window;
  EZ_GetWidgetAbsoluteGeometry(widget, &x0, &y0, &w0, &h0);
  createToplevel();
  EZ_SetWidgetPtrData(toplevel, data);
  EZ_MakeWidgetWindows(toplevel);
  window = EZ_GetWidgetWindow(toplevel);
  EZ_GetWidgetDimension(toplevel, &w, &h);

  y0 += h0 + 1;
  EZ_GetWidgetDimension(toplevel, &w, &h);
  dh = (h+9)/10; 
  itmp = 2;

  EZ_ConfigureWidget(toplevel, EZ_X, x0, EZ_Y, y0,
		     EZ_FORGET_GEOMETRY, 1,
		     EZ_MARK_CLEAN,      1,
		     0);

  while(itmp != h)
    {
      itmp += dh;
      if(itmp > h) itmp = h;
      XMoveResizeWindow(EZ_GetDisplay(), window, x0, y0, w, itmp);
      EZ_DisplayWidget(toplevel);
      XSync(EZ_GetDisplay(), False);
      EZ_SitForALittleBit(10000);
    }
  EZ_RaiseWidgetWindow(toplevel);
}

  
/***********************************************************************/
static void ehandler(w, data, ie, xe)
     EZ_Widget *w; void *data; int ie; XEvent *xe;
{
  /* remove all ConfigureNotify events on 'toplevel' */
  if(xe->type == ConfigureNotify) xe->type  = 0;
}
/***********************************************************************/
static void hideToplevel(widget, data)
     void *data; EZ_Widget *widget;
{
  int i, x0,y0,w0,h0, dw,dh, w, h, itmp, hh, xx, yy;
  Window window;
  EZ_GetWidgetAbsoluteGeometry(toplevel, &x0, &y0, &w0, &h0);
  EZ_MakeWidgetWindows(toplevel);
  window = EZ_GetWidgetWindow(toplevel);
  w = w0;
  h = h0;
  dh = (h+9)/10; 
  itmp = h;
  EZ_ConfigureWidget(toplevel, EZ_X, x0, EZ_Y, y0,
		     EZ_FORGET_GEOMETRY, 1,
		     EZ_MARK_CLEAN,      1,
		     0);

  while(itmp > 2)
    {
      itmp -= dh;
      if(itmp < 2) itmp = 2;
      XMoveResizeWindow(EZ_GetDisplay(), window, x0, y0, w, itmp);
      EZ_DisplayWidget(toplevel);
      XSync(EZ_GetDisplay(), False);
      EZ_SitForALittleBit(10000);
    }
  EZ_HideWidget(toplevel);
}
/***********************************************************************/
static void setBgnPtn(widget, data) EZ_Widget *widget; void *data;
{
  void  *obj = EZ_GetWidgetPtrData(toplevel);
  char  *cptr = (char *)EZ_GetWidgetUnknownDataPtr(widget);
  hideToplevel(widget, data);
  if(data != (void *)0xab) setObjBgPixmap(obj, cptr, data);
}
/***********************************************************************/
void setObjBgPixmap(obj, cptr, data) void *obj, *data; char *cptr;
{
  Folder *folder = (Folder *)obj;
  EZ_LabelPixmap  *pixmap = (EZ_LabelPixmap *)data;
  char   *tptr, *dest;
  int    len, white = 0;

  if((int)pixmap == 0xac) {white = 1; pixmap = NULL; }
  else if(pixmap == NULL && cptr) pixmap = EZ_CreateLabelPixmapFromImageFile(cptr);

  if(folderExists(folder))
    {
      EZ_Widget *client;
      if(folder->bgpixmap) (void)EZ_Free(folder->bgpixmap);
      folder->bgpixmap = NULL;
      if(cptr && white == 0)
	{
	  if((tptr = strrchr(cptr, '/')) != NULL) tptr++;
	  else tptr = cptr;
	  len = strlen(tptr) + 1;
	  dest = (char *) EZ_Malloc(len * sizeof(char));
	  strcpy(dest, tptr);
	  folder->bgpixmap = dest;
	}
      client = (folder->workarea != NULL ? folder->workarea :
		(folder->listtree != NULL ? folder->listtree: 
		 (folder->workareaS != NULL ? folder->workareaS : folder->flistbox)));
      
      EZ_ConfigureWidget(client, 
			 EZ_LABEL_PIXMAP,    pixmap, 
                         EZ_TEXT_BACKGROUND, white?  "white": NULL,
			 EZ_MARK_CLEAN,      True, 
			 NULL);
      if(EZ_WidgetIsViewable(client)) EZ_DisplayWidget(client);
    }
  else /* must be a widget */
    {
      EZ_Widget *wgt = (EZ_Widget *)obj;
      if(EZ_WidgetExist(wgt))
	{
	  char     *old;
	  EZ_ConfigureWidget(wgt, 
			     EZ_LABEL_PIXMAP,    pixmap, 
			     EZ_MARK_CLEAN,      True,
                             EZ_TEXT_BACKGROUND, white?  "white": NULL,
			     NULL);
	  if(EZ_WidgetIsViewable(wgt)) EZ_DisplayWidget(wgt);

          old = (char *)EZ_GetWidgetPtrData(wgt);
          if(old) EZ_Free(old);
          EZ_SetWidgetPtrData(wgt, NULL);
	  if(cptr)
	    {
              if(white == 0)
                {
                  if((tptr = strrchr(cptr, '/')) != NULL) tptr++;
                  else tptr = cptr;
                  len = strlen(tptr) + 1;
                  dest = (char *) EZ_Malloc(len * sizeof(char));
                  strcpy(dest, tptr);
                  EZ_SetWidgetPtrData(wgt, dest);
                }
	    }
	}
    }
}
/***********************************************************************/
static void  createToplevel()
{
  if(!EZ_WidgetExist(toplevel))
    {
      char **dirlist;
      EZ_Widget *btn;
      toplevel  = EZ_CreateWidgetXrm(EZ_WIDGET_FRAME, NULL,
				     "DisplayOption", "displayOption",
				     EZ_ORIENTATION,  EZ_VERTICAL,
				     EZ_X, 10, EZ_Y, 10,
				     EZ_TRANSIENT,    1,
				     EZ_EVENT_HANDLER, ehandler, NULL,
				     EZ_WM_HINTS,  USPosition,
                                     /*
                                     EZ_BG_PIXMAP,          bigPixmap, 
                                     EZ_EVENT_HANDLER,      ConfigureNotifyHandler, NULL,
                                     */
				     NULL);

      btn = EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, toplevel,
			    EZ_LABEL_STRING,  "Cancel",
			    EZ_SIZE,          50, 32,
			    EZ_CALLBACK,      setBgnPtn, 0xab,
			    EZ_GRID_CELL_GEOMETRY, 0, 0, 1, 1,
			    NULL);
      btn = EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, toplevel,
			    EZ_LABEL_STRING,  "None",
			    EZ_SIZE,          50, 32,
			    EZ_CALLBACK,      setBgnPtn, NULL,
			    EZ_GRID_CELL_GEOMETRY, 0, 1, 1, 1,
			    NULL);
      btn = EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, toplevel,
			    EZ_LABEL_STRING,  "White",
			    EZ_SIZE,          50, 32,
			    EZ_CALLBACK,      setBgnPtn, 0xac,
			    EZ_GRID_CELL_GEOMETRY, 0, 2, 1, 1,
			    NULL);

      if((dirlist = listBgPatterns(NULL)) != NULL)
	{
	  int i=3, j;
	  for(j = 0; dirlist[j] != NULL; j++)
	    {
	      EZ_LabelPixmap *pxm = EZ_CreateLabelPixmapFromImageFile(dirlist[j]);
	      if(pxm)
		{
		  int len = strlen(dirlist[j]) + 6;
		  EZ_UnknownDataType d, *ptr;
		  char *cptr;
		  btn = EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON, toplevel,
					EZ_IMAGE_FILE,    dirlist[j],
					EZ_SIZE,          32, 32,
					EZ_CALLBACK,      setBgnPtn, pxm,
					EZ_GRID_CELL_GEOMETRY, (i/3), (i%3), 1, 1,
					NULL);
		  d.i = 0;
		  EZ_SetWidgetUnknownData(btn, len/4,  d); 
		  ptr = EZ_GetWidgetUnknownDataPtr(btn);
		  cptr = (char *)ptr;
		  (void)strcpy(cptr, dirlist[j]);
		  i++;
		}
	      (void) free((char *)dirlist[j]);
	    }
	  (void)free((char *)dirlist); 
	}
    }
}
/**********************************************************************************/
char **listBgPatterns(cnt_ret) int *cnt_ret;
{
  char  *tmp, *iconpath, *envlibdir, tmp_name[1024];
  char  pattern[1024];
  char  *p, **dirlist;
  char  **ret_val = (char **)EZ_Malloc(256 * sizeof(char *));
  int   count = 0;

  iconpath = EZ_GetIconPath();
  envlibdir = getenv("EZ_LIBDIR");
  sprintf(tmp_name, "%s:%s", iconpath, envlibdir);
  tmp = tmp_name;
  while(tmp)
    {
      int len;
      p = strchr(tmp, ':');
      if(p) *p++ = '\0';
	      
      strcpy(pattern, tmp);
      len = strlen(pattern);
      if(pattern[len-1] !='/') strcat(pattern, "/BGN*");
      else strcat(pattern, "BGN*");
      dirlist = (char **) EZ_GlobFileName(pattern);
      if(dirlist)
	{
	  int i = 2, j=0;
	  while(dirlist[j] != NULL) 
	    {
	      if(count < 256) ret_val[count++] = dirlist[j++];
	    }
	  (void)free(dirlist);
	}
      tmp = p;
    }
  if(count == 0) {EZ_Free(ret_val); return(NULL);}
  ret_val[count] = NULL;
  qsort(ret_val, count, sizeof(char *), EZ_CmpStr);  
  if(cnt_ret) *cnt_ret = count;
  return(ret_val);
}
/**********************************************************************************/
#undef _BGN_C_
