home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1989 / 04 / extra / grinout.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-01-25  |  7.8 KB  |  340 lines

  1. (* ---------------------------------------------*)
  2. (*               GRINOUT.PAS                    *)
  3. (* Unit zur Verwendung der Standardein- und     *)
  4. (* Ausgabe prozeduren auf dem Graphikschirm     *)
  5. (* unter Turbo Pascal 4.0/5.0                   *)
  6. (*      (C) 1989 F. Prattes & TOOLBOX           *)
  7. (* -------------------------------------------- *)
  8.  
  9. UNIT GrInOut;
  10. INTERFACE
  11. USES
  12.   Graph, Dos, Crt;
  13.  
  14. VAR
  15.   graphmode,
  16.   graphdriver,
  17.   errorcode   : INTEGER;
  18.  
  19. PROCEDURE InitGraphic;
  20. (* Initialisiert Graphiksystem *)
  21. (* und Textgeräte-Treiber      *)
  22.  
  23. PROCEDURE EnterGraphic;
  24. (* Wiedereintritt in das Graphiksystem nach dem *)
  25. (* Ausstieg mit LeaveGraphic                    *)
  26.  
  27. PROCEDURE AssignGr(VAR f:TEXT);
  28. (* Zuweisen der Textgeräte-Treiberroutinen an   *)
  29. (* die Dateivariable f                          *)
  30.  
  31. PROCEDURE LeaveGraphic;
  32. (* Ausstieg aus dem Graphiksystem, aktiviert    *)
  33. (* wieder die Standardein- und ausgabeprozeduren*)
  34. (*  der Unit Crt                               *)
  35.  
  36. FUNCTION GetMaxPosX : INTEGER;
  37. (* Liefert die maximale Zeichenanzahl in einer  *)
  38. (* Zeile beim gegenwärtig gesetzten Zeichensatz *)
  39.  
  40. FUNCTION GetMaxPosY : INTEGER;
  41. (* Liefert die maximale Anzahl von Zeilen beim  *)
  42. (* gegenwärtig gesetzten Zeichensatz            *)
  43.  
  44. PROCEDURE ClrEol;
  45. (* Löschen bis zum Ende der Zeile               *)
  46.  
  47. PROCEDURE GotoXY(x,y:INTEGER);
  48. (* Setzen der Zeichenausgabeposition auf X, Y   *)
  49.  
  50. FUNCTION WhereX : INTEGER;
  51. (* Liefert den gegenwärtigen X-Wert *)
  52. (* bei der Textausgabe              *)
  53.  
  54. FUNCTION WhereY : INTEGER;
  55. (* Liefert den gegenwärtigen Y-Wert *)
  56. (* bei der Textausgabe              *)
  57.  
  58.  
  59. IMPLEMENTATION
  60.  
  61. VAR
  62.   ingraphmode :BOOLEAN;
  63.   posxtext,
  64.   posytext,
  65.   maxposx,
  66.   maxposy     : INTEGER;
  67.   oldexitproc : POINTER;
  68.  
  69. FUNCTION GetMaxPosX : INTEGER;
  70. BEGIN
  71.   IF ingraphmode THEN
  72.     GetMaxPosX := GetMaxX DIV TextWidth('m')
  73.   ELSE GetMaxPosX := 80
  74. END; (* GetMaxPosX *)
  75.  
  76. FUNCTION GetMaxPosY : INTEGER;
  77. BEGIN
  78.   IF ingraphmode THEN
  79.     (*das ist die Höhe e. Zeichens + Unterlänge*)
  80.     GetMaxPosY := GetMaxY DIV (TextHeight('M') +
  81.                   TextHeight('M') DIV 4)
  82.   ELSE GetMaxPosY := 25
  83. END; (* GetMaxPosY *)
  84.  
  85. PROCEDURE DrawChar(c : CHAR);
  86. (* Ausgeben des Zeichens c an der *)
  87. (* aktuellen Textposition         *)
  88. VAR
  89.   maxposx, maxposy,
  90.   th, tw, offsx    :INTEGER;
  91.  
  92.   PROCEDURE DrawSpace;
  93.   VAR
  94.     i, xbegin, ybegin : INTEGER;
  95.     tmpcolor          : WORD;
  96.  
  97.   BEGIN (* SPACE 'zeichnen' *)
  98.    tmpcolor := GetColor;
  99.    SetColor(GetBkColor);
  100.    xbegin := (posxtext-1)*tw;
  101.    ybegin := (posytext-1)*th
  102.               + TextHeight('M') DIV 4;
  103.    FOR i:=0 TO TextWidth('m') - 1 DO
  104.      Line(xbegin+i,ybegin,xbegin+i,
  105.           ybegin + th + 1);
  106.    SetColor(tmpcolor)
  107.   END; (* DrawSpace *)
  108.  
  109. BEGIN (* DrawChar *)
  110.   maxposx := GetMaxPosX; tw := TextWidth('m');
  111.   maxposy := GetMaxPosY; th := TextHeight('M');
  112.   th := th + th DIV 4;
  113.   (* Das Zeichen wird zentriert ausgegeben, *)
  114.   (* deshalb die Berechnung des Offsets     *)
  115.   offsx := tw DIV 2 - TextWidth(c) DIV 2;
  116.   CASE c OF
  117.   (* BS *)
  118.     #8 : IF posxtext > 1 THEN
  119.            Dec(posxtext)
  120.          ELSE IF posytext > 1 THEN
  121.           BEGIN
  122.            Dec(posytext);
  123.            posxtext := GetMaxPosX
  124.           END;
  125.    (* LF *)
  126.     #10: IF posytext < maxposy THEN Inc(posytext);
  127.    (* CR *)
  128.     #13: posxtext := 1;
  129.     ELSE
  130.      BEGIN
  131.       IF c=#32 THEN DrawSpace
  132.       ELSE
  133.         OutTextXY((posxtext-1)*tw + offsx,
  134.                        (posytext-1)*th + 1, c);
  135.       IF posxtext < maxposx THEN Inc(posxtext)
  136.       ELSE
  137.        BEGIN posxtext := 1;
  138.         IF posytext < maxposy THEN Inc(posytext)
  139.        END
  140.      END
  141.   END (* CASE *)
  142. END; (* DrawChar *)
  143.  
  144. (*$F+*)
  145. (* -------- Textgeräte-Treiberroutinen -------- *)
  146.  
  147. FUNCTION GrInput(VAR f:TextRec):INTEGER;
  148. (* Eingaberoutine, füllt den Puffer bufptr^ mit *)
  149. (* den eingegebenen Zeichen                     *)
  150. VAR
  151.   p     :INTEGER;
  152.   empty :BOOLEAN;
  153.   ch    :CHAR;
  154.  
  155. BEGIN
  156.   WITH f DO
  157.    BEGIN
  158.     p := 0; empty := FALSE;
  159.     WHILE NOT empty DO
  160.      BEGIN
  161.       ch := ReadKey;
  162.       IF ch = #8 THEN
  163.        BEGIN
  164.         IF p > 0 THEN
  165.          BEGIN
  166.           DrawChar(#8); DrawChar(#32);
  167.           DrawChar(#8);
  168.           Dec(p)
  169.          END
  170.        END
  171.       ELSE
  172.        BEGIN
  173.         empty := (ch = #13);
  174.         IF NOT empty AND (p < BufSize - 1) THEN
  175.          BEGIN
  176.            BufPtr^[p] := ch;
  177.            Inc(p);
  178.            DrawChar(ch)
  179.          END;
  180.        END
  181.      END; (* WHILE *)
  182.     BufPtr^[p] := #13; Inc(p);
  183.     BufPtr^[p] := #10; Inc(p);
  184.     DrawChar(#13); DrawChar(#10);
  185.     BufPos := 0; BufEnd := p
  186.    END;
  187.   GrInput := 0
  188. END; (* GrInput *)
  189.  
  190. FUNCTION GrOutput(VAR f:TextRec):INTEGER;
  191. (* Ausgaberoutine, schreibt die Zeichen im      *)
  192. (* Puffer an der aktuellen Textposition auf den *)
  193. (* Bildschirm                                   *)
  194. VAR p:INTEGER;
  195. BEGIN
  196.   WITH f DO
  197.    BEGIN
  198.     FOR p:=0 TO BufPos-1 DO DrawChar(BufPtr^[p]);
  199.     BufPos := 0
  200.    END; (* WITH *)
  201.   GrOutput := 0
  202. END; (* GrOutput *)
  203.  
  204. FUNCTION GrIgnore(VAR f:TextRec):INTEGER;
  205. (* Dummy-Routine, dient nur zum Zuweisen von    *)
  206. (* nicht benötigten Funktionen                  *)
  207. BEGIN
  208.   GrIgnore := 0
  209. END; (* GrIgnore *)
  210.  
  211. FUNCTION GrOpen(VAR f:TextRec):INTEGER;
  212. (* Zuweisen der Ein- ausgabefunktion, *)
  213. (* je nach mode                       *)
  214. BEGIN
  215.   WITH f DO
  216.    BEGIN
  217.     IF mode = fminput THEN
  218.      BEGIN
  219.       InOutFunc := @GrInput;
  220.       FlushFunc := @GrIgnore
  221.      END
  222.     ELSE
  223.      BEGIN
  224.       InOutFunc := @GrOutput;
  225.       FlushFunc := @GrOutput
  226.      END;
  227.     CloseFunc := @GrIgnore
  228.    END;
  229.   GrOpen := 0
  230. END; (* GrOpen *)
  231.  
  232. (*$F-*)
  233.  
  234. PROCEDURE AssignGr(VAR f:TEXT);
  235. BEGIN
  236.   WITH TextRec(f) DO
  237.    BEGIN
  238.     mode := fmClosed;
  239.     BufSize := SizeOf(Buffer);
  240.     BufPtr  := @Buffer;
  241.     OpenFunc:= @GrOpen;
  242.     InOutFunc := @GrOutput;
  243.     name[0] := #0
  244.   END
  245. END; (* AssignGr *)
  246.  
  247. PROCEDURE GotoXY(x,y : INTEGER);
  248. BEGIN
  249.   IF NOT ingraphmode THEN Crt.GotoXY(x,y);
  250.   posxtext:=x;
  251.   posytext:=y
  252. END; (* GotoXY *)
  253.  
  254. FUNCTION WhereX;
  255. BEGIN
  256.   IF NOT ingraphmode THEN WhereX := Crt.WhereX
  257.   ELSE  WhereX := posxtext
  258. END; (* WhereX *)
  259.  
  260. FUNCTION WhereY;
  261. BEGIN
  262.   IF NOT ingraphmode THEN WhereY := Crt.WhereY
  263.   ELSE WhereY := posytext
  264. END; (* WhereY *)
  265.  
  266. PROCEDURE ClrEol;
  267. VAR
  268.   maxposx, tw, th,
  269.   tmpx, tmpy, tmp : INTEGER;
  270.   tmpcolor        : WORD;
  271.   vp : ViewPortType;
  272. BEGIN
  273.   IF NOT ingraphmode THEN Crt.ClrEol
  274.   ELSE
  275.    BEGIN
  276.     GetViewSettings(vp);
  277.     tw := TextWidth('m'); th := TextHeight('M');
  278.     tmpx := (posxtext-1) * tw;
  279.     tmpy := (posytext-1) * th;
  280.  
  281.     SetViewPort(
  282.       tmpx,tmpy,GetMaxX,tmpy+th+th DIV 4,FALSE);
  283.     ClearViewPort;
  284.     SetViewPort(vp.x1,vp.y1,vp.x2,vp.y2,vp.Clip)
  285.    END
  286. END; (* ClrEol *)
  287.  
  288. PROCEDURE LeaveGraphic;
  289. BEGIN
  290.   IF ingraphmode THEN
  291.    BEGIN
  292.     RestoreCrtMode;
  293.     AssignCrt(Input); ReSet(Input);
  294.     AssignCrt(Output); ReWrite(Output)
  295.    END;
  296.   ingraphmode := FALSE
  297. END; (* LeaveGraphic *)
  298.  
  299. {$F+}
  300. PROCEDURE MyExitProc;
  301. BEGIN
  302.   ExitProc := oldexitproc;
  303.   CloseGraph
  304. END; (* MyExitProc *)
  305. {$F-}
  306.  
  307. PROCEDURE EnterGraphic;
  308. BEGIN
  309.   SetGraphMode(graphmode);
  310. (*  GraphDefaults; *)
  311.   AssignGr(Input);  ReSet(Input);
  312.   AssignGr(Output); ReWrite(Output);
  313. (*  ClearDevice; *)
  314.   ingraphmode := TRUE
  315. END; (* EnterGraphic *)
  316.  
  317. PROCEDURE InitGraphic;
  318. VAR
  319.   errorcode : INTEGER;
  320. BEGIN
  321.   ingraphmode := FALSE;
  322.   GotoXY(1,1);
  323.   graphdriver := Detect;
  324.   InitGraph(graphdriver,graphmode,'');
  325.   errorcode := GraphResult;
  326.   IF errorcode <> GrOk THEN BEGIN
  327.     WriteLn('Graphik-Fehler: ',
  328.             GraphErrorMsg(errorcode));
  329.     ReadLn;
  330.     Halt(1)
  331.   END;
  332.   oldexitproc := ExitProc;
  333.   ExitProc := @MyExitProc;
  334.   EnterGraphic
  335. END; (* InitGraphic *)
  336.  
  337. BEGIN
  338.   ingraphmode := FALSE
  339. END.
  340.