/* INCLUDE FILES */
#include <graphics/gfxmacros.h>
#include <stdio.h>
#include <exec/types.h>
#include <devices/printer.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <exec/exec.h>
#include <intuition/intuition.h>
#include <libraries/dosextens.h>

/* DEFINES */
#define MAUSKLICK while((nclass=message()) != MOUSEBUTTONS);
#define DRAWGAD    1
#define ZAHLGADI   2
#define ZAHLGADII  3
#define ZAHLGADIII 4
#define ZAHLGADIV  5
#define ZAHLGADV   6
#define EOS '\0'

/* VARIABLEN */
union Drucker *DruckerReq;        /* für die Druckerausgabe */
union Drucker {
   struct IOStdReq drucker_std;  
   struct IODRPReq drucker_drp;
   struct IOPrtCmdReq drucker_pcr;
};
struct MsgPort *DPforte;
extern struct MsgPort *CreatePort();
extern union Drucker *CreateExtIO();

int spa, zei, SEITE, blo, MAX, sei;  /* für den Rest */
static int zaehler2, fnr, nclass, zg1, zg2, zg3, zg4, zg5, schrift,znum;
static int farbe[85]; /* Dir oder Filename */
static int first=0, fahne=0;   /* flags */
static int nr;       /* die Nummer, ab der gelistet wird */
static unsigned char *Ename[85]; /* Name eines Eintrags */
static unsigned char filename[32] = NULL; /* Filename im Gadget */
static unsigned char drawer[200] = NULL;
static unsigned char undo[32];    /* UNDO Filename im Gadget */
static unsigned char undo2[200];
static unsigned char aktuellDir[200]; /* enthält die aktuelle Dir */
static unsigned char store[200]; /* Sicherheitskopie von aktuellDir */
static unsigned char Rwert[232]; /* Returnwert (Pfad + Filename ) */
static UBYTE zgad1[5]=NULL, undozgad1[5];
static UBYTE zgad2[5]=NULL, undozgad2[5];
static UBYTE zgad3[5]=NULL, undozgad3[5];
static UBYTE zgad4[5]=NULL, undozgad4[5];
static UBYTE zgad5[5]=NULL, undozgad5[5];
char buffer[512], hel[79], einzeichen;
UBYTE *a[15] [72];
static char *bef[] = {           /* Text der Kommando-Gadgets */
   "DF0:",
   "DF1:",
   "RAM:",
   "CD /" };
struct GfxBase              *GfxBase;
struct IntuitionBase        *IntuitionBase;
static struct IntuiMessage  *nachricht2;
static struct Window        *fenster2;
static struct RastPort      *RP1;
static struct FileLock      *dir;
static struct FileInfoBlock *eintrag;
static struct StringInfo infoN={          /* Info für Textgadget */
   (UBYTE *) filename, (UBYTE *) undo, 0,31,0,0,0,0,0,0,0,0,NULL };
static struct StringInfo infoN2={          /* Info für 2.Textgadget */
   (UBYTE *) drawer, (UBYTE *) undo2, 0,199,0,0,0,0,0,0,0,0,NULL };
static struct StringInfo info1={         /* Info für ZAHLGADI */
   (UBYTE *)zgad1,(UBYTE *)undozgad1, 0,4,0,0,0,0,0,0,0,0,NULL };
static struct StringInfo info2={         /* Info für ZAHLGADII */
   (UBYTE *)zgad2,(UBYTE *)undozgad2, 0,4,0,0,0,0,0,0,0,0,NULL };
static struct StringInfo info3={         /* Info für ZAHLGADIII */
   (UBYTE *)zgad3,(UBYTE *)undozgad3, 0,4,0,0,0,0,0,0,0,0,NULL };
static struct StringInfo info4={         /* Info für ZAHLGADIV */
   (UBYTE *)zgad4,(UBYTE *)undozgad4, 0,4,0,0,0,0,0,0,0,0,NULL };
static struct StringInfo info5={         /* Info für ZAHLGADV */
   (UBYTE *)zgad5,(UBYTE *)undozgad5, 0,4,0,0,0,0,0,0,0,0,NULL };
static struct Gadget *gaddy;
static struct Gadget Gad2={               /* 2.Textgadget */
   NULL, 12, 115, 237, 8,                 /* Pfadname */
   GADGHCOMP,RELVERIFY,STRGADGET,
   NULL,NULL,NULL,0,
   (APTR)&infoN2,DRAWGAD,NULL };
static struct Gadget Gad={               /* Textgadget */
   (APTR)&Gad2, 12, 132, 237, 8,         /* Filename */
   GADGHCOMP,RELVERIFY,STRGADGET,
   NULL,NULL,NULL,0,
   (APTR)&infoN,0,NULL };
static struct Gadget IntGad5={               /* 5. Intgadget */
   NULL, 42, 130, 32, 8,                     /* Papierlänge */
   GADGHCOMP,RELVERIFY|LONGINT|GADGIMMEDIATE,STRGADGET,
   NULL,NULL,NULL,0,
   (APTR)&info5,ZAHLGADV,NULL };
static struct Gadget IntGad4={               /* 4. Intgadget */
   (APTR)&IntGad5, 267, 84, 32, 8,           /* rechter Rand */
   GADGHCOMP,RELVERIFY|LONGINT|GADGIMMEDIATE,STRGADGET,
   NULL,NULL,NULL,0,
   (APTR)&info4,ZAHLGADIV,NULL };
static struct Gadget IntGad3={               /* 3. Intgadget */
   (APTR)&IntGad4, 153, 84, 32, 8,           /* linker Rand */
   GADGHCOMP,RELVERIFY|LONGINT|GADGIMMEDIATE,STRGADGET,
   NULL,NULL,NULL,0,
   (APTR)&info3,ZAHLGADIII,NULL };
static struct Gadget IntGad2={               /* 2. Intgadget */
   (APTR)&IntGad3, 267, 40, 32, 8,           /* bis Seite */
   GADGHCOMP,RELVERIFY|LONGINT|GADGIMMEDIATE,STRGADGET,
   NULL,NULL,NULL,0,
   (APTR)&info2,ZAHLGADII,NULL };
static struct Gadget IntGad1={               /* 1. Intgadget */
   (APTR)&IntGad2, 153, 40, 32, 8,           /* von Seite */
   GADGHCOMP,RELVERIFY|LONGINT|GADGIMMEDIATE,STRGADGET,
   NULL,NULL,NULL,0,
   (APTR)&info1,ZAHLGADI,NULL };
static struct NewWindow FensterVereinbarung = { /* Fenster */
   20,20,350,168,-1,-1,
   MOUSEBUTTONS|GADGETDOWN|GADGETUP,
   BORDERLESS|ACTIVATE,
   &Gad,NULL,NULL,NULL,NULL,0,0,0,0,
   WBENCHSCREEN };
openw()
{
   int len;
   if(!(fenster2 = (struct Window *)OpenWindow(&FensterVereinbarung)))
      closew(-1);
   RP1 = fenster2->RPort;
   for(len=0;len<=84;len++) /* Speicher belegen für Direinträge */
      Ename[len] = (UBYTE *)AllocMem(32, MEMF_PUBLIC|MEMF_CLEAR);
}
closew(wert)
int wert;
{
   if(fenster2)   CloseWindow(fenster2);
   if(first)      FreeMem(eintrag,sizeof(struct FileInfoBlock));
   if(wert<0)     DisplayBeep(0);
   else {
      for(wert=0;wert<=84;wert++) {   /* Speicher für Direinträge */
         strcpy(Ename[wert], "");     /* wieder freigeben */
         FreeMem(Ename[wert], 32);
      }
   }
   if(strlen(filename)==0) return(NULL);
   strcpy(Rwert,aktuellDir);
   if(Rwert[strlen(Rwert)-1]!=':') strcat(Rwert,"/");
   strcat(Rwert,filename);
   return(Rwert);
}
message()
{
   int nklasse=0;
   if(nachricht2 = (struct IntuiMessage *)GetMsg(fenster2->UserPort)) {
      nklasse = nachricht2->Class;
      gaddy   = (struct Gadget *)nachricht2->IAddress;
      ReplyMsg(nachricht2);
   }
   return(nklasse);
}
pruefes()
{
   struct FileHandle *handle;
   unsigned char p[232];

   strcpy(p,aktuellDir);   /* Ram-Disk angesprochen ? */
   if((p[0]==82||p[0]==114)&&(p[1]==65||p[1]==97)&&(p[2]==77||p[2]==109)
       &&p[3]==':') {
      strcat(p,"dummy");
      if(handle = Open(p,MODE_OLDFILE))
         Close(handle);
   }
}
cd(newdir)
unsigned char *newdir;
{
   int i;

   nr=0;
   strcpy(store,aktuellDir);  /* Sicherheitskopie */
   if(fahne==1)
      strcpy(aktuellDir,drawer);
   else if(*newdir == '/') {
      for(i=strlen(aktuellDir);aktuellDir[i]!='/'&&aktuellDir[i]!=':';i--);
      if(aktuellDir[i] == ':') i++;
      aktuellDir[i] = '\0';
   }
   else if(newdir[strlen(newdir)-1] != ':') {
      if(aktuellDir[strlen(aktuellDir)-1] != ':') strcat(aktuellDir,"/");
      strcat(aktuellDir, newdir);
   }
   else strcpy(aktuellDir, newdir);
   pruefes();
   if((dir = Lock(aktuellDir, ACCESS_READ)) != 0) UnLock(dir);
   else {
      strcpy(aktuellDir, store);
      return(1);
   }
   return(0);
}
readDir()
{
   int i;

   zaehler2 =0;
   eintrag=(struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock),0);
   dir = Lock(aktuellDir, ACCESS_READ);
   if((dir==NULL) || !Examine(dir,eintrag)) return(1);
   if(first == 0) {
      first++;
      if(!index(eintrag->fib_FileName, ' ')){
         strcpy(aktuellDir, eintrag->fib_FileName);
         strcat(aktuellDir, ":");
      }
      if(fahne==1)
         sprintf(aktuellDir, "RAM:");
   }
   ExNext(dir,eintrag);
   while(IoErr() != ERROR_NO_MORE_ENTRIES) {
      if(zaehler2+11>80) continue;
      if(eintrag->fib_DirEntryType >0){
         zaehler2++;
         strcpy(Ename[zaehler2],eintrag->fib_FileName);
         farbe[zaehler2]=2;
      }
      else{
         zaehler2++;
         strcpy(Ename[zaehler2],eintrag->fib_FileName);
         farbe[zaehler2]=1;
      }
      ExNext(dir, eintrag);
   }
   for(i=zaehler2+1;i<85;i++){
      strcpy(Ename[i],"");
      farbe[i]=0;
   }
   return(0);
}
print(text, col, xpos, ypos)
char *text;
int col, xpos, ypos;
{
   SetAPen(RP1, col);
   SetDrMd(RP1, JAM1);
   Move(RP1, xpos, ypos);
   Text(RP1, text, strlen(text));
}
block(col,x1,y1,x2,y2)
int col,x1,y1,x2,y2;
{
   SetAPen(RP1, col);
   SetDrMd(RP1, JAM1);
   RectFill(RP1, x1,y1,x2,y2);
}
Hlight(lnr)
int lnr;
{
   SetDrMd(RP1, COMPLEMENT);
   RectFill(RP1, 10, lnr+17,250,lnr+24);
   SetDrMd(RP1, JAM1);
}
setgad()
{
   int i;

   nr=0;
   block(2,10,5,350,168);
   block(1,0,0,340,163);
   block(2,8,19,275,107);
   block(0,10,20,250,106);
   Wdir();
   block(2,8,111,252,124);
   block(0,10,112,250,123);
   block(2,8,129,252,142);
   block(0,10,130,252,141);
   for(i=0;i<2;i++) {
      block(2,i*170+8,147,i*170+82,159);
      block(0,i*170+10,148,i*170+80,158);
   }
   print("OK",1,35,156);
   print("STOP",1,199,156);
   for(i=0;i<4;i++){
      block(2,283,i*33+19,332,i*33+41);
      block(0,285,i*33+20,330,i*33+40);
      print(bef[i],1,292,i*33+32);
   }
}
Hell(zahl)
int zahl;
{
   SetDrMd(RP1, COMPLEMENT);
   zahl=zahl-1;
   if(zahl<=3)
      RectFill(RP1,285,zahl*33+20,330,zahl*33+40);
   else
      RectFill(RP1,(zahl-4)*170+10,148,(zahl-4)*170+80,158);
   SetDrMd(RP1, JAM1);
}
checkgad()
{
   int i,xk,yk,knopfy;

   xk=fenster2->MouseX;
   yk=fenster2->MouseY;
   if(xk>285 && xk<330){
      for(i=0;i<4;i++){
         if(yk>i*33+20 && yk<i*33+40) return(i+1);
      }
   }
   if(yk>148 && yk<158){
      if(xk>10 && xk<80) return(5);
      if(xk>180 && xk<250) return(6);
   }
   if(xk>254 && xk<271 && yk>20 && yk<106){
      if(yk<nr+21 && nr>0) return(7);
      if(yk>nr+24 && nr<zaehler2-10) return(8);
      if(yk>=nr+21 && yk<=nr+24){
         while((nclass=message()) != MOUSEBUTTONS){
            knopfy=yk;
            while((yk=fenster2->MouseY)==knopfy){
               if((nclass=message())==MOUSEBUTTONS) return(0);
            }
            nr+=(yk-knopfy);
            if(nr<0 || nr>zaehler2-10){
               nr-=(yk-knopfy);
               MAUSKLICK
               return(0);
            }
            Wdir();
         }
      }
      return(0);
   }
   if(xk>10 && xk<250 && yk>23 && yk<103){
      fnr=nr+(yk-17)/8;
      return(9);
   }
   return(0);
}
fehler2()
{
   int i,j;

   block(1,283,118,332,140);
   for(i=0;i<32;i++) block(0,301-i,126-i/2,301+i,126+i/2);
   print("READ",2,286,122);
   print("READ",3,285,123);
   print("ERROR",2,282,132);
   print("ERROR",3,281,133);
   MAUSKLICK
   MAUSKLICK
   block(1,270,110,332,142);
   block(2,283,118,332,140);
   block(0,285,119,330,139);
   print(bef[3],1,292,131);
}
Wdir()
{
   int i;
   block(0,10,20,250,106);
   block(0,252,20,273,106);
   block(3,254,nr+21,271,nr+24);
   if(zaehler2==0){
      RefreshGadgets(&Gad,fenster2,NULL);
      return;
   }
   for(i=1;i<=10;i++){
      print(Ename[nr+i],farbe[nr+i],12,(i+1)*8+15);
   }
}
Ndir(new)
   char *new;
{
   if(cd(new)) fehler2();
   if(readDir()){
      fehler2();
      zaehler2=0;
   }
   Wdir();
   strcpy(drawer,aktuellDir);
   RefreshGadgets(&Gad,fenster2,NULL);
}
disk(anzeige)
char *anzeige;
{
   int gnr, wert,xa,xb;
   int ende =0;

   openw();
   setgad();
   xb = strlen(anzeige)*8;
   xa = 170 - xb /2;
   block(2,xa-19,3,xa+xb+19,15);
   block(0,xa-17,4,xa+xb+17,14);
   print(anzeige,1,xa,12);
   first=0; Ndir("DF0:");
   while(ende==0){
      nclass = message();
      switch(nclass){
         case GADGETDOWN:
            break;
         case GADGETUP:
            if(gaddy->GadgetID==DRAWGAD) {
               fahne = 1; /* Pfad wurde per Hand eingegeben */
               Ndir(drawer);
               fahne = 0;
            }
            break;
         case MOUSEBUTTONS: {
            gnr=checkgad();
            switch(gnr){
               case 1: {
                  Hell(1); /* Hell = complementäre Darstellung des Gadgets*/
                  first =0;
                  Ndir("DF0:");
                  MAUSKLICK
                  Hell(1);
                  break;
               }
               case 2: {
                  Hell(2);
                  first=0;
                  Ndir("DF1:");
                  MAUSKLICK
                  Hell(2);
                  break;
               }
               case 3: {
                  Hell(3);
                  fahne=1; first =0;
                  sprintf(drawer, "RAM:");
                  Ndir(drawer);
                  fahne=0;
                  Hell(3);
                  MAUSKLICK
                  break;
               }
               case 4: {
                  Hell(4);
                  Ndir("/");
                  MAUSKLICK
                  Hell(4);
                  break;
               }
               case 5: {
                  Hell(5);
                  wert =1;
                  ende++;
                  MAUSKLICK
                  Hell(5);
                  break;
               }
               case 6: {
                  Hell(6);
                  strcpy(filename, "");
                  wert=0;
                  ende++;
                  MAUSKLICK
                  Hell(6);
                  break;
               }
               case 7: {
                  nr--;
                  Wdir();
                  MAUSKLICK
                  break;
               }
               case 8: {
                  nr++;
                  Wdir();
                  MAUSKLICK
                  break;
               }
               case 9: {
                  Hlight((fnr-nr)*8);
                  if(farbe[fnr]==1){
                     strcpy(filename,Ename[fnr]);
                     RefreshGadgets(&Gad,fenster2,NULL);
                     MAUSKLICK
                     Hlight((fnr-nr)*8);
                     break;
                  }
                  MAUSKLICK
                  RefreshGadgets(&Gad,fenster2,NULL);
                  Hlight((fnr-nr)*8);
                  Ndir(Ename[fnr]);
                  break;
               }
               default: {
                  break;
               }
            }
            break;
         }
         default: {
            break;
         }
      }
   }
   closew(wert);
}
/* Druckerroutine */
MaskeSetzen()    /* Druckerfenster zeichnen */
{
   int i,k;
   block(2,10,5,350,168);
   block(1,0,0,340,163);
   for(i=0;i<2;i++) {
      block(2,i*170+48,147,i*170+122,159);
      block(0,i*170+50,148,i*170+120,158);
   }
   print("OK",1,75,156);
   print("STOP",1,239,156);
   for(i=0;i<4;i++) {
      block(2, 8,i*20+19,96,(i+1)*20+19);
      block(0,10,i*20+20,94,(i+1)*20+18);
   }
   print("NLQ",1,40,32);
   print("Pica",1,36,52);
   print("Elite",1,32,72);
   print("Schmal",1,28,92);
   block(2,8,107,96,127);
   block(0,10,108,94,126);
   print("Papier-",1,24,116);
   print(" länge", 1,24,124);
   block(2,8,127,96,139);
   block(0,10,128,94,138);
   for(k=0;k<2;k++) {   /* Wenn Blockmodus, dann keine Seitenabfrage */
      for(i=0;i<2;i++) {
         if(!(blo>0 && (i==0 || i==1) && (k==0 || k==1))) {
            block(2,114*k+104,i*16+19,114*k+218,i*16+35);
            block(0,114*k+106,i*16+20,114*k+217,i*16+34);
         }
         block(2,114*k+104,i*16+63,114*k+218,i*16+79);
         block(0,114*k+106,i*16+64,114*k+217,i*16+78);
      }
      block(2,114*k+104,123,114*k+218,139);
      block(0,114*k+106,124,114*k+217,138);
   }
   if(blo==0) { print("von Seite",1,125,30);
      print("bis Seite",1,240,30); }
   print("linker Rand",1,117,74);
   print("rechter Rand",1,227,74);
   block(2,104,107,332,123);
   block(0,106,108,330,122);
   print("Zeilennummerierung",1,146,118);
   print("AN",1,153,133);
   print("AUS",1,263,133);
}
Hell2(zahl)  /* complementäre Darstellung des jeweiligen Gadgets */
int zahl;
{
   SetDrMd(RP1, COMPLEMENT);
   if(zahl<=3)
      RectFill(RP1,10,zahl*20+20,94,(zahl+1)*20+18);
   else if(zahl<=5)
      RectFill(RP1,114*(zahl-4)+106,124,114*(zahl-4)+216,138);
   else 
      RectFill(RP1,(zahl-6)*170+50,148,(zahl-6)*170+120,158);
   SetDrMd(RP1, JAM1);
}
drucken(anzeige, anfse, anfze, seit, zeile)  /* Druckroutine */
char *anzeige;
int anfse, anfze, seit, zeile; /* Übergabe der maximal druckbaren Zeilen */
{
   int xa,xb,xc,ende;
   UBYTE c;

   schrift=1; znum=5; spa=seit; zei=zeile;
   zg1=1; zg2=spa+1; zg3=5; zg4=75; zg5=72;
   if(blo==0) {
      strcpy(zgad1,itoa(zg1));
      strcpy(zgad2,itoa(zg2));
   }
   strcpy(zgad3,itoa(zg3));
   strcpy(zgad4,itoa(zg4));
   strcpy(zgad5,itoa(zg5));
   if(blo==0) FensterVereinbarung.FirstGadget = &IntGad1;
   else       FensterVereinbarung.FirstGadget = &IntGad3;
   if(!(fenster2 = (struct Window *)OpenWindow(&FensterVereinbarung))) {
      FensterVereinbarung.FirstGadget = &Gad;
      return(0);
   }
   RP1 = fenster2->RPort;
   MaskeSetzen();
   if(blo==0) RefreshGadgets(&IntGad1,fenster2,NULL);
   else       RefreshGadgets(&IntGad3,fenster2,NULL);
   xb = strlen(anzeige)*8;
   xa = 170 - xb /2;
   block(2,xa-19,3,xa+xb+19,15);
   block(0,xa-17,4,xa+xb+17,14);
   print(anzeige,1,xa,12);
   Hell2(schrift); Hell2(znum);
   ende = checkit();
   CloseWindow(fenster2);
   FensterVereinbarung.FirstGadget = &Gad;
   if(ende==2) return(0); /* 2 -> Stopgadget im Printerfenster */
   DPforte=(struct MsgPort *)CreatePort(0,0); /* prt: öffnen */
   DruckerReq=(union Drucker *)
            CreateExtIO(DPforte,sizeof(union Drucker));
   if(OpenDevice("printer.device",0,DruckerReq,0) != 0) {
      sprintf(buffer, "Kann printer.device nicht öffnen!");
      Meldung(); 
      DeleteExtIO(DruckerReq, sizeof(union Drucker));
      DeletePort(DPforte);
      return(0);
   }              /* Schriftart u. Rand einstellen */
   sprintf(hel, "%cc%c[%d;%ds",27,27,zg3,zg4);  /* Reset + l. + r. Rand */
   switch(schrift) {
      case 0:  sprintf(buffer, "%c[2%cz",27,34);      /* NLQ */
               strcat(hel, buffer); break;
      case 1:  sprintf(buffer, "%c[0m",27);           /* Pica */
               strcat(hel, buffer); break;
      case 2:  sprintf(buffer, "%c[2w",27);           /* Elite */
               strcat(hel, buffer); break;
      case 3:  sprintf(buffer, "%c[4w", 27);          /* Schmal */
               strcat(hel, buffer); break;
   }
   xa =zg1-1; xb=0; xc=anfze; if(znum==5) znum=0; else znum=1;
   if(blo>0)  xa=anfse;
   while((ende=ausdruck(hel))==1) ;
   if(!ende) goto fin;
   FOREVER {  /* Hauptdruckroutine */
      if(xb==0) {  /* Anfang einer Textseite */
         if(blo>0)
            sprintf(buffer, "Block (D)rucken oder (A)bbruch!");
         else
            sprintf(buffer, "Seite %d! (U)eberspr. (A)bbr. (D)rucken!",xa+1);
         Meldung(); c=HolNachr(&einzeichen);
         if((c == 'u' || c == 'U') && !(blo>0)) {
            xa++; if(xa>zg2-1) { ende=1; break; }
            continue;
         }
         else if(c=='a' || c=='A') { ende =1; break; }
      }
      if(znum) {  /* Zeilennummerierung an */
         sprintf(buffer, "%4d ", znum); znum++;
         sprintf(hel, "%s\n", a[xa] [xc]);
         strcat(buffer, hel);
      }
      else       /* normal */
         sprintf(buffer, "%s\n", a[xa] [xc]);
      if(!(ende=ausdruck(buffer))) break;
      else if(ende==1) continue; 
      xb++; xc++;
      if(xa==zg2-1 && xc>zei) { ende=2; break; }
      if(xc>SEITE)  { xc=0; xa++; if(xa>zg2-1) {ende=2; break; } }
      if(xb>SEITE) { linefd(xb); xb=0; }
   }
fin:  if(ende==2) linefd(xb);
   if(ende) CloseDevice(DruckerReq);
   sprintf(buffer, "Ausdruck beendet!");
   Meldung();
}
linefd(xb)    /* Papier zum Anfang der nächsten Seite schieben */
int xb;
{
   sprintf(hel, "\n");
   if(zg5>xb) {
      while(ausdruck(hel)) { xb++; if(xb==zg5) break; }
   }
}
ausdruck(ausdr)   /* Senden einer Textzeile zum Drucken und Fehlerabfrage */
UBYTE *ausdr;
{
   UBYTE c;

   DruckerReq->drucker_std.io_Command = CMD_WRITE;
   DruckerReq->drucker_std.io_Data    = (APTR) ausdr;
   DruckerReq->drucker_std.io_Length  = strlen(ausdr);
   DruckerReq->drucker_pcr.io_Command = CMD_WRITE;
   if(DoIO(DruckerReq)) {
      sprintf(buffer, "Fehler! (A)bbruch oder erneuter (V)ersuch?");
      Meldung();
wi1:  if((c=HolNachr(&einzeichen)) == 'a' || c=='A') {
         DeleteExtIO(DruckerReq, sizeof(union Drucker));
         DeletePort(DPforte);
         return(0);
      }
      else if(c=='v' || c=='V')   return(1);
      goto wi1;
   }
   return(2);
}

checkit()  /* Betätigung eines Gadgets überprüfen */
{
   int ende = 0, gnr;

   while(ende==0){
      nclass = message();
      switch(nclass){
         case GADGETDOWN:
            break;
         case GADGETUP:
            switch(gaddy->GadgetID) {
               case ZAHLGADI:    /* je nach Gadget, die Richtigkeit der */
                  pruefzg1();    /* Eingabe überprüfen */
                  break;
               case ZAHLGADII:
                  pruefzg2();
                  break;
               case ZAHLGADIII:
                  pruefzg3();
                  break;
               case ZAHLGADIV:
                  pruefzg4();
                  break;
               case ZAHLGADV:
                  pruefzg5();
                  break;
            }
            break;
         case MOUSEBUTTONS:
            gnr=Mausabfrage();
            if(gnr>=0 && gnr<=3) {  /* Schriftart ausgewählt */
               if(schrift!=gnr) {
                  Hell2(schrift); schrift=gnr; Hell2(schrift);
               }
            }
            else if(gnr>=4 && gnr<=5) {  /* Zeilennummerierung An/Aus */
               if(znum!=gnr) {
                  Hell2(znum); znum=gnr; Hell2(znum);
               }
            }
            else if(gnr==6) {       /* OK */
               Hell2(6);
               pruefalles();
               ende++;
               MAUSKLICK
               Hell2(6);
            }
            else if(gnr==7) {          /* STOP */
               Hell2(7); ende=2;
               MAUSKLICK
               Hell2(7);
            }
            break;
         default:
            break;
      }
   }
   return(ende);
}
pruefalles()
{
   if (blo==0) {
      pruefzg1();
      pruefzg2();
   }
   pruefzg3();
   pruefzg4();
   pruefzg5();
}

Mausabfrage()  /* Überprüfung, ob bei Betätigung der linken Maustaste ein */
{              /* Gadget angewählt wurde */
   int i,xk,yk;

   xk=fenster2->MouseX;
   yk=fenster2->MouseY;
   if(xk>10 && xk<94){
      for(i=0;i<4;i++){       /* Schriftart */
         if(yk>i*20+20 && yk<(i+1)*20+18) return(i);
      }
   }
   if(yk>148 && yk<158){    
      if(xk>50 && xk<120) return(6);  /* OK */
      if(xk>220 && xk<290) return(7); /* STOP */
   }
   if(yk>124 && yk<138){
      for(i=0;i<2;i++) {   /* Zeilennummerierung an/aus */
         if(xk>114*i+106 && xk<114*i+216) return(i+4);
      }
   }
   return(8);
}
pruefzg1() /* Richtigkeit der Eingaben in die IntegerGadgets überprüfen */
{          /* von Seite */
   int zg1alt = zg1;
   zg1 = atoin(zgad1);
   if(zg1<1 || zg1>zg2) {
      zg1 = zg1alt;
      strcpy(zgad1,itoa(zg1));
      RefreshGadgets(&IntGad1,fenster2,NULL);
   }
}
pruefzg2()            /* bis Seite */
{
   int zg2alt = zg2;
   zg2 = atoin(zgad2);
   if(zg2<zg1 || zg2>spa+1) {
      zg2 = zg2alt;
      strcpy(zgad2,itoa(zg2));
      RefreshGadgets(&IntGad1,fenster2,NULL);
   }
}
pruefzg3()           /* linker Rand */
{
   int zg3alt = zg3;
   zg3 = atoin(zgad3);
   if(zg3<0 || zg3>zg4) {
      zg3 = zg3alt;
      strcpy(zgad3,itoa(zg3));
      if(blo>0) RefreshGadgets(&IntGad3,fenster2,NULL);
      else      RefreshGadgets(&IntGad1,fenster2,NULL);
   }
}
pruefzg4()           /* rechter Rand */
{
   int zg4alt = zg4;
   zg4 = atoin(zgad4);
   if(zg4<zg3 || zg4>137) {
      zg4 = zg4alt;
      strcpy(zgad4,itoa(zg4));
      if(blo>0) RefreshGadgets(&IntGad3,fenster2,NULL);
      else      RefreshGadgets(&IntGad1,fenster2,NULL);
   }
}
pruefzg5()         /* Papierlänge */
{
   int zg5alt = zg5;
   zg5 = atoin(zgad5);
   if(zg5<1 || zg5>72) {
      zg5 = zg5alt;
      strcpy(zgad5,itoa(zg5));
      if(blo>0) RefreshGadgets(&IntGad3,fenster2,NULL);
      else      RefreshGadgets(&IntGad1,fenster2,NULL);
   }
}
itoa(n)          /* Integer in ASCII-String umwandeln */
int n;
{
   UBYTE nach[5];
   int i=0, k, l, signum=FALSE;

   if(n<0) { signum=TRUE; n=-n; }
   do nach[i++] = n % 10 + '0'; while(n /= 10);
   if(signum) nach[i++] = '-';
   nach[i] = EOS;
   for(i=0, k=strlen(nach)-1; i<k; i++, k--) {
      l = nach[i]; nach[i] = nach[k]; nach[k] = l;
   }
   return(nach);
}
atoin(von)    /* ASCII-String in Integer umwandeln */
UBYTE *von;
{
   int n, signum=1;

   while(*von == ' ') von++;
   if(*von == '+' || *von == '-')
      signum = (*von == '+') ? 1 : -1;
   for(n=0; *von >= '0' && *von <= '9'; ++von)
      n = 10 * n + *von - '0';
   return(signum * n);
}
einstell(anzeige, flaggy, seit, zeil)  /* Seitenlänge o. Textbreite */
UBYTE *anzeige;                        /* einstellen */
int flaggy, seit, zeil;
{
   int xa,xb,ende=0;

   if(flaggy) zg5=MAX; else zg5=SEITE+1;
   strcpy(zgad5,itoa(zg5));
   FensterVereinbarung.Width = 156; FensterVereinbarung.Height = 52;
   IntGad5.LeftEdge = 65; IntGad5.TopEdge = 28;
   FensterVereinbarung.FirstGadget = &IntGad5;
   if(!(fenster2 = (struct Window *)OpenWindow(&FensterVereinbarung))) {
      tschuess();
      return(0);
   }
   RP1 = fenster2->RPort;
   block(2,10,5,156,52);
   block(1,0,0,146,47);
   xb = strlen(anzeige)*8;
   xa = 73 - xb /2;
   block(2,xa-19,3,xa+xb+19,15);
   block(0,xa-17,4,xa+xb+17,14);
   print(anzeige,1,xa,12);
   block(2,45,25,101,37);
   block(0,47,26,99,36);
   RefreshGadgets(&IntGad5,fenster2,NULL);
   while(!ende){
      if(message() == GADGETUP && gaddy->GadgetID == ZAHLGADV) ende++;
   }
   CloseWindow(fenster2);
   zg5 = atoin(zgad5);
   if(!seit && !zeil && !strlen(a[seit] [zeil])) {
      if(flaggy==0 && zg5>=10 && zg5<=72) { SEITE = zg5-1; sei=SEITE; }
      if(flaggy==1 && zg5>=10 && zg5<=78) MAX   = zg5;
   }
   tschuess();
}
tschuess()
{
   FensterVereinbarung.FirstGadget = &Gad;
   FensterVereinbarung.Width = 350; FensterVereinbarung.Height = 168;
   IntGad5.LeftEdge = 42; IntGad5.TopEdge = 130;
}
