/* 
 * Thomas Pantzer: graphix.h, v0.1b  23.9.97 22:29 
 * 
 * 
 * Copyright 1997 by Thomas Pantzer
 * 
 * Permission to use, copy, modify, and distribute this software for noncommercial 
 * use and without fee is hereby granted, provided that the above copyright notice 
 * appear in all copies and that both that copyright notice and this permission 
 * notice appear in supporting documentation, and that the name of the Author not be 
 * used in advertising or publicity pertaining to distribution of the software 
 * without specific, written prior permission. The Author makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is" 
 * without expressed or implied warranty. 
 * 
 * 
 * pantec@aix520.informatik.uni-leipzig.de   (Thomas Pantzer) 
 * 
 */


#ifndef GRAPHIX_H
#define GRAPHIX_H

#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>


// #define HAVE_MITSHM 

#ifdef HAVE_MITSHM                                                              
#include <sys/ipc.h>                                                            
#include <sys/shm.h>                                                            
#include <X11/extensions/XShm.h>                                                
#endif          


// #define ZOOM 300
#define NEURON_RADIUS   5


#define DefaultFont	0
#define SmallFont	1
#define TriplexFont	2
#define SansSerifFont	3
#define GothicFont	4

#define CopyPut   0
#define XORPut    1
#define OrPut	  2
#define AndPut    3
#define NotPut    4

//typedef struct

//#include "trigonom.h"
#include "forms.h"
#include "xfbgi.h"
#include "rle_bits.h"



typedef struct {
		int x,y;
               } t_vertex_2i;

class t_screen	{
public:
	t_vertex_2i center;
	int width,height,size,depth,TextFont,bck_w,bck_h;
	char resize,sync_often;
	
	Display *dpy;
	Visual *visual;
        FD_xfbgi *fd_xfbgi;
	t_color MapColor[1024];
	int ColorMapSize;
	unsigned char hash[256];
//	char inhibit;
	char ignore_expose;

	FL_OBJECT *colormap_canvas;

        // standalone init
        FL_OBJECT *Init(int *argc, char *argv[]);  
	FL_OBJECT *Init(int *argc, char *argv[],int vis, char *name);
        
        // xforms init with extra canvas
        void Init(int vis);      	    // pre-handler, before fl_show_form..  

        void Init(FL_OBJECT *canvas); 	    // post-handler, after fl_show_form..  

        void Show(const char *title);     // with fl_show_form(..
        void Show(int where_to_place);    
	void Hide(void);
	void Done(void);

        // with inlined canvas 
        void Init(Display *,GC );        

        // post init (before any drawing, after all windows visible)
        void Init(Window w, int x, int y); 

	void MakeColormap(FL_OBJECT *canvas);
	        
        void Save(char *fname);
        void Refresh(void);
        void Store(void);
	void SetPage(int i);
	void Resize(int x, int y);
        
        
        void MoveTo(int x, int y);
	void LineTo(int x, int y);
	void OutText(char *st);
	void SetWriteMode(int i);
	int GetWriteMode(void);
	int TextHeight(char *st);
	int TextWidth(char *st); 
	
        void Fill(int color);
//        void SetColor(int c);    

//	int LineWidth(int w);
	void DrawLine(int x1,int y1,int x2,int y2);  
	

//        void OutTextXY(int x,int y,char *t);       
//        void FillRectangle(int x,int y,int w,int h); 
//        void DrawRectangle(int x,int y,int w,int h); 
//        void DrawPoint(int x,int y);
        void SetTextStyle(int);
        void SetTextSize(int);
        void SetTextAttrib(const char *family, const char *thick, const char *slant);
//        void DrawCircle(int x, int y, int radius);
//        void FillCircle(int x, int y, int radius);
//        void PutImage(XImage *i, int sx, int sy, int dx, int dy, int w, int h);
        
        void BackingStore(char b);
        
//private:
         Window  win;
         Pixmap backpix;
         GC      gc;
         char backplane;
         int 	 page,LineWidth;
         
private:         
	 XFontStruct *XFont_Info;
         t_vertex_2i Cursor;
         char  title[256];
	 
//	void drawline(int x1,int y1,int x2,int y2);  
//   	void Init(Display *d, GC g);
}; 

#ifdef MAIN_G
#define vextern
#else
#define vextern extern
#endif

vextern t_screen VScr;
#ifdef HAVE_MITSHM                                                              
vextern XShmSegmentInfo shminfo;                                                 
#endif                                                                          


#undef vextern

#define SetColor(c)    \
   XSetForeground(VScr.dpy,VScr.gc,fl_get_flcolor(c))


#define drawline(x1,y1,x2,y2) \
            {\
             if (VScr.page == 0) \
              	 XDrawLine(VScr.dpy,VScr.win,VScr.gc,x1,y1,x2,y2); \
             \
             if ((VScr.backplane == true) && (VScr.backpix != 0)) \
                XDrawLine(VScr.dpy,VScr.backpix,VScr.gc,x1,y1,x2,y2);\
             \
             if (VScr.sync_often == true) \
                XSync(VScr.dpy,false);\
              }



#define OutTextXY(x,y,t) \
   { \
   if (VScr.page == 0)\
      XDrawString(VScr.dpy,VScr.win,VScr.gc,x,y,t,strlen(t));\
   \
   if ((VScr.backplane == true) && (VScr.backpix != 0))\
      XDrawString(VScr.dpy,VScr.backpix,VScr.gc,x,y,t,strlen(t));\
   \
   if (VScr.sync_often == true) \
      XFlush(VScr.dpy);\
   }   


#ifdef HAVE_MITSHM                                                              
#define __XPutImage__(d,wi,g,im,sx,sy,dx,dy,w,h)  \
              XShmPutImage(d,wi,g,im,sx,sy,dx,dy,w,h,True)         
#else                                                                           
#define __XPutImage__(d,wi,g,im,sx,sy,dx,dy,w,h)  \
              XPutImage(d,wi,g,im,sx,sy,dx,dy,w,h);     
#endif 

#define PutImage(ximg,sx,sy,dx,dy,w,h)\
{\
  \
   if (VScr.page == 0)\
      __XPutImage__(VScr.dpy,VScr.win,VScr.gc,ximg,sx,sy,dx,dy,w,h);\
\
   if ((VScr.backplane == true) && (VScr.backpix != 0))\
      __XPutImage__(VScr.dpy,VScr.backpix,VScr.gc,ximg,sx,sy,dx,dy,w,h);\
}






   
#define FillRectangle(x,y,w,h) \
{\
   if (VScr.page == 0)\
      XFillRectangle(VScr.dpy,VScr.win,VScr.gc,x,y,w,h);\
\
   if ((VScr.backplane == true) && (VScr.backpix != 0))\
      XFillRectangle(VScr.dpy,VScr.backpix,VScr.gc,x,y,w,h);\
}   
 
#define DrawRectangle(x,y,w,h) \
{\
   if (VScr.page == 0)\
      XDrawRectangle(VScr.dpy,VScr.win,VScr.gc,x,y,w,h);\
\
   if ((VScr.backplane == true) && (VScr.backpix != 0))\
      XDrawRectangle(VScr.dpy,VScr.backpix,VScr.gc,x,y,w,h);\
}

   
#define DrawPoint(x,y)\
{\
   if (VScr.page == 0)\
      XDrawPoint(VScr.dpy,VScr.win,VScr.gc,x,y);\
\
   if ((VScr.backplane == true) && (VScr.backpix != 0))\
      XDrawPoint(VScr.dpy,VScr.backpix,VScr.gc,x,y);\
 }   
                
                
#define DrawCircle(x, y, radius)\
{\
  if (VScr.page == 0)\
     XDrawArc(VScr.dpy,VScr.win,VScr.gc,x,y,radius,radius,0,360*72);\
\
  if ((VScr.backplane == true) && (VScr.backpix != 0))\
     XDrawArc(VScr.dpy,VScr.backpix,VScr.gc,x,y,radius,radius,0,360*72);\
}               
               
#define FillCircle(x, y, radius)\
{\
  if (VScr.page == 0)\
     XFillArc(VScr.dpy,VScr.win,VScr.gc,x,y,radius,radius,0,360*72);\
\
  if ((VScr.backplane == true) && (VScr.backpix != 0))\
     XFillArc(VScr.dpy,VScr.backpix,VScr.gc,x,y,radius,radius,0,360*72);\
}               
               
#define DrawPoly(xp,np,sh) \
{\
   if (VScr.page == 0)\
      XDrawPolygon(VScr.dpy,VScr.win,VScr.gc,xp,np,sh,CoordModeOrigin);\
\
   if ((VScr.backplane == true) && (VScr.backpix != 0))\
      XDrawPolygon(VScr.dpy,VScr.backpix,VScr.gc,xp,np,sh,CoordModeOrigin);\
}   
 

#define FillPoly(xp,np,sh) \
{\
   if (VScr.page == 0)\
      XFillPolygon(VScr.dpy,VScr.win,VScr.gc,xp,np,sh,CoordModeOrigin);\
\
   if ((VScr.backplane == true) && (VScr.backpix != 0))\
      XFillPolygon(VScr.dpy,VScr.backpix,VScr.gc,xp,np,sh,CoordModeOrigin);\
}   


#endif
