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

Display *mydisplay;
Window   mywindow;
GC       mygc;
XEvent   myevent;
KeySym   mykey;
Font     font;
XFontStruct *ethfont,*latfont;
XSizeHints myhint;
int myscreen;

extern unsigned long foreground, background,fore_init;
extern char bg_color[12],fg_color[12],eth_font_name[80],lat_font_name[80];
extern int eth,i,bandira,nolat,no_eth_font,no_lat_font,HEIGHT,WIDTH;
extern unsigned long get_color_pixel();

struct flags {
               char color[3][8]; 
               unsigned long fg[3];
               int y[4];
             } flag;

char color1[8],color2[8],color3[8];

initx() {
int argc;
char **argv,screen_color[12];

  mydisplay = XOpenDisplay ( "" );
  if ( mydisplay == NULL ) {
    fprintf(stderr,"ERROR: Cannot establish a connection to the X Server %s\n",XDisplayName(NULL));
    exit(1);
  }
  myscreen = DefaultScreen ( mydisplay );


if(bandira) {
  flag.y[0] = 0;
  flag.y[1] = ((HEIGHT/16)/3)*16 + BORDER;
  flag.y[3] = HEIGHT + 2*BORDER;
  flag.y[2] = flag.y[3] - flag.y[1];

  strcpy(flag.color[0],"green");
  strcpy(flag.color[1],"yellow");
  strcpy(flag.color[2],"red");

  strcpy(color1,"green");
  flag.fg[0] = get_color_pixel(mydisplay,color1,1);
  strcpy(color2,"yellow");
  flag.fg[1] = get_color_pixel(mydisplay,color2,1);
  strcpy(color3,"red");
  flag.fg[2] = get_color_pixel(mydisplay,color3,1);
}


HEIGHT += 2*BORDER;     /* for TOP  AND BOTTOM */ 
WIDTH  += 2*BORDER;     /* for LEFT AND RIGHT */ 

  myhint.x = 0;
  myhint.y = 0;
  myhint.width = WIDTH;
  myhint.height = HEIGHT;
  myhint.flags = PPosition | PSize;


  strcpy(screen_color,fg_color);
  foreground = get_color_pixel(mydisplay,screen_color,1);
  fore_init = foreground;
  strcpy(screen_color,bg_color); 
  if(bandira) {strcpy(screen_color,"black");strcpy(bg_color,screen_color);}
  background = get_color_pixel(mydisplay,screen_color,0);

  mywindow = XCreateSimpleWindow ( mydisplay, DefaultRootWindow(mydisplay),
	     myhint.x, myhint.y, myhint.width, myhint.height, 5,
	     foreground, background);

  XSetStandardProperties (mydisplay, mywindow, "Ethiopic Viewer v 0.4                                                                                              Admas Concepts '94", "Eth-View",
                          None, 0, 0, &myhint);
  mygc = XCreateGC (mydisplay, mywindow, 0, 0);


  if(!no_eth_font) {
     ethfont = XLoadQueryFont(mydisplay,eth_font_name);
     if(!ethfont) {                                         /*Load Default*/
        printf("\aEthiopic font \"%s\" not found!\n",eth_font_name);
        sprintf(eth_font_name,"%s/%s",FONTDIR,ETHFONTNAME);
        printf("Loading %s \n",eth_font_name);
        no_eth_font=1;
     }
  }

    latfont = XLoadQueryFont(mydisplay,lat_font_name);
    if(!latfont) 
       if(strcmp(lat_font_name,LATINFONT1)){
          printf("\aLatin font \"%s\" not found!\n Loading font \"%s\"\n",lat_font_name,LATINFONT1);
          strcpy(lat_font_name,LATINFONT1);
          latfont = XLoadQueryFont(mydisplay,lat_font_name);
       }
    if(!latfont) {
       printf("\aLatin font \"%s\" not found!\n Loading font \"%s\"\n",LATINFONT1,LATINFONT2);
       strcpy(lat_font_name,LATINFONT2);
       latfont = XLoadQueryFont(mydisplay,lat_font_name);
    }
    if(!latfont) {
       printf("\aLatin font \"%s\" not found!\n Loading font \"%s\"\n",LATINFONT2,LATINFONT3);
       strcpy(lat_font_name,LATINFONT3);
       latfont = XLoadQueryFont(mydisplay,lat_font_name);
    }
    if(!latfont) {
       printf("\a\"%s\" font not found!\n Latin will be skipped in document.\n",LATINFONT3);
       nolat=1;
       no_lat_font=1;
    }

  if(eth && !no_eth_font) font = ethfont->fid;
    else if(!no_lat_font)  font = latfont->fid;
  if(!no_eth_font || !no_lat_font) XSetFont(mydisplay,mygc,font);

     
  XSetBackground (mydisplay, mygc, background);
  XSetForeground (mydisplay, mygc, foreground);


  XSelectInput (mydisplay, mywindow, ButtonReleaseMask | 
                ButtonPressMask | KeyPressMask | ExposureMask);

  XMapRaised (mydisplay, mywindow);

}


refresh()
 {

  if (bandira) {
     for (i=0;i<4;i++) {
         XSetForeground ( mydisplay, mygc, flag.fg[i] );
         XFillRectangle ( mydisplay, mywindow, mygc, 0, flag.y[i], WIDTH, flag.y[i+1] );
    }
  }
    else {
            XSetForeground ( mydisplay, mygc, background );
            XFillRectangle ( mydisplay, mywindow, mygc, 0, 0, WIDTH, HEIGHT);
    }  
   foreground = fore_init;
   XSetForeground ( mydisplay, mygc, fore_init );
}



