[comp.windows.x] X Toolkit color problem

boreas@bucsb.UUCP (Michael A. Justice) (08/17/89)

Having finally written a working widget, I tried making sure it worked
properly in color.  After a few hours of fiddling, I tried other widgets,
notably the Label widget in the Xaw library.  Color doesn't seem to work
properly for it, either, unless set with the command-line -bg and -fg
switches.

The system is a Sun 3/60 with a CG4, X11R3, the MIT sample server.  I
don't know for certain how well-patched everything is;  however, one
staff member here (who refused to commit himself :-) said he believes
our X stuff is up-to-date as far as official patches goes, and has the
Purdue speedups installed.

The following program is what I was "testing" with, compiled with the
Xaw, Xmu, Xt, and X11 libraries.

If run as "a.out -fg red -bg green" (or other named colors) it works fine.
If run as "a.out red green" the colors are wrong, and if "a.out blue red"
is run while the "red/green" window is still up, the new widget usually
comes up with the same off-colors as the earlier one.  (Sometimes they're
different weird colors, again not the ones specified.)

The same happens if the color is named within the program as a constant
(as in    XtSetArg (args[n], XtNbackground, "red") ;   etc.) instead of
placed on the command line.  The same also happens if I declare the
string as     static char fore[] = "red";  etc.  Using the resource
manager (-xrm options on the command-line) works fine, BTW.

I took a look in Young's Xt book;  chapter six, on color, doesn't deal
with toolkit color allocation much.  Looked good for Xlib, though, and
for using Xlib calls within toolkit programs.  Sigh.

What am I doing wrong?  Or am I?

		    Thanks in advance,

			-- Michael.

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Core.h>
#include <X11/Shell.h>
#include <X11/Label.h>

void main (argc, argv)
int     argc ;
char    *argv[] ;
{
    Widget toplevel, test ;
    Arg args[5] ;
    int n ;

    toplevel = XtInitialize ("main", "Label", NULL, 0, &argc, argv) ;

    n = 0 ;
    XtSetArg (args[n], XtNlabel, "this is a test") ; n++ ;

    if (argc != 1) /* assume user used two color args, since this *is*
			supposed to set both the fore- and background */
    {
        XtSetArg (args[n], XtNforeground, argv[1]) ; n++ ;
        XtSetArg (args[n], XtNbackground, argv[2]) ; n++ ;
    }

    test = XtCreateManagedWidget("test", labelWidgetClass, toplevel, args, n) ;

    XtRealizeWidget (toplevel) ;
    XtMainLoop () ;
}
-- 
BITNet: cscj0an@buacca \  Michael Andrew Justice @ BU Graduate School (CS)
ARPA: boreas@bucsb.bu.edu  \     "My sophistication surprises you, Zorba?"
CSNET: boreas%bucsb@bu-cs      \  "Your existence surprises me, Bald Ape."
UUCP: ...!husc6!bu-cs!bucsb!boreas \ S.R. Boyett, _The_Architect_of_Sleep_

kit@EXPO.LCS.MIT.EDU (Chris D. Peterson) (08/17/89)

When using XtSetArg you must pass in the correct pixel value, not the name
of the color.  You will have to get the pixel value yourself.  Here is
one way to get that pixel value from a string.

						Chris D. Peterson     
						MIT X Consortium 

Net:	 kit@expo.lcs.mit.edu
Phone:   (617) 253 - 9608	
Address: MIT - Room NE43-213

----------------------------------------------------------------

/*	Function Name: ConvertColor
 *	Description: This converts a string into a color.
 *	Arguments: color_name - name of the color.
 *	Returns: a pixel value for that color.
 */

static Pixel
ConvertColor(w, color_name)
Widget w;
char * color_name;
{
  XrmValue from, to;

  from.size = sizeof(color_name);  
  from.addr = color_name;

  XtConvert(w, XtRString, (XrmValuePtr) &from, XtRPixel, (XrmValuePtr) &to);
  if (to.addr == NULL) exit(1);

  return( (Pixel) *(to.addr) );
}