/* 
 * Thomas Pantzer: xfbgi_cb.cc, v0.1b  10.8.99 19:1 
 * 
 * 
 * Copyright 1999 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@isys2.informatik.uni-leipzig.de   (Thomas Pantzer) 
 * 
 */

#include <stdlib.h>

#include "copyright.h"

#include "utils.h"
#include "graphix.h"
#include "forms.h"
#include "xfbgi.h"



/* callbacks for form bgi_gc */
void xfgc_canvas_cb(FL_OBJECT *ob, long data)
{
  /* fill-in code for callback */
}


int xfgc_canvas_event(FL_OBJECT *ob, Window win, int w, int h, XEvent *ev, void *d)
{
    char st[256],*ppre,f1[256],f2[256],f3[256];
    KeySym keysym;
    XComposeStatus compose;
    int i;
    
    switch (ev->type)
      {
       case KeyPress:
       case KeyRelease:
//         printf("code: %c\t%d\t0x%x\t",ev->xkey.keycode,ev->xkey.keycode,ev->xkey.keycode);
//         printf("serial: %c\t%d\t0x%x\t",ev->xkey.serial,ev->xkey.serial,ev->xkey.serial);
              
         i = XLookupString(&ev->xkey,st,20,&keysym,&compose);
#ifdef DEBUG
         fprintf(stderr,"state: %c\t%d\t0x%x\t",ev->xkey.state,ev->xkey.state,ev->xkey.state);
         fprintf(stderr,"count: %d\t keysym: %c\t%d\t0x%x\n",i,keysym,keysym,keysym);
#endif         
         if (i == 0)
            {
             switch (keysym)
               {
                case XK_F2:
		  ppre = getenv("XFGC_PPM_PATH\0");
		  if (ppre != NULL)
		     {
		      int j = 0;	
		      sprintf(f1,"%s/xfgc_ppm-%04d.rle",ppre,j);
		      sprintf(f2,"%s/xfgc_ppm-%04d.xpm",ppre,j);
		      sprintf(f3,"%s/xfgc_ppm-%04d.ppm",ppre,j);
		      while ((fexist(f1) == true)||(fexist(f2) == true)||(fexist(f3) == true))
			{
			 j++;	
			 sprintf(f1,"%s/xfgc_ppm-%04d.rle",ppre,j);
			 sprintf(f2,"%s/xfgc_ppm-%04d.xpm",ppre,j);
			 sprintf(f3,"%s/xfgc_ppm-%04d.ppm",ppre,j);
			}    
                      sprintf(st,"%s/xfgc_ppm-%04d",ppre,j);
		     }	
		   else
                     strcpy(st,"libxfgc_save");

		  fprintf(stderr,"call VScr.Save(%s)\n",st);
                  VScr.Save(st);      
                  break;
                  
                case XK_F10:
                  exit(0);
                  break;
                  
                default:
                  break;  
               }   
            }
         
         break;
       
       case ButtonPress: // Mousebutton
       case ButtonRelease:
         switch (ev->xbutton.button)
           {
            case 0:
              break;
              
            case 1:
              break;
              
            case 2:
              break;
              
            case 3:
              strcpy(st,"ppxfgc_save.xpm");
              VScr.Save(st);      
              break;
           }
         break;
       
       default:   
         VScr.Refresh();
         break;
      }   
         
    return 0;
}




