[comp.windows.x] NEED MUCH HELP WITH OVERLAYS !!!!!

rwc@mitre.org (Ronald Conti) (02/21/91)

	Sometime ago I posted that I was having some difficulties with
overlays.  They still exist! :)

	WHen the colors seem correct, I know that I'm not drawing in
to overlay plane.  When the colors are wrong, it seems that I am.
(Commenting and uncommenting out the "XSetPlaneMask" commands).

	WHAT AM I DOING WRONG!?!?!?!?!?!?!?!?!?!?!?!?!

here is the source code, if it is any help...


thank in advance,

ron conti (rwc@uci0.mitre.org)

---------------------------cut here--------------------------------------------

/**********************************************************************

  file       : overlay.c
  purpose    : Test for overlays in X11.
  description:
  This implements the overlay example of Vol.1 of the O'Reilly books
  on X (pp. 192-194).  The main is a modified form of basicwin in
  the same book.

************************************************************************/

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <stdio.h>

#define SMALL 1
#define OK 0
#define MAX_COLORS 4
#define MAX_PLANES 1
#define CANNOT_OVERLAY 0
#define CAN_OVERLAY 1

unsigned long foreground, background_pixel;
unsigned long overlay_pixel_1, overlay_pixel_2;
int overlay_plane;

Display *display;
int state;
int screen;
Window win;
int plane_masks[MAX_PLANES];
Colormap cmap;
GC gc;

void main(argc, argv)
int argc;
char **argv;
{
	extern Window win;
	unsigned int width, height, x = 0, y = 0; 	
	unsigned int border_width = 4;	/* four pixels */
	unsigned int display_width, display_height;
	char *window_name = "overlay";
	XSizeHints size_hints;
	int count;
	XEvent report;
	extern GC gc;
	XFontStruct *font_info;
	char *display_name = NULL;
	int window_size = 0;	/* OK, or too SMALL to display contents */

	/* connect to X server */

	if ( (display=XOpenDisplay(display_name)) == NULL )
	{
		(void) fprintf( stderr, " cannot connect to X server %s\n",
			       XDisplayName(display_name));
		exit( -1 );
	}

	/* get screen size from display structure macro */
	screen = DefaultScreen(display);
	display_width = DisplayWidth(display, screen);
	display_height = DisplayHeight(display, screen);

	/* size window with enough room for text */
	width = display_width/3, height = display_height/4;

	/* create opaque window */
	win = XCreateSimpleWindow(display, RootWindow(display,screen), 
				  x, y, width, height, border_width, 
				  BlackPixel(display, screen),
				  WhitePixel(display,screen));

	/* Set resize hints */
	size_hints.flags = PPosition | PSize | PMinSize;
	size_hints.x = x;
	size_hints.y = y;
	size_hints.width = width;
	size_hints.height = height;
	size_hints.min_width = 300;
	size_hints.min_height = 200;

	/* Select event types wanted */
	XSelectInput(display, win, ExposureMask | KeyPressMask | 
		     ButtonPressMask | ButtonReleaseMask | 
		     ButtonMotionMask | PointerMotionHintMask |
		     MotionNotify | StructureNotifyMask);

	/* create region for exposure event processing */
        if (get_colors() == CAN_OVERLAY) printf("can overlay !\n");

	getGC(win, &gc);

	XMapWindow(display, win);

	/* get events, use first to display text and graphics */
	while (1)  {
		XNextEvent(display, &report);
		switch  (report.type) {
		case Expose:
		  place_graphics(win, gc, width, height);
		  break;
		case ConfigureNotify:
		  width = report.xconfigure.width;
		  height = report.xconfigure.height;
		  break;
		case MotionNotify:
		  break;
		case ButtonPress:
		  x = report.xbutton.x;
		  y = report.xbutton.y;
		  break;
		case KeyPress:
		  break;
		default:
		  break;
		} /* end switch */
	} /* end while */
}

getGC(win, gc)
Window win;
GC *gc;
{
	unsigned long valuemask = 0; /* ignore XGCvalues and use defaults */
	XGCValues values;
	unsigned int line_width = 6;
	int line_style = LineSolid;
	int cap_style = CapRound;
	int join_style = JoinRound;

	/* Create default Graphics Context */
	*gc = XCreateGC(display, win, valuemask, &values);

	/* specify black foreground since default may be white on white */
	XSetForeground(display, *gc, foreground);

	/* set line attributes */
	XSetLineAttributes(display, *gc, line_width, line_style, 
			   cap_style, join_style);

}

void place_graphics(win, gc, window_width, window_height)
Window win;
GC gc;
unsigned int window_width, window_height;
{
	int x, y;
	int width, height;
	char  *some_color   = "Red";
	XColor *pix_def, *pix_exact;

	height = window_height/2;
	width = 3 * window_width/4;
	x = window_width/2 - width/2;  /* center */
	y = window_height/2 - height/2;
/*	XSetFunction(display, gc, GXxor);*/

	XSetForeground(display, gc, foreground);
	XSetBackground(display, gc, background_pixel);
	XDrawLine(display, win, gc, 0, 0, 300, 200);


/*	XSetPlaneMask(display, gc, overlay_plane);*/
	XSetForeground(display, gc, overlay_pixel_1);
	XSetBackground(display, gc, overlay_pixel_2);
	XDrawRectangle(display, win, gc, x, y, width, height); 

	XDrawArc(display, win, gc, x, y, width, height, 0, 23040);
	
	XSetForeground(display, gc, foreground);
	XSetBackground(display, gc, background_pixel);
	XDrawLine(display, win, gc, 300, 0, 300, 200);
}

int
get_colors()
{
  int depth;
  static char *name[] = {"Red", "Yellow", "Green", "Green"};
  XColor exact_defs[MAX_COLORS];
  extern Colormap cmap;
  int ncolors = 4;
  extern  int plane_masks[MAX_PLANES];
  int colors[MAX_COLORS];
  int i;
  Visual *visual;
  
  depth = DisplayPlanes(display, screen);
  visual = DefaultVisual(display, screen);
  cmap = DefaultColormap(display, screen); 
/*  cmap = XCreateColormap(display, win, visual, AllocNone);*/

  if (depth == 1) { /* one-plane monochrome */
    /* use BlackPixel and WhitePixel only */
    background_pixel = WhitePixel(display, screen);
    foreground = BlackPixel(display, screen);
    printf("unsing black and white\n");
    return (CANNOT_OVERLAY);	
  }
  else {
    /* allocate color cells */
    if (XAllocColorCells (display, cmap, False, plane_masks, 1,
			  colors, 4) == 0) {
      /* Can't get enough read/write cells to overlay.
       * Trying at least to get three colors. */
      if (XAllocColorCells (display, cmap, False, plane_masks,
			    0, colors, 3) == 0) {
	/* Can't even get that. Give up and use black and white */
	background_pixel = WhitePixel(display, screen);
	foreground = BlackPixel(display, screen);
	printf("unsing black and white\n");
      }

      ncolors = 3;
      printf("got only three colors\n");
    }
    ncolors = 4;

    /* Allocated colors succesfully. Now setting their colors: 
       3, and 4 are set to the same RGB value */
    for (i = 0; i < ncolors; i++)
      {
	if (!XParseColor (display, cmap, name[i], &exact_defs[i]))
	  {
	    fprintf(stderr, "overlay: color name %s not in database \n", 
		    name[i]);
	    exit(0);
	  }

	exact_defs[i].flags = DoRed | DoGreen | DoBlue;
      }
    printf("got RGB values\n");

    /* Set pixel value in struct to allocated ones */
    exact_defs[0].pixel = colors[0];
    exact_defs[1].pixel = colors[1];
    exact_defs[2].pixel = colors[0] | plane_masks[0];
    exact_defs[3].pixel = colors[1] | plane_masks[0];

    printf("set RGB values\n");

    /* this sets the color of the read/write cell */
    XStoreColors (display, cmap, exact_defs, ncolors);
    printf ("stored colors\n");
    background_pixel = exact_defs[0].pixel;
    foreground = exact_defs[1].pixel;


    if (ncolors == 4) {
      overlay_pixel_1 = exact_defs[2].pixel;
      overlay_pixel_2 = exact_defs[3].pixel;
      overlay_plane = plane_masks[0];
      XFlush(display);
      return(CAN_OVERLAY);
    }
    else {
      overlay_pixel_1 = colors[2];
      return(CANNOT_OVERLAY);
    }
  }
}