[comp.windows.x.motif] XmString prob...

E_CMA@vaxa.nerc-murchison.ac.uk (04/22/91)

Re: XmString Problems 
Attn: Carol Chapman and other new UIL users.

Apologies in advance, but we are not on Internet so I am having to
use a roundabout route to post this. I can see posted replies, but
I can't easily E_mail individuals outside the U.K.unless full 
gateway info is supplied. 

>What's happening is that I can use XmStringCreateLtoR to create an
>XmString and set a label.  So far, so good.  However, when I try to
>use XmStringGetLtoR to retrieve that label, I always get back a null
>string!

A failure to retrieve info from a widget can arise in several places.

1) If you wish to retrieve anything from a widget defined in a UIL file,
   it MUST have been created first, i.e. it must have a create callback
   so that it is registered in your program. The "create" may have failed
   because it wasn't there in your UIL file in the first place.

2) The routine has not been registered properly in your C program.
   The latter is usually because of a typo in the name or because you 
   have added a routine to your MrmRegisterArg vector, but have not upped 
   the item count. MrmRegisterNames() then fails to pick it up.

   The illustration below gets round that.

3) The return for XtGetValues is a compound string, and thus 
      XtSetArg( a[0], XmNlabelString, &cstring); and NOT
      XtSetArg( a[0], XmNlabelString, cstring)  as you would use for a normal 
       text string.
    
4) Note that the receiving text string in XmStringGetLtoR is doubly 
   indirected. If you get this wrong, your string usually isn't empty, 
   but its contents can be very wierd! 

5) BEWARE of typos! Make sure your cstring name matches your declaration and 
   the name in your XmStringCreateLtoR(). If you try to retrieve from a wrongly 
   named compound string, in some configurations the C compilers doesn't always
   detect this and may assume it to be an external variable, in which case you 
   get an empty string. 

.................................Illustration.................................
 e.g.
 ... in the UIL file
 value 
   k_mylabel : 1;
   .
   .
 object
   mylabel : XmLabel{
       arguments{
        XmNlabelString = "Hi there!";
       };
       callbacks{
       MrmNcreateCallback = procedure create_proc(k_mylabel);
       };
};

and in your program....
#include <Xm/Xm.h>
#include <Mrm/MrmAppl.h>
#include <Mrm/MrmDecls.h>
   .
   .

#define k_mylabel 1
   .
   .
Widget widget_array[5];
static void create_proc();
   .
   .
static MrmRegisterArg regvec[] = {
 {"create_proc",(caddr_t)create_proc},
};
static MrmCount regnum = (sizeof regvec/sizeof regvec[0]);
   .
   .
 
static void create_proc(w, tag, reason)  	
Widget w;
int *tag;
unsigned long *reason
{
 widget_array[*tag] = w;
}
   .
   .
 
 Then, for a XmLabel or XmPushButton etc. you can use...

my_proc()
  {
   char * str;
   XmString cstring;
   .
   .
   .
     XtSetArg(a[0],XmNlabelString,&cstring);
     XtGetValues(w,a[0],1);
                 
  /* This retrieves the label value into the compound string variable cstring, 
     using the default character set, then... */
   .
   .
   XmStringGetLtoR(cstring, XmSTRING_DEFAULT_CHARSET, &str);
  }


  If it is a XmText widget one must

 #include <Xm/Text.h>

      and should use...
  {
   char * str;
   .
   .

  str = XmTextGetString(widget_id);
    .
  XtFree(str);                 /*...when you have done with it.*/
  }
.......................................................................

I hope the above will assist.


*********************************************************************************
* Snailmail:			*  E_Mail					*
*				*  CBS%UK.AC.NERC-MURCHISON.VAXA::E_CMA		*
* Dr C.M.Allen			*  JANET: e_cma@uk.ac.nmh.va			*
* British Geological Survey	*  						*
* Murchison House		* 						*
* West Mains Road		*************************************************
* Edinburgh		 	* Tel: 031-667-1000 x277 from U.K.		*
* Scotland			* 						*
* EH9,3LA			* 						*
*********************************************************************************