[comp.windows.x] XDrawLine

jstravis@athena.mit.edu (John S. Travis) (12/03/89)

Can anyone help me with what must be an obvious(to others) error
I am merely trying to read in some data ponts and draw lines
connecting them. The window pops up, but nothing is drawn, not even
the XDrawLines i threw in randomly throughout the program. What's the deal?
Thanks for any help!

john travis
jstravis@athena.mit.edu

---------------------------------------------------
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <Xw/Xw.h>
#include <Xw/WorkSpace.h>
#include <stdio.h>

#define MAXPOINTS 100
#define MAXLINES 100

void get_data();
void draw_data();

typedef struct {
  int line_num;
  int num_line_points;
  double points[MAXPOINTS];
} data_struct;

int TOTAL_LINES;

main(argc,argv)
     int    argc;
     char   *argv[];
{
  Widget  toplevel,canvas;
  GC gc;
  XGCValues values;
  Arg wargs[2];
  data_struct data[MAXLINES];

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

  canvas = XtCreateManagedWidget("canvas",XwworkSpaceWidgetClass,toplevel,NULL,0);

  XtRealizeWidget(toplevel);

/* get the colors user has set for the widget */
  XtSetArg(wargs[0],XtNforeground, &values.foreground);
  XtSetArg(wargs[1],XtNbackground, &values.background);
  XtGetValues(canvas,wargs,2);
  values.line_width =4;
  gc = XtGetGC(canvas,GCForeground | GCLineWidth | GCBackground, &values);

  XDrawLine(XtDisplay(canvas), XtWindow(canvas),gc, 24,89,30,133);

  get_data(data);
  draw_data(data,XtDisplay(canvas),XtWindow(canvas),gc);
  
  XtMainLoop();
}

void get_data(data)
     data_struct  data[];   /* 100 streamlines is the max! */
{
  FILE *fin;
  int i,j,num_line_points;

  fin = fopen("str.dat","r");
  if (fin == NULL){
    printf("Unable to open %s\n","str.dat");
    exit(1);
  }

  i=0;
  printf("entering while\n");
  while (fscanf(fin,"%d", &num_line_points) != EOF)
    {
      data[i].line_num = i;
      printf("line %d", data[i].line_num);

      data[i].num_line_points = num_line_points;
      printf(" has %d points\n",data[i].num_line_points);

      for (j=0; j < (2*num_line_points); j++){
        fscanf(fin,"%lf",&data[i].points[j]);
        printf("%lf ",data[i].points[j]);
      }
      printf("\n");   i++;

    }
  TOTAL_LINES = i;
  printf("out of while\n");
}



void draw_data(data,dpy,win,gc)
     data_struct data[];
     Display *dpy;
     Window win;
     GC gc;
{
  int j,i;

  printf("lines are %d\n",TOTAL_LINES);
  printf("first two point are %lf , %lf and  %lf , %lf \n",data[0].points[0],
             data[0].points[1],data[0].points[2],data[0].points[3]);

  XDrawLine(dpy,win,gc,20,35,45,70);
  for (i=0; i<(TOTAL_LINES-1); i++)
    {
      XDrawLine(dpy,win,gc,data[i].points[0],data[i].points[1],
                data[i].points[2],data[i].points[3]);

      for(j=2; j < (data[i].num_line_points - 1); j++)
        XDrawLine(dpy,win,gc,data[i].points[j],data[i].points[j+1],
                  data[i].points[j+2], data[i].points[j+3]);

    }
}

bruce@servio.UUCP (Bruce Schuchardt) (12/06/89)

In article <1989Dec2.193728.2991@athena.mit.edu> jstravis@athena.mit.edu (John S. Travis) writes:
>Can anyone help me with what must be an obvious(to others) error
>I am merely trying to read in some data ponts and draw lines
>connecting them. The window pops up, but nothing is drawn, not even
>the XDrawLines i threw in randomly throughout the program. What's the deal?
>Thanks for any help!
>
  [ deleted ]
>
>  toplevel = XtInitialize(argv[0],"Stream",NULL,0,&argc,argv);
>  canvas = XtCreateManagedWidget("canvas",XwworkSpaceWidgetClass,toplevel,NULL,0);
>  XtRealizeWidget(toplevel);
>
  [ deleted ]
>  gc = XtGetGC(canvas,GCForeground | GCLineWidth | GCBackground, &values);
>
>  XDrawLine(XtDisplay(canvas), XtWindow(canvas),gc, 24,89,30,133);
>
  [ deleted ]
>  XtMainLoop();
>}
  [ deleted ]

I'm surprised you didn't get an Invalid Window error from the server with
this code.  Calling XtRealizeWidget() doesn't complete the job of getting
a window up on the screen.  This won't happen until some events are dispatched
(e.g., in XtMainLoop()).  You can draw all you want, but it won't go on the
widget's window if the widget doesn't have a mapped window yet.

I had a similar problem, where I wanted to create and map a widget and then
do some other good stuff all in the same function.  Here's what I did:

  /* try to get a real widget window */
  while (!XtIsRealized(widget)  &&  XtPending()) {
    XtNextEvent(&event);
    XtDispatchEvent(&event);
  }

  if (XtIsRealized(widget)) {
    /* do your drawing here */
  }
  else {
    /* something went haywire */
  }

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Bruce Schuchardt          Ph: (503) 629-8383
  Servio Logic              bruce@servio.SLC.COM
  Beaverton, OR             uunet!servio!bruce