From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
Newsgroups: fj.mail-lists.x-window
Subject: Re: help required with simple font program
Date: 11 Nov 92 13:42:55 GMT
Distribution: fj
Organization: McGill Research Centre for Intelligent Machines

Date: Fri, 6 Nov 92 11:21:05 GMT
Message-Id: <1992Nov6.112105.22033@thunder.mcrcim.mcgill.edu>
Newsgroups: comp.windows.x

In article <1992Nov1.210942.13899@dcs.warwick.ac.uk>, cgarnett@dcs.warwick.ac.uk (Chris. Garnett) writes:

> As a novice X window programmer, I am trying to learn from a recently
> published book, "X Window Inside and Out" by Levi Reiss and Joseph
> Radin (McGraw Hill).

> However, one program I have laboriously typed in doesn't seem to draw
> the text on screen as it should (example 6-1, if anybody has the
> book).  I have checked the code diligently,and I'm convinced there's
> an error in the original, as I have found several in the book
> already.

If you transcribed it reliably, there is indeed an error in the
original.

> The executable produces a window on the screen, but the text is not
> drawn in it.

Actually, it is; it's just that it's being drawn white-on-white, so you
can't see it.

> If anybody could help, the listing follows... ;-)

> #include <X11/Xos.h>   /* these two includes */
> #include <stdio.h>     /* were not in the original */

I removed these before compiling.  Works fine - for me - without them,
once I fix the real problem (which I'll get to below).

Here are the relevant excerpts.

> 					XDrawText(thedisplay, thewindow,
> 						thegc, 40, 30, line,
> 						MAX_ELEMENTS);

> 	background = WhitePixel(thedisplay, thescreen);
> 	foreground = BlackPixel(thedisplay, thescreen);

> 	thewindow = XCreateSimpleWindow(thedisplay,
> 		DefaultRootWindow(thedisplay), thehints.x, thehints.y,
> 		thehints.width, thehints.height, BORDER_WIDTH,
> 		foreground, background);
> 
> 	thegc = XCreateGC(thedisplay, thewindow, 0, 0);

The problem is that the thegc's foreground is 0 and its background is
1, because nothing else has been specified.  (As a portability note,
note that your code is correct only when the compiler supports
prototypes and a prototype for XCreateGC is in scope; the last two
arguments are of type "unsigned long int" and "XGCValues *", not two
ints.  You will get away with it on many - far too many - machines
because int, long, and pointer are all the same size, and integer-zero
is passed the same way as a pointer.  If you care about portability,
you should write ...XCreateGC(..., 0L, (XGCValues *)0); instead.)

Okay, so its foreground is 0 and its background is 1.  Why is this a
problem?  First of all, because there's no guarantee that 0 and 1
display as anything useful.  (As a simple example, consider a TrueColor
display, where 0 and 1 are likely to be visually indistinguishable.)
But the background doesn't matter anyway, because nothing you do uses
it.  The problem is that you're drawing text with a pixel value of 1
into a window whose background is the WhitePixel of the screen.  If, as
is the case on some (but not all) "monochrome" displays, these are the
same, you get white-on-white.

I added a line "XSetForeground(thedisplay,thegc,foreground);" just
after the XCreateGC call, and it worked like a champ.

					der Mouse

			    mouse@larry.mcrcim.mcgill.edu