#include "xwindow.h"

Display*         TheDisplay=NULL;

xwindow::xwindow(int Width,int Height,char* FG,char* BG)
{
  if(NULL==TheDisplay) if(NULL==(TheDisplay=XOpenDisplay("")))
    { cerr << "Can't open display.\n";exit(1);}
  TheScreen      = DefaultScreen(TheDisplay);
  TheHints.width = Width;TheHints.height=Height;
  ImData      =Imlib_init(TheDisplay);
  TheWindow=XCreateWindow(TheDisplay,DefaultRootWindow(TheDisplay),
			  0,0,TheHints.width,TheHints.height,0,
			  ImData->x.depth,InputOutput,
			  ImData->x.visual,0,&XWinAttr);
  CMap=DefaultColormap(TheDisplay,DefaultScreen(TheDisplay));
  TheGC          =XCreateGC(TheDisplay,TheWindow,0,0);
  ForegroundColor=colorID(FG); BackgroundColor=colorID(BG);
  XSelectInput(TheDisplay,TheWindow,ButtonPressMask|KeyPressMask|ExposureMask);
  setBGImage("Back2.png");
}

void xwindow::setBGImage(char* ImageFileName)
{
  ImImage     =Imlib_load_image(ImData,ImageFileName);
  Imlib_render(ImData,ImImage,TheHints.width,TheHints.height);
  PMap =Imlib_move_image(ImData,ImImage);
  PMask=Imlib_move_mask (ImData,ImImage);
  if (PMask)
    XShapeCombineMask(TheDisplay,TheWindow,ShapeBounding,0,0,PMask,ShapeSet);
  XSetWindowBackgroundPixmap(TheDisplay,TheWindow,PMap);
  Imlib_free_pixmap(ImData,PMap);
  Imlib_free_pixmap(ImData,PMask);
  XSync(TheDisplay,False);
}

void xwindow::setBGTile(char* ImageFileName)
{
  ImImage     =Imlib_load_image(ImData,ImageFileName);
  Imlib_render(ImData,ImImage,ImImage->rgb_width,ImImage->rgb_height);
  PMap =Imlib_move_image(ImData,ImImage);
  PMask=Imlib_move_mask (ImData,ImImage);
  if (PMask)
    XShapeCombineMask(TheDisplay,TheWindow,ShapeBounding,0,0,PMask,ShapeSet);
  XSetWindowBackgroundPixmap(TheDisplay,TheWindow,PMap);
  Imlib_free_pixmap(ImData,PMap);
  Imlib_free_pixmap(ImData,PMask);
  XSync(TheDisplay,False);
}


