/*
 *  WindowMaker window manager
 * 
 *  Copyright (c) 1997 Alfredo K. Kojima
 * 
 *  This program 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 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "wconfig.h"

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef XPM
#include <X11/xpm.h>
#endif

#include <wraster.h>

#include <stdlib.h>
#include <string.h>
#include "generic/wwmlib.h"
#include "WindowMaker.h"
#include "wcore.h"



#ifdef XPM
/*
 *---------------------------------------------------------------------- 
 * wPixmapCreateFromXPMData--
 * 	Creates a WPixmap structure and initializes it with the supplied
 * XPM structure data.
 * 
 * Returns:
 * 	A WPixmap structure or NULL on failure.
 * 
 * Notes:
 * 	DEF_XPM_CLOSENESS specifies the XpmCloseness
 *---------------------------------------------------------------------- 
 */
WPixmap *
wPixmapCreateFromXPMData(WScreen *scr, char **data)
{
    WPixmap *pix;
    XpmAttributes attribs;
    
    pix = wmalloc(sizeof(WPixmap));
    pix->shared = 0;
    attribs.valuemask = XpmSize|XpmReturnPixels|XpmColormap|XpmCloseness;
    attribs.closeness = DEF_XPM_CLOSENESS;
    attribs.colormap = scr->colormap;
    if (XpmCreatePixmapFromData(dpy, scr->w_win, data, &(pix->image),
			    &(pix->mask), &attribs)!=XpmSuccess) {
	wfree(pix);
	return NULL;
    }
    pix->width = attribs.width;
    pix->height = attribs.height;
    pix->depth = scr->w_depth;
    return pix;
}
#endif /* XPM */


/*
 *---------------------------------------------------------------------- 
 * wPixmapCreateFromXBMData--
 * 	Creates a WPixmap structure and initializes it with the supplied
 * XBM structure data, size and mask.
 * 
 * Returns:
 * 	A WPixmap structure or NULL on failure.
 * 
 *---------------------------------------------------------------------- 
 */
WPixmap *
wPixmapCreateFromXBMData(WScreen *scr, char *data, char *mask, 
			 int width, int height, unsigned long fg,
			 unsigned long bg)
{
    WPixmap *pix;
    
    pix = wmalloc(sizeof(WPixmap));
    pix->shared = 0;
    pix->image = XCreatePixmapFromBitmapData(dpy, scr->w_win, data, width, 
					     height, fg, bg, scr->w_depth);
    if (pix->image==None) {
	wfree(pix);
	return NULL;
    }
    if (mask) {
	pix->mask = XCreateBitmapFromData(dpy, scr->w_win, mask, width, 
					  height);
    } else {
	pix->mask = None;
    }
    pix->width = width;
    pix->height = height;
    pix->depth = scr->w_depth;
    return pix;
}


WPixmap*
wPixmapCreate(WScreen *scr, Pixmap image, Pixmap mask)
{
    WPixmap *pix;
    Window foo;
    int bar;
    unsigned int width, height, depth, baz;
    
    pix = wmalloc(sizeof(WPixmap));
    pix->shared = 0;
    pix->image = image;
    pix->mask = mask;
    XGetGeometry(dpy, image, &foo, &bar, &bar, &width, &height, &baz, &depth);
    pix->width = width;
    pix->height = height;
    pix->depth = depth;
    return pix;
}

#if 0
/*
 *---------------------------------------------------------------------- 
 * wPixmapLoadXBMFile--
 * 	Creates a WPixmap structure and loads a XBM file into it with
 * an optional mask file. If a mask is not wanted, mask_path should be
 * NULL.
 * 
 * Returns:
 * 	A WPixmap structure or NULL on failure.
 * 
 * Notes:
 * 	If the mask bitmap is not successfully loaded the operation
 * continues as no mask was supplied.
 *---------------------------------------------------------------------- 
 */
WPixmap *
wPixmapLoadXBMFile(WScreen *scr, char *path, char *mask_path)
{
    WPixmap *pix;
    int junk;

    if (!path) return NULL;
    pix = wmalloc(sizeof(WPixmap));
    
    pix->shared = 0;
    if (XReadBitmapFile(dpy, scr->w_win, path, (unsigned *)&(pix->width), 
			(unsigned *)&(pix->height),
			&(pix->image), &junk, &junk)!=BitmapSuccess) {
	wfree(pix);
	return NULL;
    }
    if (mask_path!=NULL) {
	if (XReadBitmapFile(dpy, scr->w_win, path, (unsigned *)&junk, 
			    (unsigned *)&junk, &(pix->mask), 
			    &junk, &junk) !=BitmapSuccess) {
	    wWarning(_("could not load mask bitmap file \"%s\". Won't use mask"),
		     mask_path);
	    pix->mask = None;
	}
    } else {
	pix->mask = None;
    }
    pix->depth = 1;
    return pix;
}

#endif 

/*
 *---------------------------------------------------------------------- 
 * wPixmapDestroy--
 * 	Destroys a WPixmap structure and the pixmap/mask it holds.
 * 
 * Returns:
 * 	None
 *----------------------------------------------------------------------
 */
void 
wPixmapDestroy(WPixmap *pix)
{
    if (!pix->shared && !pix->client_owned) {
	if (pix->mask)
	  XFreePixmap(dpy, pix->mask);
	if (pix->image)
	  XFreePixmap(dpy, pix->image);
    }
    wfree(pix);
}

