# include "Ximage.h"

void Refresh (void) {

  int i, Nsection;
  Graphic *graphic;
  Section *section;

  if (!USE_XWINDOW) return;
  // if (HAVE_BACKING) return;

  graphic = GetGraphic();
  
  /* XClearWindow   (graphic.display, graphic.window); */
  XSetForeground (graphic->display, graphic->gc, graphic->back);
  XFillRectangle (graphic->display, graphic->window, graphic->gc, 0, 0, graphic->dx, graphic->dy);
  XSetForeground (graphic->display, graphic->gc, graphic->fore);
  
  // reset the sizes for all sections
  Nsection = GetNumberOfSections ();
  for (i = 0; i < Nsection; i++) {
      section = GetSectionByNumber (i);
      DrawSectionBG (graphic, section);
      DrawImage (section->image);
      DrawGraph (section->graph);
  }

  FlushDisplay ();
}

void DrawSectionBG (Graphic *graphic, Section *section) {

  int Ncolors;
  int X0, Y0, dX, dY;

  if (section->bg == -1) return;

  Ncolors = KapaColormapSize();

  if (section->bg < 0) return;
  if (section->bg >= Ncolors) return;

  XSetForeground (graphic->display, graphic->gc, graphic->color[section->bg]);

  dY = graphic[0].dy * section[0].dy;
  dX = graphic[0].dx * section[0].dx;
  Y0 = graphic[0].dy - graphic[0].dy * section[0].y - dY;
  X0 = graphic[0].dx * section[0].x;
  // fprintf (stderr, "section: (%s) %d %d - %d %d\n", section[0].name, X0, Y0, dX, dY);

  XFillRectangle (graphic[0].display,  graphic[0].window, graphic[0].gc, X0, Y0, dX, dY);
  return;
}
