/***************************************************************************/
/*                         Window Tool				           */
/*      								   */
/*         Geschrieben und Compiliert mit Turbo C 2.0			   */
/*                                                                         */
/*     (c) 1990 Bernhard Mrz & Verlag Simon DISC_EDV_REPORT		   */
/*									   */
/***************************************************************************/

#include <dos.h>


void cursoroff(void);
void cursoron (void);


/************************************************************************/
/*                                                                      */
/*       Window Manager                                                 */
/*                                                                      */
/*       Release 2.1  	 	                                        */
/*                                                                      */
/*       Date: 13.06.1990                 Copyright:  Bernhard Mrz     */
/*                                                                      */
/************************************************************************/
/*									*/
/* bergabeparameter: int fenster(1,2,3,4,5,6)                          */
/*                                                                      */
/* 1 = Fensteranfang Spalte						*/
/* 2 = Fensteranfang Zeile						*/
/* 3 = Fensterhintergrundfarbe						*/
/* 4 = Fenstertextfarbe							*/
/* 5 = Rollfarbe							*/
/* 6 = Zeiger auf Fenstertext						*/
/*									*/
/* Funktion liefert einen int_Wert zurck ber gewhlte Position        */
/*                                                                      */
/* bei ESC wird 0 zurckgegeben						*/
/*									*/
/************************************************************************/

int fenster(int a,int b,int e,int f,int g,char *name[])
{
  int c,d,y,z,anzahl=0,laenge=0;
  void *bufptr;
  void *zeig;
  void *text[100];

  for(z=0; name[z] != '\0'; ++z)
   {
    for(y=0;name[z][y] != '\0';++y);

     if(y>laenge)
       laenge=y-1;

    ++anzahl;
   }

  --a;

  if(a > (77-laenge))
     a=77-laenge;

  d=anzahl+1+b;
  c=laenge+2+a;

  bufptr = (void*) malloc((c-a+1)*(d-b+1)*2);
  gettext(a,b,c,d,bufptr);

  zeig=bufptr;

  window(a,b,c,d); textbackground(e); textcolor(f); clrscr();

  gotoxy(1,1);  cputs("");gotoxy(c-a+1,1);  cputs("");
  gotoxy(1,d-b);cputs("");gotoxy(c-a+1,d-b);cputs("");

  gettext(a,d-1,c,d-1,text);puttext(a,d,c,d,text);


  for(z=2;z<=c-a;z++)
   {gotoxy(z,1); cputs(""); gotoxy(z,d-b+1); cputs("");}
  for(z=2;z<=d-b;z++)
   {gotoxy(1,z); cputs(""); gotoxy(c-a+1,z); cputs("");}

  gotoxy(2,2);

  for(z=0; name[z] != '\0'; ++z)
   {
    gotoxy(2,z+2);
    cputs(name[z]);
   }

   z=0;
   gotoxy(2,z+2); textbackground(f); textcolor(g); cputs(name[z]);

  for(;;)
   {
   int taste;

    if(kbhit() != 0)
     {
      taste=getch();
      if(taste==27)
       {
	fensteralt(a,b,c,d,zeig);
	return(0);
       }
      if(taste==13)
       {
	fensteralt(a,b,c,d,zeig);
	return(z+1);
       }
      if(taste==0);
       {
	taste=getch();
	if(taste==80)
	 {
	  gotoxy(2,z+2);textbackground(e);textcolor(f);cputs(name[z]);
	  ++z;
	   if(z>=anzahl)
	    { z=0; }
	  gotoxy(2,z+2);textbackground(f);textcolor(g);cputs(name[z]);
	 }
	if(taste==72)
	 {
	  gotoxy(2,z+2);textbackground(e);textcolor(f);cputs(name[z]);
	  --z;
	   if(z<0)
	    { z=anzahl-1; }
	  gotoxy(2,z+2);textbackground(f);textcolor(g);cputs(name[z]);
	 }
	if(taste==77)
	 {
	  fensteralt(a,b,c,d,zeig);
	  return(77);
	 }
	if(taste==75)
	 {
	  fensteralt(a,b,c,d,zeig);
	  return(75);
	 }
       }
     }
   }
}


/************************************************************************/
/*                                                                      */
/*      Text wieder herstellen						*/
/*                                                                      */
/************************************************************************/


fensteralt(int a,int b,int c,int d,void *bufptr)
{
  puttext(a,b,c,d,bufptr);
  free(bufptr);
}

/************************************************************************/
/*                                                                      */
/*      Fenster mit Rahmen ohne Text					*/
/*                                                                      */
/************************************************************************/
/*									*/
/* bergabeparameter: void rahmen(1,2,3,4,5,6)                          */
/*                                                                      */
/* 1 = Fensteranfang Spalte						*/
/* 2 = Fensteranfang Zeile						*/
/* 3 = Fensterende Spalte     						*/
/* 4 = Fensterende Zeile						*/
/* 5 = Fensterhintergrundfarbe						*/
/* 6 = Fensterrahmenfarbe						*/
/*									*/
/* Funktion liefert einen Zeiger auf alten Inhalt zurck		*/
/*									*/
/************************************************************************/

void *rahmen(int a,int b,int c,int d,int e,int f)
{
  int z;
  void *bufptr;
  void *zeig;
  void *text[100];


  bufptr = (void*) malloc((c-a+1)*(d-b+1)*2);
  gettext(a,b,c,d,bufptr);

  zeig=bufptr;

  window(a,b,c,d); textbackground(e); textcolor(f); clrscr();

  gotoxy(1,1);  cputs("");gotoxy(c-a+1,1);  cputs("");
  gotoxy(1,d-b);cputs("");gotoxy(c-a+1,d-b);cputs("");

  gettext(a,d-1,c,d-1,text);puttext(a,d,c,d,text);


  for(z=2;z<=c-a;z++)
   {gotoxy(z,1); cputs(""); gotoxy(z,d-b+1); cputs("");}
  for(z=2;z<=d-b;z++)
   {gotoxy(1,z); cputs(""); gotoxy(c-a+1,z); cputs("");}

  gotoxy(2,2);

  return(zeig);

}

/************************************************************************/
/*                                                                      */
/*   Cursorfunktionen							*/
/*                                                                      */
/************************************************************************/

void cursoroff(void)
{
	struct WORDREGS x;
	struct BYTEREGS h;
	union REGS regs;
	regs.h.ah=0x01; regs.h.ch =0x20;int86(0x10,&regs,&regs);

	/***** Schaltet Cursor aus *********/
}


void cursoron(void)
{
   struct WORDREGS x;
   struct BYTEREGS h;
   union REGS regs;
   regs.h.ah=0x01; regs.h.ch =0x40;int86(0x10,&regs,&regs);

   /***** Schaltet Cursor ein **********/



   regs.h.ah=0x01; regs.h.ch =0x06;regs.h.cl=0x07;int86(0x10,&regs,&regs);

   /***** setzt Cursorgre **********/
}

/*************************************************************************/
/*                                                                       */
/*  Hilfszeile am unteren Bildschirmrand                                 */
/*                                                                       */
/*************************************************************************/

note(char *notiz)
{
 window(1,25,80,25); textcolor(15); textbackground(1); clrscr();
 gotoxy(3,1);
 cputs(notiz);
}

/*************************************************************************/
/*									 */
/*  Warten auf Escape-Taste                                              */
/*                                                                       */
/*************************************************************************/

esc()
{
 while(getch() != 27) {}
}

/************************************************************************/
/*                                                                      */
/*      Fenster mit Rahmen und Text und Bezeichner			*/
/*                                                                      */
/************************************************************************/
/*									*/
/* bergabeparameter: void brahmen(1,2,3,4,5,6)                         */
/*                                                                      */
/* 1 = Fensteranfang Spalte						*/
/* 2 = Fensteranfang Zeile						*/
/* 3 = Fensterende Spalte     						*/
/* 4 = Fensterende Zeile						*/
/* 5 = Fensterhintergrundfarbe						*/
/* 6 = Fensterrahmenfarbe						*/
/* 7 = Bezeichner 							*/
/*									*/
/* Funktion liefert einen Zeiger auf alten Inhalt zurck		*/
/*									*/
/************************************************************************/

void *brahmen(int a,int b,int c,int d,int e,int f,char *bezeichner)
{
  int z;
  void *bufptr;
  void *zeig;
  void *text[100];


  bufptr = (void*) malloc((c-a+1)*(d-b+1)*2);
  gettext(a,b,c,d,bufptr);

  zeig=bufptr;

  window(a,b,c,d); textbackground(e); textcolor(f); clrscr();

  gotoxy(1,1);  cputs("");gotoxy(c-a+1,1);  cputs("");
  gotoxy(1,d-b);cputs("");gotoxy(c-a+1,d-b);cputs("");

  gettext(a,d-1,c,d-1,text);puttext(a,d,c,d,text);


  for(z=2;z<=c-a;z++)
   {gotoxy(z,1); cputs(""); gotoxy(z,d-b+1); cputs("");}
  for(z=2;z<=d-b;z++)
   {gotoxy(1,z); cputs(""); gotoxy(c-a+1,z); cputs("");}

  gotoxy(3,1); cputs(bezeichner);

  gotoxy(c-a-5,d-b+1);cputs(" Esc ");

  gotoxy(2,2);

  return(zeig);

}

/************************************************************************/
/*                                                                      */
/*      Kopfleiste mit automatischer Berechnung der Position		*/
/*                                                                      */
/************************************************************************/
/*									*/
/* bergabeparameter: void head(1,2,3,4,5,6,7)                          */
/*                                                                      */
/* 1 = Auswahlstelle       						*/
/* 2 = Fensterhintergrundfarbe						*/
/* 3 = Fensterrahmenfarbe     						*/
/* 4 = Markierungsfarbe 						*/
/* 5 = Farbe 1. Buchstabe     						*/
/* 6 = Textfarbe        						*/
/* 7 = Kopfleistentext							*/
/*									*/
/* Funktion liefert einen int Wert ber die Position zurck		*/
/*									*/
/************************************************************************/

head(int wahl,int hfarb,int rfarb,int mfarb,int bfarb,int tfarb,char *text[])
{
 int i=0,x,y,xalt,yalt,anfpos;

 --wahl;

 cursoroff();

 if(wahl==-2)
  {
   rahmen(1,1,80,3,hfarb,rfarb);
  }

 window(1,1,80,3); gotoxy(3,2);

 while(text[i] != '\0')
  {
   textcolor(tfarb);

   if(i==wahl) {textbackground(mfarb); anfpos=wherex();}

   else {textbackground(hfarb);}


   x=wherex(); y=wherey();
   cputs(text[i]);
   xalt=wherex(); yalt=wherey();
   textcolor(bfarb);
   gotoxy(x,y); putch(text[i][0]);
   xalt +=4;
   gotoxy(xalt,yalt);
   ++i;
  }
 cursoron();
 return(anfpos);
}

/************************************************************************/
/*                                                                      */
/*      Fenster zurcksetzen                                		*/
/*                                                                      */
/************************************************************************/
/*									*/
/* Parameter: keine                                                     */
/*                                                                      */
/************************************************************************/

winreset()
{
 textcolor(7); textbackground(0); window(1,1,80,25); clrscr();
}

/************************************************************************/
/*                                                                      */
/*      ungltige Taste gedrckt                            		*/
/*                                                                      */
/************************************************************************/
/*									*/
/* Parameter: keine                                                     */
/*                                                                      */
/************************************************************************/

falschtast()
{
 note("falsche Taste oder Funktion nicht implementiert               - Esc -");
 warnton();
 esc();
}

/************************************************************************/
/*                                                                      */
/*      Warnton ausgeben                                     		*/
/*                                                                      */
/************************************************************************/
/*									*/
/* Parameter: keine                                                     */
/*                                                                      */
/************************************************************************/

warnton()
{
 int i;

 sound(500);

 for(i=0;i<=20000;++i);

 nosound();
}

/************************************************************************/
/*                                                                      */
/*      Bildschirmhintergrund                                  		*/
/*                                                                      */
/************************************************************************/
/*									*/
/* bergabeparameter: int fenster(1,2,3,4,5,6)                          */
/*                                                                      */
/* 1 = Hintergrund Anfang  						*/
/* 2 = Hintergrund Ende   						*/
/* 3 = Farbe des Bildschrimhintergrunds					*/
/*									*/
/************************************************************************/
background(int a,int b,int c)
{
 int i;

 textcolor(c);
 for(i=a;i<=b;++i)
 cputs("");
}