#include "declare.h"
void view(char x1,char y1,char x2,char y2,char color)
#define LINE 1024
{
 extern int s_v;
 FILE *f;
 char x=x1+1,y=y1+1,x1copy,y1copy,x2copy,y2copy,full_screen=0,ch;
 int i,xfile,yfile,line_f,fl_end,count;
 int *n;
 char *buffer;
 union keyb
 {
  int k;
  char ch[2];
 } key;
 union REGS r;
 char *str0,*str;
 str0 = new char[LINE];
 str = new char[x2-x1+1];
 n = new int [256];
 buffer = new char[3700];
 f = fopen("tmpfile.tmp","r");
 n[0] = line_f = 0;
 for(i=0;i<x2-x1+1;i++) str[i] = 0;
 for(y=y1+1,i=0;i<255;i++,y++,line_f++)
 {
 if(!fgets(str0,LINE,f)) break;
  n[i+1] = n[i] + strlen(str0) + 1;
  if(n[i+1] == n[i])
  {
   fl_end = 1;
   break;
  }
  if(y<y2)
  {
   str0[n[i+1]-n[i]-2] = 0;
   if((n[i+1]-n[i])<x2-x1-1) str = strcpy(str,str0);
   else str = strncpy(str,str0,x2-x1-1);
   menu(x1+1,y,str,color);
  }
 }
 menu(0,24,"ESC Выход  F5 Раскрыть\\Закрыть           ",3);
 cursor(0,24,80,MMNU);
 x1copy = x1;
 x2copy = x2;
 y1copy = y1;
 y2copy = y2;
 x = x1 + 1;
 y = y1 + 1;
 r.h.ah = 2;
 r.h.bh = 0;
 r.h.dh = y;
 r.h.dl = x;
 int86(0x10,&r,&r);
 xfile = yfile = 0;
 for(i=0;i<23;i++) save_video(0,1+i,80,buffer);
 s_v = 0;
 do
 {
  key.k = bioskey(0);
  switch(key.ch[1])
  {
   case 63:                             //F5
	    if(full_screen)
	    {
	     x1 = x1copy;
	     x2 = x2copy;
	     y1 = y1copy;
	     y2 = y2copy;
	     x = x1 + 1;
	     y = y1 + 1;
	     r.h.ah = 2;
	     r.h.bh = 0;
	     r.h.dh = y;
	     r.h.dl = x;
	     int86(0x10,&r,&r);
	     full_screen = 0;
	     delete str;
	     str = new char[x2-x1+1];
	     for(i=0;i<x2-x1+1;i++) str[i] = 0;
	     for(i=0;i<23;i++) restore_video(0,1+i,80,buffer);
	     s_v = 0;
	    }
	    else
	    {
	     x1 = 0;
	     x2 = 79;
	     y1 = 1;
	     y2 = 23;
	     full_screen = 1;
	     delete str;
	     str = new char[x2-x1+1];
	     for(i=0;i<x2-x1+1;i++) str[i] = 0;
	     make_window(x1,y1,x2,y2,color,0);
	     maliar_space(x1+1,y1+1,x2-1,y2-1,color);
	    }
	    for(ch=y1+1;ch<y2&&(line_f-yfile-ch+y1+1)>0;ch++)
	    {
	     if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-2)>=xfile)
	     {
	      fseek(f,n[yfile+ch-y1-1]+xfile,SEEK_SET);
	      fgets(str0,LINE,f);
	      str0[n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile-2] = 0;
	      if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile)<x2-x1-1) str = strcpy(str,str0);
	      else str = strncpy(str,str0,x2-x1-1);
	      for(i=strlen(str);i<x2-x1-1;i++) str[i] = ' ';
	     }
	     else for(i=0;i<x2-x1-1;i++) str[i] = ' ';
	     menu(x1+1,ch,str,color);
	    }
	   break;
   case 71: x = x1 + 1;                  //HOME
	    r.h.ah = 2;
	    r.h.bh = 0;
	    r.h.dh = y;
	    r.h.dl = x;
	    int86(0x10,&r,&r);
	    if(!xfile) continue;
	    xfile = 0;
	    for(ch=y1+1;ch<y2&&(line_f-yfile-ch+y1+1)>0;ch++)
	    {
	     if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-2)>=xfile)
	     {
	      fseek(f,n[yfile+ch-y1-1]+xfile,SEEK_SET);
	      fgets(str0,LINE,f);
	      str0[n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile-2] = 0;
	      if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile)<x2-x1-1) str = strcpy(str,str0);
	      else str = strncpy(str,str0,x2-x1-1);
	      for(i=strlen(str);i<x2-x1-1;i++) str[i] = ' ';
	     }
	     else for(i=0;i<x2-x1-1;i++) str[i] = ' ';
	     menu(x1+1,ch,str,color);
	    }
	    break;
   case 72: if(y!=y1+1)                       //вверх
	    {
	     y--;
	     r.h.ah = 2;
	     r.h.bh = 0;
	     r.h.dh = y;
	     r.h.dl = x;
	     int86(0x10,&r,&r);
	    }
	    else if(yfile)
		 {
		  yfile--;
		  for(ch=y1+1;ch<y2&&(line_f-yfile-ch+y1+1)>0;ch++)
		  {
		   if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-2)>=xfile)
		   {
		    fseek(f,n[yfile+ch-y1-1]+xfile,SEEK_SET);
		    fgets(str0,LINE,f);
		    str0[n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile-2] = 0;
		    if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile)<x2-x1-1) str = strcpy(str,str0);
		    else str = strncpy(str,str0,x2-x1-1);
		    for(i=strlen(str);i<x2-x1-1;i++) str[i] = ' ';
		   }
		   else for(i=0;i<x2-x1-1;i++) str[i] = ' ';
		   menu(x1+1,ch,str,color);
		  }
		 }
	    break;
   case 77: if(x != x2-1)                     //право
	    {
	     x++;
	     r.h.ah = 2;
	     r.h.bh = 0;
	     r.h.dh = y;
	     r.h.dl = x;
	     int86(0x10,&r,&r);
	    }
	    else
	    {
	     xfile++;
	     for(ch=y1+1;ch<y2&&(line_f-yfile-ch+y1+1)>0;ch++)
	     {
 	      if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-2)<xfile) continue;
 	      fseek(f,n[yfile+ch-y1-1]+xfile,SEEK_SET);
	      fgets(str0,LINE,f);
	      str0[n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile-2] = 0;
	      if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile)<x2-x1-1) str = strcpy(str,str0);
	      else str = strncpy(str,str0,x2-x1-1);
	      for(i=strlen(str);i<x2-x1-1;i++) str[i] = ' ';
	      menu(x1+1,ch,str,color);
	     }
	    }
	    break;
   case 75: if(x != x1+1)                     //лево
	    {
	     x--;
	     r.h.ah = 2;
	     r.h.bh = 0;
	     r.h.dh = y;
	     r.h.dl = x;
	     int86(0x10,&r,&r);
	    }
	    else if(xfile)
		 {
		  xfile--;
		  for(ch=y1+1;ch<y2&&(line_f-yfile-ch+y1+1)>0;ch++)
		  {
 		   if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-2)<xfile) continue;
 		    fseek(f,n[yfile+ch-y1-1]+xfile,SEEK_SET);
		    fgets(str0,LINE,f);
		    str0[n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile-2] = 0;
		    if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile)<x2-x1-1) str = strcpy(str,str0);
		    else str = strncpy(str,str0,x2-x1-1);
		    for(i=strlen(str);i<x2-x1-1;i++) str[i] = ' ';
		   menu(x1+1,ch,str,color);
		  }
		 }
	    break;
   case 80: if(y != y2-1)                     //низ
	    {
	     y++;
	     r.h.ah = 2;
	     r.h.bh = 0;
	     r.h.dh = y;
	     r.h.dl = x;
	     int86(0x10,&r,&r);
	    }
	    else if(yfile<line_f-y2+y1+1)
		 {
		  yfile++;
		  for(ch=y1+1;ch<y2&&(line_f-yfile-ch+y1+1)>0;ch++)
		  {
 		   if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-2)>=xfile)
 		   {
 		    fseek(f,n[yfile+ch-y1-1]+xfile,SEEK_SET);
		    fgets(str0,LINE,f);
		    str0[n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile-2] = 0;
		    if((n[yfile+ch-y1]-n[yfile+ch-y1-1]-xfile)<x2-x1-1) str = strcpy(str,str0);
		    else str = strncpy(str,str0,x2-x1-1);
		    for(i=strlen(str);i<x2-x1-1;i++) str[i] = ' ';
		   }
		   else for(i=0;i<x2-x1-1;i++) str[i] = ' ';
		   menu(x1+1,ch,str,color);
		  }
		 }
	    break;

  }
 }
 while(key.ch[1] != 1);
 fseek(f,0,SEEK_SET);
 for(i=0,y=y1+1;y<y2&&(line_f-yfile-y+y1)>0;y++,i++)
 {
  fgets(str0,LINE,f);
  str0[n[i+1]-n[i]-2] = 0;
  if((n[i+1]-n[i])<x2-x1-1) str = strcpy(str,str0);
  else str = strncpy(str,str0,x2-x1-1);
  menu(x1+1,y,str,color);
 }
 r.h.ah = 2;
 r.h.bh = 0;
 r.x.dx = 0x1900;
 int86(0x10,&r,&r);
 for(i=0;i<23;i++) restore_video(0,1+i,80,buffer);
 s_v = 0;
 menu(0,24," F1 - помощь   Enter - Выбор           ",3);
 cursor(0,24,80,MMNU);
 fclose(f);
 delete str;
 delete str0;
 delete n;
 delete buffer;
// remove("tmpfile.tmp");
}

