[comp.windows.x] Help on Resource Conversion anyone?

uhley@tybalt.caltech.edu (John Uhley) (02/11/89)

I'd like to be able to specify a floating point resource and use 
XtSetArg to modify it on the creation of a widget.  Thus:

	{ XtSomeResource, XtCSOMETHING, XtRFloat, sizeof(float),
		XtOffSet(Widget, thing.floating_field), XtRFloat, 
		(caddr_t)&def_float}

is my resource and I want to be able to say:

	XtSetArg(arg[n], XtSomeResource, 1.00); n++;
	XtCreateManagedWidget(..., arg, n);

and have my "thing.floating_field" field set to "1.00"

I've tried adding a class_init routine which does an

XtAddConverter(XtRFloat, XtRFloat, CvtFloatToFloat, NULL, 0);

but although this gets called, CvtFloatToFloat (which I've written)
is NEVER called.

Anyone have a clue as to what I've done wrong?

	Thanks,

		John
		uhley@tybalt.caltech.edu

ben@hpcvlx.HP.COM (Benjamin Ellsworth) (02/14/89)

> I'd like to be able to specify a floating point resource and use 
> XtSetArg to modify it on the creation of a widget.
>
> ... I want to be able to say:
> 
> 	XtSetArg(arg[n], XtSomeResource, 1.00); n++;
> 	XtCreateManagedWidget(..., arg, n);
> 
> and have my "thing.floating_field" field set to "1.00"
> 
> I've tried adding a class_init routine which does an
> 
> XtAddConverter(XtRFloat, XtRFloat, CvtFloatToFloat, NULL, 0);
> 
> but although this gets called, CvtFloatToFloat (which I've written)
> is NEVER called.
> 
> Anyone have a clue as to what I've done wrong?

This is a brain bug.  You must realize that converters are "NEVER"
called on arguments that are passed in via arglists.  It is assumed
that the application has loaded the right type in the arglist.

[ The following may not apply to you on your machine. ]

Unfortunately, this is where funny little compiler/architecture
problems come into play.  The type of arg.value is caddr_t.  Due to the
implementation of promotion rules on some machines (I'm not a compiler 
wizard, so the implementation could be entirely correct), when a float 
is assigned to a caddr_t, it is converted to an int.  But you don't
want an int?  Too dang bad.  You'll have to come up with a work-around.

Good Luck,

-----------------------------------------------------------------------
Benjamin Ellsworth      | ben%hp-pcd@hp-sde.sde.hp.com | INTERNET
Hewlett-Packard Company | {backbone}!hplabs!hp-pcd!ben | UUCP
1000 N.E. Circle        | (USA) (503) 750-4980         | FAX
Corvallis, OR 97330     | (USA) (503) 757-2000         | VOICE
-----------------------------------------------------------------------
                     All relevant disclaimers apply.
-----------------------------------------------------------------------

shankar@hpcllca.HP.COM (Shankar Unni) (02/15/89)

> Unfortunately, this is where funny little compiler/architecture
> problems come into play.  The type of arg.value is caddr_t.  Due to the
> implementation of promotion rules on some machines (I'm not a compiler 
> wizard, so the implementation could be entirely correct), when a float 
> is assigned to a caddr_t, it is converted to an int.  But you don't
> want an int?  Too dang bad.  You'll have to come up with a work-around.

Like

    float f = 1.00;
    ...
    XtSetArg(arg[n], XtSomeResource, *((caddr_t *)&f)); n++;

Assuming, of course, that a caddr_t and a float have the same size (otherwise
you're hosed..);
---
Shankar Unni.