home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmos2002.zip / SRC / TEXTLINE.MOD < prev    next >
Text File  |  1996-09-17  |  9KB  |  215 lines

  1. IMPLEMENTATION MODULE TextLines;
  2.  
  3.         (********************************************************)
  4.         (*                                                      *)
  5.         (*  Drawing horizontal and vertical lines in text mode  *)
  6.         (*                                                      *)
  7.         (*  Programmer:         P. Moylan                       *)
  8.         (*  Last edited:        3 September 1996                *)
  9.         (*  Status:             Working                         *)
  10.         (*                                                      *)
  11.         (********************************************************)
  12.  
  13. FROM SYSTEM IMPORT
  14.     (* type *)  CARD8;
  15.  
  16. FROM Windows IMPORT
  17.     (* type *)  Window,
  18.     (* proc *)  ReadBack, SetCursor, WriteChar;
  19.  
  20. FROM LowLevel IMPORT
  21.     (* proc *)  LSB, IORB, IANDB;
  22.  
  23. (************************************************************************)
  24. (*                      GLOBAL DECLARATIONS                             *)
  25. (************************************************************************)
  26.  
  27. TYPE
  28.     (* On PC-compatible machines, line graphics can be done with        *)
  29.     (* characters from the following range.                             *)
  30.     (*          │┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌                *)
  31.  
  32.     GraphicsCharRange = [CHR(179)..CHR(218)];
  33.  
  34.     (* Internally, we represent a graphics character by a 4-tuple       *)
  35.     (* (N,E,S,W), where the components represent the north, east,       *)
  36.     (* south, and west sides of the character.  Each component is       *)
  37.     (* encoded as 0=nothing, 1=single, 2=double, 3=triple; and we pack  *)
  38.     (* all four as an 8-bit quantity.  Of course there's no hardware    *)
  39.     (* support for the triple-line case, but it simplifies the coding   *)
  40.     (* if we pretend that there is.                                     *)
  41.  
  42.     PackedCode = CARD8;
  43.     EncodingTable = ARRAY PackedCode OF CHAR;
  44.     DecodingTable = ARRAY GraphicsCharRange OF PackedCode;
  45.  
  46. CONST
  47.     (* The following table converts packed codes to characters.  The    *)
  48.     (* packed code should be read as NNEESSWW.                          *)
  49.  
  50.     CharTable = EncodingTable {
  51.  
  52.                 (* Codes 00000000..00001111 *)
  53.         ' ','─','═','═','│','┐','╕','╕','║','╖','╗','╗','║','╖','╗','╗',
  54.                 (* Codes 00010000..00011111 *)
  55.         '─','─','═','═','┌','┬','╤','╤','╓','╥','╦','╦','╓','╥','╦','╦',
  56.                 (* Codes 00100000..00101111 *)
  57.         '═','═','═','═','╒','╤','╤','╤','╔','╦','╦','╦','╔','╦','╦','╦',
  58.                 (* Codes 00110000..00111111 *)
  59.         '═','═','═','═','╒','╤','╤','╤','╔','╦','╦','╦','╔','╦','╦','╦',
  60.  
  61.                 (* Codes 01000000..01001111 *)
  62.         '│','┘','╛','╛','│','┤','╡','╡','║','╢','╣','╣','║','╢','╣','╣',
  63.                 (* Codes 01010000..01011111 *)
  64.         '└','┴','╧','╧','├','┼','╪','╪','╟','╫','╬','╬','╟','╫','╬','╬',
  65.                 (* Codes 01100000..01101111 *)
  66.         '╘','╧','╧','╧','╞','╪','╪','╪','╠','╬','╬','╬','╠','╬','╬','╬',
  67.                 (* Codes 01110000..01111111 *)
  68.         '╘','╧','╧','╧','╞','╪','╪','╪','╠','╬','╬','╬','╠','╬','╬','╬',
  69.  
  70.                 (* Codes 10000000..10001111 *)
  71.         '║','╜','╝','╝','║','╢','╣','╣','║','╢','╣','╣','║','╢','╣','╣',
  72.                 (* Codes 10010000..10011111 *)
  73.         '╙','╨','╩','╩','╟','╫','╬','╬','╟','╫','╬','╬','╟','╫','╬','╬',
  74.                 (* Codes 10100000..10101111 *)
  75.         '╚','╩','╩','╩','╠','╬','╬','╬','╠','╬','╬','╬','╠','╬','╬','╬',
  76.                 (* Codes 10110000..10111111 *)
  77.         '╚','╩','╩','╩','╠','╬','╬','╬','╠','╬','╬','╬','╠','╬','╬','╬',
  78.  
  79.                 (* Codes 11000000..11001111 *)
  80.         '║','╜','╝','╝','║','╢','╣','╣','║','╢','╣','╣','║','╢','╣','╣',
  81.                 (* Codes 11010000..11011111 *)
  82.         '╙','╨','╩','╩','╟','╫','╬','╬','╟','╫','╬','╬','╟','╫','╬','╬',
  83.                 (* Codes 11100000..11101111 *)
  84.         '╚','╩','╩','╩','╠','╬','╬','╬','╠','╬','╬','╬','╠','╬','╬','╬',
  85.                 (* Codes 11110000..11111111 *)
  86.         '╚','╩','╩','╩','╠','╬','╬','╬','╠','╬','╬','╬','╠','╬','╬','╬'};
  87.  
  88.     (* The following table converts characters to packed codes. *)
  89.  
  90.     CodeTable = DecodingTable {
  91.                     44H,45H,46H,89H,09H,06H,8AH,88H,    (* │┤╡╢╖╕╣║ *)
  92.                     0AH,82H,81H,42H,05H,50H,51H,15H,    (* ╗╝╜╛┐└┴┬ *)
  93.                     54H,11H,55H,64H,98H,0A0H,28H,0A2H,  (* ├─┼╞╟╚╔╩ *)
  94.                     2AH,0A8H,22H,0AAH,62H,91H,26H,19H,  (* ╦╠═╬╧╨╤╥ *)
  95.                     90H,60H,24H,18H,99H,66H,41H,14H};   (* ╙╘╒╓╫╪┘┌ *)
  96.  
  97. (************************************************************************)
  98. (*                   DECODING OF GRAPHICS CHARACTERS                    *)
  99. (************************************************************************)
  100.  
  101. PROCEDURE Decode (char: CHAR): PackedCode;
  102.  
  103.     (* Converts a character to a PackedCode representation.     *)
  104.  
  105.     TYPE CharSet = SET OF CHAR;
  106.  
  107.     CONST GraphicsChars = CharSet
  108.                         {MIN(GraphicsCharRange)..MAX(GraphicsCharRange)};
  109.  
  110.     BEGIN
  111.         IF char IN GraphicsChars THEN
  112.             RETURN CodeTable[char];
  113.         ELSE
  114.             RETURN VAL(PackedCode,0);
  115.         END (*IF*);
  116.     END Decode;
  117.  
  118. (************************************************************************)
  119. (*                       WRITING TO THE SCREEN                          *)
  120. (************************************************************************)
  121.  
  122. PROCEDURE PutChar (w: Window;  row, col: CARDINAL;
  123.                                 N, E, S, W: LineType;  mask: PackedCode);
  124.  
  125.     (* Adds a new part of a line, described by (N,E,S,W), at location   *)
  126.     (* (row,col) in window w.  The mask is applied to the graphics      *)
  127.     (* character, if any, that is already present at that screen        *)
  128.     (* location: we can selectively remove parts of that character.     *)
  129.  
  130.     VAR code: PackedCode;
  131.  
  132.     BEGIN
  133.         (* Decode the existing character. *)
  134.  
  135.         code := IANDB (Decode(ReadBack (w, row, col)), mask);
  136.  
  137.         (* Encode and write the character for a combined code. *)
  138.  
  139.         code := IORB (code, LSB(ORD(N),6) + LSB(ORD(E),4)
  140.                                 + LSB(ORD(S),2) + ORD(W));
  141.         SetCursor (w, row, col);
  142.         WriteChar (w, CharTable[code]);
  143.  
  144.     END PutChar;
  145.  
  146. (************************************************************************)
  147. (*                  THE EXTERNALLY CALLABLE PROCEDURES                  *)
  148. (************************************************************************)
  149.  
  150. PROCEDURE HLine (w: Window;  row, col1, col2: CARDINAL;  type: LineType);
  151.  
  152.     (* Draws a horizontal line from (row,col1) to (row,col2).   *)
  153.  
  154.     VAR j: CARDINAL;
  155.  
  156.     BEGIN
  157.         IF col1 > col2 THEN
  158.             j := col1;  col1 := col2;  col2 := j;
  159.         END (*IF*);
  160.         PutChar (w, row, col1, none, type, none, none, 0CFH);
  161.         FOR j := col1+1 TO col2-1 DO
  162.             PutChar (w, row, j, none, type, none, type, 0CCH);
  163.         END (*FOR*);
  164.         PutChar (w, row, col2, none, none, none, type, 0FCH);
  165.     END HLine;
  166.  
  167. (************************************************************************)
  168.  
  169. PROCEDURE VLine (w: Window;  col, row1, row2: CARDINAL;  type: LineType);
  170.  
  171.     (* Draws a vertical line from (row1,col) to (row2,col).     *)
  172.  
  173.     VAR i: CARDINAL;
  174.  
  175.     BEGIN
  176.         IF row1 > row2 THEN
  177.             i := row1;  row1 := row2;  row2 := i;
  178.         END (*IF*);
  179.         PutChar (w, row1, col, none, none, type, none, 0F3H);
  180.         FOR i := row1+1 TO row2-1 DO
  181.             PutChar (w, i, col, type, none, type, none, 033H);
  182.         END (*FOR*);
  183.         PutChar (w, row2, col, type, none, none, none, 03FH);
  184.     END VLine;
  185.  
  186. (************************************************************************)
  187.  
  188. PROCEDURE Box (w: Window;  top, left, width, height: CARDINAL;
  189.                                                         type: LineType);
  190.  
  191.     (* Draws a rectangular box whose top left corner is at (top,left)   *)
  192.     (* and with the given width and height.                             *)
  193.  
  194.     BEGIN
  195.         (* Put in the corners. *)
  196.  
  197.         PutChar (w, top, left, none, type, type, none, 0C3H);
  198.         PutChar (w, top, left+width, none, none, type, type, 0F0H);
  199.         PutChar (w, top+height, left, type, type, none, none, 00FH);
  200.         PutChar (w, top+height, left+width, type, none, none, type, 03CH);
  201.  
  202.         (* Now the sides. *)
  203.  
  204.         HLine (w, top, left, left+width, type);
  205.         HLine (w, top+height, left, left+width, type);
  206.         VLine (w, left, top, top+height, type);
  207.         VLine (w, left+width, top, top+height, type);
  208.  
  209.     END Box;
  210.  
  211. (************************************************************************)
  212.  
  213. END TextLines.
  214.  
  215.