/***************************************************************************
**     Words: Ein Programm zum Lernen des Vokabulars einer Fremdsprache   **
****************************************************************************
**                            geschrieben von                             **
**                            Daniel Neukomm                              **
**                             Sustenweg 17                               **
**                             CH-3014 Bern                               **
**                           Tel 031/41 80 97                             **
**                    od.   00 41 31 41 80 97 (von Deutschland aus)       **
****************************************************************************
**                Dieses Programm darf man frei kopieren.                 **
**             Das Kopieren ist nur für diejenigen verboten,              **
**         die daraus einen geschäftlichen Profit schlagen wollen         **
**             Die Copyright-Erklärung und meine Adresse muss             **
**                      im Programm stehen bleiben!                       **
***************************************************************************/
/*    In diesem Programmteil wird eine Vokabeldatei auf dem Bildschirm
      oder auf dem Drucker Normalschrift oder in Spickerschrift ausgegeben*/

#include <intuition/intuition.h>
#include <libraries/dos.h>
extern char Device[20],Sprache[15],File[20];
extern long handle;
extern short Anzahl;
extern char *Vocy;
extern long handle,Font9,Font8,flags;
extern struct Window *Window;
extern struct TextAttr TextAttr9;

Ausgabe()
{
static struct IntuiText okText = {3,0,JAM1,23,4,&TextAttr9,(UBYTE *) "ok",0};
static struct IntuiText cancelText ={3,0,JAM1,5,4,&TextAttr9,(UBYTE *)"cancel",0};
static struct IntuiText intuiText[3] ={
   {3,0,JAM1,2,4,&TextAttr9,(UBYTE *)"1: Bildschirm",0},
   {3,0,JAM1,2,4,&TextAttr9,(UBYTE *)"2: Drucker",0},
   {3,0,JAM1,2,4,&TextAttr9,(UBYTE *)"3: Drucker (Kleinschrift)",0}
      };
static short Koord[] = {
   0,0, 70,0, 70,17, 0,17, 0,0 };
static struct Border Border = {
   -1,-1,
   1,0,JAM1,
   5,Koord,NULL  };
static short Koord2[] = {
   0,0, 280,0, 280,17, 0,17, 0,0 };
static struct Border Border2 = {
   -1,-1,
   2,0,JAM1,
   5,Koord2,NULL  };
static struct Gadget okGadget = {
   0,180,172,69,16,GADGHCOMP,GADGIMMEDIATE| RELVERIFY,
   BOOLGADGET,(APTR)&Border,0,&okText,0,0,7,0   };
static struct Gadget cancelGadget = {
   0,390,172,69,16,GADGHCOMP,GADGIMMEDIATE | RELVERIFY,
   BOOLGADGET,(APTR)&Border,0,&cancelText,0,0,8,0   };
static struct Gadget AusgabeGadget[3] = {
 { 0,180,53,279,16,SELECTED,GADGIMMEDIATE | RELVERIFY | TOGGLESELECT,
   BOOLGADGET,(APTR)&Border2,0,&intuiText[0],0,0,1,0   },
 { 0,180,73,279,16,GADGHCOMP,GADGIMMEDIATE | RELVERIFY | TOGGLESELECT,
   BOOLGADGET,(APTR)&Border2,0,&intuiText[1],0,0,2,0   },
 { 0,180,93,279,16,GADGHCOMP,GADGIMMEDIATE | RELVERIFY | TOGGLESELECT,
   BOOLGADGET,(APTR)&Border2,0,&intuiText[2],0,0,3,0   }
      };
static char prt[] ="Drucker nicht ansprechbar\n";
   struct RastPort *rp;
   struct IntuiMessage *Nachricht;
   struct IntuiMessage *GetMsg();
   struct Gadget *gadget;
   ULONG Class;
   USHORT Code;
   static short Geraet=1,geoeffnet=0;
   short altGe,i,j;
   long whandle;
   register long *Zeiger;
   register char *lstr,*rstr;
   register short a,li,re;
   Write(handle,"\f",1);
   if (Load()<0) return(-1);
   rp=Window->RPort;
   Move (rp,250,25);
   SetAPen(rp,3);
   SetFont(rp,Font9);
   Text(rp,"Ausgabegerät ?",14);
   SetFont(rp,Font8);
   AddGadget(Window,&okGadget,-1);
   OnGadget(&okGadget,Window,0);
   AddGadget(Window,&cancelGadget,-1);
   OnGadget(&cancelGadget,Window,0);
   for (a=0;a<3;a++)
      {
      AddGadget(Window,&AusgabeGadget[a],-1);
      OnGadget(&AusgabeGadget[a],Window,0);
      }
   ModifyIDCMP(Window,GADGETUP|RAWKEY);
   do
      {
      WaitPort (Window->UserPort);
      Nachricht=GetMsg(Window->UserPort);
      Class=Nachricht->Class;
      if(Class==RAWKEY)
         Code=Nachricht->Code;
      else
         {
         gadget=(struct Gadget *)Nachricht->IAddress;
         Code=gadget->GadgetID;
         }
      ReplyMsg(Nachricht);
      altGe=Geraet;
      switch (Code)
         {
         case 1:
         case 29: Geraet=1;break;
         case 2:
         case 30: Geraet=2;break;
         case 3:
         case 31: Geraet=3;break;
         default: continue;
         }
      SetFont (rp,Font9);
         do
         {
         if (altGe==Geraet)
              { SetAPen(rp,3);AusgabeGadget[Geraet-1].Flags=SELECTED;}
         else { SetAPen(rp,0);AusgabeGadget[altGe-1].Flags=GADGHCOMP;}
         RectFill(rp,180,33+altGe*20,458,48+altGe*20);
         Move(rp,182,43+20*altGe);
         if (altGe==Geraet)
              { SetAPen(rp,0);SetBPen(rp,3);}
         else SetAPen(rp,3);
         Text(rp,intuiText[altGe-1].IText,strlen(intuiText[altGe-1].IText));
         SetBPen(rp,0);
         if (altGe==Geraet) break;
         altGe=Geraet;
         } while (Class==RAWKEY);
      SetFont (rp,Font8);
      } while (Code!=7 && Code!=8);
   ModifyIDCMP(Window,flags);
   for (a=0;a<3;a++)
      RemoveGadget(Window,&AusgabeGadget[a]);
   RemoveGadget(Window,&cancelGadget);
   RemoveGadget(Window,&okGadget);
   Write(handle,"\f",1);
   if (Code==8) return(-1);
   switch (Geraet)
   {
   case 1: whandle=handle;break;
   case 2: if((whandle=Open("prt:",MODE_NEWFILE))==0)
              {Write(handle,prt,26);return(-2);}
           geoeffnet=1;
           break;
   case 3: if (geoeffnet==0)
              {
              if((whandle=Open("prt:",MODE_NEWFILE))==0)
               {Write(handle,prt,26);return(-2);}
               Write(whandle,"\033[0z",4);
                  /* 1/8 line spacing  eigentlich unnötig */
              Close(whandle);
              geoeffnet=1;
              }
           if((whandle=Open("par:",MODE_NEWFILE))==0)
               {Write(handle,prt,26);return(-2);}
           Write(whandle,"\033A\005",3);
   /* Zeilenabstand 5/72 inch auf Epson-Drucker*/
           Close (whandle);
           if((whandle=Open("prt:",MODE_NEWFILE))==0)
              {Write(handle,prt,26);return(-2);}
           if(Write(whandle,"\033[2w\033[4w\033[2v",12)<0) goto Abbruch;
                    /* elite on;condensed on;superscript on*/
           break;
   }
   Zeiger=(long *)Vocy;
   for (i=0,j=0;i<Anzahl;i++,Zeiger++)
      {
      lstr=(char *)*Zeiger;
      if ((li=instr(lstr,"/"))<0)
         {
         if(Write(whandle,lstr,strlen(lstr))<0) goto Abbruch;
         if(Write(whandle,"\n",1)<0) goto Abbruch;
         if (++j==22) {j=0;if (Geraet==1) if(Taste()) goto Abbruch;}
         }
      else
         {
         rstr=lstr+li+1;
         re=strlen(rstr);
         while (li!=0||re!=0)
            {
            if (li>38)
              {
              for (a=38;a>0 && *(lstr+a)>='@';a--)
              ;
              if (a!=38) a++;
              if (a==1) a=38;
              li-=a;
              }
            else {a=li;li=0;}
            if (a>0)
               {
               if (Write(whandle,lstr,a)<0) goto Abbruch;
               lstr+=a;
               if (*lstr==' ') {lstr++;li--;}
               }
            if(Write(whandle,"                                       ",39-a)<0) goto Abbruch;
            if (re>38)
              {
              for (a=38;a>0 && *(rstr+a)>='@';a--)
              ;
              if (a!=38) a++;
              if (a==1) a=38;
              re-=a;
              }
            else {a=re;re=0;}
            if (a>0)
               {
               if (Write(whandle,rstr,a)<0) goto Abbruch;
               rstr+=a;
               if (*rstr==' ') {rstr++;re--;}
               }
            if (rstr!=39) if(Write(whandle,"\n",1)<0) goto Abbruch;
            if (++j==22) {j=0;if(Geraet==1) if(Taste()) goto Abbruch;}
            }
         }
      }
Abbruch:
   switch (Geraet)
   {
   case 1: if (j!=0) Taste();break;
   case 2: Close(whandle);break;
   case 3: Write(whandle,"\033[0w\033[1z\033[0v",12);
              /*Drucker normal pitch;1/6 line spacing superscript off*/
           Close(whandle);
   }
}

Taste()
{
   char Buffer[3];
   Write(handle,"\033[33mBitte drücken Sie eine Taste\033[31m",38);
               /*die speziellen Codes bewirken, dass zuerst auf Farbe 3
                 geschaltet wird und am Schluss wieder auf Farbe 1
                 zurückgekehrt wird.*/
   Read (handle,Buffer,3);
   Write(handle,"\f",1);
   return(Buffer[0]==27);
}
