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

#define _TRANSPARENT_C_
#include "ezfm.h"
/*************************************************************************/
void ezfmSetroot       MY_ANSIARGS((void *obj, void *data));
void ezfmScaleSetroot  MY_ANSIARGS((void *obj, void *data));
/*************************************************************************/
static GC              theGC;                  /* a private GC       */
static Pixmap          rootBgPixmap = None;    /* root bg PixmapID   */
static Pixmap          spixmap;                /* for quick updating */
static EZ_Pixmap       *smallPixmap = NULL;    /* an EZ pixmap       */
static EZ_Widget       *dialogue, *button;
static EZ_Widget       *warningToplevel;

static char            lastRootPathname[256];
static EZ_Pixmap      *lastSetRoot = NULL;
static unsigned char  *lastRootData = NULL;
/*
 * this variable defines the rule to mix bg pixmap with a 
 * specified color      [r     g    b          percentages]
 */
float  trgbtd[6] = { 0.0, 0.0, 0.0, 70.0, 100.0, 0.0}; 
/************************************************************
 *  
 *  Forward declarations 
 */
void ezfmPopupWarning MY_ANSIARGS((char *a, char *b, char *c, char *d));
/************************************************************
 *
 *  This procedure read the root bg pixmap and returns
 *  the pixmap ID or None. The root bg pixmap is registered
 *  as the XROOTPMAP_ID property of the root window.
 */
  
static Pixmap readRootBgPixmap()
{
  Atom            aType;
  int             aFormat, ans;
  unsigned long   bytesAfter, length;
  unsigned char   *data;
  Pixmap          pixmap = None;

  ans = XGetWindowProperty(theDisplay,
                           theRootWindow,
                           XROOTPMAP_ID,
                           0L, 1L,
                           False,
                           AnyPropertyType,
                           &aType,
                           &aFormat,
                           &length,
                           &bytesAfter,
                           &data);
  if((ans == Success) && (aType == XA_PIXMAP))  pixmap = *((Pixmap *) data);
  return(pixmap);
}
/************************************************************
 *
 *  This procedure create a EZWGL internal Pixmap to
 *  be used for bg tiling 
 */
static EZ_Pixmap *makeBgPixmap(Pixmap pixmap, int force)
{
  EZ_Pixmap       *ezpixmap = NULL;

  if(pixmap != None && (pixmap != rootBgPixmap || force))
    {
      int x, y, w=0, h, bw, depth;
      Window root;
      Pixmap cRoot=None, junk;

      EZ_GetLabelPixmapInfo(lastSetRoot, &cRoot, &junk, &w, &h);
      if(pixmap == cRoot)  /* last root was set by myself */
        {
          unsigned char *nrgb;
          int rr = (int)trgbtd[0], gg = (int)trgbtd[1], bb = (int)trgbtd[2];
          int percent = (int)trgbtd[3], brightness = (int)trgbtd[4];
          ezfmPopupWarning(NULL, "Processing Root Background Pixmap", NULL,"This can be VERY slow!");
          XSync(theDisplay, False);
          nrgb = EZ_ScaleRGBX(lastRootData, w,h,w,h,
                              percent, percent, percent, rr, gg, bb, brightness, 1);
          
          ezpixmap = EZ_CreateBackgroundPixmapsFromRawRGB(nrgb, w, h);
          (void)free(nrgb);
          EZ_HideWidget(warningToplevel);
        }
      else
        {
          int   (*OldErrHdlr)();
          xerrCode = 0;
          OldErrHdlr = XSetErrorHandler(xerrHdlr); 
          XGetGeometry(theDisplay, pixmap, &root, &x,&y,&w,&h, &bw, &depth);
          XSetErrorHandler(OldErrHdlr); 
          if(xerrCode == 0)
            {
              ezfmPopupWarning(NULL, "Reading Root Background Pixmap", NULL,"This can be VERY slow!");
              XSync(theDisplay, False);
              XGrabServer(theDisplay);
              if(w)  ezpixmap =EZ_CreateBackgroundPixmapsFromXPixmapX(pixmap,0,0,w,h, (int)trgbtd[3],
                                                                      (int)trgbtd[0],(int)trgbtd[1],
                                                                      (int)trgbtd[2],(int)trgbtd[4]);
              XUngrabServer(theDisplay);
              EZ_HideWidget(warningToplevel);
            }
        }
      rootBgPixmap = pixmap;
      return(ezpixmap);
    }
  return(NULL);
}
/********************************************************************************/
static void setBtnBg(int force)
{
  static int  btnX, btnY;  /* absolute position */
  int         x,y,w,h, dirty = 0;
  EZ_Pixmap   *tmp;

  if(EZ_GetWidgetWindow(button) && rootBgPixmap != None)
    {
      EZ_GetWidgetAbsoluteGeometry(button, &x, &y, &w, &h);
      if(bigPixmap != NULL && (x != btnX || y != btnY) )
        {
          /* first update spixmap */
          XSetTile(theDisplay, theGC, rootBgPixmap);
          XSetTSOrigin(theDisplay, theGC, -x, -y);
          if(spixmap == None) spixmap = XCreatePixmap(theDisplay,rootBgPixmap, w, h, theDepth);
          XFillRectangle(theDisplay, spixmap, theGC, 0,0,w,h);
          dirty = 1;
        }

      if(spixmap && (dirty || force))
        {
          /* then update the internal tiling pixmap */
          tmp = EZ_CreateBackgroundPixmapsFromXPixmapX(spixmap,0,0,w,h, trgbtd[3],
                                                       (int)trgbtd[0],(int)trgbtd[1],
                                                       (int)trgbtd[2],(int)trgbtd[4]);
          /* and finally update the bg tile of button */
          if(smallPixmap == NULL) smallPixmap = tmp;
          else 
            {
              EZ_SwapLabelPixmaps(smallPixmap, tmp);
              EZ_FreeLabelPixmap(tmp);
            }
          btnX = x;  btnY = y;
          EZ_ConfigureWidget(button, EZ_TILE_ORIGIN, 1, 0, 0,
                             EZ_BG_PIXMAP, smallPixmap, 0);
        }
    }
}

/********************************************************************************
 * Registered to dialogue, When the window moves, reset tile origin 
 */
static void eventHandler(EZ_Widget *widget, void *data, int etype, XEvent *xev)
{
  if(xev->type == ConfigureNotify)
    {
      int x, y, w, h, tox, toy;
      EZ_GetWidgetAbsoluteGeometry(widget, &x,&y,&w,&h);
      EZ_GetWidgetTileOrigin(widget, &tox, &toy);
      if(tox != -x || toy != -y)
        {
          EZ_ConfigureWidget(widget, EZ_TILE_ORIGIN, 1, -x, -y, 0);
          setBtnBg(1);
        }
    }
}
/*************************************************************************************/

static void redisplayWidget(EZ_Widget *widget, void *data)
{
  if(EZ_GetParentWidget(widget) == NULL)
    {
      int x, y,w,h;
      if(EZ_WidgetIsViewable(widget))
        {
          EZ_GetWidgetAbsoluteGeometry(widget, &x,&y,&w,&h);
          EZ_ConfigureWidget(widget, EZ_BG_PIXMAP, bigPixmap, 
                             EZ_TILE_ORIGIN, 1, -x, -y,
                             0);
          EZ_DisplayWidget(widget);
        }
      else EZ_ConfigureWidget(widget, EZ_BG_PIXMAP, bigPixmap, 0);
      EZ_AddEventHandler(widget, ConfigureNotifyHandler, NULL, 0);
    }
}
/********************************************************************************
 *
 *  Registered to NULL (events not associated to EZ widgets)
 *  We select PropertyNotify events for the root window.
 *  Whenever the root bg pixmap changes, we update our toplevel widgets.
 */

static void proptyNotifyHandler(EZ_Widget *widget, void *data, int etype, XEvent *xev)
{
  if(xev->type == PropertyNotify)
    {
      /*
      printf("PropertyNotify: %lx %lx %d\n",xev->xproperty.window, xev->xproperty.atom,
              xev->xproperty.state );
              */
      if( (xev->xproperty.window == theRootWindow) && (xev->xproperty.atom == XROOTPMAP_ID))
        {
          if((xev->xproperty.state == PropertyNewValue))
            {
              EZ_Pixmap *tmp;
              Pixmap pixmap = readRootBgPixmap();
              if(pixmap)
                {
                  if( (tmp= makeBgPixmap(pixmap, 0)) != NULL)
                    {
                      if(bigPixmap == NULL)  { bigPixmap = tmp; }
                      else
                        {
                          EZ_SwapLabelPixmaps(bigPixmap,tmp);
                          EZ_FreeLabelPixmap(tmp);
                        }
                      EZ_TraverseAllWidgets(redisplayWidget, NULL);
                      setBtnBg(1);
                    }
                }
              else rootBgPixmap = None;                  
            }
          else rootBgPixmap = None;
        }
    }
}

/********************************************************************************
 *
 * slider callback.
 */
static void sliderCB(EZ_Widget *slider, void *data)
{
  int          x,y,w,h;
  char         str[16];
  float        f = EZ_GetSliderValue(slider);
  int          i = EZ_GetWidgetIntData(slider);

  trgbtd[i] = f;
  if(i < 3)
    {
      sprintf(str, "%03d", (int)f);
      EZ_ConfigureWidget(slider, EZ_LABEL_STRING, str, 0);
      sprintf(str, "#%02x%02x%02x",(int)trgbtd[0], (int)trgbtd[1], (int)trgbtd[2]);
      EZ_ConfigureWidget(data, EZ_FOREGROUND, str, 0);
    }
  setBtnBg(1);
  trgbtd[5] = 1.0;
}
/********************************************************************************
 *
 * Button callback.
 */
static void btnCB(EZ_Widget *widget, void *data)
{
  EZ_Pixmap *tmp;
  if(trgbtd[5] == 0.0) return;
  if((tmp = makeBgPixmap(rootBgPixmap, 1)) != NULL)
    {
      trgbtd[5] = 0.0;
      EZ_SwapLabelPixmaps(bigPixmap,tmp);
      EZ_FreeLabelPixmap(tmp);
      EZ_TraverseAllWidgets(redisplayWidget, NULL);
    }
  EZ_HideWidget(data);
}
/********************************************************************************/
static void closeCB(EZ_Widget *widget, void *data) { EZ_HideWidget(data);}
/********************************************************************************/

void makeDialogue()
{
  EZ_Widget     *slider, *glass, *btn;
  
  dialogue =    EZ_CreateWidgetXrm(EZ_WIDGET_FRAME,       NULL,
                                   "dialogue",           "Dialogue",
                                   EZ_LABEL_STRING,      "Transparency ...",
                                   EZ_EVENT_HANDLER,      eventHandler, NULL,
                                   EZ_SIZE,               400, 300,
                                   EZ_IPADX,              10,
                                   EZ_IPADY,              10,
                                   EZ_BG_PIXMAP,          bigPixmap,
				   EZ_GRID_CONSTRAINS,    EZ_COLUMN, 0, 100, 100, 0,
				   EZ_GRID_CONSTRAINS,    EZ_COLUMN, 1, 100, 100, 0,
				   EZ_GRID_CONSTRAINS,    EZ_ROW,    1, 100, 100, 0,
				   EZ_GRID_CONSTRAINS,    EZ_ROW,    2, 10, 0,   0,
				   EZ_GRID_CONSTRAINS,    EZ_ROW,    3, 10, 0,   0,
                                   EZ_TRANSIENT,          True,
                                   0);

  button =      EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON,  dialogue,
                                EZ_SIZE,                  128, 128,
                                EZ_BORDER_WIDTH,          4,
                                EZ_PROPAGATE,             0,
                                EZ_LABEL_STRING,          "Apply",
                                EZ_FOREGROUND,            "white",
                                EZ_FONT_NAME,             "-adobe-helvetica-bold-r-normal-*-*-240-*-*-*-*-*-*",
                                EZ_CALLBACK,              btnCB, dialogue,
                                EZ_GRID_CELL_GEOMETRY,    0, 1, 1, 1,
                                0);
  
  btn =         EZ_CreateWidget(EZ_WIDGET_NORMAL_BUTTON,  dialogue,
                                EZ_PROPAGATE,             0,
                                EZ_LABEL_STRING,          "Close",
                                EZ_UNDERLINE,              0,
                                EZ_FOREGROUND,            "red",
                                EZ_CALLBACK,              closeCB, dialogue,
                                EZ_GRID_CELL_GEOMETRY,    0, 3, 2, 1,
                                0);

  glass =       EZ_CreateWidget(EZ_WIDGET_FRAME,          dialogue,
                                EZ_LABEL_STRING,          "Glass color",
                                EZ_FILL_MODE,             EZ_FILL_BOTH,
                                EZ_GRID_CELL_GEOMETRY,    1, 0, 1, 2,
                                EZ_GRID_CELL_PLACEMENT,   EZ_FILL_BOTH, EZ_CENTER, 
                                0);

  slider = EZ_CreateWidget(EZ_WIDGET_VERTICAL_SLIDER,  glass,
                           EZ_SLIDER_RESOLUTION,       1.0,
                           EZ_PADX,                    0,
                           EZ_PADY,                    0,
                           EZ_FONT_NAME,               "-*-lucidatypewriter-bold-r-*-*-12-*-*-*-*-*-*-*",
                           EZ_SLIDER_RANGE,            0.0, 255.0,
                           EZ_SLIDER_INIT_VALUE,       trgbtd[0],
                           EZ_FOREGROUND,              "red",
                           EZ_SLIDER_DISPLAY_VALUE,    0,
                           EZ_LABEL_STRING,            "red",
                           EZ_CALLBACK,                sliderCB, glass,
                           EZ_CLIENT_INT_DATA,         0,
                           EZ_WIDTH,                   0,
                           0);

  slider = EZ_CreateWidget(EZ_WIDGET_VERTICAL_SLIDER, glass,
                           EZ_SLIDER_RESOLUTION,      1.0,
                           EZ_PADX,                   0,
                           EZ_PADY,                   0,
                           EZ_FONT_NAME,              "-*-lucidatypewriter-bold-r-*-*-12-*-*-*-*-*-*-*",
                           EZ_SLIDER_RANGE,           0.0, 255.0,
                           EZ_SLIDER_INIT_VALUE,      trgbtd[1],
                           EZ_FOREGROUND,             "green",
                           EZ_LABEL_STRING,           "grn",
                           EZ_SLIDER_DISPLAY_VALUE,   0,
                           EZ_CLIENT_INT_DATA,        1,
                           EZ_CALLBACK,               sliderCB, glass,
                           EZ_WIDTH,                  0,
                           0);

  slider = EZ_CreateWidget(EZ_WIDGET_VERTICAL_SLIDER, glass,
                           EZ_SLIDER_RESOLUTION,      1.0,
                           EZ_PADX,                   0,
                           EZ_PADY,                   0,
                           EZ_FONT_NAME,              "-*-lucidatypewriter-bold-r-*-*-12-*-*-*-*-*-*-*",
                           EZ_SLIDER_RANGE,           0.0, 255.0,
                           EZ_SLIDER_INIT_VALUE,      trgbtd[2],
                           EZ_FOREGROUND,             "blue",
                           EZ_LABEL_STRING,           "blu",
                           EZ_SLIDER_DISPLAY_VALUE,   0,
                           EZ_CLIENT_INT_DATA,        2,
                           EZ_CALLBACK,               sliderCB, glass,
                           EZ_WIDTH,                  0,
                           0);

  
  slider = EZ_CreateWidget(EZ_WIDGET_HORIZONTAL_SLIDER, dialogue,
                           EZ_SLIDER_RESOLUTION,      1.0,
                           EZ_PADX,                   0,
                           EZ_PADY,                   0,
                           EZ_FONT_NAME,              "-*-lucidatypewriter-bold-r-*-*-12-*-*-*-*-*-*-*",
                           EZ_SLIDER_RANGE,           0.0, 100.0,
                           EZ_SLIDER_INIT_VALUE,      trgbtd[3],
                           EZ_SLIDER_DISPLAY_VALUE,   0,
                           EZ_LABEL_STRING,           "Transparency",
                           EZ_CLIENT_INT_DATA,         3,
                           EZ_CALLBACK,                sliderCB, NULL,
                           EZ_GRID_CELL_GEOMETRY,      0, 2, 1, 1,
                           EZ_GRID_CELL_PLACEMENT,     EZ_FILL_BOTH, EZ_CENTER, 
                           0);
  slider = EZ_CreateWidget(EZ_WIDGET_HORIZONTAL_SLIDER, dialogue,
                           EZ_SLIDER_RESOLUTION,      1.0,
                           EZ_PADX,                   0,
                           EZ_PADY,                   0,
                           EZ_FONT_NAME,              "-*-lucidatypewriter-bold-r-*-*-12-*-*-*-*-*-*-*",
                           EZ_SLIDER_RANGE,           0.0, 100.0,
                           EZ_SLIDER_INIT_VALUE,      trgbtd[4],
                           EZ_SLIDER_DISPLAY_VALUE,   0,
                           EZ_LABEL_STRING,           "Darken Factor",
                           EZ_CLIENT_INT_DATA,         4,
                           EZ_CALLBACK,                sliderCB, NULL,
                           EZ_GRID_CELL_GEOMETRY,      1, 2, 1, 1,
                           EZ_GRID_CELL_PLACEMENT,     EZ_FILL_BOTH, EZ_CENTER, 
                           0);
}
/********************************************************************************/
static char *transparentResource = 
"
*tileOrigin: 2 0 0
*fileIcon.labeledIcon: 3
"
;
/********************************************************************************/
void setupTransparencyDialogue(flag) int flag;
{
  unsigned long valuemask; 
  XGCValues     values;  
  XrmDatabase   dbt;

  if(flag)
    {
      valuemask = GCFillStyle;
      values.fill_style = FillTiled;
      theGC =  XCreateGC(theDisplay, theRootWindow, valuemask, &values);  
      EZ_SelectInput(theRootWindow, PropertyChangeMask);
      EZ_AddEventHandler(NULL, proptyNotifyHandler, NULL, 0);
      
      dbt = XrmGetStringDatabase(transparentResource);
      XrmCombineDatabase(dbt, EZ_GetResourceDatabase(), 1);      
      lastRootPathname[0] = 0;
      bigPixmap = makeBgPixmap(readRootBgPixmap(), 0);
    }
}
/********************************************************************************/
void doTransparencyDialogue(EZ_Widget *widget, void *data)
{
  if(transparent == 0)
    ezfmWarning("You have to start ezfm with the -transparent option to",
                "use the pseudo-transparency feature");  
  else if(readRootBgPixmap() == None)
    ezfmWarning("You don't have a root background pixmap installed",
                "Click the right mouse button on an image file to install one");  
  else
    {
      if(dialogue == NULL)  makeDialogue(); 
      EZ_DisplayWidget(dialogue);
      EZ_RaiseWidgetWindow(dialogue);
    }
}
/********************************************************************************/
static EZ_Widget *warningToplevel = NULL;

void ezfmPopupWarning(lab1, lab2, lab3, lab4)
     char *lab1, *lab2, *lab3, *lab4;
{
  EZ_Widget           *frame, *tmp;
  EZ_UnknownDataType  ud;

  if(! EZ_WidgetExist(warningToplevel))
    {
      frame = EZ_CreateWidgetXrm(EZ_WIDGET_FRAME,       NULL,
				 "WarningA", "warningA",
				 EZ_TRANSIENT,          True,
				 EZ_FONT_ID,            ezfmFontId[LABEL_LARGE_FONT],
				 EZ_ORIENTATION,        EZ_VERTICAL,
				 EZ_FILL_MODE,          EZ_FILL_BOTH,
				 EZ_PADX,               20,
				 EZ_PADY,               10,
				 EZ_IPADY,              10,
				 EZ_SIZE,               0, 0,
				 EZ_LABEL_STRING,       "Ezfm Warning",
				 EZ_BG_PIXMAP,          bigPixmap, 
				 EZ_EVENT_HANDLER,      ConfigureNotifyHandler, NULL,
				 NULL);
      warningToplevel = frame;
      {ud.p = NULL; EZ_SetWidgetUnknownData(frame, 4, ud);}
      
      tmp =   EZ_CreateWidget(EZ_WIDGET_LABEL,       frame,
			      EZ_FONT_ID,            ezfmFontId[LABEL_MEDIUM_FONT],
			      EZ_LABEL_STRING,       lab1,
			      EZ_TEXT_LINE_LENGTH,   100,
			      EZ_PADY,               0,
			      NULL);
      { ud.p  = tmp;   EZ_SetWidgetUnknownData(frame, 0, ud);}

      tmp =   EZ_CreateWidget(EZ_WIDGET_LABEL,       frame,
			      EZ_FONT_ID,            ezfmFontId[TEXT_BOLD_FONT],
			      EZ_FOREGROUND,         "red",
			      EZ_LABEL_STRING,       lab2,
			      EZ_PADY,               0,
			      EZ_TEXT_LINE_LENGTH,   100,
			      NULL);
      { ud.p  = tmp;   EZ_SetWidgetUnknownData(frame, 1, ud);}
      tmp =   EZ_CreateWidget(EZ_WIDGET_LABEL,       frame,
			      EZ_FONT_ID,            ezfmFontId[LABEL_MEDIUM_FONT],
			      EZ_LABEL_STRING,       lab3,
			      EZ_PADY,               0,
			      EZ_TEXT_LINE_LENGTH, 100,
			      NULL);
      { ud.p  = tmp;   EZ_SetWidgetUnknownData(frame, 2, ud);}
      tmp =   EZ_CreateWidget(EZ_WIDGET_LABEL,       frame,
			      EZ_FONT_ID,            ezfmFontId[TEXT_BOLD_FONT],
			      EZ_LABEL_STRING,       lab4,
			      EZ_FOREGROUND,         "blue",
			      EZ_PADY,               0,
			      EZ_TEXT_LINE_LENGTH,   100,
			      NULL);
      { ud.p  = tmp;   EZ_SetWidgetUnknownData(frame, 3, ud);}
    }
  /* setup error string */
  {
    EZ_UnknownDataType *ptr = EZ_GetWidgetUnknownDataPtr(warningToplevel);
    tmp = (EZ_Widget *)ptr[0].p;
    EZ_ConfigureWidget(tmp, EZ_LABEL_STRING, lab1, NULL);
    tmp = (EZ_Widget *)ptr[1].p;
    EZ_ConfigureWidget(tmp, EZ_LABEL_STRING, lab2, NULL);
    tmp = (EZ_Widget *)ptr[2].p;
    EZ_ConfigureWidget(tmp, EZ_LABEL_STRING, lab3, NULL);
    tmp = (EZ_Widget *)ptr[3].p;
    EZ_ConfigureWidget(tmp, EZ_LABEL_STRING, lab4, NULL);
    tmp = (EZ_Widget *)ptr[4].p;
    EZ_DisplayWidgetUnderPointer(warningToplevel, -1, -1);
  }
}
/***************************************************************************/  
static setroot(obj, data, scale)
     void *obj, *data; int scale;
{
  FileInfo *finfo = (FileInfo *)data;
  if(finfo)
    {
      char          *fname = finfo->pathname;
      unsigned char *rgbdata = NULL, *newrgb = NULL;
      int sW, sH, iheight, iwidth;
      EZ_Pixmap  *theImage = NULL;
      
      if(!strcmp(fname, lastRootPathname) && scale == (int) lastRootPathname[254]) return;
      
      if(EZ_ReadImageFile(fname, &iwidth, &iheight, &rgbdata))
        {
          if(scale)
            {
              EZ_GetDisplaySize(&sW, &sH);
              newrgb = EZ_ScaleRGB(rgbdata, iwidth, iheight, sW, sH, 100, 1);
              if(newrgb)
                {
                  (void) free(rgbdata);
                  rgbdata = newrgb;
                  iwidth = sW;
                  iheight = sH;
                }
            }
          theImage = EZ_CreateLabelPixmapFromRawRGB(rgbdata, iwidth, iheight);
        }
      if(theImage == NULL)
        {
          if(rgbdata) (void)free(rgbdata);
          ezfmWarning("Couldn't read image", fname);
        }
      else
        {
          Pixmap         pixmap=None, shape;
          int            width=0,  height=0;
          
          EZ_GetLabelPixmapInfo(theImage, &pixmap, &shape, &width, &height);
          /* this is unfortunately the only correct way */
          XGrabServer(theDisplay);
          XChangeProperty(theDisplay, theRootWindow, XROOTPMAP_ID, XA_PIXMAP, 32, 
                          PropModeReplace, (unsigned char *) &pixmap, 1);
          XUngrabServer(theDisplay);
          
          XFlush(theDisplay);
          XSetWindowBackgroundPixmap(theDisplay, theRootWindow, pixmap);
          XClearWindow(theDisplay, theRootWindow);
          XFlush(theDisplay);

          /* remember the root pixmap */
          strcpy(lastRootPathname, fname); 
          lastRootPathname[254] = (char)scale;
          if(lastRootData) (void)free(lastRootData);
          lastRootData = rgbdata;
          if(lastSetRoot) EZ_FreeLabelPixmap(lastSetRoot);
          lastSetRoot = theImage;
        }
    }
}
/***************************************************************************/  
void ezfmSetroot(obj, data) void *obj; void *data;
{ setroot(obj, data, 0);}
/***************************************************************************/  
void ezfmScaleSetroot(obj, data) void *obj; void *data;
{ setroot(obj, data, 1);}
/***************************************************************************/  
#undef _TRANSPARENT_C_

