[comp.windows.x] Widget Name

hwa@ece-csc.UUCP (George Hwa) (04/12/89)

	I am working on an application that uses numerous widgets.  For one
of the callback functions of a CommandWidget, I need to know the name
of the widget that called the callback.  How can I do this?


	Thanks in advance.

	Russ Cook
	NC State University
	hwa@ece-csc.ncsu.edu

spa%hara.fctunl.rccn.pt@MITVMA.MIT.EDU (04/13/89)

>       I am working on an application that uses numerous widgets.  For one
> of the callback functions of a CommandWidget, I need to know the name
> of the widget that called the callback.  How can I do this?
>
>
>       Thanks in advance.
>
>       Russ Cook
>       NC State University
>       hwa@ece-csc.ncsu.edu

Have your callback routine do:

        1) an XtGetValues on the XtNlabel argument (slow but safe)
        2) use the value of

                w->core.name

Solution 2 is only valid if you #include <X11/IntrinsicP.h>
---
Salvador Pinto Abreu                 BITNET/Internet: spa@hara.fctunl.rccn.pt
                                                  or: FCT_SPA@PTIFM.BITNET
UUCP: ...{mcvax,inria}!inesc!unl!spa          PSI/VMS: PSI%(+2680)05010310::SPA
ARPA: spa%hara.fctunl.rccn.pt@mitvma.mit.edu  Phone: (+351) (1) 295-4464 x.0560
                                                Fax: (+351) (1) 295-4461
Snail:  Dept. de Informatica, Universidade Nova de Lisboa
        2825 Monte Caparica, PORTUGAL

kit@EXPO.LCS.MIT.EDU (Chris D. Peterson) (04/19/89)

>       I am working on an application that uses numerous widgets.  For one
> of the callback functions of a CommandWidget, I need to know the name
> of the widget that called the callback.  How can I do this?

> Have your callback routine do:

>         1) an XtGetValues on the XtNlabel argument (slow but safe)

This won't work, the default is to have the label use the name,
but if someone adds the line "*Command*label: foo" to their resource
file this will screw you over.  Label and Name are NOT the same.

>         2) use the value of w->core.name.

This breaks abstraction barriers, but works.  The best thing is to 
write your own XtWidgetToName() function, so that you can convert
over when this function gets added to the Intrinsics.  Unfortunatly 
until this function gets added to the Instrincs there is no way to 
do this cleanly.


                                                Chris D. Peterson
                                                MIT X Consortium

ifocs9d@aucs.UUCP (Rick Giles) (06/07/89)

How do I get the name which was used in creating an HP widget later on in
the program? I'm guessing that it's possible using XtGetValues, but haven't
been able to discover the correct name/address pair for an argument list.
Any help will be appreciated.

(A more general question is, How should I associate extra data with a
widget, and later access it?).

Rick Giles

Bitnet:   FRGILES@Acadia                        | Procrastinators
Internet: FRGILES%Acadia.BITNET@CUNYVM.CUNY.EDU | finish
UUCP:     uunet!dalcs!aucs!ifocs9d              | first.

csvsj@garnet.berkeley.edu (Steve Jacobson) (06/09/89)

In article <1937@aucs.UUCP> ifocs9d@aucs.UUCP (Rick Giles) writes:
>
>How do I get the name which was used in creating an HP widget later on in
>the program? I'm guessing that it's possible using XtGetValues, but haven't
>been able to discover the correct name/address pair for an argument list.
>Any help will be appreciated.
>
>(A more general question is, How should I associate extra data with a
>widget, and later access it?).
>
>Rick Giles
>
>Bitnet:   FRGILES@Acadia                        | Procrastinators
>Internet: FRGILES%Acadia.BITNET@CUNYVM.CUNY.EDU | finish
>UUCP:     uunet!dalcs!aucs!ifocs9d              | first.

Here's one way to do it:

You can put data or a pointer to data in the closure member of the
XtCallbackRec. Make a simple noop() function that just returns and
use that for the callback member. Use XtAddCallback to add it to one
of the widget's callback lists. The destroy callback list is a good
one to use.

You can then use XtGetValues to retrieve the Callback list to examine the
closure value. To change the closure value, you have to first use
XtRemoveCallback to remove the structure with the existing closure value,
and then use XtAddCallback to add it back with the new closure value.

You have to be a little careful - most widgets don't have a destroy
callback, but you can't always assume that the callback you added will 
always be the only one. For example, the HP menu managing widget adds a
destroy callback to its parent widget. So you either have to "know" where
your callback structure is in the list, or you need to check the callback
member, looking for the pointer to your noop() function.

This stuff only works with the R3 intrinsics (Xt). The callbacks were screwed
up in R2; it was possible to use the callbacks in this manner, but you
had to do some really ugly things (the method outlined above isn't
so pretty, but at least it doesn't violate the toolkit's object data
hiding practices).

All this stuff would be unneccesary if there was a "client data" resource
in the Core widget. Then simple and sensible XtGetValues and XtSetValues
calls would do the trick.

I asked about this in a posting and mailing, but got no response.

ben@hpcvlx.HP.COM (Benjamin Ellsworth) (06/10/89)

> How do I get the name which was used in creating an HP widget later 
> on in the program?

It's hard.  IMHO it is a hole in the Xt functionality.

> I'm guessing that it's possible using XtGetValues...

Keep guessing.

If I had to do it I would:

	1- In a separate file include IntrinsicP.h and write a function

		char * XtName(w)
			Widget w;
		{
			return(w->core.name);
			/* Or you could malloc and copy... */
		}
	
	2- Make that file into a .o and link with it.

Yeah, it's a somewhat cluttered approach, but when the Intrinsics 
support the functionality all you have to do is throw away one file and
change your link line.

>Any help will be appreciated.

Small unmarked bills are best...  ;-)

> (A more general question is, How should I associate extra data with a
> widget, and later access it?).

Not easy to do if you want to attach it to the widget structure itself.
In R3 you could take over the extension field, but you might get
clobbered later.  I think that I would use the Xrm database.

-----------------------------------------------------------------------
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.
-----------------------------------------------------------------------

asente@decwrl.dec.com (Paul Asente) (06/11/89)

In article <100920081@hpcvlx.HP.COM> ben@hpcvlx.HP.COM (Benjamin Ellsworth) writes:
>> How do I get the name which was used in creating an HP widget later 
>> on in the program?
>
>It's hard.  IMHO it is a hole in the Xt functionality.

It is.  You could wait for R4, which will have an XtName function.  In the
meantime writing a function like Benjamin suggested is the best solution.

	-paul asente
	    asente@decwrl.dec.com	decwrl!asente

sean@luna.dpl.scg.hac.com (Sean Cohan) (10/04/90)

I am using XQueryPointer and XtWindowToWidget to get the widget at the current 
location of the pointer.  I need to get the name of the returned widget.  I am
using X11R3 so I can't use XtName that is available in X11R4 and Motif1.01.

If I create a widget like:

	button = XtCreateWidget("Button", XmPushButtonWidgetClass, parent, args,
                                argcnt);

How can I retrieve the widget name "Button"?

Right now I am trying to access the widget structure like:

	printf("widget's name: %s\n", button->core.name);

but I get a segmentation fault.  Looking at it with gdb, I get a valid address
for button, but button->core is bogus.

Any suggestions?

Thanks in advance.