/*
 *	file : background.c
 */
#include	"ptyx.h"
#include	"background.h"

#include	<stdio.h>
#include	<string.h>
#include	<X11/Xlib.h>
#include	<X11/Xutil.h>
#include	<X11/xpm.h>
#ifdef	IMLIB
#include	<X11/imlib.h>
#endif


static	Pixmap		xBackground = 0, xBackgroundMask;
static	Display		*pxDisplay;

static	XpmAttributes	xAttributes;

#ifdef	IMLIB
static	ImlibData	*pxImageHandler;
static	Image		*pxImage;
#endif

static	GC		pxCopyGC;

char	*background_filename = NULL;
int	iAdjuster;
int	iWidth, iHeight;
#ifdef	IMLIB
int	bUsePixmap;
int	bHalfTone = 0;
#endif	IMLIB

static	char	*filename = NULL;

extern	void	show_status_all();
extern	void	show_chat_buf();


void	adjust_background( int value )
{
	if( !background_filename )	return;
	if(value<0)
		iAdjuster = (iAdjuster + iHeight
			-((-value)%iHeight))%iHeight;
	else	iAdjuster = (iAdjuster + value)%iHeight;
	show_chat_buf();
	show_status_all();
}

void	set_filename( char *file )
{
#ifdef	IMLIB
	char	*te;
#endif
	if( file ){
		if( filename )	free( filename );
		filename = strdup( file );
#ifdef	IMLIB
		te = strstr( filename, "xpm" );
		if( te && !strcmp( te, "xpm" ))
			bUsePixmap = 1;
		else	bUsePixmap = 0;
#endif
	}
}

void	delete_picture()
{
	if( !background_filename )	return;

#ifdef	IMLIB
	if( bUsePixmap ){
#endif
		XpmFreeAttributes( &xAttributes );
		XFreePixmap( pxDisplay, xBackground );
		XFreePixmap( pxDisplay, xBackgroundMask );
#ifdef	IMLIB
	}
	else{
		ImlibDestroyImage( pxImageHandler, pxImage );
		ImlibFreeColors( pxImageHandler );
	}
#endif
}


int	draw_box( Display *display, Drawable d, 
		int x, int y, int width, int height )
{
	register int	begin_x, end_x, begin_y, end_y;
	register int	count_x, count_y;

	if( width<=0 || height<=0 )	return(0);

	if( background_filename==NULL && filename==NULL )	return(-1);
	if( filename ){
		int		result;
		XGCValues	xgc;

		pxDisplay = display;

#ifdef	IMLIB
		if( bUsePixmap ){
#endif
			result = XpmReadFileToPixmap( display, d, filename, &xBackground,
				&xBackgroundMask, &xAttributes );
			if( result==0 ){
				iWidth = xAttributes.width;
				iHeight = xAttributes.height;
			}
#ifdef	IMLIB
		}
		else{

			pxImageHandler = ImlibInit( display );
			if( !pxImageHandler ){
				result = -1;
				fprintf( stderr, "I can not Initialize Imlib\n");
				goto ImEnd;
			}
			pxImage = ImlibLoadImage( pxImageHandler, filename, NULL );
			if ( pxImage==NULL ){
				fprintf( stderr, "I cannot load Image '%s'\n", filename );
				result = -1;
				goto ImEnd;
			}
			else{
				register int	length, limit;
				limit = pxImage->rgb_width*3*pxImage->rgb_height;
				if( bHalfTone )
					for( length=0 ; length < 
						 limit ; length++ )
						pxImage->rgb_data[length] /= 2;
			}

			xBackground = ImlibRender( pxImageHandler, pxImage,
				pxImage->rgb_width, pxImage->rgb_height );
			xBackground = ImlibCopyImageToPixmap( pxImageHandler, pxImage );
			iWidth = pxImage->width;
			iHeight = pxImage->height;
			result = 0;
		}
		ImEnd:
#endif
		if( result<0 ){
			fprintf( stderr, "Error!! I can not load pixmap !!\n" );
			xBackground = 0;
			free(filename);
			filename = NULL;
			return;
		}
		else{
			background_filename = filename;
#ifdef	DEBUG
			fprintf( stderr, " Size : %d, %d\n", 
				iWidth,
				iHeight );
#endif
			filename = NULL;
		}
		xgc.function = GXcopy;
		xgc.graphics_exposures = 0;
		pxCopyGC = XCreateGC( display, d, 
			GCFunction|GCGraphicsExposures, &xgc );
	}
	y += iAdjuster;

	begin_y = y;
	for( count_y = y/iHeight ; 
		count_y<=(y+height-1)/iHeight ; count_y ++ ){

		end_y = (begin_y/iHeight)*iHeight + iHeight - 1;
		if( end_y > y + height - 1 )	end_y = y + height - 1;

		begin_x = x;
		for( count_x = x/iWidth ; 
			count_x<=(x+width-1)/iWidth ; count_x ++ ){

			end_x = (begin_x/iWidth)*iWidth + iWidth - 1;
			if( end_x > x + width - 1 )	end_x = x + width - 1;


			XCopyArea( display, xBackground, d, pxCopyGC, 
				begin_x % iWidth,
				begin_y % iHeight, 
				end_x - begin_x + 1, end_y - begin_y + 1,
				begin_x, begin_y-iAdjuster );
			begin_x = (count_x+1) * iWidth;
		}
		begin_y = (count_y+1) * iHeight;
	}
	return(0);
}


