/*

file filter and accumulator test

             CREATOR: DAn williams
                DATE:
         DESCRIPTION: test the viewport class
            FILENAME: fileaccumtest.cpp
             STARTED: jan 22
    OPERATING SYSTEM: Linux
1st VERSION FINISHED:

  Apr 15 / 2003  update adding propfilter. dan.

*/


#include <stdio.h>
#include <stdlib.h>

void Error( char * desc );

#include "accumulator2.h" 
#include "filefilter.h"
#include "displayfilter.h"
#include "propaccumulator.h"
#include "propfilter.h"
#include "vif.h" 

#include <conio.h>

//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)
{
  
  VIF             device;  
  DisplayFilter1  DFilter;
  Accumulator     Cbuffer; // Content Buffer
  FileFilter1     FFilter;
  PropAccum       Tbuffer; // Tag buffer
  PropFilter      PFilter;
  Navigator       scout;
 
  int             command = IDLE;
  int             character;
  
/*
            Cbuffer-+-->DFilter
                    +-->FFilter
                    +-->scout
        
Tbuffer---->Pfilter---->Dfilter

            device---->Dfilter
*/



  PFilter.SetSource( &Tbuffer );            // propfilter lined to tag buffer
  DFilter.SetDisplay( &device );            // link data to display
  DFilter.SetSource( &Cbuffer );            // link data to content buffer
  DFilter.SetPropFilter( &PFilter );        // data to props
  FFilter.LoadFile( &Cbuffer, "test.txt" ); // load a file into content buffer
  scout.SetAccumulator( &Cbuffer );         // set up our scout

  Point2D_t   ScreenPos;
  Point2D_t   OldScreenPos;
  Point2D_t   OnScreenPos;
 
  OldScreenPos.X = ScreenPos.X   = 0;
  OldScreenPos.Y = ScreenPos.Y   = 0;
  OnScreenPos.X  = OnScreenPos.Y = 0;
  
 
  ChrOffset CursorPos, OldCursorPos;
  ChrOffset TempPos;
  
  CursorPos    = 0;
  OldCursorPos = CursorPos;
  
  DFilter.SetScroll( ScreenPos.X, ScreenPos.Y );
  if (Tbuffer.set( 0, 0, CursorPos, PROP_CURSOR_START ) != NoError ) { // index:owner 0:0, pos = 0, type = 0
    printf("error setting cursor\n");
  }
  DFilter.ReDraw();
  
  
  while(command) {
    
    command = IDLE;
    character = wgetch(conio_scr);
        
    if (character == 'q') {
       command = QUIT;
       
    }else if (character == 259){ // up
	   
//      fprintf(stderr, "Up: Pre: CursorPos %X\n", CursorPos);
      
      TempPos   = scout.LinePos(CursorPos) - 1;  // get the cur line pos
      CursorPos = scout.PrevLine(CursorPos);     // go to prev line
      if (scout.LineEnd(CursorPos) > (CursorPos + TempPos)){ //go to pos if poss.
        CursorPos += TempPos;
      } else {
        CursorPos = scout.LineEnd(CursorPos);   // else go to end of line
      }
      
   //   fprintf(stderr, "Up: Post: CursorPos %X\n", CursorPos);
      
      if (OldCursorPos != CursorPos) { // if we did move...
          OnScreenPos.Y--; // gee I hate backwards co-ordinate systems..
      }
      
	 }else if (character == 258){ // down
      
//      fprintf(stderr, "Down: Pre: CursorPos %X\n", CursorPos);
       
      TempPos   = scout.LinePos(CursorPos) - 1;  // get the cur line pos
      CursorPos = scout.NextLine(CursorPos);     // go to next line
      if (scout.LineEnd(CursorPos) > (CursorPos + TempPos)){ //go to pos if poss.
        CursorPos += TempPos;
      } else {
        CursorPos = scout.LineEnd(CursorPos);   // else go to end of line
      }
      
//      fprintf(stderr, "Down: Post: CursorPos %X\n", CursorPos);      
      
      if (OldCursorPos != CursorPos) { // if we did move...
          OnScreenPos.Y++; // gee I hate backwards co-ordinate systems..
      }
      
      // check what line we are on
      // if were off the screen, scroll up
        // * the problem with this is knowing where teh screen ends
        // * aside from that we can scroll up by moving the screen_start
        // pointer 1 line forward.
        // * but we have to dispense with the concept of knowing our 
        // ultimate y position
      
      
       
    }else if (character == 260){ // left
			
//      fprintf(stderr, "Left: Pre: CursorPos %X\n", CursorPos);         
          
      if (CursorPos > 0) {
		  CursorPos--;
		}
      
//      fprintf(stderr, "Left: Post: CursorPos %X\n", CursorPos);      	
       
    }else if (character == 261){ // right
    
 //     fprintf(stderr, "Right: Pre: CursorPos %X\n", CursorPos);     
    
		  CursorPos++;     
        
//      fprintf(stderr, "Right: Post: CursorPos %X\n", CursorPos);
              
    }
	 
    //do off-the-screen scrolling
    if ((scout.LinePos(CursorPos) + 1) > (ScreenPos.X + device.Width)) { // scroll right 
          ScreenPos.X = scout.LinePos(CursorPos) - device.Width + 1;
    }
    if ((scout.LinePos(CursorPos))  < ScreenPos.X + 1) {  // scroll left
          ScreenPos.X = scout.LinePos(CursorPos) - 1;
    }
    
    
	
    
    // check for changes and update as nesc.
    
   if ((OldScreenPos.X != ScreenPos.X) |
       (OldScreenPos.Y != ScreenPos.Y) |
       (OldCursorPos != CursorPos)) { 
       
      DFilter.SetScroll ( ScreenPos.X, ScreenPos.Y );
    
    // DFilter.Scroll( scout.PrevLine(CursorPos) );
    
     // reposition the cursor
     // Cursor type should be 2 for start, 3 for end 
     Tbuffer.set( 0, 0, CursorPos, PROP_CURSOR_START );
    // Tbuffer.set( 0, 0, CursorPos, 0 ); // index:owner 0:0, pos = 0, type = 0  
     DFilter.ReDraw();    
     
     OldCursorPos = CursorPos;
     OldScreenPos.X = ScreenPos.X;
     OldScreenPos.Y = ScreenPos.Y;
   }
	// reposition the cursor
  }

  return ( 0 );
}

//subroutines


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

