[comp.windows.x.motif] writing Widgets with Float resources

nazgul@alfalfa.com (Kee Hinckley) (05/30/91)

> It appears that no matter what I try, XtSetValues passes junk
> as the float;  as soon as I get into my SetValues function and
> take a look at what is passed, I get some bizarre value.  This
Look at the structure.  sizeof(float) is probably greater than
sizeof(long).  You'll need to use pointers to float or some such.

Alfalfa Software, Inc.          |       Poste:  The EMail for Unix
nazgul@alfalfa.com              |       Send Anything... Anywhere
617/646-7703 (voice/fax)        |       info@alfalfa.com

I'm not sure which upsets me more: that people are so unwilling to accept
responsibility for their own actions, or that they are so eager to regulate
everyone else's.

anderson@rubato.lbl.gov (05/31/91)

I have started to write a few Motif widgets (subclassing Primitive,
for instance), and have things working fine as long as various
resources are "ints" (XmRInt type resources).  But I need them
to be "floats"  (XmRFloat types).

It appears that no matter what I try, XtSetValues passes junk
as the float;  as soon as I get into my SetValues function and
take a look at what is passed, I get some bizarre value.  This
does not occur when the resource file is read and values are
assigned - from the resource file everything is fine; only when
I do the XtSetValues programmatically do I have this trouble...

I've searched all the commercial and PD widgets out there that I
have had access to, and no one seems to have a working example.
I don't want to have to pass it in as a string...

My development environment is X11R4 and Motif 1.1 from DEC on a
DECstation 5000.


Has anyone successfully created a widget of their own using some
float resources?  I find it a bit odd that even OSF doesn't seem
to have any.


Thanks for any help or advice you can lend.

	Mark Anderson
	anderson@rubato.lbl.gov
	anderson@rhea.aps.anl.gov

nmm@cl.cam.ac.uk (Nick Maclaren) (05/31/91)

In article <910530224357.250@sony> nazgul@alfalfa.com (Kee Hinckley) writes:
>> It appears that no matter what I try, XtSetValues passes junk
>> as the float;  as soon as I get into my SetValues function and
>> take a look at what is passed, I get some bizarre value.  This
>Look at the structure.  sizeof(float) is probably greater than
>sizeof(long).  You'll need to use pointers to float or some such.

This may be the reason, though I rather doubt it.  I suspect that you
have been caught by a combination of the 'designs' of C and X.

If XtArgVal is char *, you are in uncharted waters.  Let us assume that
it is long (which it often is).  You are then going to end up with a
coercion from float to long, which converts the value numerically.  If
you try to avoid this by casting it to void * or char *, you are back
in uncharted waters!

I advise trying XtVaSetValues - this is at least POSSIBLE to implement
in ANSI C.  However, be warned about automatic conversion to double
in argument lists - there are some horrible traps in some compilers.

Nick Maclaren
University of Cambridge Computer Laboratory
nmm@cl.cam.ac.uk

doyle@doyled.b23b.ingr (Doyle C. Davidson) (05/31/91)

>
>I have started to write a few Motif widgets (subclassing Primitive,
>for instance), and have things working fine as long as various
>resources are "ints" (XmRInt type resources).  But I need them
>to be "floats"  (XmRFloat types).

To paraphrase "X Window System Toolit" by Asente and Swick 
(this is a great book!):

    A problem exists with storing FLOAT value into an argument list;
the C compiler will cast the FLOAT value to an integer due to
the definition of XtArgVal.  The most reliable way to do this 
is to declare a union type:
    union {
	float f;
	int   i;
    } hack;
Assign the FLOAT value to hack.f and store hack.i into the argument list.
If the resource is defined as a pointer to a FLOAT value, this problem
does not occur.  This only works if sizeof(float) == sizeof(int).

IMHO: The proper way to do PORTABLE float/double resources is to implement
them as pointer to values.  You will have to copy the value into
a private field since the resource field itself will be a pointer.

I have done this before but I backed away and used ints for my resource
value since float was overkill since screen resolution is integer based.

>	Mark Anderson

Doyle
-------------------------------------------------------------------
Doyle C. Davidson              |
Intergraph Corp.               | Intergraph - Everywhere you look!!
Third Party Software           |
1 Madison Industrial Park      |
Huntsville, AL 35806           |  These comments are...
(205) 730-2000                 |
                               |         X-clusively my own.
..!uunet!ingr!doyle            |
- or - doyle@ingr.com          |
-------------------------------------------------------------------

marbru@auto-trol.com (Martin Brunecky) (06/01/91)

In article <13743@dog.ee.lbl.gov> anderson@rubato.lbl.gov () writes:
>
>I have started to write a few Motif widgets (subclassing Primitive,
>for instance), and have things working fine as long as various
>resources are "ints" (XmRInt type resources).  But I need them
>to be "floats"  (XmRFloat types).
>
>It appears that no matter what I try, XtSetValues passes junk
>as the float;  as soon as I get into my SetValues function and
>take a look at what is passed, I get some bizarre value. 

  We do use WsRFloat resources (as well as WsRDouble, but those are
  passed by reference).
  Your problem is not that mysterious.
  It is the "C" compiler which typecasts the "value", and for the
  conversion from float it does what you see.

  The way we do SetValues on WsRFloat resources is ....

  typedef union u
   {
   long int   int;
   float      float;
   }

  u value;
  value.float = desired float value
  XtSerArg ( arg[n], WsNangle, value.int );

  This way, the typecast in SetArg does no damage, and we get our floats
  delivered right.

-- 
=*= Opinions presented here are solely of my own and not those of Auto-trol =*=
Martin Brunecky                      marbru%auto-trol@sunpeaks.central.sun.com
(303) 252-2499                       (better avoid:  marbru@auto-trol.COM )
Auto-trol Technology Corp. 12500 North Washington St., Denver, CO 80241-2404 

david@mnetor.UUCP (David So) (06/01/91)

In article <13743@dog.ee.lbl.gov> anderson@rubato.lbl.gov () writes:
=
=>I have started to write a few Motif widgets (subclassing Primitive,
=>for instance), and have things working fine as long as various
=>resources are "ints" (XmRInt type resources).  But I need them
=>to be "floats"  (XmRFloat types).
=
=
    If sizeof(float) is the same as sizeof(long) for your compiler,
    then the problem is with the compiler trying to cast a float value
    to long/int.  Try this to see if that helps:

    XtSetArg(arg, resource_name, *((long *)&float_value));

    or

    XtSetArg(arg, resource_name, *((int *)&float_value));


=>Has anyone successfully created a widget of their own using some
=>float resources?  I find it a bit odd that even OSF doesn't seem
=>to have any.
=
=
    'float' resource was quite a recent feature that didn't exist
    in the early stage of Intrinsics development when Motif
    widgets were designed.
-- 
David So

dob@inel.gov (Dave Brooks) (06/05/91)

Bob Toole writes:
> It's been so long ago, I forget exactly what the problem is.
> Something to do with automatic promotion of floats to doubles and how
> the intrinsics deal with the arguments whose size is greater than an int.

This is correct.  On some machines, float is larger than int.
Consequently, passing one as a resource really passes a pointer to the
float, which of course will hose you up.

One of the changes from R3 to R4, or perhaps it was R2 to R3, dealt with
this exact subject.  passing floats to and from scrollbars was changed to
pointer to float for the reasons mentioned above.

regards,
dlb
--
------------------------------------------------------------------------------
David L. Brooks
Idaho National Engineering Lab.  INTERNET: dob@INEL.GOV
POB 1625 MS 2603                 Phone: (208) 526-0826
Idaho Falls, Id. 83415		 FAX:   (208) 526-9936
------------------------------------------------------------------------------