Transparent XPM Pixmap in Motif Widget

This forum is read only. No new submissions are accepted.

Questions about motif? Contact us

2 posts / 0 new
Last post
Transparent XPM Pixmap in Motif Widget

Submitted by Anonymous (not verified) on Fri, 06/15/2012 - 12:07

Hi,
I am looking for some examples to show me how to implement transparent XPM pixmaps in a Motif Widget (Motif 2.x.x). There is supposed to be a 'XPM Tips' article on this site, but I could not find it. Any help is greatly appreciated.
Thanks
Styx
‹ How can I highlight a portion of text in a Label Widget ? XmStringDrawImage problems ›

Yuriy Syrota

Fri, 11/25/2005 - 16:32#1

Re: Transparent XPM Pixmap in Motif Widget

styx wrote:

Hi,
I am looking for some examples to show me how to implement transparent XPM pixmaps in a Motif Widget (Motif 2.x.x). There is supposed to be a 'XPM Tips' article on this site, but I could not find it. Any help is greatly appreciated.
Thanks
Styx
Please see below:

<br />
#include <X11/Xlib.h><br />
#include <X11/Xutil.h><br />
#include <X11/xpm.h><br />
#include <stdio.h><br />
#include <string.h></p>
<p>void xpm_error(char * wherefrom, int rv);<br />
void print_statistics(XpmAttributes * attributes, XImage * image);</p>
<p>int main(int argc, char * argv[])<br />
{<br />
Display * display;<br />
unsigned long screen;<br />
Window root;<br />
Window win;<br />
XSizeHints xhint;<br />
XEvent event;<br />
Colormap colormap;<br />
unsigned long depth;<br />
XColor dummy1, color;<br />
char * colorname = "#a6b2c7";<br />
XpmAttributes attributes;<br />
Pixmap dummy, pixmapshape, bkgpixmap;<br />
XImage * image = NULL, * dummy2 = NULL;<br />
int rv = 0, gcmask = 0;<br />
GC imagegc;<br />
XGCValues xgcv;</p>
<p> if (!(display = XOpenDisplay(NULL)))<br />
return 1;<br />
screen = DefaultScreen(display),<br />
root = DefaultRootWindow(display),<br />
depth = DefaultDepth(display, screen),<br />
colormap = DefaultColormap(display, screen);<br />
/* Allocate color used for both window background and image background */<br />
if (!XAllocNamedColor(display, colormap, colorname, &dummy1, &color))<br />
{<br />
fprintf(stderr, "Unable to parse `%s'\n", colorname);<br />
color.pixel = WhitePixel(display, screen);<br />
}</p>
<p> memset(&xhint, 0, sizeof(xhint)); /* if I don't do that, it gets weird. */</p>
<p> attributes.valuemask = XpmColormap | XpmDepth | XpmCloseness;<br />
attributes.colormap = colormap;<br />
attributes.depth = DefaultDepth(display, screen);<br />
attributes.closeness = 40000;<br />
attributes.exactColors = False;</p>
<p> if (0 != (rv = XpmReadFileToImage(display, argv[1], &image, &dummy2, &attributes)))<br />
{<br />
xpm_error("XpmReadFileToImage()", rv);<br />
printf("DEBUG: rv = %d\n", rv);<br />
}<br />
if (0 != (rv = XpmReadFileToPixmap(display, root, argv[1], &dummy,<br />
&pixmapshape, &attributes)))<br />
{<br />
xpm_error("XpmReadFileToPixmap()", rv);<br />
printf("DEBUG: rv = %d\n", rv);<br />
}</p>
<p> if (0 != (rv = XpmReadFileToPixmap(display, root, "wood.xpm", &bkgpixmap,<br />
&dummy, &attributes)))<br />
{<br />
xpm_error("XpmReadFileToPixmap()", rv);<br />
printf("DEBUG: rv = %d\n", rv);<br />
}<br />
XFreePixmap(display, dummy); /* We need only pixmap shapemask. */<br />
if (dummy2)<br />
XDestroyImage(dummy2); /* Only image itself is needed. */</p>
<p> win = XCreateSimpleWindow(display, root, 0, 0,<br />
image->width + 20, image->height + 20,<br />
1, color.pixel, color.pixel);</p>
<p> gcmask = GCForeground | GCBackground;<br />
xgcv.foreground = color.pixel;<br />
xgcv.background = color.pixel;<br />
imagegc = XCreateGC(display, win, gcmask, &xgcv);<br />
XSetClipMask(display, imagegc, pixmapshape);</p>
<p> XSetStandardProperties(display, win, "pixmaplib2", "pixmaplib2",<br />
None, argv, argc, &xhint);<br />
XSelectInput(display, win, ExposureMask | ButtonPressMask);<br />
XMapWindow(display, win);</p>
<p> XSetWindowBackgroundPixmap(display, win, bkgpixmap);<br />
print_statistics(&attributes, image);<br />
/* press button #3 in order to close the program. */<br />
while (1)<br />
{<br />
XNextEvent(display, &event);<br />
if (event.type == Expose && event.xexpose.count == 0)<br />
{<br />
int junk2;<br />
unsigned int width, height, x, y, junk;<br />
Window tempwin;</p>
<p> XGetGeometry(display, win, &tempwin, &junk2, &junk2, &width,<br />
&height, &junk, &junk);<br />
/*printf("width == %d, height == %d\n", width, height);*/<br />
x = width / 2 - image->width / 2;<br />
y = height / 2 - image->height / 2;</p>
<p> XSetClipOrigin(display, imagegc, x, y);<br />
XPutImage(display, win, imagegc, image,<br />
0, 0, x, y, image->width, image->height);<br />
}<br />
else if (event.type == ButtonPress && event.xbutton.button == Button3)<br />
break;<br />
}<br />
printf("Bye\n");<br />
XCloseDisplay(display);<br />
return 0;<br />
}</p>
<p>/******************************************************************************/<br />
/* For debugging */<br />
void print_statistics(XpmAttributes * attributes, XImage * image)<br />
{<br />
printf("\nXpmAttributes: width == %u, height == %u,\n"<br />
"number of colors == %d, pixels used == %d\n",<br />
attributes->width, attributes->height,<br />
attributes->ncolors, attributes->npixels);<br />
printf("XImage: width == %u, height == %u\n",<br />
image->width, image->height);<br />
fflush(stdout);<br />
}</p>
<p>/******************************************************************************/<br />
void xpm_error(char * call, int error)<br />
{<br />
switch (error)<br />
{<br />
case XpmSuccess:<br />
return;<br />
case XpmColorError:<br />
fprintf(stderr,<br />
"%s: Could not allocate requested color\n", call);<br />
"%s: Could not allocate requested color\n", call);<br />
break;<br />
case XpmOpenFailed:<br />
fprintf(stderr,<br />
"%s: Unable to open image file\n", call);<br />
break;<br />
case XpmFileInvalid:<br />
fprintf(stderr,<br />
"%s: Invalid XPM file\n", call);<br />
break;<br />
case XpmNoMemory:<br />
fprintf(stderr,<br />
"%s: Not enough memory\n", call);<br />
break;<br />
case XpmColorFailed:<br />
fprintf(stderr,<br />
"%s: Failed to parse or alloc some color\n", call);<br />
break;<br />
default:<br />
fprintf(stderr, "%s: Unknown error occured\n", call);<br />
}<br />
exit(1);<br />
}<br />