From: jim@VINCENT.GCU.EDU (James H. Grosbach)
Newsgroups: comp.windows.x.motif
Subject: XSetWMColormapWindows
Date: 16 Oct 92 18:48:11 GMT
Distribution: inet
Organization: The Internet


I'm trying to get the WM to install a virtual colormap for a
drawingarea widget. The program does a core dump with a 
segmentation fault when run. I'm at a loss at this point. I've
been through the manuals and sample code I've available to no
avail.

This is a sample program which reproduces the problem I'm having
in my real application. This program just creates a Form with a 
drawingarea child and attempts to install a colormap for the
drawingarea. The expose callback for the DA is set to display the
current palette entries (which should be a grey-scale).

---
#include <stdio.h>
#include <Xm/Xm.h>          /* Motif Toolkit */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <Xm/DrawingA.h>
#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/CascadeB.h>
#include <Xm/PushB.h>
#include <Xm/Frame.h>

void setup_visual(void);
void setup_colormap(void);
void setup_gc(void);

Display		*display;
GC		gc;
Widget		toplevel;
Widget		drawarea;
Colormap	cmap;
XVisualInfo	vinfo;	    /* for finding desired visual */
Visual		*visual;    /* hopefully we get one we want */

void showCB (w, client_data, call_data)
Widget w;
caddr_t client_data;
caddr_t call_data;
{
    int		i;

    XSetForeground(display, gc, 0);
    XFillRectangle(display, XtWindow(w), gc, 0, 0, 255, 99);	
    for (i=0;i<256;i++){
        XSetForeground(display, gc, i);
	XDrawLine (display, XtWindow(w), gc, i, 0, i, 99);
    }
}

/*
 * setup_visual -
 */
void setup_visual(void)
{
    if (XMatchVisualInfo(display, DefaultScreen(display), 8, PseudoColor,
      &vinfo) == 0) {
	fprintf(stderr, "Error: cannot find 8 bit PseudoColor visual.\n");
	exit(-1);
    }
    visual = vinfo.visual;
}

/*
 * setup_colormap -
 */
void setup_colormap(void)
{
    int		  i;
    XColor color[256];

    /*
     * Load the default values for the palette.
     */
    for (i=0;i<256;i++){
	color[i].pixel=i;
	color[i].flags=DoRed | DoGreen | DoBlue;
    }
    XQueryColors(display, DefaultColormap(display, DefaultScreen(display)),
	color, 256);
	
    for (i=64;i<256;i++){
	color[i].pixel=i;
	color[i].flags=DoRed | DoGreen | DoBlue;
	color[i].red=i*256;
	color[i].green=i*256;
	color[i].blue=i*256;
    }
    cmap = XCreateColormap(display, RootWindow(display,
	DefaultScreen(display)), visual, AllocAll);
    XStoreColors(display, cmap, color, 255);
}

/*
 * setup_gc -
 */
void setup_gc(void)
{
    XGCValues values;

    values.foreground=BlackPixel(display, DefaultScreen(display));
    values.background=WhitePixel(display, DefaultScreen(display));

    gc=XCreateGC(display, RootWindow(display,
	DefaultScreen(display)), (GCForeground|GCBackground), &values);
}

void main(argc, argv)
unsigned int argc;
char **argv;
{
    WidgetClass	    *dummy_class;
    Widget	    testPalettemain = NULL, fm;
    Arg		    arglist[10] ;
    XtAppContext    app_context;
    int		    n;
    XmString	    xmstr;
    Window	windows[2];

    XtToolkitInitialize();

    app_context = XtCreateApplicationContext();

    display = XtOpenDisplay(app_context, NULL, argv[0], "Testpal",
                            NULL, 0, &argc, argv);
    if (display == NULL) {
        fprintf(stderr, "%s:  Can't open display\n", argv[0]);
        exit(1);
    }

    setup_visual();
    setup_colormap();
    setup_gc();

    n = 0;
    XtSetArg(arglist[n], XmNallowShellResize, True);  n++;
    toplevel=XtAppCreateShell("TestPalette", NULL,
	applicationShellWidgetClass, display, arglist, n);

    /*
     * Create the main testPalettemain.
     */
    n=0;
    testPalettemain=XmCreateForm (toplevel, "maintestPalettemain", arglist, 0);

    /*
     * Create the frame
     */
    n=0;
    XtSetArg (arglist[n], XmNtopOffset, 10);n++;
    XtSetArg (arglist[n], XmNbottomOffset, 10);n++;
    XtSetArg (arglist[n], XmNleftOffset, 10);n++;
    XtSetArg (arglist[n], XmNrightOffset, 10);n++;
    XtSetArg (arglist[n], XmNtopWidget, mb);n++;
    XtSetArg (arglist[n], XmNtopAttachment, XmATTACH_WIDGET);n++;
    XtSetArg (arglist[n], XmNbottomAttachment, XmATTACH_FORM);n++;
    XtSetArg (arglist[n], XmNleftAttachment, XmATTACH_FORM);n++;
    XtSetArg (arglist[n], XmNrightAttachment, XmATTACH_FORM);n++;
    fm=XmCreateFrame (testPalettemain, "frame", arglist, n);
    XtManageChild (fm);
    /*
     * Create the drawing area.
     */
    n=0;
    XtSetArg (arglist[n], XmNwidth, 256);n++;
    XtSetArg (arglist[n], XmNheight, 100);n++;
    XtSetArg (arglist[n], XmNbackground, 0);n++;
    XtSetArg (arglist[n], XmNcolormap, cmap);n++;
    drawarea=XmCreateDrawingArea (fm, "da", arglist, n);
    XtManageChild (drawarea);
    XtAddCallback (drawarea, XmNexposeCallback, showCB, NULL);

    XtManageChild(testPalettemain);

    XtRealizeWidget(toplevel);


    /**** Here's where the problem is ***/
        /*
         * Let the WM know that the drawarea has a separate colormap.
         */
        windows[0]=XtWindow(drawarea);
        windows[1]=XtWindow(toplevel);
        XtSetWMColormapWindows (display, XtWindow(toplevel),
	    windows, 2);
	flag++;

    /*
     *  Loop and process events
     */
    XtAppMainLoop(app_context);
}
---

Thanks in advance.

	jim
James H. Grosbach
Dynamical Systems Laboratory
Grand Canyon University
(602) 589-2877
jim@vincent.gcu.edu