[comp.windows.x] Beginner's prblem with widgets

daniel@vu-vlsi.Villanova.EDU (Daniel C. Gnanaprakasam) (02/15/89)

I am a novice user of X toolkit and I am having problems getting my first
program to work.  The X11 R2 server is running on a Sun 3/50 under 4.0 OS.  The
program I tried was the one given in the paper titled "Using the X Toolkit or
How to Write a Widget", by Joel McCormack and Paul Asente. ( Usenix Conference 
Summer 1988 ).  

The following is the program I tried.

/** ------------------------------------------------------- */

#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Form.h>
#include <X11/Label.h>
#include <X11/Command.h>

void Callback(widget,clientData,callData)
     Widget widget;
     caddr_t clientData, callData;
{
  (void) printf("Goodbye, cruel world \n");
  exit(0);
}

int main(argc,argv)
     unsigned int argc;
     char **argv;
{
  Widget toplevel,box,label,command;
  Arg arg[25];
  unsigned int n;

  toplevel = XtInitialize("goodbye","Goodbye",NULL,0,&argc,argv);

  box = XtCreateManagedWidget("box",formWidgetClass,toplevel,(Arg *)NULL,0);

  n=0;
  XtSetArg(arg[n],XtNx,10);                n++;
  XtSetArg(arg[n],XtNy,10);                n++; 
  XtSetArg(arg[n],XtNlabel,"Goodbye world"); n++;
  label = XtCreateManagedWidget("label",labelWidgetClass,box,arg,n);

  n=0;
  XtSetArg(arg[n],XtNx,50);                  n++;
  XtSetArg(arg[n],XtNy,40);                  n++; 
  XtSetArg(arg[n],XtNlabel,"Click and die"); n++;
  command = XtCreateManagedWidget("command",commandWidgetClass,box,arg,n);
  XtAddCallback(command,XtNcallback,Callback,NULL);

  XtRealizeWidget(toplevel);
  XtMainLoop();

}

/** ------------------------------------------------------ **/

I compiled and linked by 

cc -o xtk xtk.c -lXaw -lXt -lX

When I run it, only the label widget with the label "Goodbye label" appears
on the display.  


Thanks for the help

daniel

kit@ATHENA.MIT.EDU (Chris D. Peterson) (02/16/89)

The problem you are having it a misunderstanding of what the x and y
resources of a child mean to the parent.  The parent (in this case the form
widget) is free to move his managed children wherever he wants to, and ignore
their x and y locations if he so chooses.  This is what the form is doing. 
In fact what is actually happening is the the label and command widget are on
top of one another.

Your callback is fine, either of the following main's will do the right
thing for you.  They will act differently when resized.  I will leave it 
as an exercise for the reader to figure out why :-).

Case 1:
-------

int main(argc,argv)
     unsigned int argc;
     char **argv;
{
  Widget toplevel,box,label,command;
  Arg arg[25];
  Cardinal n;

  toplevel = XtInitialize("goodbye","Goodbye",NULL,0,&argc,argv);

  box = XtCreateManagedWidget("box",formWidgetClass,toplevel,(Arg *)NULL,0);

  n=0;
  XtSetArg(arg[n],XtNlabel,"Goodbye world"); n++;
  label = XtCreateManagedWidget("label",labelWidgetClass,box,arg,n);

  n=0;
  XtSetArg(arg[n],XtNlabel,"Click and die"); n++;
  XtSetArg(arg[n],XtNfromVert, label); n++;
  command = XtCreateManagedWidget("command",commandWidgetClass,box,arg,n);
  XtAddCallback(command,XtNcallback,Callback,NULL);

  XtRealizeWidget(toplevel);
  XtMainLoop();

}

Case 2:
-------

change the line 

#include <X11/Form.h>

to

#include <X11/Box.h>

int main(argc,argv)
     unsigned int argc;
     char **argv;
{
  Widget toplevel,box,label,command;
  Arg arg[25];
  unsigned int n;

  toplevel = XtInitialize("goodbye","Goodbye",NULL,0,&argc,argv);

  box = XtCreateManagedWidget("box",boxWidgetClass,toplevel,(Arg *)NULL,0);

  n=0;
  XtSetArg(arg[n],XtNlabel,"Goodbye world"); n++;
  label = XtCreateManagedWidget("label",labelWidgetClass,box,arg,n);

  n=0;
  XtSetArg(arg[n],XtNlabel,"Click and die"); n++;
  command = XtCreateManagedWidget("command",commandWidgetClass,box,arg,n);
  XtAddCallback(command,XtNcallback,Callback,NULL);

  XtRealizeWidget(toplevel);
  XtMainLoop();

}

						Chris D. Peterson     
						MIT X Consortium /
						Project Athena 

Net:	kit@athena.mit.edu		
Phone: (617) 253 - 1326			
USMail: MIT - Room E40-321
	77 Massachusetts Ave.		
	Cambridge, MA 02139		

daniel@vu-vlsi.villanova.EDU (Daniel C. Gnanaprakasam) (02/22/89)

Thanks for your help regarding widgets. It worked now

daniel