(*************************************************************************)
(*                                                                       *)
(*  Epson LQ Printer Configurer - Version 1.0                            *)
(*                                                                       *)
(*  Author: Forrest A. Blood IV - 11/23/91                               *)
(*  Based on the original program for a Gemini 10x by G. Moore - 12/4/87 *)
(*                                                                       *)  
(*  This program allows you to configure an Epson LQ in various ways     *)
(*  including "Ultra Tiny" print.  This program has been tested on an    *)
(*  LQ-510 and an LQ-570 printer and may not work on other models. The   *)
(*  source code is well documented so you can modify the printer codes   *)
(*  for your specific printer.  You need Personal Pascal Version 2 to    *)
(*  compile this.                                                        *)
(*                                                                       *)
(*  This source code and compiled program are released into the public   *)
(*  domain and may not be sold for profit.                               *)
(*                                                                       *)
(*************************************************************************)

{$S10,D-}

Program Epson_Acc;

{$I GEMSUBS} 
{$I AUXSUBS}

LABEL 1991;

CONST 
  
 {The following are all CONST defins from the Resource Construction Program}
   PRT_BOX = 0;
   ULTRATNY = 4;
   PICA = 3;
   ELITE = 5;
   CONDENSE = 6;
   PAPER = 17;
   SKIPOVER = 18;
   D_WIDE = 19;
   D_STRIKE = 21;
   ITALICS = 20;
   BOLD = 22;
   FORMFEED = 16;
   TYPERITE = 14;
   LINEFEED = 12;
   DONE = 13;
   INITIAL = 15;
   LEFT = 25;
   RIGHT = 26;
   DOWN = 28;
   LPP = 27;
   UP = 29;
   LFSIZE = 30;

VAR

  {The following capitalized variables represent whether or
   not the indicated button is selected}

   PICA_,
   ELITE_,
   CONDENSE_,
   ULTRA_TINY_,
   PAPER_,
   SKIPOVER_,
   D_WIDE_,
   D_STRIKE_,
   ITALICS_,
   BOLD_ : Boolean;

   J,
   Choice,
   ToBeEdited,       {Which of the four buttons is to be edited}
   CurLeft,          {Current Left Margin}
   CurRight,         {Current Right Margin}
   CurLPP,           {Current Number of Lines Per Page}
   CurLFS : Integer; {Current Line Feed Size}

   Box : Dialog_Ptr;

   OutStr : Str255;  {String to send to the printer}

   P : File of Text;

   TheScreen : Screen_type;

   Initial_Draw,          {Has dialog been drawn yet?}
   ChangeSize : Boolean;  {Has size changed (eg from PICA to CONDENSED)}  

(****************************************************************)
Function bcostat(device : integer) : boolean;   
   BIOS(8);
(****************************************************************)
Procedure TypeWriter;

Begin
  Clear_Screen;
  Curs_Home;
  Curs_On;
  Writeln('Typewriter mode -- type /EX to quit');  
  Writeln;
  Repeat
    Readln(OutStr);
    If (OutStr <> '/EX') and (OutStr <> '/ex') then Writeln(P,OutStr);
  Until (OutStr = '/EX') or (OutStr = '/ex');
  Curs_Off;
  Clear_Screen;
End;
(****************************************************************)
Procedure UpdateNumber(Index,Value:Integer);

VAR S : Str255;
    NewValue,
    J, K : Integer;

{Gets current resolution:  0 = Low, 1 = Medium, 2 = High}
Function Get_rez:integer;
   XBIOS(4);

Begin
    {Convert the integer to a string}
    K := Value div 100;
    If K = 0 then S := ' ' else S := Chr(Ord('0') + K);
    If (Value < 10) then S := '  ' else   
    begin
      Value := Value mod 100;
      K := Value div 10;
      S := concat(S,Chr(Ord('0') + K));
      Value := Value Mod 10;
    end;
    S := concat(S,Chr(Ord('0') + Value));

    {Draw string directly to dialog box since Show_Dialog is WAY too slow}    
    Case Index of
      LEFT   : J := 99;
      RIGHT  : J := 115;
      LPP    : J := 131;
      LFSIZE : J := 147;
    end;

    If Get_rez = 0 then K := 288 else K := 448;
    If Get_rez = 2 then J := J * 2;
    Draw_Mode(1);
    Draw_String(K,J,S);

End;
(****************************************************************)

Begin
  If Init_Gem >= 0 then
  Begin
    If not Load_Resource('EPSONLQ.RSC') then
    J := Do_Alert('[3][ EPSONLQ.RSC not | found! ][ ABORT ]',1)
  else
    Begin
        Find_Dialog(PRT_BOX,Box);
        Center_Dialog(Box);
        Init_Mouse;
      
        PICA_ := True;
        ELITE_ := False;
        CONDENSE_ := False;
        ULTRA_TINY_ := False;
        PAPER_ := False;
        SKIPOVER_ := False;
        D_WIDE_ := False;
        D_STRIKE_ := False;
        ITALICS_ := False;
        BOLD_ := False;

        ToBeEdited := RIGHT;

        CurLeft := 0;
        CurRight := 80;
        CurLPP := 66;
        CurLFS := 30;

        Initial_Draw := False;
        ChangeSize := False;

        Rewrite(P,'PRN:');

        Obj_SetState(Box,LEFT,Selected | Shadowed,Initial_Draw);  
 
        Repeat
          If PICA_ 
            then Obj_SetState(Box,PICA,(Selected | Shadowed),Initial_Draw)
            else Obj_SetState(Box,PICA,(Shadowed),Initial_Draw);
          
          If ELITE_ 
            then Obj_SetState(Box,ELITE,(Selected | Shadowed),Initial_Draw)
            else Obj_SetState(Box,ELITE,(Shadowed),Initial_Draw);
          
          If CONDENSE_ 
            then Obj_SetState(Box,CONDENSE,(Selected | Shadowed),Initial_Draw)
            else Obj_SetState(Box,CONDENSE,(Shadowed),Initial_Draw);
          
          If ULTRA_TINY_ 
            then Obj_SetState(Box,ULTRATNY,(Selected | Shadowed),Initial_Draw)
            else Obj_SetState(Box,ULTRATNY,(Shadowed),Initial_Draw);
          
          If PAPER_ 
            then Obj_SetState(Box,PAPER,(Selected | Shadowed),Initial_Draw)
            else Obj_SetState(Box,PAPER,(Shadowed),Initial_Draw);
          
          If SKIPOVER_ 
            then Obj_SetState(Box,SKIPOVER,(Selected | Shadowed),Initial_Draw)
            else Obj_SetState(Box,SKIPOVER,(Shadowed),Initial_Draw);
          
          If D_WIDE_ 
            then Obj_SetState(Box,D_WIDE,(Selected | Shadowed),Initial_Draw)
            else Obj_SetState(Box,D_WIDE,(Shadowed),Initial_Draw);
          
          If D_STRIKE_ 
            then Obj_SetState(Box,D_STRIKE,(Selected | Shadowed),Initial_Draw)
            else Obj_SetState(Box,D_STRIKE,(Shadowed),Initial_Draw);
          
          If ITALICS_ 
            then Obj_SetState(Box,ITALICS,(Selected | Shadowed),Initial_Draw)
            else Obj_SetState(Box,ITALICS,(Shadowed),Initial_Draw);
          
          If BOLD_ 
            then Obj_SetState(Box,BOLD,(Selected | Shadowed),Initial_Draw)
            else Obj_SetState(Box,BOLD,(Shadowed),Initial_Draw);

          Obj_SetState(Box,LINEFEED,Shadowed,Initial_Draw);  
          Obj_SetState(Box,FORMFEED,Shadowed,Initial_Draw);  
          Obj_SetState(Box,TYPERITE,Shadowed,Initial_Draw);  
          Obj_SetState(Box,INITIAL,Shadowed,Initial_Draw);  
          Obj_SetState(Box,UP,Normal,Initial_Draw);  
          Obj_SetState(Box,DOWN,Normal,Initial_Draw); 

          If (not Initial_Draw) then Choice := Do_Dialog(Box,0)
                                else Choice := Redo_Dialog(Box,0);
      
          OutStr := '';

          Initial_Draw := True;
 
          If (Obj_State(Box,PAPER) & Selected) = 0
            then begin
                   PAPER_ := False;
                   OutStr := Concat(OutStr,#27,#120,#0); {DISABLE LQ Mode}
                 end
            else begin
                   PAPER_ := True;
                   OutStr := Concat(OutStr,#27,#120,#1); {ENABLE LQ Mode}
                 end;           
 
          If (Obj_State(Box,SKIPOVER) & Selected) = 0
          then begin
                   SKIPOVER_ := False;
                   OutStr := Concat(OutStr,#27,#79); {DISABLE PERFORATION SKIPOVER}
               end
            else begin
                   SKIPOVER_ := True;
                   OutStr := Concat(OutStr,#27,#78,#6); {ENABLE PERFORATION SKIPOVER (SKIPS 2 LINES)}
                 end;           
 
          If (Obj_State(Box,D_WIDE) & Selected) = 0
            then begin
                   D_WIDE_ := False;
                   OutStr := Concat(OutStr,#27,'W',#0); {DISABLE DOUBLEWIDE PRINT}
                 end  
            else begin
                   D_WIDE_ := True;
                   OutStr := Concat(OutStr,#27,'W',#1); {ENABLE DOUBLEWIDE PRINT}
                 end;           
 
          If (Obj_State(Box,D_STRIKE) & Selected) = 0
            then begin
                   D_STRIKE_ := False;
                   OutStr := Concat(OutStr,#27,'H'); {DISABLE DOUBLESTRIKE}
                 end  
            else begin
                   D_STRIKE_ := True;
                   OutStr := Concat(OutStr,#27,#71); {ENABLE DOUBLESTRIKE}
                 end;           

          If (Obj_State(Box,ITALICS) & Selected) = 0
            then begin
                   ITALICS_ := False;
                   OutStr := Concat(OutStr,#27,#53); {DISABLE ITALICS}
                 end  
            else begin
                   ITALICS_ := True;
                   OutStr := Concat(OutStr,#27,#52); {ENABLE ITALICS}
                 end;           
 
          If (Obj_State(Box,BOLD) & Selected) = 0
            then begin
                   BOLD_ := False;
                   OutStr := Concat(OutStr,#27,#70); {DISABLE EMPHASIZED (BOLD) PRINT}
                 end  
            else begin
                   BOLD_ := True;
                   OutStr := Concat(OutStr,#27,#69); {ENABLE EMPHASIZED PRINT}
                 end;           

          If (Obj_State(Box,PICA) & Selected) = 0
            then PICA_ := False
            else begin
                   If not PICA_ then ChangeSize := True;
                   PICA_ := True;
                   OutStr := Concat(OutStr,#27,#80); {SELECT PICA (10 CPI)}
                 end;           
 
          If (Obj_State(Box,ELITE) & Selected) = 0
            then ELITE_ := False
            else begin
                   If not ELITE_ then ChangeSize := True;
                   ELITE_ := True;
                   OutStr := Concat(OutStr,#27,#77); {SELECT ELITE (12 CPI)}
                 end;           
 
          If (Obj_State(Box,CONDENSE) & Selected) = 0
            then CONDENSE_ := False
            else begin
                   If not CONDENSE_ then ChangeSize := True;
                   CONDENSE_ := True;
                   OutStr := Concat(OutStr,#27,#103); {SELECT CONDENSED PRINT (17 CPI)}
                 end;           

          If (Obj_State(Box,ULTRATNY) & Selected) = 0
            then begin
                   ULTRA_TINY_ := False;
                   OutStr := Concat(OutStr,#27,#84); {CANCEL SUBSCRIPT MODE}
                   If not D_STRIKE_ then OutStr := Concat(OutStr,#27,'H'); {CANCEL DOUBLE STRIKE}
                 end  
            else begin
                   If not ULTRA_TINY_ then ChangeSize := True;
                   ULTRA_TINY_ := True;
                   OutStr := Concat(OutStr,#27,#83,#0,#15); {SELECT SUPERSCRIPT MODE & CONDENSED}
                   D_STRIKE_ := True;
                 end;           
                  
          If ChangeSize then
          begin
            CurLeft := 0;
            UpdateNumber(LEFT,CurLeft);
          end;  
          
          If ULTRA_TINY_ and ChangeSize then
          begin
            CurLFS := 17;
            UpdateNumber(LFSIZE,CurLFS);    
            CurLPP := 122;
            UpdateNumber(LPP,CurLPP);
            CurRight := 136;
            UpdateNumber(RIGHT,CurRight);
          end;    
    
          If (not ULTRA_TINY_) and ChangeSize then
          begin
            CurLFS := 30;
            UpdateNumber(LFSIZE,CurLFS);    
            CurLPP := 66;
            UpdateNumber(LPP,CurLPP);
            If PICA_ then CurRight := 80;
            If ELITE_ then CurRight := 96;
            If CONDENSE_ then CurRight := 120;
            UpdateNumber(RIGHT,CurRight);
          end;

          ChangeSize := False;

          If (Obj_State(Box,LEFT) & Selected) <> 0 then ToBeEdited := LEFT;        
          If (Obj_State(Box,RIGHT) & Selected) <> 0 then ToBeEdited := RIGHT;        
          If (Obj_State(Box,LPP) & Selected) <> 0 then ToBeEdited := LPP;        
          If (Obj_State(Box,LFSIZE) & Selected) <> 0 then ToBeEdited := LFSIZE;        

          J := 0;
          If (Obj_State(Box,UP) & Selected) <> 0 then J := 1;        
          If (Obj_State(Box,DOWN) & Selected) <> 0 then J := -1;        
              
          Case ToBeEdited of
            LEFT : If (CurLeft + J > -1) and (CurLeft + J < 136) then
                   Begin
                     CurLeft := CurLeft + J;
                     UpdateNumber(LEFT,CurLeft);
                   End;
            RIGHT : If (CurRight + J > 0) and (CurRight + J < 137) then
                    Begin
                      CurRight := CurRight + J;
                      UpdateNumber(RIGHT,CurRight);
                    End;
            LPP : If (CurLPP + J > 0) and (CurLPP + J < 128) then
                  Begin
                    CurLPP := CurLPP + J;
                    UpdateNumber(LPP,CurLPP);
                  End;
            LFSIZE : If (CurLFS + J > 0) and (CurLFS + J < 256) then
                     Begin
                       CurLFS := CurLFS + J;
                       UpdateNumber(LFSIZE,CurLFS);
                     End;
          end; {CASE}

          OutStr := Concat(OutStr,#27,#108,Chr(CurLeft));  {SET LEFT MARGIN}
          OutStr := Concat(OutStr,#27,#81,Chr(CurRight)); {SET RIGHT MARGIN}
          OutStr := Concat(OutStr,#27,#67,Chr(CurLPP));   {SET LINES PER PAGE} 
          OutStr := Concat(OutStr,#27,#51,Chr(CurLFS));   {SET LINE FEED SIZE in 1/180" INCREMENTS}

          If (Obj_State(Box,LINEFEED) & Selected) <> 0
            then OutStr := Concat(OutStr,#10); {SEND LINEFEED}

          If (Obj_State(Box,FORMFEED) & Selected) <> 0
            then OutStr := Concat(OutStr,#12);  {SEND FORMFEED}
  
          If Choice = INITIAL then    
          Begin
            PICA_ := True;
            ELITE_ := False;
            CONDENSE_ := False;
            ULTRA_TINY_ := False;
            PAPER_ := False;
            SKIPOVER_ := False;
            D_WIDE_ := False;
            D_STRIKE_ := False;
            ITALICS_ := False;
            BOLD_ := False;

            CurLFS := 30;
            UpdateNumber(LFSIZE,CurLFS);    
            CurLPP := 66;
            UpdateNumber(LPP,CurLPP);
            CurRight := 80;
            UpdateNumber(RIGHT,CurRight);
            CurLeft := 0;
            UpdateNumber(LEFT,CurLeft);

            OutStr := Concat(#27,#64);  {CODE TO INITIALIZE PRINTER TO POWER UP STATE}         
          End;

          While not bcostat(0) do
          Begin
            J := do_alert('[3][ Printer is not | responding! ][ Abort ]',1);
            If J = 1 then goto 1991;
          End;

          Write(P,OutStr); {Send OutStr to the printer!}
                    
          If Choice = TYPERITE then
           Begin
            Hide_Mouse;
            Save_Scrn(TheScreen);
            TypeWriter;
            Restr_Scrn(TheScreen);
            Show_Mouse;
          End;
        Until Choice = Done;

    END;

1991:
    Exit_Gem;
    END; 
END.

