[comp.windows.x] xloadimage into widgets

basti@orthogo.UUCP (Sebastian Wangnick) (12/12/90)

/* This piece of code embeds xloadimage 2.0 functionality into a Xt converter
 * so that you can load image files of different formats into any pixmap 
 * resource straight from your resource database file. This means that you
 * can set the background pixmap of any widget to an image stored in a file
 * in FBM image, Sun rasterfile, CMU WM raster, PBM, Faces, GIF, Utah RLE, 
 * X window dump, MacPaint, X pixmap, X bitmap or G3 fax image formats
 * (that's the current list of xloadimage 2.0 readable formats). xloadimage 
 * is written by Jim Frost (jimf@saber.com) and is publicly available via
 * anonymous ftp as expo.lcs.mit.edu:contrib/xloadimage.2.02.tar.Z.
 *
 * No warranties, of course. 
 *
 * basti@orthogo.uucp
 */

/* #define MOTIF */
             
#ifdef MOTIF
#include <Xm/XmP.h>
#else
#include <X11/IntrinsicP.h>
#endif

#include <X11/StringDefs.h>
#include <X11/Xresource.h>

static Arg args[3];
static int num;
 
#ifdef MOTIF
static void TryMotif (Widget widget, char* name, XrmValuePtr to)
	{Pixel foreground = BlackPixelOfScreen(XtScreen(widget));
	Pixel background = WhitePixelOfScreen(XtScreen(widget));
	static Pixmap pixmap;
	XtSetArg(args[num=0],XtNforeground,&foreground); num++;
	XtSetArg(args[num],XtNbackground,&background); num++;
	XtGetValues(widget,args,num);
	pixmap = XmGetPixmap(XtScreen(widget),name,foreground,background);
	if (pixmap==XmUNSPECIFIED_PIXMAP)
		XtStringConversionWarning(name,XtRPixmap);
	else
		{to->addr = (caddr_t) &pixmap;
		to->size = sizeof(Pixmap);
		}
	}
#else
#define TryMotif(widget,name,to)
#endif

#define Pixel ImagePixel
#include <image.h>
#undef Pixel
#include <imagetypes.h> 
int findImage (char* name, char* fullname);
unsigned int sendImageToX(Display* disp, int scrn, Visual* visual, 
		Image* image, Pixmap* pixmap, Colormap* cmap, unsigned int verbose);
void goodImage(Image* image, char* func) {}
extern int errno; 
extern char* sys_errlist[];

Display* Disp;
int Scrn;

static void CvtStringToPixmap (XrmValuePtr closure, Cardinal *num_closure,
		XrmValuePtr from, XrmValuePtr to)
	{static char* fname = "CvtStringToPixmap";
    static XtResource resource =
		{"createTraceback","CreateTraceback",XtRBool,sizeof(Bool),0,
		XtRImmediate, (caddr_t) FALSE};
	Bool traceback;
	char fullname[BUFSIZ];
	Widget widget;
	Visual* visual;
	Colormap cmap;
	static Pixmap pixmap;
	Image* image = NULL;
	int itype;
	to->addr = NULL;
	to->size = 0;
	if (*num_closure != 1)
		{fprintf(stderr,"%s: Wrong number of arguments\n",fname);
		XtStringConversionWarning(from->addr,XtRPixmap);
		return;
		}
	widget = (Widget) closure->addr;
	if (widget->core.self != widget)
		{fprintf(stderr,"%s: Funny widget\n",fname);
		XtStringConversionWarning(from->addr,XtRPixmap);
		return;
		}
	Disp = XtDisplay(widget);
	Scrn = DefaultScreen(Disp);
	visual = DefaultVisual(Disp,Scrn);
	cmap = widget->core.colormap;
	XtGetApplicationResources(widget,&traceback,&resource,1,NULL,0);
	if (findImage(from->addr,fullname)<0)
		{if (traceback)
			printf("%s: Can't find imagefile %s (%s)\n",
					fname,from->addr,sys_errlist[errno]);
		TryMotif(widget,from->addr,to);
		return;
		}
	for (itype=0; ImageTypes[itype].loader; itype++)
		if (image=ImageTypes[itype].loader(from->addr,from->addr,0))
			break;
	if (!image)
		{if (traceback)
			printf("%s: Can't load imagefile %s\n",fname,from->addr);
		TryMotif(widget,from->addr,to);
		return;
		}
	if (!BITMAPP(image) && !RGBP(image))
		{if (traceback)
			printf("%s: Strange image type %d in image %s\n",
					fname,image->type,image->title);
		TryMotif(widget,from->addr,to);
		return;
		}
	if (!sendImageToX(Disp,Scrn,visual,image,&pixmap,&cmap,0))
		{if (traceback)
			printf("%s: Can't send image %s to X\n",fname,image->title);
		TryMotif(widget,from->addr,to);
		return;
		}
	if (traceback)
		printf("%s: %s is a %dx%d %s\n",fname,image->title,
				image->width,image->height,ImageTypes[itype].name);
	XtSetArg(args[num=0],XtNcolormap,cmap); num++;
	XtSetArg(args[num],XtNwidth,image->width); num++;
	XtSetArg(args[num],XtNheight,image->height); num++;
	XtSetValues(widget,args,num);
	to->addr = (caddr_t) &pixmap;
	to->size = sizeof(Pixmap);
	}

void Register
#ifdef MOTIF
		(Widget toplevel)
#else
		(void)
#endif
	{static XtConvertArgRec widgetConvertArg = {XtBaseOffset,0,sizeof(Widget)};
#ifdef MOTIF
	XtDestroyWidget(XtCreateWidget("dummy",xmManagerWidgetClass,toplevel,NULL,0));
	XtDestroyWidget(XtCreateWidget("dummy",xmPrimitiveWidgetClass,toplevel,NULL,0));
#endif
	XtAddConverter(XtRString,XtRPixmap,CvtStringToPixmap,&widgetConvertArg,1);
	}

Have a nice day,

Sebastian Wangnick (basti@orthogo.uucp)