[comp.windows.x] Stupid question about Xor'ing a Pixmap

jeremy@EIK.II.UIB.NO (10/04/90)

I hope somebody can give me a hint about the following (what I thought
was a simple problem):

I have a colour machine and want to plot a small cursor in an
application.  The (bits that are set in the) cursor should be coloured
- not just black.  I want to use a pixmap or bitmap as the cursor. The
cursor has to be able to move around and so I need to be able to remove
it and put it somewhere else. The background is not necessarily blank
so I need to put back what was there before. (The cursor shows a point
in a graph, it's not the mouse cursor).

Sooo.. my question is what combination of Pixmap/Bitmap and GC should I
use. I've tried using XCopyPlane of a bitmap with an Xor GC, removing
the old cursor by plotting it again in the same place, but I seem to
come unstuck if the background colour is not 0. I think I got it to
work with background set to 0, but that's not really what I want. I
also got it to work if I draw the cursor using graphics commands, but
that's not really what I want either - the cursor should be user
specifiable as a bitmap.

I'd be most grateful for any hints/tips . I'm tearing my hair out over
this one - I'm sure the answer must be simple.

-------------------------------------------------------------------------------
 Jeremy Cook                      .----.
 Parallel Processing Laboratory  /    /                      /  /        /
 University of Bergen           /    /                      /  /        /
 Thormoehlensgate 55           /----' .----. .----. .----. /  / .----. /----.
 N-5008 Bergen                /      _____/ /      _____/ /  / _____/ /    / 
 Norway                      /      (____/ /      (____/ /  / (____/ (____/
-------------------------------------------------------------------------------
 email : jeremy@eik.ii.uib.no    | "My other computer is a MasPar MP1208"
 phone : +47 5 54 41 74 (direct) |
 fax   : +47 5 54 41 99          |
-------------------------------------------------------------------------------

mouse@LARRY.MCRCIM.MCGILL.EDU (10/05/90)

> I have a colour machine and want to plot a small cursor in an
> application.  The (bits that are set in the) cursor should be
> coloured - not just black.  I want to use a pixmap or bitmap as the
> cursor.  The cursor has to be able to move around and so I need to be
> able to remove it and put it somewhere else.  The background is not
> necessarily blank so I need to put back what was there before.

> Sooo.. my question is what combination of Pixmap/Bitmap and GC should
> I use.  I've tried using XCopyPlane of a bitmap with an Xor GC,
> removing the old cursor by plotting it again in the same place, but I
> seem to come unstuck if the background colour is not 0.

One possibility is to make the "cursor" a small window of its own and
use the SHAPE extension to give it the shape you want.  Restoring the
stuff underneath it when you move it amounts to just handling
exposures, which you must be doing anyway (either by handling Expose
events or by arranging for the server to do it for you, such as by
setting background pixmaps).  Obviously, this way can't work unless
your server supports the SHAPE extension.

If you really want to do it by drawing graphics on the window...you
want to overlay a shape in some color on top of a drawing involving
many colors, right?  Then what I would suggest is that when allocating
the colormap cells you use XAllocColorPlanes to allocate an "overlay"
plane in the colormap.  (If the cursor requires more than one bit of
overlay, ask for more than one plane when allocating.)  Then set up the
colormap so that for some setting of the "overlay" bits (all zero,
probably) the colors are appropriate for your graphics.  Then for each
color you want in the cursor, choose some combination of the overlay
planes and load the desired color into all N of the cells having that
combination of overlay bits (N being the number of colors your
underlying graphics need).  Then just fiddle with the plane-mask in the
GC to draw in only the underlying graphics bits or only in the overlay
bits, or just draw with GXxor and be careful.  (Using GXxor on
multi-plane displays requires care.  There is a strong temptation to
put the foreground pixel value into the GC's foreground; this works
properly only when the background pixel value happens to be zero.  What
you want to put into the GC's foreground is the XOR of the foreground
and background pixel values.)

All of the above are designed to avoid having to deal with repairing
the underlying graphics any more than necessary.  However, presumably
you can repair the graphics; if nothing else, exposures can happen.
You could draw the cursor by blitting a Pixmap onto the window, then
remove it by repairing the drawing, as if an Expose event had happened.
As for specifically how to draw the cursor, I'd suggest forming a
bitmap describing the shape and then either setting that as the
clip-mask of the GC and filling (for a solid cursor) or copying (for a
colorful cursor) or setting it as the stipple and doing a fill with a
fill-style of FillStippled.  (The latter method works only when only
one color is to be overlaid.)

What's best depends on tradeoffs like how many colors the underlying
drawing needs and how many colors the cursor needs.  If you'd care to
drop me a line describing your needs and your server's capabilities in
a little more detail I'd be glad to try to pick a method more
specifically suited to your situation.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu