71) How can I set a multiline label?

[Last modified: September 92]

Answer: In .Xdefaults

      *XmLabel*labelString:             Here\nis\nthe\nLabel

This method does not seem to work in some of the older Motif 1.0 versions.

In code,

      char buf[128];
      XmString msg;
      sprintf(buf, "Here\nis\nthe\nLabel");
      msg = XmStringCreateLtoR(buf, XmSTRING_DEFAULT_CHARSET);
      XtSetArg (args[n], XmNlabelString, msg);

Gives a four line label, using the escape sequence \n for a newline.  However,
XmStringCreateLtoR() is obsoleted from version 1.1 on, and may disappear.
This is because it it is only in the AES as "trial-use" and has been proposed
for removal from the AES. Realistically, it will probably not be removed from
any backward compatible versions of Motif, but the potential is there.  If it
does disappear (or if you want to avoid using the non-AES compliant
XmSTRING_DEFAULT_CHARSET), try this from Jean-Philippe Martin-Flatin
<syj@ecmwf.co.uk>

#include <Xm/Xm.h>
#include <string.h>

/*-----------------------------------------------------
    Create a new XmString from a char*

    This function can deal with embedded 'newline' and
    is equivalent to the obsolete XmStringCreateLtoR,
    except it does not use non AES compliant charset
    XmSTRING_DEFAULT_CHARSET
----------------------------------------------------*/
XmString xec_NewString(char *s)
{
    XmString xms1;
    XmString xms2;
    XmString line;
    XmString separator;
    char     *p;
    char     *t = XtNewString(s);   /* Make a copy for strtok not to */
                                    /* damage the original string    */


    separator = XmStringSeparatorCreate();
    p         = strtok(t,"\n");
    xms1      = XmStringCreateSimple(p);

    while (p = strtok(NULL,"\n"))
    {
        line = XmStringCreateSimple(p);
        xms2 = XmStringConcat(xms1,separator);
        XmStringFree(xms1);
        xms1 = XmStringConcat(xms2,line);
        XmStringFree(xms2);
        XmStringFree(line);
    }

    XmStringFree(separator);
    XtFree(t);
    return xms1;
}


Do not use XmStringCreateSimple() - it does not process the newline character
in the way you want.

In UIL, you have to explicitly create a compound string with a separator.
Here's what W. Scott Meeks suggests:

value nl : compound_string('', seperate=true);

object my_label : XmLabel
{
    arguments
    {
        XmNlabelString = 'Here' & nl & 'is' & nl & 'the' & nl & 'Label';
    };
};
Go Back Up

Go To Previous

Go To Next