
	#include<stdio.h>
	#include<string.h>
	#include<process.h>
	#include<conio.h>

void	textline(int x,int x1,int y,int color)
{
int	i;
textcolor(color);
gotoxy(x,y);
for(i=0;i<x1;i++)
	putch('Û');
}
void	text_g_line(int x,int x1,int y,char ch,int bkcolor)
{
int	i;
textbackground(bkcolor);
gotoxy(x,y);
for(i=0;i<x1;i++)
	putch(ch);
}

void	text_v_line(int x,int y,int y1,char ch)
{
int	i;
for(i=0;i<y1;i++)
	{
	gotoxy(x,y+i);
	putch(ch);
	}
}

void	text_rect(int x,int y,int x1,int y1,int t)
{
gotoxy(x,y);
cprintf("Õ");
text_g_line(x+1,x1-2,y,'Í',t);
cprintf("¸");
gotoxy(x,y+y1);
cprintf("Ô");
text_g_line(x+1,x1-2,y+y1,'Í',t);
cprintf("¾");
text_v_line(x,y+1,y1-1,'³');
text_v_line(x+x1-1,y+1,y1-1,'³');
}
 void	text_bar(int x,int y,int x1,int y1,int bkcolor, int color)
{
int a;
textcolor(color);
textbackground(bkcolor);
for(a=0;a<y1;a++)
text_g_line(x,x1,a+y,' ',bkcolor);
text_rect(x,y,x1,y1,bkcolor);
}
void	text_xy(int x,int y,char *str,int color,int bkcolor)
{
gotoxy (x,y);
textcolor(color);
textbackground(bkcolor);
cprintf("%s",str);
}
void	textxy(int x,int y,char *str,int color)
{
gotoxy (x,y);
textcolor(color);
cprintf("%s",str);
}
void	text_menu(int x,int y,char *str,int color,int bkcolor,int key_color)
{
int i;
gotoxy (x,y);
for(i=0;str[i]!=0;i++)
{
	textcolor(color);
	 textbackground(bkcolor);
	if(str[i]=='~')
	{
		textcolor(key_color);
		i++;
		cprintf("%c",str[i]);
	}
	else
		cprintf("%c",str[i]);
  }
  }
   void cls(int bkcolor)
   {
   textbackground(bkcolor);
   clrscr();
   }
   void two_cls(int x,int y,int x1,int y1,int bkcolor)
 {
 int a;
  for(a=0;a<y1;a++) text_g_line(x,x1,a+y,' ',bkcolor);
 }
 char mn[20][50]={
 "   ~Work  with textfile   ",
 " ~Open work file to print ",
 "          ~Print          ",
 "    ~Exit from programm!  "
 };

 void   DrawMenu(int x,int y,int cur,char mn[20][50],int num_items)
{
int	i;
for(i=0;i<num_items;i++)
	{
	if(i==cur) text_menu(x,y+i,mn[i],0,2,4);
	else	   text_menu(x,y+i,mn[i],0,7,4);
	 }
}
int  curs(char mn[20][50], int num_items)
{
	int k;
	int n=0;
	text_bar(18,7,30,5,7,9);
DrawMenu(20,8,n,mn,num_items);
for(;;)
	{
	  k=getch();

	  if(k==27) return(-1);
	  if(k==13)
		{
		 two_cls(5,4,47,9,0);
		 return(n);
		}
	  if(k==0)
	    {
	     k=getch();


	     if(k==72)
			{
		 n--;
		 if(n<0)n=num_items-1;
		 DrawMenu(20,8,n,mn,num_items);
			}
	      if(k==80)
			{
		  n++;
		if(n>num_items-1)n=0;
		DrawMenu(20,8,n,mn,num_items);
			}
	    }
	}
}

/* char *str;*/
void	get_file_name(char *str)
  {
     int   i=0;
     text_bar(10,7,35,3,7,9);
     text_xy(11,8,"Please insert way to print file",5,7);
     text_g_line(11,30,9,' ',3);
     gotoxy(11,9);
    while((str[i]=getchar())!='\n') i++;
    if(i)str[i-1]=0;
 }

int	print_file(char *str)
{
   int	n=0;
   char	c;
    FILE *in,*fprn;
    in=fopen(str,"r");
    if(in==NULL)
       {
	 text_bar(10,7,37,4,7,9);
	 text_xy(11,8,"         Error opening file       ",9,7);
	 text_xy(11,9,"     Insert opening file right    ",9,7);
	 text_xy(11,10,"            Press any key !       ",9,7);
	 getch();
	 return(-1);
       }
     fprn=fopen("prn","w");
    while((c=getc(in))!=EOF)
  {
    putc(c,fprn);
    n++;
    if(n%50)
    putc(' ',fprn);
    else
    putc('\n',fprn);

  }
  fclose(in);
  fclose(fprn);
  cls(0);
  return(1);
}
 void text_work_file()
  {
  cls(0);
  text_g_line(1,79,1,' ',9);
  text_xy(25,1,"Text Redactor",14,9);
  text_g_line(1,79,2,' ',15);
  text_bar(1,3,79,21,7,15);
  getch();
  cls(0);
  }
  void glav_pult_menu()
  {
  int i,errorcode;
  char filename[150];
   for(;;)
  {
  cls(0);
  text_xy(4,2,"             Programm for print file,by Kyklin Aleksei                      ",2,9);
  i=curs(mn,4);
  if(i!=-1)
  {
  if(i==0) text_work_file();
  if(i==1) get_file_name(filename);
  if(i==2) errorcode=print_file(filename);
  if(i==3) break;
  }
  else break;
  //if(errorcode==-1) break;
  }
}
	    void main()
	    {
	    system("vaewer.exe print.pcx");
	    cls(0);
	    glav_pult_menu();
	    cls(0);
	    }


