#include "xsimple.h"
#include <string.h>

static unsigned long curbgcolor = -1;
static unsigned long curfgcolor = -1;
static int curlinewidth = -1;


void setlinewidth(int width)
{
   if (width != curlinewidth) {
       XSetLineAttributes(xv.d, xv.gc, width, LineSolid, CapButt, JoinMiter);
       curlinewidth = width;
   }
}

void cls(unsigned long pixel)
{
   if (pixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, pixel);
       curfgcolor = pixel;
   }
   XFillRectangle(xv.d, xv.pmap, xv.gc, 0, 0, xv.width, xv.height);
}

void pset(int x, int y, unsigned long pixel)
{
   if (pixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, pixel);
       curfgcolor = pixel;
   }
   XDrawPoint(xv.d, xv.pmap, xv.gc, x, y);
}

void line(int x1, int y1, int x2, int y2, unsigned long pixel)
{
   if (pixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, pixel);
       curfgcolor = pixel;
   }
   XDrawLine(xv.d, xv.pmap, xv.gc, x1, y1, x2, y2);
}

void poly(XPoint *points, int npoints, unsigned long pixel)
{
   if (pixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, pixel);
       curfgcolor = pixel;
   }
   XDrawLines(xv.d, xv.pmap, xv.gc, points, npoints, CoordModeOrigin);
}

void fillpoly(XPoint *points, int npoints, unsigned long pixel)
{
   if (pixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, pixel);
       curfgcolor = pixel;
   }
   XFillPolygon(xv.d, xv.pmap, xv.gc, points, npoints, Nonconvex,
           CoordModeOrigin);
}

void rect(int x, int y, int width, int height, unsigned long pixel)
{
   if (pixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, pixel);
       curfgcolor = pixel;
   }
   XDrawRectangle(xv.d, xv.pmap, xv.gc, x, y, width - 1, height - 1);
}

void fillrect(int x, int y, int width, int height, unsigned long pixel)
{
   if (pixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, pixel);
       curfgcolor = pixel;
   }
   XFillRectangle(xv.d, xv.pmap, xv.gc, x, y, width, height);
}

void ellipse(int x, int y, int width, int height, unsigned long pixel)
{
   if (pixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, pixel);
       curfgcolor = pixel;
   }
   XDrawArc(xv.d, xv.pmap, xv.gc, x, y, width - 1, height - 1, 0, 360 * 64);
}

void fillellipse(int x, int y, int width, int height, unsigned long pixel)
{
   if (pixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, pixel);
       curfgcolor = pixel;
   }
   XFillArc(xv.d, xv.pmap, xv.gc, x, y, width - 1, height - 1, 0, 360 * 64);
}

void string(int x, int y, char *s, unsigned long pixel)
{
   if (pixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, pixel);
       curfgcolor = pixel;
   }
   XDrawString(xv.d, xv.pmap, xv.gc, x, y, s, strlen(s));
}

void imgstring(int x, int y, char *s, unsigned long bgpixel,
       unsigned long fgpixel)
{
   if (bgpixel != curbgcolor) {
       XSetBackground(xv.d, xv.gc, bgpixel);
       curbgcolor = bgpixel;
   }
   if (fgpixel != curfgcolor) {
       XSetForeground(xv.d, xv.gc, fgpixel);
       curfgcolor = fgpixel;
   }
   XDrawImageString(xv.d, xv.pmap, xv.gc, x, y, s, strlen(s));
}