/*

file filter, display filter, accumulator, and contentfilter test
  gee whiz, its just about a qualified editor...

             CREATOR: DAn williams
                DATE:
         DESCRIPTION: finally, I can edit with it...
            FILENAME: fileaccumtest.cpp
             STARTED: mar 2 2002
    OPERATING SYSTEM: Linux
1st VERSION FINISHED:

*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h> //strchr
#include <conio.h>

#include "accumulator2.h"
#include "filefilter.h"
#include "displayfilter.h"
#include "contentfilter.h"
#include "vif.h" 


void Error( char * desc );

//global variables

#define QUIT  0
#define IDLE  1
#define DOWN  2
#define LEFT  4
#define RIGHT 6
#define UP    8

int main(int argc, char** argv)
{
  
  Accumulator     data;
  FileFilter1     FFilter;
  ContentFilter   CFilter;
  DisplayFilter1  window;
  VIF             device;
  
  int      command = IDLE;
  int      character;
  long int X  = 0,      Y  = 0;
  long int OX = X,      OY = Y;
  int      CursorX = 1, CursorY = 1;
  long     posTemp;

  char regular[] = {" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,./<>?`~!@#$%^&*()_+|-=\'\""};

  window.SetDisplay(&device);
  FFilter.LoadFile(&data, "test.txt");
  CFilter.SetAccumulator( &data );
  window.SetSource(&data);
  window.ReDraw();

  // seek to first character
  CFilter.Seek( 0, SEEK_SET );

  // reposition the cursor
  device.CursorPos(CursorX, CursorY);
  
  while(command) {
    
    command = IDLE;
    character = wgetch(conio_scr); // its important to note that this return simple results
        
    if (character == 27) { // escape = quit
       command = QUIT;
       
    }else if (character == 261){ // right
	 
	   CFilter.Seek( 1, SEEK_CUR );
		 
    }else if (character == 260){ // left
	 
	   CFilter.Seek( -1, SEEK_CUR );
             
    }else if (character == 263){ // backspace       
       
       CFilter.Seek( -1, SEEK_CUR );
       CFilter.Replace("");
      
    }else if (character == 330){ // delete
    
       CFilter.Replace("");  
       
    }else if (( character == 0x0A ) || ( character == 0x0D )){ // enter... "EVIL LINEFEEDS!!!"
    
       CFilter.Putc( 0x0A );
      
    }else if (strchr(regular, character) != NULL){ // insert regular character   
      
      CFilter.Putc( character );      
      
    }else if (character == 258){ // down
	 
        // the minus 1 accounts for nextline pointing to the first character.
        // this needs to check to see that its LinePos is valid on the next line.
        CFilter.Seek( CFilter.NextLine() + CFilter.LinePos() - 1  , SEEK_SET );
		 
    }else if (character == 259){ // up
	
        // the minus 1 accounts for nextline pointing to the first character.
        // this needs to check to see that its LinePos is valid on the next line.
        CFilter.Seek( CFilter.PrevLine() + CFilter.LinePos() - 1  , SEEK_SET );
  
   
   /*
      
		if (CursorY > 0) {
		 CursorY--;
		} else {
       command = UP;
       Y = MAX(0, Y - 1);
		} 
       */
       
       
    }
	 
	 /*
   if ((OX != X) | (OY != Y)) { 
     window.SetScroll ( X, Y );
     window.ReDraw();    
     OX = X;
     OY = Y;
   }
   */
	// reposition the cursor
  // window.SetScroll ( 1, 1 );
   window.ReDraw();
	device.CursorPos(1, 1); // temp home for cursor
	
  }

  return ( 0 );
}

//subroutines


void Error( char * desc ){
  printf ("%s\n", desc);
  exit ( 0 );
}

