Nevermind. Figured it out. Needed to have the colormap apply to the
top level widget, not the Glx widget. Had read about this previously,
but never got it to work before. For posterity, here's the working
sample code.

--Rob

#include
#include
#include
#include

#define BUF_SIZE 8
#define CMAP_OFFSET 0
#define GL_WIDTH 256
#define GL_HEIGHT 256

static unsigned char s_LRBuf[GL_WIDTH*GL_HEIGHT];
static void uiGlxInitCB (Widget w,
XtPointer clientData,
GLwDrawingAreaCallbackStruct *cbs);
static void uiGlxExposeCB (Widget w,
XtPointer clientData,
GLwDrawingAreaCallbackStruct *cbs);
static void clearGLCB (Widget w, Widget glW, XtPointer callData);

Widget topLevel;

main (int argc, char **argv)
{
Widget /*topLevel,*/ paneW, glW, formW, clearPB;
XtAppContext appContext;
int xIdx, yIdx;

topLevel = XtVaAppInitialize (&appContext, "Demo", NULL, 0,
&argc, argv, NULL, NULL);

paneW = XtVaCreateWidget ("", xmPanedWindowWidgetClass, topLevel,
XmNsashWidth, 1,
NULL);

glW = XtVaCreateManagedWidget ("", glwMDrawingAreaWidgetClass,
paneW,
XmNwidth, 3*GL_WIDTH,
XmNheight, 3*GL_HEIGHT,
GLwNdoublebuffer, True,
GLwNrgba, False,
GLwNbufferSize, BUF_SIZE,
NULL);

XtAddCallback (glW, GLwNginitCallback,
(XtCallbackProc) uiGlxInitCB, NULL);

XtAddCallback (glW, GLwNexposeCallback,
(XtCallbackProc) uiGlxExposeCB, NULL);

formW = XtVaCreateWidget ("", xmFormWidgetClass, paneW, NULL);

clearPB = XtVaCreateManagedWidget ("Clear",
xmPushButtonWidgetClass, formW,
XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, formW,
XmNtopAttachment, XmATTACH_FORM,
NULL);

XtAddCallback (clearPB, XmNactivateCallback,
(XtCallbackProc) clearGLCB, glW);

XtManageChild (formW);
XtManageChild (paneW);
XtRealizeWidget (topLevel);

/* Construct a gray ramp left-right */
for (yIdx = 0; yIdx < GL_HEIGHT; yIdx++)
{
for (xIdx = 0; xIdx < GL_WIDTH; xIdx++)
{
s_LRBuf[yIdx*GL_WIDTH + xIdx] = xIdx + CMAP_OFFSET;
}
}

XtAppMainLoop (appContext);
}


static void uiGlxInitCB (Widget w,
XtPointer clientData,
GLwDrawingAreaCallbackStruct *cbs)
{
XVisualInfo *vInfo;
GLXContext glContext;
Window winList[1];
Colormap cmapID;
XColor colorSt;
int idx;

Colormap defCmap, theColormap;
XColor dummy_col;
int n;
Display *dpy;

// this appears to be necessary to get the colormap working
dpy = XtDisplay (topLevel);

XtVaGetValues (w,
GLwNvisualInfo, &vInfo,
NULL);
glContext = glXCreateContext (XtDisplay (w), vInfo, 0, GL_FALSE);

GLwDrawingAreaMakeCurrent (w, glContext);
glPixelZoom(1.0, 1.0);

XtVaGetValues (w,
XtNvisual, &vInfo,
NULL);

theColormap = XCreateColormap (XtDisplay (w), XtWindow (w),
vInfo->visual, AllocAll);

for (idx = 0; idx < 256; idx++)
{
colorSt.flags = DoRed | DoGreen | DoBlue;
colorSt.pixel = CMAP_OFFSET + idx;
colorSt.red = colorSt.blue = colorSt.green = idx << 8;
XStoreColor (XtDisplay (w), theColormap, &colorSt);
}

XtVaSetValues ( topLevel,
XmNcolormap, theColormap,
NULL);
}

static void uiGlxExposeCB (Widget w,
XtPointer clientData,
GLwDrawingAreaCallbackStruct *cbs)
{
printf ("Refreshing the screen...\n");
glRasterPos2i (0,0);
glDrawPixels (GL_WIDTH, GL_HEIGHT, GL_COLOR_INDEX,
GL_UNSIGNED_BYTE,s_LRBuf);
GLwDrawingAreaSwapBuffers (w);
}


static void clearGLCB (Widget w, Widget glW, XtPointer callData)
{
printf ("Clearing the widget\n");

glClearColor (0, 0, 0, 0);
glClear (GL_COLOR_BUFFER_BIT);
GLwDrawingAreaSwapBuffers (glW);
}



< OpenGL under Redhat Linux 9 (Mesa 6.2) on a Toshiba notebook with a
default 8-bit pseudocolor visual (yes you read it right; output from
xdpyinfo is at the bottom of this post). I'm trying to run in
color_index mode and set the color table to a 256-entry grayscale
ramp, and having no success. Yes, I know; many posts say that
color_index mode is not supported any more, but it is, the 8-bit
pseudocolor visual is on my machine, it is even the default visual,
and I've compiled and run sample color_index applications that use
GLUT and that work perfectly.

I've read many, many posts on use of the colormap, and tried most of
the methods, and have not gotten anything to work. For the sake of
brevity, since my app is huge, I've included a test application that I
pulled down off the web that shows the basic problem. It sets up a
glwMDrawingAreaWidgetClass widget with GLwNrgba set to false (so it
should be in color_index mode), writes a 256-entry colormap as a ramp,
and creates a sample image as a ramp. If the new color map was
installed correctly, I'd see a gray ramp (as I see in a similar sample
app using GLUT). But instead I see the "technicolor" from the default
color map.

Note that it goes through the paces of almost working. That is, if I
set "AllocAll" to AllocNone" it blows up ("attempt to access private
resource denied"), and if the CMAP_OFFSET is set to, say, 256 instead
of 0, it blows up ("integer parameter out of range").

Please help me regain my sanity. I want to see my wife and kids again
before I die...

--Rob Siegel

// build with cc -o test test.c -L/usr/X11R6/lib -lGLw -lGLU -lGL -lXm
-lXt -lX11
#include
#include
#include
#include

#define BUF_SIZE 8
#define CMAP_OFFSET 0
#define GL_WIDTH 512
#define GL_HEIGHT 150

static unsigned short s_LRBuf[GL_WIDTH*GL_HEIGHT];
static void uiGlxInitCB (Widget w,
XtPointer clientData,
GLwDrawingAreaCallbackStruct *cbs);
static void uiGlxExposeCB (Widget w,
XtPointer clientData,
GLwDrawingAreaCallbackStruct *cbs);
static void clearGLCB (Widget w, Widget glW, XtPointer callData);

Widget topLevel;

main (int argc, char **argv)
{
Widget /*topLevel,*/ paneW, glW, formW, clearPB;
XtAppContext appContext;
int xIdx, yIdx;

topLevel = XtVaAppInitialize (&appContext, "Demo", NULL, 0,
&argc, argv, NULL, NULL);

paneW = XtVaCreateWidget ("", xmPanedWindowWidgetClass, topLevel,
XmNsashWidth, 1,
NULL);

glW = XtVaCreateManagedWidget ("", glwMDrawingAreaWidgetClass,
paneW,
XmNwidth, GL_WIDTH,
XmNheight, GL_HEIGHT,
GLwNdoublebuffer, True,
GLwNrgba, False,
GLwNbufferSize, BUF_SIZE,
NULL);

XtAddCallback (glW, GLwNginitCallback,
(XtCallbackProc) uiGlxInitCB, NULL);

XtAddCallback (glW, GLwNexposeCallback,
(XtCallbackProc) uiGlxExposeCB, NULL);

formW = XtVaCreateWidget ("", xmFormWidgetClass, paneW, NULL);

clearPB = XtVaCreateManagedWidget ("Clear",
xmPushButtonWidgetClass, formW,
XmNleftAttachment, XmATTACH_WIDGET,
XmNleftWidget, formW,
XmNtopAttachment, XmATTACH_FORM,
NULL);

XtAddCallback (clearPB, XmNactivateCallback,
(XtCallbackProc) clearGLCB, glW);

XtManageChild (formW);
XtManageChild (paneW);
XtRealizeWidget (topLevel);

/* Construct a gray ramp left-right */
for (yIdx = 0; yIdx < GL_HEIGHT; yIdx++)
{
for (xIdx = 0; xIdx < GL_WIDTH; xIdx++)
{
s_LRBuf[yIdx*GL_WIDTH + xIdx] = xIdx / (GL_WIDTH/256) +
CMAP_OFFSET;
}
}

XtAppMainLoop (appContext);
}



static void uiGlxInitCB (Widget w,
XtPointer clientData,
GLwDrawingAreaCallbackStruct *cbs)
{
XVisualInfo *vInfo;
GLXContext glContext;
Window winList[1];
Colormap cmapID;
XColor colorSt;
int idx;

XtVaGetValues (w,
GLwNvisualInfo, &vInfo,
NULL);
glContext = glXCreateContext (XtDisplay (w), vInfo, 0, GL_FALSE);

GLwDrawingAreaMakeCurrent (w, glContext);
glPixelZoom(1.0, 1.0);

XtVaGetValues (w,
XtNvisual, &vInfo,
NULL);

cmapID = XCreateColormap (XtDisplay (w), XtWindow (w),
vInfo->visual, AllocAll);

for (idx = 0; idx <= 255; idx++)
{
colorSt.flags = DoRed | DoGreen | DoBlue;
colorSt.pixel = CMAP_OFFSET + idx;
colorSt.red = colorSt.blue = colorSt.green = idx << 8;
XStoreColor (XtDisplay (w), cmapID, &colorSt);
}
// this seems to have no effect on the colormap

XtVaSetValues (w,
XtNcolormap, cmapID,
XtNvisual, vInfo,
XtNdepth,8,
NULL);

/*
** Register the glx colormap with the window manager
*/
winList[0] = XtWindow (w);

XSetWMColormapWindows (XtDisplay (w),
XtWindow (w),
winList, 1);
}

static void uiGlxExposeCB (Widget w,
XtPointer clientData,
GLwDrawingAreaCallbackStruct *cbs)
{
printf ("Refreshing the screen...\n");
glRasterPos2i (0, 0);
glDrawPixels (GL_WIDTH, GL_HEIGHT, GL_COLOR_INDEX,
GL_UNSIGNED_SHORT,s_LRBuf);
GLwDrawingAreaSwapBuffers (w);
}


static void clearGLCB (Widget w, Widget glW, XtPointer callData)
{
printf ("Clearing the widget\n");

glClearColor (0, 0, 0, 0);
glClear (GL_COLOR_BUFFER_BIT);
GLwDrawingAreaSwapBuffers (glW);
}

///////////////////////////////////////////////////////////////////////////

Below is the output from xdpyinfo:
name of display: :0.0
version number: 11.0
vendor string: The XFree86 Project, Inc
vendor release number: 40300000
XFree86 version: 4.3.0
maximum request size: 4194300 bytes
motion buffer size: 256
bitmap unit, bit order, padding: 32, LSBFirst, 32
image byte order: LSBFirst
number of supported pixmap formats: 7
supported pixmap formats:
depth 1, bits_per_pixel 1, scanline_pad 32
depth 4, bits_per_pixel 8, scanline_pad 32
depth 8, bits_per_pixel 8, scanline_pad 32
depth 15, bits_per_pixel 16, scanline_pad 32
depth 16, bits_per_pixel 16, scanline_pad 32
depth 24, bits_per_pixel 32, scanline_pad 32
depth 32, bits_per_pixel 32, scanline_pad 32
keycode range: minimum 8, maximum 255
focus: window 0x1e00004, revert to Parent
number of extensions: 29
BIG-REQUESTS
DOUBLE-BUFFER
DPMS
Extended-Visual-Information
FontCache
GLX
LBX
MIT-SCREEN-SAVER
MIT-SHM
MIT-SUNDRY-NONSTANDARD
RANDR
RECORD
RENDER
SECURITY
SGI-GLX
SHAPE
SYNC
TOG-CUP
X-Resource
XC-APPGROUP
XC-MISC
XFree86-Bigfont
XFree86-DGA
XFree86-Misc
XFree86-VidModeExtension
XInputExtension
XKEYBOARD
XTEST
XVideo
default screen number: 0
number of screens: 1

screen #0:
dimensions: 1024x768 pixels (347x260 millimeters)
resolution: 75x75 dots per inch
depths (7): 8, 1, 4, 15, 16, 24, 32
root window id: 0x50
depth of root window: 8 planes
number of colormaps: minimum 1, maximum 1
default colormap: 0x20
default number of colormap cells: 256
preallocated pixels: black 0, white 1
options: backing-store NO, save-unders NO
largest cursor: 64x64
current input event mask: 0xfa203f
KeyPressMask KeyReleaseMask ButtonPressMask
ButtonReleaseMask EnterWindowMask LeaveWindowMask
ButtonMotionMask StructureNotifyMask
SubstructureNotifyMask
SubstructureRedirectMask FocusChangeMask
PropertyChangeMask
ColormapChangeMask
number of visuals: 12
default visual id: 0x27
visual:
visual id: 0x27
class: PseudoColor
depth: 8 planes
available colormap entries: 256
red, green, blue masks: 0x0, 0x0, 0x0
significant bits in color specification: 8 bits
visual:
visual id: 0x28
class: GrayScale
depth: 8 planes
available colormap entries: 256
red, green, blue masks: 0x0, 0x0, 0x0
significant bits in color specification: 8 bits
visual:
visual id: 0x29
class: StaticColor
depth: 8 planes
available colormap entries: 256
red, green, blue masks: 0x7, 0x38, 0xc0
significant bits in color specification: 8 bits
visual:
visual id: 0x2a
class: TrueColor
depth: 8 planes
available colormap entries: 8 per subfield
red, green, blue masks: 0x7, 0x38, 0xc0
significant bits in color specification: 8 bits
visual:
visual id: 0x2b
class: TrueColor
depth: 8 planes
available colormap entries: 8 per subfield
red, green, blue masks: 0x7, 0x38, 0xc0
significant bits in color specification: 8 bits
visual:
visual id: 0x2c
class: TrueColor
depth: 8 planes
available colormap entries: 8 per subfield
red, green, blue masks: 0x7, 0x38, 0xc0
significant bits in color specification: 8 bits
visual:
visual id: 0x2d
class: TrueColor
depth: 8 planes
available colormap entries: 8 per subfield
red, green, blue masks: 0x7, 0x38, 0xc0
significant bits in color specification: 8 bits
visual:
visual id: 0x2e
class: DirectColor
depth: 8 planes
available colormap entries: 8 per subfield
red, green, blue masks: 0x7, 0x38, 0xc0
significant bits in color specification: 8 bits
visual:
visual id: 0x2f
class: DirectColor
depth: 8 planes
available colormap entries: 8 per subfield
red, green, blue masks: 0x7, 0x38, 0xc0
significant bits in color specification: 8 bits
visual:
visual id: 0x30
class: DirectColor
depth: 8 planes
available colormap entries: 8 per subfield
red, green, blue masks: 0x7, 0x38, 0xc0
significant bits in color specification: 8 bits
visual:
visual id: 0x31
class: DirectColor
depth: 8 planes
available colormap entries: 8 per subfield
red, green, blue masks: 0x7, 0x38, 0xc0
significant bits in color specification: 8 bits
visual:
visual id: 0x32
class: StaticGray
depth: 8 planes
available colormap entries: 256
red, green, blue masks: 0x0, 0x0, 0x0
significant bits in color specification: 8 bits>>