dynamically changing a label

This forum is read only. No new submissions are accepted.

Questions about motif? Contact us

4 posts / 0 new
Last post
dynamically changing a label

Submitted by Anonymous on Mon, 04/05/2004 - 20:58.

Hi,
I am a newbie to motif programming.

After i intialize the GUI with " XtAppMainLoop" I want to change the label. The application is very simple it involves displaying some numbers on a text field. The numbers update at about 10-Hz. I see that all the callbacks (that i have seen) are triggered via. some user controlled event.

If you can send me a sample code to do this that would be great.

Tue, 04/06/2004 - 13:35#1

Tue, 04/06/2004 - 13:35#1

Anonymous

Try this...

You should be able to use XtVaSetValues and set the XmNlabelString property to the text that you want.

Example:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Widget o_Label;
XmString s_String;

s_String = XmStringCreateLocalized("MyLabel");
XtVaSetValues(o_Label, XmNlabelString, s_String, NULL);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

NOTE: XmStringCreateLocalized accepts a char as input and creates a XmString item that can then be used to modify the text of the label. When working with text Motif uses XmString's as input a lot (Combobox, Lists, Labels, Buttons); however, for some reason with textboxes they don't

Hope this help.
C. Lamb

 

Fri, 04/09/2004 - 06:48#2

Fri, 04/09/2004 - 06:48#2

Anonymous

A sample

Hello, here is a sample you requested.

<br />
#include <Xm/Xm.h><br />
#include <Xm/Label.h><br />
#include <Xm/LabelG.h><br />
#include <time.h><br />
#include <sys/time.h></p>
<p>char time_string_buf[13];</p>
<p>char *<br />
get_time_string()<br />
{<br />
struct timeval tv;<br />
struct tm *tm;<br />
gettimeofday(&tv, NULL);<br />
tm = localtime(&(tv.tv_sec));<br />
snprintf(time_string_buf, sizeof(time_string_buf),<br />
"%02d:%02d:%02d.%03ld",<br />
tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);<br />
return time_string_buf;<br />
}</p>
<p>void<br />
show_time(XtPointer closure, XtIntervalId *id)<br />
{<br />
Widget label = (Widget)closure;<br />
XmString s = XmStringCreateLocalized(get_time_string());<br />
XtVaSetValues(label, XmNlabelString, s, NULL);<br />
XmStringFree(s);<br />
XtAppAddTimeOut(XtWidgetToApplicationContext(label),<br />
100, show_time, (XtPointer)label);<br />
}</p>
<p>int<br />
main (int argc, char** argv)<br />
{<br />
Widget toplevel, label;<br />
XtAppContext app;<br />
XmString s;</p>
<p> XtSetLanguageProc(NULL, NULL, NULL);<br />
toplevel = XtVaOpenApplication(&app, "Demos", NULL, 0,<br />
&argc, argv,<br />
NULL, sessionShellWidgetClass, NULL);</p>
<p> s = XmStringCreateLocalized(get_time_string());</p>
<p> label = XtVaCreateManagedWidget("time_label",<br />
xmLabelWidgetClass, toplevel,<br />
XmNlabelString, s,<br />
NULL);</p>
<p> XtAppAddTimeOut(XtWidgetToApplicationContext(label),<br />
100, show_time, (XtPointer)label);</p>
<p> XmStringFree(s);</p>
<p> XtRealizeWidget(toplevel);<br />
XtAppMainLoop(app);</p>
<p> return 0;<br />
}<br />

johnsheryl (not verified)
I didn't used the sample code

I didn't used the sample code. Did you used it ? Does it working now ?