home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR16 / SPOOLAQ.ZIP / SPOOL2.PAS < prev    next >
Pascal/Delphi Source File  |  1993-06-12  |  18KB  |  561 lines

  1. {************************************************}
  2. {                                                }
  3. {   program SPOOL2                               }
  4. {   demo program based on TVGUID15.PAS           }
  5. {   (Copyright (c) 1990 by Borland International)}
  6. {                                                }
  7. {   this program uses the SPOOL-tpu to print     }
  8. {   in the background                            }
  9. {                                                }
  10. {   this is part of SPOOL.EXE                    }
  11. {************************************************}
  12. {$V-}
  13. {$X+}
  14.  
  15. program Spool2;
  16.  
  17. uses Objects, Drivers, Views, Menus, Dialogs, MsgBox, App, Spooler;
  18.  
  19. const
  20.   FileToRead        = 'SPOOL2.PAS';
  21.   MaxLines          = 100;
  22.   WinCount: Integer =   0;
  23.   cmFileOpen        = 100;
  24.   cmNewWin          = 101;
  25.   cmNewDialog       = 102;
  26.   cmFileToSpool     = 103;                       (* !!! added !!! *)
  27.  
  28. var
  29.   LineCount: Integer;
  30.   Lines: array[0..MaxLines - 1] of PString;
  31.  
  32. type
  33.   DialogData = record
  34.     CheckBoxData: Word;
  35.     RadioButtonData: Word;
  36.     InputLineData: string[128];
  37.   end;
  38.  
  39.   TMyApp = object(TApplication)
  40.     MySpooler : PSpooler;
  41.  
  42.     constructor Init;
  43.     destructor  Done;                         virtual;
  44.     procedure HandleEvent(var Event: TEvent); virtual;
  45.     procedure InitMenuBar;                    virtual;
  46.     procedure InitStatusLine;                 virtual;
  47.     procedure Idle;                           virtual;     (* !!! added !!! *)
  48.     procedure FileToSpool;                                 (* !!! added !!! *)
  49.     procedure NewDialog;
  50.     procedure NewWindow;
  51.   end;
  52.  
  53.   PInterior = ^TInterior;
  54.   TInterior = object(TScroller)
  55.     constructor Init(var Bounds: TRect; AHScrollBar, AVScrollBar: PScrollBar);
  56.     procedure Draw; virtual;
  57.   end;
  58.  
  59.   PDemoWindow = ^TDemoWindow;
  60.   TDemoWindow = object(TWindow)
  61.     RInterior, LInterior: PInterior;
  62.     constructor Init(Bounds: TRect; WinTitle: String; WindowNo: Word);
  63.     function MakeInterior(Bounds: TRect; Left: Boolean): PInterior;
  64.     procedure SizeLimits(var Min, Max: TPoint); virtual;
  65.   end;
  66.  
  67.   PDemoDialog = ^TDemoDialog;
  68.   TDemoDialog = object(TDialog)
  69.   end;
  70.  
  71. procedure ReadFile;
  72. var
  73.   F: Text;
  74.   S: String;
  75. begin
  76.   LineCount := 0;
  77.   Assign(F, FileToRead);
  78.   {$I-}
  79.   Reset(F);
  80.   {$I+}
  81.   if IOResult <> 0 then
  82.   begin
  83.     Writeln('Cannot open ', FileToRead);
  84.     Halt(1);
  85.   end;
  86.   while not Eof(F) and (LineCount < MaxLines) do
  87.   begin
  88.     Readln(F, S);
  89.     Lines[LineCount] := NewStr(S);
  90.     Inc(LineCount);
  91.   end;
  92.   Close(F);
  93. end;
  94.  
  95. procedure DoneFile;
  96. var
  97.   I: Integer;
  98. begin
  99.   for I := 0 to LineCount - 1 do
  100.     if Lines[I] <> nil then DisposeStr(Lines[i]);
  101. end;
  102.  
  103. { TInterior }
  104. constructor TInterior.Init(var Bounds: TRect; AHScrollBar,
  105.   AVScrollBar: PScrollBar);
  106. begin
  107.   TScroller.Init(Bounds, AHScrollBar, AVScrollBar);
  108.   Options := Options or ofFramed;
  109.   SetLimit(128, LineCount);
  110. end;
  111.  
  112. procedure TInterior.Draw;
  113. var
  114.   Color: Byte;
  115.   I, Y: Integer;
  116.   B: TDrawBuffer;
  117. begin
  118.   Color := GetColor(1);
  119.   for Y := 0 to Size.Y - 1 do
  120.   begin
  121.     MoveChar(B, ' ', Color, Size.X);
  122.     i := Delta.Y + Y;
  123.     if (I < LineCount) and (Lines[I] <> nil) then
  124.       MoveStr(B, Copy(Lines[I]^, Delta.X + 1, Size.X), Color);
  125.     WriteLine(0, Y, Size.X, 1, B);
  126.   end;
  127. end;
  128.  
  129. { TDemoWindow }
  130. constructor TDemoWindow.Init(Bounds: TRect; WinTitle: String; WindowNo: Word);
  131. var
  132.   S: string[3];
  133.   R: TRect;
  134. begin
  135.   Str(WindowNo, S);
  136.   TWindow.Init(Bounds, WinTitle + ' ' + S, wnNoNumber);
  137.   GetExtent(Bounds);
  138.   R.Assign(Bounds.A.X, Bounds.A.Y, Bounds.B.X div 2 + 1, Bounds.B.Y);
  139.   LInterior := MakeInterior(R, True);
  140.   LInterior^.GrowMode := gfGrowHiY;
  141.   Insert(Linterior);
  142.   R.Assign(Bounds.B.X div 2, Bounds.A.Y, Bounds.B.X, Bounds.B.Y);
  143.   RInterior := MakeInterior(R,False);
  144.   RInterior^.GrowMode := gfGrowHiX + gfGrowHiY;
  145.   Insert(RInterior);
  146. end;
  147.  
  148. function TDemoWindow.MakeInterior(Bounds: TRect; Left: Boolean): PInterior;
  149. var
  150.   HScrollBar, VScrollBar: PScrollBar;
  151.   R: TRect;
  152. begin
  153.   R.Assign(Bounds.B.X-1, Bounds.A.Y+1, Bounds.B.X, Bounds.B.Y-1);
  154.   VScrollBar := New(PScrollBar, Init(R));
  155.   VScrollBar^.Options := VScrollBar^.Options or ofPostProcess;
  156.   if Left then VScrollBar^.GrowMode := gfGrowHiY;
  157.   Insert(VScrollBar);
  158.   R.Assign(Bounds.A.X+2, Bounds.B.Y-1, Bounds.B.X-2, Bounds.B.Y);
  159.   HScrollBar := New(PScrollBar, Init(R));
  160.   HScrollBar^.Options := HScrollBar^.Options or ofPostProcess;
  161.   if Left then HScrollBar^.GrowMode := gfGrowHiY + gfGrowLoY;
  162.   Insert(HScrollBar);
  163.   Bounds.Grow(-1,-1);
  164.   MakeInterior := New(PInterior, Init(Bounds, HScrollBar, VScrollBar));
  165. end;
  166.  
  167. procedure TDemoWindow.SizeLimits(var Min, Max: TPoint);
  168. var R: TRect;
  169. begin
  170.   TWindow.SizeLimits(Min, Max);
  171.   Min.X := LInterior^.Size.X + 9;
  172. end;
  173.  
  174.  
  175. { TMyApp }
  176. constructor TMyApp.Init;
  177. var
  178.     Rectangle : TRect;
  179. begin
  180.     TApplication.Init;
  181.     MySpooler := nil;
  182. end;
  183.  
  184. destructor TMyApp.Done;
  185. begin
  186.     TApplication.Done;
  187.     if MySpooler<>nil then
  188.         Dispose( MySpooler, Done);
  189. end;
  190.  
  191. procedure TMyApp.HandleEvent(var Event: TEvent);
  192. begin
  193.   TApplication.HandleEvent(Event);
  194.   if Event.What = evCommand then
  195.   begin
  196.     case Event.Command of
  197.       cmNewWin:       NewWindow;
  198.       cmNewDialog:    NewDialog;
  199.       cmFileToSpool : FileToSpool;               (* !!! added !!! *)
  200.     else
  201.       Exit;
  202.     end;
  203.     ClearEvent(Event);
  204.   end;
  205. end;
  206.  
  207. procedure TMyApp.InitMenuBar;
  208. var R: TRect;
  209. begin
  210.   GetExtent(R);
  211.   R.B.Y := R.A.Y + 1;
  212.   MenuBar := New(PMenuBar, Init(R, NewMenu(
  213.     NewSubMenu('~F~ile', hcNoContext, NewMenu(
  214.       NewItem('~O~pen', 'F3', kbF3, cmFileOpen, hcNoContext,
  215.       NewItem('~N~ew', 'F4', kbF4, cmNewWin, hcNoContext,
  216.       NewLine(
  217. (*    added: the new entry in the menu: *)
  218.       NewItem('~S~tart Spooling', '', 0, cmFileToSpool, hcNoContext,
  219.       NewLine(
  220.       NewItem('E~x~it', 'Alt-X', kbAltX, cmQuit, hcNoContext,
  221.       nil))))))),
  222.     NewSubMenu('~W~indow', hcNoContext, NewMenu(
  223.       NewItem('~N~ext', 'F6', kbF6, cmNext, hcNoContext,
  224.       NewItem('~Z~oom', 'F5', kbF5, cmZoom, hcNoContext,
  225.       NewItem('~D~ialog', 'F2', kbF2, cmNewDialog, hcNoContext,
  226.       nil)))),
  227.     nil))
  228.   )));
  229. end;
  230.  
  231. procedure TMyApp.InitStatusLine;
  232. var R: TRect;
  233. begin
  234.   GetExtent(R);
  235.   R.A.Y := R.B.Y - 1;
  236.   StatusLine := New(PStatusLine, Init(R,
  237.     NewStatusDef(0, $FFFF,
  238.       NewStatusKey('', kbF10, cmMenu,
  239.       NewStatusKey('~Alt-X~ Exit', kbAltX, cmQuit,
  240.       NewStatusKey('~F4~ New', kbF4, cmNewWin,
  241.       NewStatusKey('~Alt-F3~ Close', kbAltF3, cmClose,
  242.       nil)))),
  243.     nil)
  244.   ));
  245. end;
  246.  
  247. (* ╔════════════════════════════════════════════════════════════════╗ *)
  248. (* ║                                                                ║ *)
  249. (* ║     added: output the data to the printer                      ║ *)
  250. (* ║                                                                ║ *)
  251. (* ╚════════════════════════════════════════════════════════════════╝ *)
  252. procedure TMyApp.Idle;
  253. begin
  254.     TApplication.Idle;
  255.     if MySpooler<>nil then
  256.         MySpooler^.SpoolOneChar;
  257. end; (* ------------------------------------------- Idle *)
  258.  
  259. (* ╔════════════════════════════════════════════════════════════════╗ *)
  260. (* ║                                                                ║ *)
  261. (* ║     added: which file to spool, which port to use etc.         ║ *)
  262. (* ║                                                                ║ *)
  263. (* ╚════════════════════════════════════════════════════════════════╝ *)
  264. procedure TMyApp.FileToSpool;
  265. type
  266.     SpoolDlgData = record
  267.         FileName    : string[64];
  268.         Port        : word;
  269.         HWAccess    : word;
  270.         Baud        : word;
  271.         Data        : word;
  272.         Stop        : word;
  273.         Parity      : word;
  274.         FlowControl : word;
  275.     end;
  276. var
  277.     Dialog        : PDialog;
  278.     FileName      : PInputLine;
  279.     Rectangle     : TRect;
  280.     Button        : PButton;
  281.     RadioButtons  : PRadioButtons;
  282.     Text          : PStaticText;
  283.     LabelText     : PLabel;
  284.     RetCode       : Word;
  285.     DialogData    : SpoolDlgData;
  286.     PortNo,
  287.     BaudRate,
  288.     DataBits,
  289.     StopBits,
  290.     Parity,
  291.     FlowControl   : word;
  292. begin
  293. (* ------------------------------------------------------------------ *)
  294. (*                                                                    *)
  295. (*       only 1 file allowed (demo only)                              *)
  296. (*                                                                    *)
  297. (* ------------------------------------------------------------------ *)
  298.     if MySpooler<>nil then
  299.     begin
  300.         MessageBox( 'Spooler could be created only once.',
  301.                     nil, mfInformation + mfOKButton );
  302.         exit;
  303.     end;
  304. (* ------------------------------------------------------------------ *)
  305. (*                                                                    *)
  306. (*       OK: not active yet: present dialog                           *)
  307. (*                                                                    *)
  308. (* ------------------------------------------------------------------ *)
  309.     Rectangle.Assign(2,1,77,20);
  310.     Dialog := New(PDialog,Init(Rectangle,'enter data for printing'));
  311.  
  312.     Rectangle.Assign(3,9,34,10);
  313.     Text := New(PStaticText,Init(Rectangle,'printer connected via COMx:'));
  314.     Dialog^.Insert(Text);
  315.  
  316.     Rectangle.Assign(3,3,27,4);
  317.     FileName := New(PInputLine,Init(Rectangle,64));
  318.     Rectangle.Assign(3,2,27,3);
  319.     LabelText := New(PLabel,Init(Rectangle,'name of file to print:',FileName));
  320.     Dialog^.Insert(LabelText);
  321.     Dialog^.Insert(FileName);
  322.  
  323.     Rectangle.Assign(33,2,45,8);
  324.     RadioButtons:= New(PRadioButtons,Init(Rectangle,
  325.                        NewSItem('LPT 1',
  326.                        NewSItem('LPT 2',
  327.                        NewSItem('COM 1',
  328.                        NewSItem('COM 2',
  329.                        NewSItem('COM 3',
  330.                        NewSItem('COM 4',nil))))))));
  331.     Rectangle.Assign(31,1,47,2);
  332.     LabelText := New(PLabel,Init(Rectangle,'outputting via:',RadioButtons));
  333.     Dialog^.Insert(LabelText);
  334.     Dialog^.Insert(RadioButtons);
  335.  
  336.     Rectangle.Assign(50,3,67,5);
  337.     RadioButtons:= New(PRadioButtons,Init(Rectangle,
  338.                        NewSItem('Yes',
  339.                        NewSItem('No: use DOS',nil))));
  340.     Rectangle.Assign(50,2,67,3);
  341.     LabelText := New(PLabel,Init(Rectangle,'hardware-access:',RadioButtons));
  342.     Dialog^.Insert(LabelText);
  343.     Dialog^.Insert(RadioButtons);
  344.  
  345.     Rectangle.Assign(13,10,73,11);
  346.     RadioButtons:= New(PRadioButtons,Init(Rectangle,
  347.                        NewSItem(' 300',
  348.                        NewSItem(' 600',
  349.                        NewSItem('1200',
  350.                        NewSItem('2400',
  351.                        NewSItem('4800',
  352.                        NewSItem('9600',nil))))))));
  353.     Rectangle.Assign(2,10,13,11);
  354.     LabelText := New(PLabel,Init(Rectangle,'baud:',RadioButtons));
  355.     Dialog^.Insert(LabelText);
  356.     Dialog^.Insert(RadioButtons);
  357.  
  358.     Rectangle.Assign(19,11,45,12);
  359.     RadioButtons:= New(PRadioButtons,Init(Rectangle,
  360.                        NewSItem('8 bits  ',
  361.                        NewSItem('7 bits',nil))));
  362.     Rectangle.Assign(2,11,9,12);
  363.     LabelText := New(PLabel,Init(Rectangle,'data:',RadioButtons));
  364.     Dialog^.Insert(LabelText);
  365.     Dialog^.Insert(RadioButtons);
  366.  
  367.     Rectangle.Assign(19,12,45,13);
  368.     RadioButtons:= New(PRadioButtons,Init(Rectangle,
  369.                        NewSItem('1 bit   ',
  370.                        NewSItem('2 bits',nil))));
  371.     Rectangle.Assign(2,12,8,13);
  372.     LabelText := New(PLabel,Init(Rectangle,'stop:',RadioButtons));
  373.     Dialog^.Insert(LabelText);
  374.     Dialog^.Insert(RadioButtons);
  375.  
  376.     Rectangle.Assign(19,13,43,14);
  377.     RadioButtons:= New(PRadioButtons,Init(Rectangle,
  378.                        NewSItem('odd     ',
  379.                        NewSItem('even',nil))));
  380.     Rectangle.Assign(2,13,10,14);
  381.     LabelText := New(PLabel,Init(Rectangle,'parity:',RadioButtons));
  382.     Dialog^.Insert(LabelText);
  383.     Dialog^.Insert(RadioButtons);
  384.  
  385.     Rectangle.Assign(19,14,42,15);
  386.     RadioButtons:= New(PRadioButtons,Init(Rectangle,
  387.                        NewSItem('XON/XOFF',
  388.                        NewSItem('DTR',nil))));
  389.     Rectangle.Assign(2,14,16,15);
  390.     LabelText := New(PLabel,Init(Rectangle,'flow control:',RadioButtons));
  391.     Dialog^.Insert(LabelText);
  392.     Dialog^.Insert(RadioButtons);
  393.  
  394.     Rectangle.Assign(13,16,21,18);
  395.     Button := New(PButton,Init(Rectangle,'O~K~',cmOK,bfDefault));
  396.     Button^.AmDefault := True;
  397.     Dialog^.Insert(Button);
  398.  
  399.     Rectangle.Assign(48,16,60,18);
  400.     Button := New(PButton,Init(Rectangle,'Cancel',cmCancel,bfNormal));
  401.     Dialog^.Insert(Button);
  402.  
  403.     Dialog^.SelectNext(False);
  404. (*                                                                    *)
  405. (*       set the default-values for the dialog                        *)
  406. (*                                                                    *)
  407.     with DialogData do
  408.     begin
  409.         FileName    := '' ;
  410.         Port        := 0;                        (* = LPT 1    *)
  411.         HWAccess    := 0;                        (* = Yes      *)
  412.         Baud        := 5;                        (* = 9600     *)
  413.         Data        := 0;                        (* = 8 bits   *)
  414.         Stop        := 0;                        (* = 1 bit    *)
  415.         Parity      := 0;                        (* = odd      *)
  416.         FlowControl := 0;                        (* = XON/XOFF *)
  417.     end;
  418.     Dialog^.SetData(DialogData);
  419.     RetCode := DeskTop^.ExecView(Dialog);
  420.     if RetCode=cmOK then
  421.         Dialog^.GetData(DialogData);
  422.  
  423.     Dispose(Dialog,Done);
  424. (* ------------------------------------------------------------------ *)
  425. (*                                                                    *)
  426. (*       create Spooler-object                                        *)
  427. (*                                                                    *)
  428. (* ------------------------------------------------------------------ *)
  429.     if RetCode=cmOK then
  430.     begin
  431.         GetExtent( Rectangle );
  432.         with Rectangle do
  433.         begin
  434.             a.x := b.x - 9;            (* we want it to appear in the *)
  435.             b.x := b.x - 1;            (* upper right corner, in the  *) 
  436.             b.y := a.y + 1;            (* right position of the menu  *)
  437.         end;
  438.         MySpooler := new( PSPooler, Init(Rectangle, DialogData.FileName) );
  439.         insert( MySpooler );
  440.         if DialogData.Port<2 then
  441.         begin
  442. (*  following the code for outputting the data via LPT1               *)
  443.             case DialogData.Port of
  444.                0  :  MySpooler^.SetParallel( Spool_LPT1 );
  445.                1  :  MySpooler^.SetParallel( Spool_LPT2 );
  446.             end;
  447.         end
  448.         else
  449.         begin
  450. (*  if COM1 - COM4 is selected we must know more:                     *)
  451.             case DialogData.Port of
  452.                2  :  PortNo := Spool_COM1;
  453.                3  :  PortNo := Spool_COM2;
  454.                4  :  PortNo := Spool_COM3;
  455.                5  :  PortNo := Spool_COM4;
  456.             end;
  457.             case DialogData.Baud of
  458.                0  :  BaudRate := Spool_300;
  459.                1  :  BaudRate := Spool_600;
  460.                2  :  BaudRate := Spool_1200;
  461.                3  :  BaudRate := Spool_2400;
  462.                4  :  BaudRate := Spool_4800;
  463.                5  :  BaudRate := Spool_9600;
  464.             end;
  465.             case DialogData.Data of
  466.                0  : DataBits := Spool_8Bit;
  467.                1  : DataBits := Spool_7Bit;
  468.             end;
  469.             case DialogData.Stop of
  470.                0  :  StopBits := Spool_1Stop;
  471.                1  :  StopBits := Spool_2Stop;
  472.             end;
  473.             case DialogData.Parity of
  474.                0  :  Parity := Spool_Odd;
  475.                1  :  Parity := Spool_Even;
  476.             end;
  477.             case DialogData.FlowControl of
  478.                0  :  FlowControl := Spool_FlowXONXOFF;
  479.                1  :  FlowControl := Spool_FlowDTR;
  480.             end;
  481. (*          switch spooler to serial                                  *)
  482.             MySpooler^.SetSerial( PortNo, BaudRate, DataBits, StopBits,
  483.                                   Parity, FlowControl);
  484.         end;
  485. (*      DOS or direct HW-access:                                      *)
  486.         MySpooler^.UseDos(DialogData.HWAccess=1);
  487. (*      let's go:                                                     *)
  488.         MySpooler^.StartSpooling;
  489.     end;
  490. end; (* ---------------------------------------------- FileToSpool *)
  491.  
  492.  
  493. procedure TMyApp.NewDialog;
  494. var
  495.   Bruce: PView;
  496.   Dialog: PDemoDialog;
  497.   R: TRect;
  498.   C: Word;
  499. begin
  500.   R.Assign(20, 6, 60, 19);
  501.   Dialog := New(PDemoDialog, Init(R, 'Demo Dialog'));
  502.   with Dialog^ do
  503.   begin
  504.     R.Assign(3, 3, 18, 6);
  505.     Bruce := New(PCheckBoxes, Init(R,
  506.       NewSItem('~H~varti',
  507.       NewSItem('~T~ilset',
  508.       NewSItem('~J~arlsberg',
  509.       nil)))
  510.     ));
  511.     Insert(Bruce);
  512.     R.Assign(2, 2, 10, 3);
  513.     Insert(New(PLabel, Init(R, 'Cheeses', Bruce)));
  514.     R.Assign(22, 3, 34, 6);
  515.     Bruce := New(PRadioButtons, Init(R,
  516.       NewSItem('~S~olid',
  517.       NewSItem('~R~unny',
  518.       NewSItem('~M~elted',
  519.       nil)))
  520.     ));
  521.     Insert(Bruce);
  522.     R.Assign(21, 2, 33, 3);
  523.     Insert(New(PLabel, Init(R, 'Consistency', Bruce)));
  524.     R.Assign(3, 8, 37, 9);
  525.     Bruce := New(PInputLine, Init(R, 128));
  526.     Insert(Bruce);
  527.     R.Assign(2, 7, 24, 8);
  528.     Insert(New(PLabel, Init(R, 'Delivery instructions', Bruce)));
  529.     R.Assign(15, 10, 25, 12);
  530.     Insert(New(PButton, Init(R, '~O~k', cmOK, bfDefault)));
  531.     R.Assign(28, 10, 38, 12);
  532.     Insert(New(PButton, Init(R, 'Cancel', cmCancel, bfNormal)));
  533.     SelectNext(False);
  534.   end;
  535.   C := DeskTop^.ExecView(Dialog);
  536.   Dispose(Dialog, Done);
  537. end;
  538.  
  539. procedure TMyApp.NewWindow;
  540. var
  541.   Window: PDemoWindow;
  542.   R: TRect;
  543. begin
  544.   Inc(WinCount);
  545.   R.Assign(0, 0, 45, 13);
  546.   R.Move(Random(34), Random(11));
  547.   Window := New(PDemoWindow, Init(R, 'Demo Window', WinCount));
  548.   DeskTop^.Insert(Window);
  549. end;
  550.  
  551. var
  552.   MyApp: TMyApp;
  553.  
  554. begin
  555.   ReadFile;
  556.   MyApp.Init;
  557.   MyApp.Run;
  558.   MyApp.Done;
  559.   DoneFile;
  560. end.
  561.