/* rubber.c   basic rubber band                    */
/* Draw rectangle. left mouse down=first point     */
/* Drag to second point. left mouse up=final point */

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>

static Display *dpy;
static Window window1;
static GC gc_rubber;
static Screen *screen;

static int winWidth = 500;
static int winHeight = 500;
static int tracking = 0; /* left button down, sense motion */
static int startX = 0;
static int startY = 0;
static int currentX = 0;
static int currentY = 0;

void display(void);

void rubberRect(int x0, int y0, int x1 , int y1)
{ /* can apply to all figures */
  /* draw a rubber rectangle, mouse down, tracks mouse */
  int x, y;
  x=x0;
  if(x1<x0) x=x1;
  y=y0;
  if(y1<y0) y=y1;
  XDrawRectangle(dpy, window1, gc_rubber, x, y, abs(x1-x0), abs(y1-y0));
}

void mouseMotion(int x, int y)
{
  if(tracking)
  {
    currentX = x;
    currentY = y;
  } 
  display();
}

void startMotion(int x, int y)
{
  tracking = 1;
  startX = x;
  startY = y;
  currentX = x;
  currentY = y; /* start zero size, may choose to ignore later */
  display();
}

void stopMotion(int x, int y)
{
  tracking = 0; /* no more rubber_rect */

  /* save final figure data for 'display' to draw */
  currentX = x;
  currentY = y;
  display();
}


void display(void)
{
  int i;
  
  XClearWindow(dpy, window1);
  if(tracking) rubberRect(startX, startY, currentX, currentY);

  /* draw figures per list */

}

int main(int argc, char *argv[])
{
  XEvent event;         
  XGCValues xgcvalues;
  int x, y;

  dpy = XOpenDisplay(0); 
  if (!dpy)
  {
    printf("Display not opened!\n");
    exit(1);
  }
  screen = XDefaultScreenOfDisplay(dpy);
  XSetWindowAttributes xswa;
                   
  /* Create the window1 window */
  xswa.event_mask = ExposureMask | ButtonPressMask |
                    ButtonReleaseMask | Button1MotionMask ;
  xswa.background_pixel = XWhitePixelOfScreen(screen);
  window1 = XCreateWindow(dpy,
                          XRootWindowOfScreen(screen),
                          100, 
                          100,
                          winWidth,
                          winHeight,
                          0,
                          XDefaultDepthOfScreen(screen),
                          InputOutput,
                          XDefaultVisualOfScreen(screen),
                          CWEventMask | CWBackPixel,
                          &xswa);
  /* Create graphics context. */
  xgcvalues.background = XWhitePixelOfScreen(screen);
  xgcvalues.foreground = XBlackPixelOfScreen(screen);
  xgcvalues.line_style = LineOnOffDash;
  gc_rubber = XCreateGC(dpy, window1, GCForeground | GCBackground |
                        GCLineStyle, &xgcvalues );
  XStoreName(dpy, window1, "rubber");
  XMapWindow(dpy, window1);
  while(1)
  {
    XNextEvent(dpy, &event);
    switch (event.type)
    { 
      case Expose:
	if(event.xexpose.window == window1) display();
        break;
      case ButtonPress:
	if(event.xbutton.button == Button1)
	{
          y = event.xbutton.y;
          x = event.xbutton.x;
          startMotion(x, y);
	}
	if(event.xbutton.button == Button2) display();
        break;
      case ButtonRelease:
	if(event.xbutton.button == Button1)
	{
          y = event.xbutton.y;
          x = event.xbutton.x;
          stopMotion(x, y);
	}
        break;
      case MotionNotify:
        y = event.xmotion.y;
        x = event.xmotion.x;
        mouseMotion(x, y);
        break;
    }
  }
  return 0;
}


