xvobject *rectangles = NULL;
int      rectangle_count = 0;

/*
 *  this helper routine opens the location file, reads in the (x,y) device
 *  coordinates, and creates the rectangle objects.  the file reading is 
 *  not robust, it expects x y locations in the first column of the file,
 *  separated by a single space, no extraneous characters. no errorchecking.
 */

static int get_rectangle_locations(
   char     *filename,
   xvobject imageobj)
{
	kfile    *file;
	int      status, x, y;
	char     temp[KLENGTH];

	/* open the file */
	if ((file = kfopen(filename, "r")) == NULL)
        {
           kerror(NULL, "get_rectangle_locations",
                  "Unable to open '%s' to get rectangle locations.", filename);
           return(FALSE);
        }

	while (1)
        {
	   /* on eof, the kgets will fail and we break from routine */
	   if (kfgets(temp, KLENGTH, file) == NULL)
                return(TRUE);

	   /* scan for 2 ints separated by a single space */
	   status = ksscanf(temp, "%d %d", &x, &y);
	   if (status != 2) 
	   {
	      kerror(NULL, "get_rectangle_locations", 
                     "bogus line in location file");
	      return(FALSE);
	   }

	   /* ok, got the device coordinates */
	   rectangle_count++;

	   /* 
	    * create a rectangle object at that location. store it in array
            * so we can destroy it later. Note the XVW_XPOSITION and 
            * XVW_YPOSITION are used because we are dealing in device 
	    * coordinates, NOT world coordinates (that would have been 
	    * XVW_RECTANGLE_XCORNER, XVW_RECTANGLE_YCORNER, 
	    * XVW_RECTANGLE_XDIAGONAL, and XVW_RECTANGLE_YDIAGONAL)
            * you can make them any way you want, i chose 5 x 5 in red.
	    * add event handler on ButtonPress which will print information.
	    */
	   rectangles = (xvobject *) krealloc(rectangles, rectangle_count *
					      (sizeof(xvobject)));
	   rectangles[rectangle_count-1] = 
			xvw_create_rectangle(imageobj, "rectangle");
           xvw_set_attributes(rectangles[rectangle_count-1],
                           XVW_XPOSITION,           x,
                           XVW_YPOSITION,           y,
                           XVW_WIDTH,               5,
                           XVW_HEIGHT,              5,
			   XVW_FOREGROUND_COLOR, "red",
                           NULL);
	   xvw_add_event(rectangles[rectangle_count-1], ButtonPress, 
			 print_location_info, rectangles[rectangle_count-1]);
        }
}

/*
 *  this is the event handler which is fired when the user clicks on one
 *  of the rectangle objects.  i just display the (x, y) location.
 */
void print_location_info(
    xvobject object,
    XEvent   *event,
    kaddr    client_data,
    int      *dispatch)
{
	int x, y;
	xvw_get_attributes(object, 
			   XVW_XPOSITION, &x,
                           XVW_YPOSITION, &y,
			   NULL);
	kinfo(KSTANDARD, "The rectangle is located at %d, %d", x, y);
}
