[comp.windows.x] XtConvert & SetValues for floats

mikeg%monsoon.c3@lanl.gov (Michael P. Gerlek) (07/25/89)

From article <1600@bacchus.dec.com>, by asente@decwrl.dec.com (Paul Asente):
> In article <1208@quintus.UUCP> pds@quintus.UUCP (Peter Schachte) writes:
>> I'm trying to convert strings to the appropriate argument types to be
>> put into the ArgList to be passed to XtCreateWidget() or
>> XtAppCreateShell(), so I don't have a widget yet to pass to XtConvert().
>> Can I pass it a NULL?
> 
> No.  When creating a widget the right thing is usually to pass in some
> other widget on the same display, usually the new widget's parent.  If
> you're creating the very first widget in the application you can't do
> conversions; create the widget then use XtSetValues.

I have a related problem: I've written a widget which needs to use
floats in it's resource list, but I can't get it to work quite right:

The initialize procedure assigns the correct default values, but when I
try to do a set_values after realizing the widget on the float resource
I get garbage (0).  And yet a parallel resource which I kludged into
using strings (manually converted into floats) for the same purpose
works fine. 

So, okay, I'm new to Xt -- what am I missing? Should I be declaring a
Converter? If so, _why_? I've R'ed the FM, and found no help.  And there
aren't any other widgets that use floats to look at for a hint.  Is
there something wierd about floats, or have I made a mistake in my code
somewhere [minimal test case +150 lines, too long to include here]?

Help! And thanks...

[  M.P.Gerlek (mikeg@watson.c3.lanl.gov)      "Unix is just Public     ]
[  Los Alamos Nat'l Lab / Merrimack College       Domain VMS."         ]
[  Disclaimer: Yes, Mom, I'll play nice.             -anonymous        ]

ben@hpcvlx.HP.COM (Benjamin Ellsworth) (07/26/89)

> ... Is there something wierd about floats ...?

You betcha!

The Arg struct has a char * for name and a caddr_t for value.  When a
float is loaded into a caddr_t, your friendly C compiler converts the
float into an int.  Ain't that great?!

Now perhaps you can understand why there are no examples of widgets
which use float resources.  :-(

You can set up your resource as a pointer to a float, but that brings
its own problems.

Good Luck!

-----------------------------------------------------------------------
Benjamin Ellsworth      | ben@cv.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.
-----------------------------------------------------------------------

swick@ATHENA.MIT.EDU (Ralph R. Swick) (07/26/89)

> The initialize procedure assigns the correct default values, but when I
> try to do a set_values after realizing the widget on the float resource
> I get garbage (0).
...
> Is
> there something wierd about floats, or have I made a mistake in my code

Yeah, there's something wierd about floats.  If you try
to pass floats by value rather than by reference and assign
a float value to an arglist entry in one of the normal ways,
the C compiler will cleverly convert the float to a long.

You have to do some ugly stuff with unions to convince the
compiler not to convert the value when storing it in the arglist.

mikeg%monsoon.c3@lanl.gov (Michael P. Gerlek) (07/26/89)

From article <100920091@hpcvlx.HP.COM>, by ben@hpcvlx.HP.COM (Benjamin Ellsworth):
> 
>> ... Is there something wierd about floats ...?
> 
> You betcha!
> [...]
> Now perhaps you can understand why there are no examples of widgets
> which use float resources.  :-(

Hmm.  Okay - FYI, I've discovered the Athena Scroll widget uses floats
to represent percentages.  I've not used the Athena kit (I use the HP
set), but is this a known bug in the Scroll widget, is there some
work-around, or will this will be fixed in R4?

Swick@Athena said I could do "some ugly stuff with unions to convince
the compiler not to convert the value when storing it in the arglist". 
Can anyone provide any pointers [no pun intended] on what this means
and/or how to do this?

Thanks again. 


[  M.P.Gerlek (mikeg@monsoon.c3.lanl.gov)      "Unix is just Public    ]
[  Los Alamos Nat'l Lab / Merrimack College       Domain VMS."         ]
[  Disclaimer: Yes, Mom, I'll play nice.             -anonymous        ]

net@TUB.BITNET (Oliver Laumann) (07/27/89)

> The Arg struct has a char * for name and a caddr_t for value.  When a
> float is loaded into a caddr_t, your friendly C compiler converts the
> float into an int.  Ain't that great?!
>
> Now perhaps you can understand why there are no examples of widgets
> which use float resources.  :-(

When float resources are used, a pointer to a float is stored into the
argument value instead of a literal float.  The Xaw scroll bar widget at
least (which has XtRFloat resources) works quite well.

Or am I missing something?

--
Oliver Laumann              net@TUB.BITNET              net@tub.UUCP

slp@genrad.uucp (Stephen L. Peters) (07/28/89)

In article <8907270952.AA01762@tub.UUCP> net@TUB.BITNET (Oliver Laumann) writes:
>> Now perhaps you can understand why there are no examples of widgets
>> which use float resources.  :-(
>
>When float resources are used, a pointer to a float is stored into the
>argument value instead of a literal float.  The Xaw scroll bar widget at
>least (which has XtRFloat resources) works quite well.
>
>Or am I missing something?

Alas, you're missing something.  Or more to the point, the people who
didn't check that floats didn't work missed something.

The float resource will not automatically be converted to a pointer.
No way, no how.  If you want to do this, you have to do it explicitly
(something that I ended up doing when I ran across this problem) by
giving the pointer to XtSetArg, and having your widget expect pointers
to floats.

The Xaw scroll bar widget does not work.  I looked closely at what was
happening with it myself when I started having problems with my
widgets.  When you pass the scroll bar a float with XtSetArg, it
truncates the percentage to an integer (giving a 0) and then, within
the scrollbar code, it looks at the zero and converts it to the
default.  In other words, unless you pass a 1.0 with the XtSetArg, you
will always get the default value for XtNshown or XtNtop.

The scrollbar widget gets around this by having the
XtScrollbarSetThumb procedure, so it looks like it works to the user.
It doesn't, though.

			-- SLPster