[comp.windows.x] motif XmString to char

sohara@lamont.ldgo.columbia.edu (suzanne ohara) (10/17/90)

I am learning to program with motif and have run into what
seems a very simple problem. I have not been able to find
a way to convert a motif XmString to a regular C char string.
Is this possible? I would appreciate any suggestions.

Thank you,
Suzanne O'Hara

bazavan@hpcilzb.HP.COM (Valentin Bazavan) (10/17/90)

Use XmStringGetLtoR. For instance:

	char  *s;
	XmString xmstr;
	/* Create a compound string */
	xmstr = XmStringCreateLtoR("Hello", XmSTRING_DEFAULT_CHARSET);
	/* Get the text segment of the compound string */
	XmStringGetLtoR(xmstr, XmSTRING_DEFAULT_CHARSET, &s);

Valentin Bazavan

jordan@morgan.COM (Jordan Hayes) (10/17/90)

Suzanne O'Hara <sohara@lamont.ldgo.columbia.edu> asks:

	I am learning to program with motif and have run into what
	seems a very simple problem. I have not been able to find a way
	to convert a motif XmString to a regular C char string.

This should work:

----- cut here -----

#include <Xm/Xm.h>

char	*
XmStringGet(w, str)
	Widget		w;	/* widget that owns the XmString */
	XmString	str;	/* the XmString itself */
{
	char			*text, *buf;
	Boolean			sep;
	XmStringContext		context;
	XmStringDirection	dir;
	XmStringCharSet		set;

	if (XmStringInitContext(&context, str) == FALSE) {
		XtAppWarning(XtWidgetToApplicationContext(w),
		    "couldn't initialize context!\n");
		return((char *)NULL);
	}

	buf = (char *)NULL;

	while (XmStringGetNextSegment(context, &text, &set, &dir, &sep))  {
		if (sep)
			break;
		if (text == (char *)NULL || *text == (char)NULL)
			continue;
		if (buf) {
			buf = XtRealloc(buf, strlen(buf) + strlen(text) + 2);
			(void)strcat(buf, text);
		} else
			buf = XtNewString(text);
	}

	XmStringFreeContext(context);
	return(buf);
}
----- cut here -----

/jordan

achan@sparkle.nec.com (Amy Chan) (10/17/90)

In article <2863@lamont.ldgo.columbia.edu> sohara@lamont.ldgo.columbia.edu (suzanne ohara) writes:
>I am learning to program with motif and have run into what
>seems a very simple problem. I have not been able to find
>a way to convert a motif XmString to a regular C char string.
>Is this possible? I would appreciate any suggestions.
>

A function called XmStringGetLtoR will search for a text segment in
the input compound string that matches the given character set
identifier.

Amy Chan
achan@tdd.sj.nec.com

nazgul@alphalpha.com (Kee Hinckley) (10/18/90)

In article <2863@lamont.ldgo.columbia.edu> sohara@lamont.ldgo.columbia.edu (suzanne ohara) writes:
>seems a very simple problem. I have not been able to find
>a way to convert a motif XmString to a regular C char string.
XmStringGetLtoR().  Don't free the value - it's a pointer to the internal
representation.
-- 
Alphalpha Software, Inc.	|	motif-request@alphalpha.com
nazgul@alphalpha.com		|-----------------------------------
617/646-7703 (voice/fax)	|	Proline BBS: 617/641-3722

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.

marbru@auto-trol.UUCP (Martin Brunecky) (10/18/90)

In article <2863@lamont.ldgo.columbia.edu> sohara@lamont.ldgo.columbia.edu (suzanne ohara) writes:
>I am learning to program with motif and have run into what
>seems a very simple problem. I have not been able to find
>a way to convert a motif XmString to a regular C char string.
>Is this possible? I would appreciate any suggestions.
>
   No, it should NOT be possible, as XmString may contain
   multibyte characters, i.e. potential imbeded NUL characters.

   In practice, however, you could do it assuming that you only
   have 8bit characters, or your multibyte characters don't have
   any NUL bytes ...

   Then, also note there is XConsortium document defining Compound
   Strings, and that is what XmString (in my naivity) should become
   (but I am not sure it already happened).

-- 
=*= Opinions presented here are solely of my own and not those of Auto-trol =*=
Martin Brunecky [BORN TO BASH UIL]                  marbru@auto-trol.COM
(303) 252-2499                                 {...}ncar!ico!auto-trol!marbru
Auto-trol Technology Corp. 12500 North Washington St., Denver, CO 80241-2404 

dbrooks@osf.osf.org (David Brooks) (10/18/90)

Suzanne O'Hara <sohara@lamont.ldgo.columbia.edu> asks:

	I am learning to program with motif and have run into what
	seems a very simple problem. I have not been able to find a way
	to convert a motif XmString to a regular C char string.

If you are interested only in left-to-right segments, you can use:

	result = XmStringGetLtoR(cstring, charset, &charp);

Remember to XtFree(charp) when you no longer need it.
-- 
David Brooks				dbrooks@osf.org
Systems Engineering, OSF		uunet!osf.org!dbrooks

mago@delphi.it (Giovanni Beani) (10/18/90)

In some article, i2unix!lamont!sohara (suzanne ohara) writes:

>I am learning to program with motif and have run into what
>seems a very simple problem. I have not been able to find
>a way to convert a motif XmString to a regular C char string.
>Is this possible? I would appreciate any suggestions.

>Thank you,
>Suzanne O'Hara

This is a way to do that:


----- Begin Included Message -----

static char *xm_string_to_string(cs)
XmString cs;
{
  XmStringContext context;
  XmStringCharSet charset;
  XmStringDirection direction;
  Boolean separator;
  static char *primitive_string;

  XmStringInitContext (&context,cs);
  XmStringGetNextSegment (context,&primitive_string,
              &charset,&direction,&separator);
  XmStringFreeContext (context);
  return ((char *) primitive_string);
}

----- End Included Message -----

Bye Bye
	Giovanni

-----
Giovanni Beani               

DELPHI S.p.A.
"Research   Engineering   Phone: +39 (584)395225   EMail: gbeani@delphi.uucp
& Development Division"   Fax  : +39 (584)395366  
Via Vetraia 11  - 55049   Tlx  : 501542 DELPHI I
Viareggio (LUCCA) ITALY

nazgul@alphalpha.com (Kee Hinckley) (10/18/90)

In article <15121@paperboy.OSF.ORG> dbrooks@osf.org (David Brooks) writes:
>	result = XmStringGetLtoR(cstring, charset, &charp);
>
>Remember to XtFree(charp) when you no longer need it.
Whoops.  I said exactly the opposite.  David's right, I'm wrong.

-- 
Alphalpha Software, Inc.	|	motif-request@alphalpha.com
nazgul@alphalpha.com		|-----------------------------------
617/646-7703 (voice/fax)	|	Proline BBS: 617/641-3722

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.

jordan@morgan.COM (Jordan Hayes) (10/19/90)

Suzanne O'Hara <sohara@lamont.ldgo.columbia.edu> asks:

	I am learning to program with motif and have run into what
	seems a very simple problem. I have not been able to find a way
	to convert a motif XmString to a regular C char string.

To which David Brooks <dbrooks@osf.org> writes:

	If you are interested only in left-to-right segments, you can use:

	result = XmStringGetLtoR(cstring, charset, &charp);

I have always seen this defined as

Boolean
XmStringGetLtoR(string, charset, text)
	XmString	string;
	XmStringCharSet	charset;
	char		**text;

My understanding of XmStringGetLtoR is used to search `string' to see
if there is a segment that matches `text' and if so, returns TRUE.  I
believe she wanted the "opposite" of XmStringCreate ... if this (when
*text == (char *)NULL) *is* the equivalent of what I posted yesterday,
please document it better!

/jordan

nazgul@alphalpha.com (Kee Hinckley) (10/20/90)

In article <9010191623.AA03058@s6.Morgan.COM> jordan@morgan.COM (Jordan Hayes) writes:
>XmStringGetLtoR(string, charset, text)
>	XmString	string;
>	XmStringCharSet	charset;
>	char		**text;
>
>My understanding of XmStringGetLtoR is used to search `string' to see
>if there is a segment that matches `text' and if so, returns TRUE.  I
No, it searches the string and returns a pointer to the first
segment that matches charset.  Note that since \n is represented
by a separator this means it only returns the first line, so your
solution (looping through the segments) is actually more generically
correct if you don't know whether the value is multi-line or single
line (or multi-font).  However it assumes that you know what the
charset is (or don't care).
					-kee
-- 
Alphalpha Software, Inc.	|	motif-request@alphalpha.com
nazgul@alphalpha.com		|-----------------------------------
617/646-7703 (voice/fax)	|	Proline BBS: 617/641-3722

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.