home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / games / 144 / pascal / print.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-06-16  |  12.5 KB  |  387 lines

  1.   {$S0,D-}
  2.  
  3. Program Print ;
  4.  
  5.   Const
  6.     maxlines       = 5 ;
  7.     AC_Open        = 40 ;
  8.     BEG_Mctrl      = 3 ;
  9.     END_Mctrl      = 2 ;
  10.     {$I Gemconst.pas}
  11.  
  12.   Type
  13.     {$I Gemtype.pas}
  14.     Pointer            = ^Char ;
  15.     Int_In_Parms       = ARRAY[ 0..15 ] OF Integer ;
  16.     Int_Out_Parms      = ARRAY[ 0..45 ] OF Integer ;
  17.     Addr_In_Parms      = ARRAY[ 0..1 ] OF Pointer ;
  18.     Addr_Out_Parms     = ARRAY[ 0..0 ] OF Pointer ;
  19.  
  20.   VAR
  21.     working : String[249] ;
  22.     defpath,
  23.     inpath,
  24.     linestr,
  25.     test : string ;
  26.     char_wide,
  27.     char_height,
  28.     bch,
  29.     bcw,
  30.     ap_id,
  31.     menu_id,
  32.     pagecount,
  33.     linecount,
  34.     counter,
  35.     title_1,
  36.     prompt_1,
  37.     prompt_2,
  38.     prompt_3,
  39.     window,
  40.     cancel_btn,
  41.     drive,
  42.     rez,
  43.     choice : Integer ;
  44.     program_name : Str255 ;
  45.     Stop_PRINT : Boolean ;
  46.     msg  : Message_Buffer ;
  47.     print_dialog : Dialog_Ptr ;
  48.  
  49.  
  50.   {$I Gemsubs}
  51.  
  52.  
  53.   PROCEDURE IO_Check( b : Boolean ) ;
  54.     EXTERNAL ;
  55.  
  56.  
  57.   FUNCTION IO_Result : Integer ;
  58.     EXTERNAL ;
  59.  
  60.  
  61.   FUNCTION CurDrv : Integer ;
  62.     GEMDOS( $19 ) ;
  63.  
  64.  
  65.   FUNCTION GetRez : Integer ;
  66.     XBIOS ( 4 ) ;
  67.  
  68.   PROCEDURE Obj_Draw ( BOX : Dialog_Ptr ;
  69.                        Item : Tree_Index ;
  70.                        DEPTH, X, Y, W, H : Integer ) ;
  71.     EXTERNAL ;
  72.  
  73.   PROCEDURE AES_Call( op : Integer;
  74.                 VAR int_in : Int_In_Parms; VAR int_out : Int_Out_Parms ;
  75.                 VAR addr_in : Addr_In_Parms ; VAR addr_out : Addr_Out_Parms ) ;
  76.     EXTERNAL ;
  77.  
  78.   { This is a hook into the AES call "WIND_Update" that will allow us to temp-}
  79.   { porarily stop the Screen Manager while processing our own dialog box.}
  80.  
  81.  
  82.   PROCEDURE WIND_Update ( ctrl : Integer ) ;
  83.   VAR
  84.     int_in   : Int_In_Parms ;
  85.     int_out  : Int_Out_Parms ;
  86.     addr_in  : Addr_In_Parms ;
  87.     addr_out : Addr_Out_Parms ;
  88.   BEGIN
  89.     int_in[0] := ctrl ;
  90.     AES_Call( 107, int_in, int_out, addr_in, addr_out ) ;
  91.   END ;
  92.  
  93.  
  94.   { This procedure is where the accessory waits for a mesaage to activate }
  95.   { and start to print a file. }
  96.  
  97.   PROCEDURE Event_Loop ;
  98.   VAR event, dummy : Integer ;
  99.       again : Boolean ;
  100.   BEGIN
  101.     again := FALSE ;
  102.     REPEAT
  103.       event := Get_Event( E_Message,0,0,0,0,FALSE,0,0,0,0,
  104.                           FALSE,0,0,0,0,msg,
  105.                           dummy,dummy,dummy,dummy,dummy,dummy ) ;
  106.   { Open up only if "OPEN" message has been received, and the proper menu }
  107.   { identification number is given! }
  108.       IF (msg[0] = AC_Open) AND (msg[4] = menu_id) THEN again := TRUE ;
  109.     UNTIL again ;
  110.   END ;
  111.  
  112.  
  113.   { This procedure converts an INTEGER number into a string }
  114.  
  115.   PROCEDURE Convert( number : Integer ; VAR tempstr : string ) ;
  116.     VAR
  117.       temp : string ;
  118.       tempnum,
  119.       count1,
  120.       count2,
  121.       divideby : Integer ;
  122.       first : Boolean ;
  123.  
  124.     PROCEDURE Num( whatnum : Integer ; VAR str : string ) ;
  125.       CONST
  126.         numbers = '123456789' ;
  127.       BEGIN
  128.         IF whatnum = 0 THEN
  129.           str := '0'
  130.         ELSE
  131.           str := Copy( numbers, whatnum, 1) ;
  132.       END;
  133.  
  134.     BEGIN
  135.       tempstr := '' ;
  136.       first := true ;
  137.       FOR count1 := maxlines DOWNTO 1 DO BEGIN
  138.         divideby := 1 ;
  139.         FOR count2 := 1 TO count1 DO
  140.           divideby := divideby*10 ;
  141.         tempnum := number div divideby ;
  142.         number := number mod divideby ;
  143.         Num( tempnum, temp ) ;
  144.         IF tempnum>0 THEN
  145.           first := false ;
  146.         IF NOT first THEN
  147.           tempstr := Concat( tempstr, temp ) ;
  148.       END ;
  149.       Num( number, temp ) ;
  150.       tempstr := Concat( tempstr, temp ) ;
  151.     END ;
  152.  
  153.  
  154.   { This function asks whether you want to stop the printing.... If so, it }
  155.   { returns TRUE to the asking procedure. }
  156.  
  157.   FUNCTION AskStop : Boolean ;
  158.   VAR choice : Integer ;
  159.       str : Str255 ;
  160.   BEGIN
  161.     str := '[2][ |Do you wish to STOP printing?][ Yes | No ]' ;
  162.     choice := Do_Alert( str,2 ) ;
  163.     IF choice = 1 THEN AskStop := TRUE
  164.     ELSE AskStop := FALSE ;
  165.   END ;
  166.  
  167.  
  168.   { This procedure prints one line on the printer.  It also then loops back }
  169.   { to GEM to see if either the UNDO key has been pressed, or whether the }
  170.   { left mouse button has been pressed over the "CANCEL" box. If either these }
  171.   { conditions have been met, it then asks you if you want to terminate the }
  172.   { printing. }
  173.  
  174.   PROCEDURE Println( str : Str255 ) ;
  175.   VAR event,
  176.       what_key,
  177.       bcnt,
  178.       bstate,
  179.       mx,
  180.       my,
  181.       kbd : Integer ;
  182.   BEGIN
  183.     event := Get_Event( E_Keyboard|E_Timer|E_Button,
  184.                         1, 1, 1, 0,
  185.                         FALSE, 0, 0, 0, 0,
  186.                         FALSE, 0, 0, 0, 0,
  187.                         msg, what_key, bcnt,
  188.                         bstate, mx, my, kbd ) ;
  189.     IF (event & E_Keyboard <> 0 ) THEN
  190.       BEGIN
  191.         IF (NOT Stop_PRINT) AND ((what_key = $6100) OR (what_key = $1C0D)) THEN
  192.           Stop_PRINT := AskStop ;
  193.       END ;
  194.     IF (event & E_Button <> 0) AND (bcnt>0) AND
  195.             (mx > (35*char_wide)) AND
  196.             (mx < (45*char_wide)) AND
  197.             (my > (16*char_height + char_height DIV 2)) AND
  198.             (my < (18*char_height + char_height DIV 2)) AND
  199.             (NOT Stop_PRINT) THEN Stop_PRINT := AskStop ;
  200.     IF (NOT Stop_PRINT) THEN
  201.       BEGIN
  202.         IF Length( str ) = 80 THEN Write( str )
  203.         ELSE Writeln( str ) ;
  204.       END ;
  205.   END ;
  206.  
  207.  
  208.   { This procedure writes a passed string (numbers is this program) on the }
  209.   { screen in the interactive dialog box. Note that the mouse is hide as the}
  210.   { string is printed. }
  211.  
  212.   PROCEDURE ListMessage( str : Str255 ; pos : Integer ) ;
  213.   VAR len,
  214.       c : Integer ;
  215.   BEGIN
  216.     len := Length(str) ;
  217.     IF len < 14 THEN FOR c := 1 TO 14-len DO str := Concat( str, ' ' ) ;
  218.     Hide_Mouse ;
  219.     Draw_String( 40*char_wide, (11 + pos)*char_height + char_height DIV 3 + 1,
  220.                  str ) ;
  221.     Show_Mouse ;
  222.   END ;
  223.  
  224.  
  225.   { This procedure prints the page header on the top of each new page. }
  226.  
  227.   PROCEDURE Header ;
  228.   VAR
  229.     temp1,
  230.     temp2 : String ;
  231.     counter : Integer ;
  232.   BEGIN
  233.     temp1 := inpath ;
  234.     Convert( pagecount, temp2 ) ;
  235.     ListMessage( temp2, 4 ) ;
  236.     For counter := 74-Length(temp2) DOWNTO Length(temp1) DO
  237.       temp1 := Concat(temp1,' ') ;
  238.     Insert( 'Page ', temp1, 74-Length(temp2) ) ;
  239.     Insert( temp2, temp1, 79-Length(temp2) ) ;
  240.     Println( temp1 ) ;
  241.     Println( '' ) ;
  242.     Println( '' ) ;
  243.   END ;
  244.  
  245.  
  246.   { This procedure sets up the items needed for the interactive dialog box }
  247.   { to be drawn. }
  248.  
  249.   PROCEDURE Setup_Dialog ;
  250.   BEGIN
  251.     print_dialog := New_Dialog( 10, 0, 0, 32, 13 ) ;
  252.     title_1 := Add_DItem( print_dialog, G_String, None, 5, 1,
  253.                22, 1, 0, $1180 ) ;
  254.     prompt_1 := Add_DItem( print_dialog, G_String, None, 3, 4,
  255.                30, 1, 0, $1180 ) ;
  256.     prompt_2 := Add_DItem( print_dialog, G_String, None, 3, 6,
  257.                15, 1, 0, $1180 ) ;
  258.     prompt_3 := Add_DItem( print_dialog, G_String, None, 3, 8,
  259.                15, 1, 0, $1180 ) ;
  260.     cancel_btn := Add_DItem( print_dialog, G_BoxText,
  261.                Selectable|Default|Exit_Btn, 11, 10, 10, 2, 2, $1180 ) ;
  262.   END ;
  263.  
  264.  
  265.   { This procedure finds the file name in the path to the file to be printed }
  266.   { and concatenates it the the passed string. }
  267.  
  268.   PROCEDURE Add_Path (VAR str : Str255 ) ;
  269.   VAR len,
  270.       x : Integer ;
  271.   BEGIN
  272.     len := Length( inpath ) ;
  273.     LOOP
  274.       EXIT IF (inpath[ len ] = '\') OR (len = 1) ;
  275.       len := len - 1 ;
  276.     END ;
  277.     str := '  File Name: ' ;
  278.     FOR x := (len + 1) TO Length( inpath )  DO
  279.         str := Concat( str, inpath[ x ] ) ;
  280.   END ;
  281.  
  282.  
  283.   { This procedure first attempts to open up a window the full size fo the }
  284.   { screen. This is necessary to prevent GEM from misdirecting button }
  285.   { presses for the interactive dialog box to the windows beneath the box. }
  286.   { Whether the window is opened successfully or not, the dialog box is then }
  287.   { drawn on the screen. }
  288.  
  289.   PROCEDURE ShowProgress ;
  290.   VAR str : Str255 ;
  291.   BEGIN
  292.     Set_DText( print_dialog, title_1,
  293.                'Currently PRINTING File', System_Font, TE_Center ) ;
  294.     Add_Path ( str ) ;
  295.     Set_DText( print_dialog, prompt_1, str, System_Font, TE_Right ) ;
  296.     Set_DText( print_dialog, prompt_2,
  297.                ' Line Count:', System_Font, TE_Right ) ;
  298.     Set_DText( print_dialog, prompt_3,
  299.                'Page Number:', System_Font, TE_Right ) ;
  300.     Set_DText( print_dialog, cancel_btn, 'CANCEL',
  301.                System_Font, TE_Center ) ;
  302.     Obj_SetState( print_dialog, cancel_btn, Normal, FALSE ) ;
  303.     Text_Color( Black ) ;
  304.     Center_Dialog( print_dialog ) ;
  305.     Obj_Draw( print_dialog, 0, 1, 0, 0, 80*char_wide, 24*char_height ) ;
  306.   END ;
  307.  
  308.  
  309.   { This is the main program. }
  310.  
  311.   BEGIN
  312.     program_name := '  File Printer' ;
  313.     ap_id := Init_Gem ;  { Initialize GEM and register our accessoary }
  314.     menu_id := Menu_Register( ap_id, program_name ) ;
  315.     IF (ap_id >= 0) AND (menu_id >=0) THEN
  316.       BEGIN { Get the current screen characteristics for positioning later }
  317.         sys_Font_Size( char_wide, char_height, bcw, bch ) ;
  318.         rez := GetRez ;
  319.         IF rez = 0 THEN char_wide := char_wide DIV 2 ;
  320.         WHILE TRUE DO
  321.           BEGIN
  322.             Event_Loop ; { Loop until called }
  323.             pagecount := 1 ;  { Initialize our page/line counts for printing }
  324.             linecount := 1 ;
  325.             choice := 1 ;
  326.             drive := CurDrv ; { Find the current drive; If "A" or "B" prompt }
  327.             IF drive < 2 THEN { the user to insert a diskette }
  328.             choice := Do_Alert('[3][ | |Insert Source Disk][ OK | Cancel ]', 1)
  329.             ELSE choice := 1 ;
  330.             IF choice = 1 THEN BEGIN
  331.               defpath := 'A:\*.*' ;
  332.               defpath[1] := Chr( Ord(defpath[1]) + drive ) ;
  333.             IF Get_In_File( defpath, inpath ) THEN BEGIN { Get the file path }
  334.               test := Copy( inpath, Length(inpath), 1 ) ; { to print }
  335.               IF test<>'\' THEN BEGIN
  336.                 IO_check( FALSE ) ; { Find out whether line numbers are to be }
  337.                 choice := Do_Alert  { added, and give one more way to stop prg}
  338.                   ('[2][ |Do you want line numbers?][No|Yes|Cancel]',1);
  339.                 Reset( Input, inpath) ;
  340.                 IF IO_Result <>0 THEN choice := 3 ; { If there is ann error }
  341.                 IF choice<3 THEN BEGIN   { open, boomb out. }
  342.                   WIND_Update( BEG_Mctrl ) ; { Stop the screen manager }
  343.                   Setup_Dialog ;
  344.                   Stop_PRINT := FALSE ;
  345.                   ShowProgress ; { Initialize the interactive dialog box }
  346.                   ListMessage( '1', 2 ) ;
  347.                   ListMessage( '1', 4 ) ;
  348.                   Rewrite( Output, 'PRN:' ) ; { Open the printer for output }
  349.                   Header ;         { Print the initial header }
  350.                   counter := 1 ;
  351.                   REPEAT
  352.                     Readln( working ) ;
  353.                     IF IO_Result <> 0 THEN Stop_PRINT := TRUE ;
  354.                     IF (NOT Stop_PRINT) THEN BEGIN
  355.                       Convert( linecount, linestr ) ;{Now loop, printing each}
  356.                       ListMessage( linestr, 2 ) ;{line, then reading the next}
  357.                       IF ( choice=2 ) THEN BEGIN {until done, or stop message}
  358.                         While Length(linestr)<5 DO { received. }
  359.                           linestr := Concat( linestr, ' ' ) ;
  360.                         working := Concat( linestr, ' ', working ) ;
  361.                       END ;
  362.                       Println( working ) ;
  363.                       linecount := linecount + 1 ;
  364.                       counter := counter + (Length(working) DIV 81) + 1 ;
  365.                       IF counter>54 THEN BEGIN { Allow 54 lines per page only }
  366.                         pagecount := pagecount+1 ;
  367.                         Println( Chr(12) ) ;
  368.                         Header ;
  369.                         counter := 1 ;
  370.                       END ;
  371.                     END ;
  372.                   UNTIL EOF OR Stop_PRINT ;
  373.                   Writeln( Chr(12) ) ; { End printing with a Form Feed }
  374.                   Close( Output ) ;
  375.                   Close( Input ) ;
  376.                   End_Dialog( print_dialog ) ;
  377.                   Delete_Dialog( print_dialog ) ;
  378.                   WIND_Update( END_Mctrl ) ; { Restart the Screen Manager }
  379.                 END ;
  380.               END ;
  381.             END ;
  382.           END ;
  383.       END ;
  384.     END ;
  385.     Exit_GEM ; { Exit gem only if we can not register our accessory. }
  386.   END .
  387.