[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Motif programming blues



Can anyone tell me why the following code (in attachments) doesn't work,
or point to me someplace that describes in DETAIL the XmLabelWidget class?
The lesstif docs describe functions fine, but precious little in the
way of the widgets and gadgets.

Problem: you give memo no arguments, you get a blank box.  Give it
arguments, the label text is "msg", not the arguments you gave it.
I typed this verbatim (minus some syntactical differences, like ANSI-style
function headers instead of K&R, and moved a variable declaration to the
top of the function instead of in the middle) from Douglas A. Young's "The
X Window System Programming and Applications with Xt: OSF/Motif Edition".
I figure this qualifies as fair use since the damn code doesn't work.

gcc -o memo.c concat.c -L/usr/X11R6/lib/ -lXm -lXt -lX11

--
G. Branden Robinson                 |          You live and learn.
Purdue University                   |          Or you don't live long.
branden@purdue.edu                  |          -- Robert Heinlein
http://www.ecn.purdue.edu/~branden/ |
/* memo.c: display a message in a window */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <Xm/Xm.h>
#include <Xm/Label.h>

extern XmString xs_concat_words();

void main(int argc, char *argv[])
{
  Widget toplevel, msg_widget;
  Arg wargs[1];
  int n;
  XmString message;

  /* initialize intrinsics */

  toplevel = XtInitialize(argv[0], "Memo", NULL, 0, &argc, argv);

  /* 
   * if a message is given on command line, use it as XmNlabelString
   * argument for widget
   */

  n = 0;
  if ((message = xs_concat_words(argc - 1, &argv[1])) != NULL) {
    XtSetArg(wargs[n], XmNlabelString, message);
    n++;
  }

  /* create XmLabel widget */

  msg_widget = XtCreateManagedWidget("msg", xmLabelWidgetClass, toplevel,
   wargs, n);

  /* realize widgets and enter event loop */

  XtRealizeWidget(toplevel);
  XtMainLoop();
}
/* concat.c: utility function to concatenate an array of strings into a
 * single compund string with spaces between words */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <Xm/Xm.h>
#include <Xm/Label.h>

XmString xs_concat_words(int n, char *words[])
{
  XmString xmstr, tmp;
  int i;

  /* if no words, return empty string */

  if (n <= 0)
    return (XmStringCreate("", XmSTRING_DEFAULT_CHARSET));

  xmstr = (XmString) NULL;

  for (i = 0; i < n; i++) {
    if (i > 0) { /* prepend all but first word with a space */
      tmp = XmStringCreate(" ", XmSTRING_DEFAULT_CHARSET);
      xmstr = XmStringConcat(xmstr, tmp);
    }
    tmp = XmStringCreate(words[i], XmSTRING_DEFAULT_CHARSET);
    xmstr = XmStringConcat(xmstr, tmp);
  }
  return (xmstr);
}

Reply to: