/* 
   PXKImage.m

   NSImage for GNUstep GUI X/DPS Backend.

   The Image class is an object wrapper around X pixmaps.
   It is used among other things for storing the system
   icons, such as arrows, dimple, pop-up, etc.

   Copyright (C) 1996 Free Software Foundation, Inc.

   Author:  Pascal Forget <pascal@wsc.com>
   Date: October 1995
   
   This file is part of the GNUstep GUI X/DPS Backend.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library 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
   Library General Public License for more details.

   If you are interested in a warranty or support for this source code,
   contact Scott Christley <scottc@net-community.com> for more information.
   
   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ 

#include <gnustep/xdps/PXKImage.h>
#include <gnustep/xdps/PXKView.h>
#include <gnustep/xdps/PXKScreen.h>

NSMutableArray *_NSImages;

@implementation PXKImage

+ initialize;
{
    if (!_NSImages) {
	_NSImages = [[NSMutableArray alloc] initWithCapacity:15];
    }
    
    return self;
}

- drawInView:(NSView *)view at:(const NSPoint *)location;
{
    const NSPoint *p = location;

    XPutImage(pxkDisplay, [(PXKView *)view xWindow], pxkBlackGC, [self xImage],
	      0,0, p[0].x, p[0].y, image_size.width, image_size.height);

    return self;
}

- buildImageFromString:(const char *)image;
{
    char buf[15];
    int  version;
    int  width;
    int  height;
    int  x, y;
    char pixel;
    XColor c1, c2;
    unsigned int white = WhitePixel(pxkDisplay, [(PXKScreen *)
						    [NSScreen mainScreen]
						    xScreen]);
    unsigned int lightgray;
    unsigned int darkgray;
    unsigned int black = BlackPixel(pxkDisplay, [(PXKScreen *)[NSScreen mainScreen] xScreen]);

    strncpy(buf, image+12, 4);
    width = atoi(buf);

    strncpy(buf, image+16, 4);
    height = atoi(buf);

#if 0
    fprintf(stdout, "width: %d  height: %d\n", width, height);
#endif
    
    XAllocNamedColor(pxkDisplay,
		     XDefaultColormap(pxkDisplay, [(PXKScreen *)[NSScreen mainScreen] xScreen]),
		     "light gray", &c1, &c2);

    lightgray = c1.pixel;
    
    XAllocNamedColor(pxkDisplay,
		     XDefaultColormap(pxkDisplay, [(PXKScreen *)[NSScreen mainScreen] xScreen]),
		     "dark gray", &c1, &c2);

    darkgray = c1.pixel;

    for (y=0; y<height; y++) {
	for (x=0; x<width; x++) {

	    pixel = image[20 + x + y*width]; 
#if 0
	    fprintf(stdout, "%c", pixel);
#endif
	    switch(pixel) {
	      case 'g': XPutPixel([self xImage], x, y, lightgray); break;
	      case 'b': XPutPixel([self xImage], x, y, black); break;
	      case 'G': XPutPixel([self xImage], x, y, darkgray); break;
	      case 'w': XPutPixel([self xImage], x, y, white); break;
	      default: break;
	    }
	}
#if 0
	fprintf(stdout, "\n");
#endif
    }
    return self;
}
    
- initWithSize:(const NSSize *)initialSize;
{
    int x, y;
    XColor c1, c2;
    const NSSize *si = initialSize;
    XImage *xImage;

    [super init];
    image_size = si[0];

#if 0    
    puts("NSImage: initWithSize:");
#endif
    
    /*
     * This is the only way I found to reliably create an x image.
     */    
    xImage = XGetImage(pxkDisplay, RootWindow(pxkDisplay, [(PXKScreen *)[NSScreen mainScreen] xScreen]),
		       0,0, image_size.width, image_size.height, 
		       AllPlanes, XYPixmap);

    /*
     * Fill the image with the default color, lightgray
     */
    XAllocNamedColor(pxkDisplay,
		     XDefaultColormap(pxkDisplay, [(PXKScreen *)[NSScreen mainScreen] xScreen]),
		     "light gray", &c1, &c2);

    for (x=0; x<image_size.width; x++) {
	for (y=0; y<image_size.height; y++) {
	    XPutPixel([self xImage], x, y, c1.pixel);
	}
    }
    
#if 0
    XPutPixel(xImage, 5,5, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 5,6, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 5,7, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 5,8, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 6,5, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 6,6, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 6,7, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 6,8, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 7,5, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 7,6, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 7,7, BlackPixel(NSDisplay, NSScreen));
    XPutPixel(xImage, 7,8, BlackPixel(NSDisplay, NSScreen));
#endif
    
    [_NSImages addObject:self];    
    
    return self;
}

- initFromBitmap:(char *)bits size:(const NSSize *)newSize;
{
    const NSSize *si = newSize;

    [super init];
    image_size = si[0];
  
    /* See X book number 1, page 157-158 */    

    fprintf(stdout, "w: %f h:%f\n", image_size.width, image_size.height);
#if 0
    xPixmap = XCreateBitmapFromData(NSDisplay,
				    RootWindow(NSDisplay, NSScreen),
				    _NSRightArrow_bitmap,
				    15, 15);
#endif
    puts("Pixmap created");
    
    [_NSImages addObject:self];    
    puts("image added");
    return self;
}
    
- initFromPath:(const char *)filename size:(const NSSize *)newSize;
{
    const NSSize *si = newSize;
    image_size = si[0];

    /* Not implemented, see X book number 1, page 157-158 */
    
    [_NSImages addObject:self];
    return self;
}

- (XImage *)xImage;
{
  //return xImage;
  return NULL;
}

@end
