void MyFrame::init_graphics() { /* global variables */ Widget canvas; Display *theDisplay; XImage *pic_array[MAXIMAGES]; GC invertGC, drawGC; int scrn; Display *dpy; Window grwin; Colormap fixedcmap; unsigned long blackPix, whitePix; unsigned int depth; Visual *use_visual; dpy = XtDisplay(canvas); grwin = XtWindow(canvas); scrn = DefaultScreen(dpy); use_visual = DefaultVisual(dpy, scrn); #if 1 fixedcmap = XCreateColormap(dpy, grwin, use_visual, AllocNone); #else fixedcmap = DefaultColormap(dpy, scrn); #endif fixedcmap = InitColorTableFixed(fixedcmap); XtVaGetValues(canvas, XmNdepth, &depth, NULL); XtVaSetValues(toplevel, XmNcolormap, fixedcmap, NULL); XtSetWMColormapWindows(toplevel, &canvas, 1); /**************************************************************/ blackPix = _get_lookup_for_color(0, 0, 0); whitePix = _get_lookup_for_color(255, 255, 255); drawGC = XCreateGC(XtDisplay(canvas), XtWindow(canvas), (unsigned long)0, NULL); theDisplay = XtDisplay(toplevel); XSetFunction(theDisplay, drawGC, GXcopy); XSetForeground(theDisplay, drawGC, blackPix); XSetBackground(theDisplay, drawGC, whitePix); invertGC = XCreateGC(XtDisplay(canvas), XtWindow(canvas), (unsigned long)0, NULL); XSetFunction(theDisplay, invertGC, GXcopy); XSetForeground(theDisplay, invertGC, whitePix); XSetBackground(theDisplay, invertGC, blackPix); }
/*-------------------------------------------------------------*/ void setup_display(Widget w) { int i,j, cindex; int theScreen; XColor *colors = NULL; XGCValues xgcv; Colormap theColormap; XVisualInfo *p_visinfo; XVisualInfo *vis_list; XVisualInfo vis_template; Widget *list_w = NULL; /* List of widgets with its own colormap */ theScreen = DefaultScreen(XtDisplay(w)); vis_template.screen = theScreen; vis_list = XGetVisualInfo(XtDisplay(w),VisualScreenMask, &vis_template, &j); for (i=0, p_visinfo = vis_list; i<j;i++, p_visinfo++) { if(p_visinfo->class == PseudoColor && p_visinfo->depth == 8) { theVisual = p_visinfo->visual; vis_depth = p_visinfo->depth; break; } } theVisual = DefaultVisual(XtDisplay(w),theScreen); vis_depth = 8; /* Create the XColor entries for the colormap */ if((colors = (XColor *)calloc(COLORMAP_SIZE, sizeof(XColor))) == NULL) { fprintf(stderr, "No memory for setting up colormap\n"); exit(1); } /* Set up the color cells to greyscale */ for(cindex = 0; cindex < COLORMAP_SIZE; cindex++) { colors[cindex].red = CSCALE * cindex; colors[cindex].green = CSCALE * cindex; colors[cindex].blue = CSCALE * cindex; colors[cindex].pixel = cindex; colors[cindex].flags = DoRed | DoGreen | DoBlue; } /* Create the colormap */ theColormap = XCreateColormap(XtDisplay(top_level), RootWindow(XtDisplay(top_level), theScreen), theVisual, AllocAll); /* Store the colors into the colormap */ XStoreColors(XtDisplay(top_level), theColormap, colors, COLORMAP_SIZE); /* Set the background and foreground colors before free(colors) */ bg = colors[0].pixel; fg = colors[255].pixel; ysmap=XCopyColormapAndFree(XtDisplay(top_level), theColormap); /* Now we can release the memory used by the colors */ /* because the X server already has this information. */ free(colors); /* Set background, foreground, visual_depth and colormap for draw_1 */ argcount = 0; XtSetArg(args[argcount], XmNforeground, fg ); argcount++; XtSetArg(args[argcount], XmNbackground, bg ); argcount++; XtSetArg(args[argcount], XmNdepth , vis_depth ); argcount++; XtSetArg(args[argcount], XmNcolormap , theColormap); argcount++; XtSetValues(draw_1, args, argcount); /* Set background, foreground, visual_depth and colormap for draw_2 */ argcount = 0; XtSetArg(args[argcount], XmNforeground, fg ); argcount++; XtSetArg(args[argcount], XmNbackground, bg ); argcount++; XtSetArg(args[argcount], XmNdepth , vis_depth ); argcount++; XtSetArg(args[argcount], XmNcolormap , theColormap); argcount++; XtSetValues(draw_2, args, argcount); /* Set the WM_COLORMAP_WINDOWS property so that the Motif */ /* window manager knows about the windows that have their */ /* own colormap. */ if(theColormap != DefaultColormap(XtDisplay(top_level), theScreen)) { list_w = (Widget *) malloc(2*sizeof(Widget)); list_w[0] = draw_1; list_w[1] = draw_2; XtSetWMColormapWindows(top_level, list_w, (Cardinal) 2); } /* Define a GC for both drawing areas with these colors */ xgcv.foreground = fg; xgcv.background = bg; image_gc_1 = XCreateGC(XtDisplay(w), XtWindow(draw_1), (GCForeground | GCBackground), &xgcv); image_gc_2 = XCreateGC(XtDisplay(w), XtWindow(draw_2), (GCForeground | GCBackground), &xgcv); /* Define a GC to be used in editing image operations*/ xgcv.foreground = fg ^ bg; xgcv.background = bg; xgcv.function = GXxor; xorGC = XtGetGC(draw_1, GCForeground | GCBackground | GCFunction, &xgcv); xgcv.foreground = fg; xgcv.background = bg; theGC = XtGetGC(draw_1, GCForeground | GCBackground, &xgcv); }
int main(int argc, char ** argv) { setbuf(stdout, NULL); setbuf(stderr, NULL); Display * display = XOpenDisplay(NULL); assert(display != NULL); XSynchronize(display, True); XtAppContext context = XtCreateApplicationContext(); Widget appshell = XtVaAppInitialize(&context, "SoXtTest", NULL, 0, &argc, argv, NULL, NULL); fprintf(stderr, "appshell: %p\n", appshell); #if WANT_VISUALID int i, numvisuals; unsigned int wanted = WANT_VISUALID; XVisualInfo templ; XVisualInfo * visuals = XGetVisualInfo(display, VisualNoMask, &templ, &numvisuals); for ( i = 0; i < numvisuals; i++ ) { if ( visuals[i].visualid == wanted ) goto selected; } assert(0 && "no visual selected"); selected: Visual * visual = visuals[i].visual; int visualid = visuals[i].visualid; int depth = visuals[i].depth; Colormap colormap = 0; fprintf(stderr, "visualid: %d, depth: %d, class: %s\n", visualid, depth, visuals[i].c_class == DirectColor ? "DirectColor" : "Other"); int numcmaps; XStandardColormap * stdcolormaps = NULL; if ( XmuLookupStandardColormap(display, visuals[i].screen, visuals[i].visualid, visuals[i].depth, XA_RGB_DEFAULT_MAP, False, True) && XGetRGBColormaps(display, RootWindow(display, visuals[i].screen), &stdcolormaps, &numcmaps, XA_RGB_DEFAULT_MAP) ) { for ( int j = 0; j < numcmaps; j++ ) { if (stdcolormaps[j].visualid == visuals[i].visualid) { colormap = stdcolormaps[j].colormap; goto cont; } } colormap = XCreateColormap(display, RootWindow(display, visuals[i].screen), visuals[i].visual, AllocNone); fprintf(stderr, "standard RGB colormaps did not work with visual - created own (%ld)", colormap); } else { assert(0); } cont: fprintf(stderr, "colormap: %ld\n", colormap); #else Visual * visual = NULL; int depth = 0; Colormap colormap = 0; int snum = XDefaultScreen(display); visual = XDefaultVisual(display, snum); depth = XDefaultDepth(display, snum); colormap = XDefaultColormap(display, snum); fprintf(stderr, "visual: %p, depth: %d\n", visual, depth); fprintf(stderr, "colormap: %ld\n", colormap); #endif XtVaSetValues(appshell, XmNwidth, 100, XmNheight, 100, XmNvisual, visual, XmNcolormap, colormap, XmNdepth, depth, NULL); Widget form = XtVaCreateManagedWidget( "form", xmFormWidgetClass, appshell, NULL); Widget button = XtVaCreateManagedWidget( "button", xmPushButtonWidgetClass, form, XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, NULL); Pixmap pixmap = createPixmapFromXpm(button, home_xpm); XtVaSetValues(button, XmNlabelType, XmPIXMAP, XmNlabelPixmap, pixmap, XmNlabelInsensitivePixmap, pixmap, XmNselectPixmap, pixmap, XmNselectInsensitivePixmap, pixmap, NULL); Widget list[] = { appshell, form, button, NULL }; XtSetWMColormapWindows(appshell, list, 3); XtRealizeWidget(appshell); XtAppMainLoop(context); }