EXTERNAL
! =======================================================================
! True BASIC, Inc
! 12 Commerce Avenue
! West Lebanon, New Hampshire 03784-9758
! (800) 872-2742,  (603) 298-8517
! =======================================================================
! Author: Paul Castonguay
! =======================================================================

SUB Page(Line$(),Arg$)
! =======================================================================
! PURPOSE: To send to the printer whatever text is currently in the
!          True BASIC editor.  A header containing an optional title,
!          time, date, and page number is placed at the top of every
!          page.  This program is exemplary of how you can control your
!          printer from within your own programs.  Although it is
!          supplied by True BASIC, Inc, it is not really an official
!          part of the language.  Rather it is a support utility
!          that you should customize depending on your own requirements
!          and preferences.  Remember, True BASIC is a programming
!          language, and YOU are the programmer.  After modifying this
!          utility, save a COMPILED version under the name PAGE.
!
!
! SCOPE: PUBLIC
!
!
! PLATFORM DEPENDANCE: Commodore Amiga only.
!
!   Because this utility uses the platform dependant PrinterLIB* LIBRARY
!   (within the AmigaTools drawer) it will work as written only on a
!   Commodore Amiga.  However, you will find a similar version of this
!   same utility on the MAC and IBM versions of True BASIC.  To get the
!   best performance from this program on your Amiga, make sure you have
!   selected the proper printer driver from within Commodore's preference
!   program.
!  
!
! PARAMETERS:
!
!    INPUT:
!
!       Line$() ... Array containing the lines of text that are currently
!                   in the True BASIC editor.  Each element of the array
!                   contains a different line.
!
!       Arg$ ...... An argument passed by the user on the True BASIC
!                   COMMAND line.
!
!
! TEMPLATE:
!
!    To execute the compiled version of this utility:
!
!          DO PAGE
!
!       or:
!
!          DO PAGE, Title_String$
!
!    To execute the source code version:
!
!          DO PAGE.TRU
!
!       or:
!
!          DO PAGE.TRU, Title_String$
!
!    For example, to print this file from the True BASIC editor, enter
!    the following into the COMMAND window:
!
!       DO PAGE, PAGE.TRU Listing
! 
! =======================================================================

   ! ******************************************************
   ! LIBRARY used to access custom features of your printer
   ! ******************************************************
   LIBRARY "{AmigaTools}PrinterLIB*"

   ! *********************************
   ! Declare whatever functions within
   ! the library that you want to use.
   ! *********************************
   DECLARE FUNCTION NLQ_ON$, NLQ_OFF$
   DECLARE FUNCTION Italics_ON$, Italics_OFF$
   DECLARE FUNCTION Boldface_ON$, Boldface_OFF$

   ! **************************************
   ! Define title to appear at top of page.
   ! If the user does not supply one, use
   ! the name True BASIC(tm).
   ! **************************************
   IF Arg$ <> "" THEN
      LET Title$ = Arg$
   ELSE
      LET Title$ = "True BASIC(tm)"
   END IF

   ! **********************************************
   ! Constants that you should set for your printer
   ! **********************************************
   LET Page_Width  = 80           ! Width of printing area
   LET Top_Margin  = 4            ! Top margins (2 minimum)
   LET Page_Length = 58           ! Lines of text you want printed per
                                  ! page, excluding the top margin

   ! **********************************************
   ! Set up the printer from its default condition.
   ! Use near letter quality elite print style.
   ! **********************************************
   CALL Open_Channel(#99)
   CALL InitializePrinter(#99)
   PRINT #99: NLQ_ON$;
   CALL Elite_ON(#99)
   CALL LRMargins(#99, 7, 95)

   ! *******************************
   ! Print header for the first page
   ! *******************************
   LET Line_Number = Top_Margin + 1
   LET Pages       = 1
   CALL Print_Header(#99, Page_Width, Title$, Pages, Top_Margin)

   ! ****************************************
   ! Loop through all the lines of the editor
   ! ****************************************
   FOR I = 1 TO UBOUND(Line$)

      ! ****************************************
      ! Put a line from the editor into a string
      ! ****************************************
      LET Print_Text$ = Line$(I)
      LET CommentFlag = 0

      ! ***********************************************
      ! A loop is used to deal with lines in the editor
      ! that are longer that your printer's page width.
      ! ***********************************************
      DO

         ! ***********************************
         ! Change pages and print a new header
         ! if the last page is complete
         ! ***********************************
         IF Line_Number >= Page_Length-Top_Bottom_Margin THEN
            CALL New_Page(#99)
            LET Line_Number = Top_Margin + 1
            LET Pages       = Pages + 1
            CALL Print_Header(#99, Page_Width, Title$, Pages, Top_Margin)
         END IF
 
         ! ***************************************************
         ! Determine position of comment to change print style
         ! ***************************************************
         LET Ex_Point = POS(Print_Text$, CHR$(33))

         IF CommentFlag = 1 THEN

            ! *******************************************
            ! Print comment in letter quality italic mode
            ! *******************************************
            PRINT #99: Italics_ON$;
            PRINT #99: Print_Text$[1:Page_Width]
            PRINT #99: Italics_OFF$;

         ELSEIF Ex_Point = 0 OR Ex_Point > Page_Width THEN

            ! *****************************************************
            ! Print program instruction in letter quality bold mode
            ! *****************************************************
            PRINT #99: Boldface_ON$;
            PRINT #99: Print_Text$[1:Page_Width]
            PRINT #99: Boldface_OFF$;

         ELSEIF Ex_Point <= Page_Width THEN

            ! *****************************************************
            ! Print program instruction in letter quality bold mode
            ! *****************************************************
            LET CommentFlag = 1
            PRINT #99: Boldface_ON$;
            PRINT #99: Print_Text$[1:Ex_Point - 1];
            PRINT #99: Boldface_OFF$;

            ! *******************************************
            ! Print comment in letter quality italic mode
            ! *******************************************
            PRINT #99: Italics_ON$;
            PRINT #99: Print_Text$[Ex_Point:Page_Width]
            PRINT #99: Italics_OFF$;

         END IF
 
         ! *****************
         ! Update line count
         ! *****************
         LET Line_Number = Line_Number + 1
 
         ! ***************************************
         ! Delete what was printed from the string
         ! ***************************************
         LET Print_Text$[1:Page_Width] = ""
 
      ! ******************************************
      ! Repeat loop if there is more left to print
      ! ******************************************
      LOOP UNTIL Len(Print_Text$) = 0

   ! ******************************
   ! Repeat loop until all lines of
   ! the editor have been printed.
   ! ******************************
   NEXT I

   ! ******************
   ! Flip the last page
   ! ******************
   CALL New_Page(#99)

   ! *********************************
   ! Reset printer to its default mode
   ! *********************************
   PRINT #99: NLQ_OFF$;
   CALL InitializePrinter(#99)

   ! ***********************************
   ! Printer channel not needed any more
   ! ***********************************
   CLOSE #99

END SUB




! =========================================
! PURPOSE: To open a channel to the printer
! =========================================
SUB Open_Channel(#999)
   OPEN #999: PRINTER
   SET #999: MARGIN 160
END SUB




! =======================================================================
! PURPOSE: To print a header containing the desired name and a page
!          number.
!
! SCOPE: PUBLIC
!
! PARAMETERS:
!
!    INPUT:
!
!       #999 ......... Previously opened channel to the printer.
!       Title$ ....... Desired page title.
!       Line_Width ... Previously defined line width.
!       Page ......... Current page number
!       Top_Margin ... Previously defined top margin
!
! =======================================================================
SUB Print_Header(#999, Page_Width, Title$, Page, Top_Margin)

    DECLARE FUNCTION Date_Time$

    ! *************************
    ! Build time and date stamp
    ! *************************
    LET X$   = ""
    LET X$   = Date_Time$
    LET X$   = X$ & "Page "
    LET X$   = X$ & STR$(Page)
    LET XTAB = Page_Width - (LEN(X$) + LEN(Title$))

    PRINT #999: Title$;
    PRINT #999: Repeat$(" ",XTAB);
    PRINT #999: X$

    ! **************************************
    ! Print blank lines to make a top margin
    ! **************************************
    FOR Line = 1 to Top_Margin - 1
       PRINT #999:
    NEXT Line

END SUB




! =======================================================================
! PURPOSE: Return a string with date and time for use in header
! =======================================================================
FUNCTION Date_Time$

   LET Date_Time$ = ""
   LET datetime$ = Date$[5:6] & "/" & Date$[7:8] & "/" & Date$[3:4]
   LET datetime$ = Time$[1:5] & " - "  & datetime$ & "     "
   LET Date_Time$ = datetime$

END FUNCTION

