[comp.windows.x.motif] XmString type converter

paul@slhisc.uucp (Paul Tam) (01/31/91)

I have posted this question and have gotten no answer. Now I 
want to post the question again and hope people outthere be 
able to help me with this.

This is a common problem with motif labelString. Resource manager
can't handle multiple line label string specified in resource file
.

Someone mentioned using a self-defined type converter. So I started
out testing the converter concept. So far I have been able to define
a converter, add the converter, and the converter is called when 
widgets are created. In the converter function, I convert the label
string into XmString type by using XmStringCreateLtoR. I also use
XmStringLength to find out the length of the XmString and store them 
back to the return struct. 

However, the application blows up after returning from the converter.
Does anybody have experience with labelString converter ? Or know what
is going when returning from the converter.

Another question is does motif 1.0.3 with X11 R3 fully support self-defined
type converter. The reason I ask this question is that when I put the 
XtAddConverter function call right after XtInitialize, the converter
was never invoked. If I move it down a little bit then It get called. 
Strange!!

I hope someone can help me with this and thanks to all you news readers.
 

lynnes@ALEX.CSS.GOV (Christopher Lynnes) (02/01/91)

>I have posted this question and have gotten no answer. Now I 
>want to post the question again and hope people outthere be 
>able to help me with this.
>
>This is a common problem with motif labelString. Resource manager
>can't handle multiple line label string specified in resource file
>
	You can add your own String->XmString converter.
Here is a crude solution:

#include <Xm/Xm.h>
#include <X11/Intrinsic.h>
#include <X11/Xresource.h>

void
CvtXmString(args, cnt, from, to)
        XrmValuePtr     *args;
        Cardinal        cnt;
        XrmValuePtr     from;
        XrmValuePtr     to;
{
        char    *s = from->addr;
        static XmString xms;

        if (s == NULL)
        {
                to->size = 0;
                to->addr = (caddr_t)NULL;
        }
        else
        {
                xms = XmStringCreateLtoR(s, XmSTRING_DEFAULT_CHARSET);
                to->size = sizeof(XmString);
                to->addr = (caddr_t)&xms;
        }
}

Then, add this converter function after initializing the toolkit, but 
before you create your widgets (except for the toplevel one), like so:

        XtAddConverter(XtRString, XmRXmString, CvtXmString, NULL, 0);

(You will need to include Xm.h and StringDefs.h here if you aren't already).

Then, a resource specification looking like:

	TestUil*main.labelString: This is a test\nThis is only a test

should do the trick (works for me).


Chris Lynnes                                ===== : = :::::
Teledyne Geotech                           ===== :: == :::::
Alexandria, Virginia                            ::: ===
(703) 739-7316                                  ::   ==
lynnes@seismo.CSS.GOV                           :     =

marbru@attc.UUCP (Martin Brunecky) (02/02/91)

In article <1991Jan31.143450.24173@slhisc.uucp> paul@slhisc.uucp (Paul Tam) writes:
>This is a common problem with motif labelString. Resource manager
>can't handle multiple line label string specified in resource file
>
>Someone mentioned using a self-defined type converter. So I started
>out testing the converter concept. So far I have been able to define
>a converter, add the converter, and the converter is called when 
>widgets are created. In the converter function, I convert the label
>...
>However, the application blows up after returning from the converter.
>
   Are you sure you are returning the "right" stuff back from the
   converter ?
   The result of your converter must be a *static* XmString foo
   (which in fact is some kind of a pointer), so you return
   toVal->size = sizeof(XmString);
   toVal->addr = &foo;

   The "foo" must be *static*, as it must exist *after* your converter
   exited, so that the calling code can make a copy of "foo" into the
   conversion cache (kinda klugy, but there are reasons for it).

   Next, you should NOT free the XmString created by the conversion,
   as you are returning a *pointer* to it - so the result of the
   conversion must "live for ever". You can and will reuse the "foo",
   but everytime you are called (with a new string), you will eat
   memory for the new XmString itself.

>
>Another question is does motif 1.0.3 with X11 R3 fully support self-defined
>type converter. The reason I ask this question is that when I put the 
>XtAddConverter function call right after XtInitialize, the converter
>was never invoked. If I move it down a little bit then It get called. 
>Strange!!

   Motif has a "block" of converters, ALL installed (in Motif 1.0) on the
   first call to XmRegisterConverters, typically called by the Motif
   widget's ClassInitialize, i.e when the first Xm widget is created.
   To overload Motif converters, just call XmRegisterConverters, and
   then add/load/overload your own converters.
   (note XmRegisterConverters uses a static flag "registered" to avoid
    reloading converters after the very first call).

The best joke of the season: Write Motif application without access to Motif
sources.


-- 
=*= Opinions presented here are solely of my own and not those of Auto-trol =*=
Martin Brunecky                           {...}sunpeaks!auto-trol!marbru
(303) 252-2499                        (sometimes also:  marbru@auto-trol.COM )
Auto-trol Technology Corp. 12500 North Washington St., Denver, CO 80241-2404 

gerday@mahogany (Jan B. Gerday) (02/05/91)

I'm sorry if this solution has already been mentioned, but what
I like to use for multi-line labels is the same syntax as is
used for translation table continuation, i.e. "\n\"

ex: in my app-defaults file for xmh I have

Xmh*commandBox.button1.label:  Close\n\Window

which makes the button like this  ----------
                                  | Close  |
                                  | Window |
                                  ----------

Alternatively the same layout can be specified like this:

Xmh*commandBox.button1.label:  	Close\n\
				Window

I hope this helps.

---------------------------------------------------------------
Jan Gerday	    << C'etait chouette... on a bien rigole! >>
gerday@shell.com   			- Petit Nicolas

...!{sun,psuvax1,bcm,rice,decwrl,cs.utexas.edu}!shell!gerday
Consultant to:
Shell Oil Company, IC-6F04
P.O. Box 20329,  Houston, TX 77225-0320
        (Tel: 713.795.3075)
---------------------------------------------------------------

-- 
Jan Gerday	    << C'etait chouette... on a bien rigole! >>
gerday@shell.com   			- Petit Nicolas

..!{sun,psuvax1,bcm,rice,decwrl,cs.utexas.edu}!shell!gerday