/* %Z%%M% %I% %E% */

/*
 * Copyright (c) 1994, 1995 by Wayne C. Gramlich.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * for any purpose is hereby granted without fee provided that the above
 * copyright notice and this permission are retained.  The author makes
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/*
 * This file provide a bunch of interface routines for accessing objects
 * of type Display from Xlib from STIPPLE code.
 */

#ifndef XLIB_H
#include "xlib.h"
#endif

/* The initial object: */
Xlib_pixmap_struct xlib_pixmap___initial_object = {
    1,
    0,
    0,
    (Pixmap)0,
    0,
    &xlib_screen___initial_object
};
Xlib_pixmap xlib_pixmap___initial = &xlib_pixmap___initial_object;

/*
 * xlib_pixmap__address_get(xlib_pixmap)
 *	This routine will return the address of {pixmap}.
 */
unsigned
xlib_pixmap__address_get(
    Xlib_pixmap xlib_pixmap)
{
    return (unsigned)xlib_pixmap;
}

/*
 * xlib_pixmap__bitmap_file_read(screen, file_name)
 *	This routine will read the bitmap information from the file named
 *	{file_name} and return the corresponding bitmap information
 *	in an {Xlib_bitmap} associated with {screen}.
 */
Xlib_pixmap
xlib_pixmap__bitmap_file_read(
    Xlib_screen screen,
    String file_name)
{
    Xlib_display display;
    Xlib_pixmap pixmap;

    pixmap = xlib_pixmap___initial;
    display = screen->display;
    if (display->closed) {
	xlib_display___closed(display);
    } else {
	unsigned height;
	int hot_x;
	int hot_y;
	Str name;
	Pixmap pix;
	int result;
	unsigned width;

	name = string__unix_string(file_name);
	result = XReadBitmapFile(display->display,
	  (Drawable)screen->root_window->window, name,
	  &width, &height, &pix, &hot_x, &hot_y);
	switch (result) {
	  case BitmapSuccess:
	    pixmap = (Xlib_pixmap)malloc(sizeof(*pixmap));
	    pixmap->closed = 0;
	    pixmap->depth = 1;
	    pixmap->height = height;
	    pixmap->pixmap = pix;
	    pixmap->width = width;
	    pixmap->screen = screen;
	    break;
	  case BitmapOpenFailed:
	    pixmap = pixmap;	/* Plant breakpoint here */
	    break;
	  case BitmapFileInvalid:
	    pixmap = pixmap;	/* Plant breakpoint here */
	    break;
	  case BitmapNoMemory:
	    pixmap = pixmap;	/* Plant breakpoint here */
	    break;
	  default:
	    pixmap = pixmap;	/* Plant breakpoint here */
	}
    }
    return pixmap;
}

/*
 * xlib_pixmap__close(pixmap)
 *	This routine will make {pixmap} unusable.
 */
void
xlib_pixmap__close(
    Xlib_pixmap pixmap)
{
    Xlib_display display;

    display = pixmap->screen->display;
    if (display->closed) {
	xlib_display___closed(display);
    } else {
	XFreePixmap(display->display, pixmap->pixmap);
    }
    pixmap->closed = 1;
    pixmap->depth = 1;
    pixmap->height = 0;
    pixmap->pixmap = (Pixmap)0;
    pixmap->width = 0;
    pixmap->screen = xlib_screen___initial;
}

/*
 * xlib_pixmap__closed_get(xlib_pixmap)
 *	This routine will return 1 if the connection to the X server
 *	is closed and 0 otherwise.
 */
int
xlib_pixmap__closed_get(
    Xlib_pixmap xlib_pixmap)
{
    return xlib_pixmap->closed;
}

/*
 * xlib_pixmap__create(screen, width, height, depth)
 *	This routine will create and return an {Xlib_pixmap}
 *	that is associated with {screen} and is	{width} pixels
 *	wide, {height} pixels high, and {debth} bits per pixel.
 */
Xlib_pixmap
xlib_pixmap__create(
    Xlib_screen screen,
    unsigned width,
    unsigned height,
    unsigned depth)
{
    Xlib_display display;
    Xlib_pixmap pixmap;

    pixmap = xlib_pixmap___initial;
    display = screen->display;
    if (display->closed) {
	xlib_display___closed(display);
    } else {
	Pixmap pix;

	pix = XCreatePixmap(display->display,
	  (Drawable)screen->root_window->window, width, height, depth);
	pixmap = (Xlib_pixmap)malloc(sizeof(*pixmap));
	pixmap->closed = 0;
	pixmap->depth = depth;
	pixmap->height = height;
	pixmap->pixmap = pix;
	pixmap->screen = screen;
	pixmap->width = width;
    }
    return pixmap;
}

/*
 * xlib_pixmap__depth_get(xlib_pixmap)
 *	This routine will return the depth of {xlib_display} is pixels.
 */
unsigned
xlib_pixmap__depth_get(
    Xlib_pixmap xlib_pixmap)
{
    return xlib_pixmap->depth;
}

/*
 * xlib_pixmap__external__initialize()
 *	This routine will "initialize" the {xlib_pixmap} external
 *	code.
 */
void
xlib_pixmap__external__initialize(void)
{
    assert(xlib_pixmap___initial == &xlib_pixmap___initial_object);
}

/*
 * xlib_pixmap__height_get(xlib_pixmap)
 *	This routine will return the height of {xlib_display} is pixels.
 */
unsigned
xlib_pixmap__height_get(
    Xlib_pixmap xlib_pixmap)
{
    return xlib_pixmap->height;
}

/*
 * xlib_pixmap__pixmap_number_get(pixmap)
 *	This  routine will return the pixmap number associated with {pixmap}.
 */
unsigned
xlib_pixmap__pixmap_number_get(
    Xlib_pixmap pixmap)
{
    return pixmap->pixmap;
}

/*
 * xlib_pixmap__width_get(xlib_pixmap)
 *	This routine will return the width of {xlib_display} is pixels.
 */
unsigned
xlib_pixmap__width_get(
    Xlib_pixmap pixmap)
{
    return pixmap->width;
}

/*
 * xlib_pixmap__screen_get(pixmap)
 *	This routine will return the {xlib_screen} associated with
 *	{pixmap}.
 */
Xlib_screen
xlib_pixmap__screen_get(
    Xlib_pixmap pixmap)
{
    return pixmap->screen;
}



