home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / turbo4 / mcinput.pas < prev    next >
Pascal/Delphi Source File  |  1987-12-08  |  6KB  |  235 lines

  1.  
  2. {           Copyright (c) 1985, 87 by Borland International, Inc.            }
  3.  
  4. unit MCINPUT;
  5.  
  6. interface
  7.  
  8. uses Crt, Dos, MCVars, MCUtil, MCDisply, MCParser, MCLib;
  9.  
  10. function GetKey : Char;
  11. { Liest ein Zeichen von der Tastatur via BIOS-Aufruf. }
  12.  
  13. function InCharset(Ch: Char): Boolean;
  14. { Prüft auf "erlaubtes Zeichen" inklusive Umlaute. }
  15.  
  16. function EditString(var S : IString; Legal : IString;
  17.                     MaxLength : Word) : Boolean;
  18. { Editieren eines Strings: "Legal" legt den Satz erlaubter Zeichen fest.
  19.   Ergebnis FALSE bei Abbruch durch Eingabe von ESC. }
  20.  
  21. procedure GetInput(C : Char);
  22. { Liest und bearbeitet eine Eingabe über die Tastatur, die mit dem als C
  23.   übergebenen Zeichen begonnen wurde. }
  24.  
  25. function GetWord(var Number : Word; Low, High : Word) : Boolean;
  26. { Liest einen (positiven) Wert des Typs Word ein }
  27.  
  28. function GetCell(var Col, Row : Word) : Boolean;
  29. { Liest eine Zelladresse und liefert FALSE bei Abbruch der Eingabe mit ESC. }
  30.  
  31. function GetYesNo(var YesNo : Char; Prompt : String) : Boolean;
  32. { Gibt den als Prompt übergebenen String aus und wartet danach auf
  33.   'J' oder 'N' - liefert FALSE bei Abbruch mit ESC. }
  34.  
  35. function GetCommand(MsgStr, ComStr : String) : Word;
  36. { Liest einen Befehl und führt ihn aus. }
  37.  
  38. { ********************************************************** }
  39. { ********************************************************** }
  40.  
  41. implementation
  42.  
  43. function GetKey;
  44. var C : Char;
  45. begin
  46.   C := ReadKey;
  47.   repeat
  48.     if C = NULL then  { Spezialtaste }
  49.     begin
  50.       C := ReadKey;
  51.       if Ord(C) > 127 then C := NULL
  52.        else GetKey := Chr(Ord(C) + 128);
  53.     end
  54.     else
  55.       GetKey := C;
  56.   until C <> NULL;
  57. end;
  58.  
  59. function InCharset;
  60. begin
  61.   if (Ch >= ' ') and (Ch <= '~') then InCharSet := True
  62.     else if Ch in ['ä','ö','ü','ß','Ä','Ö','Ü'] then InCharSet := True
  63.      else InCharSet := False;
  64. end;
  65.  
  66. function EditString;
  67. var
  68.   CPos : Word;
  69.   Ins : Boolean;
  70.   Ch : Char;
  71. begin
  72.   Ins := True;
  73.   ChangeCursor(Ins);
  74.   CPos := Succ(Length(S));
  75.   SetColor(White);
  76.   repeat
  77.     GotoXY(1, ScreenRows + 5);
  78.     Write(S, '':(79 - Length(S)));
  79.     GotoXY(CPos, ScreenRows + 5);
  80.     Ch := GetKey;
  81.     case Ch of
  82.       HOMEKEY : CPos := 1;
  83.       ENDKEY : CPos := Succ(Length(S));
  84.       INSKEY : begin
  85.                 Ins := not Ins; ChangeCursor(Ins);
  86.               end;
  87.       LEFTKEY : if CPos > 1 then Dec(CPos);
  88.       RIGHTKEY : if CPos <= Length(S) then Inc(CPos);
  89.       BS : if CPos > 1 then
  90.             begin
  91.              Delete(S, Pred(CPos), 1); Dec(CPos);
  92.             end;
  93.       DELKEY : if CPos <= Length(S) then Delete(S, CPos, 1);
  94.       CR : ;
  95.       UPKEY, DOWNKEY : Ch := CR;
  96.       ESC : S := '';
  97.       else begin
  98.         if ((Legal = '')
  99.          or (Pos(Ch, Legal) <> 0)) and InCharset(Ch)
  100.          and (Length(S) < MaxLength) then
  101.         begin
  102.           if Ins then Insert(Ch, S, CPos)
  103.           else if CPos > Length(S) then S := S + Ch
  104.             else S[CPos] := Ch;
  105.           Inc(CPos);
  106.         end;
  107.       end;
  108.     end; { case }
  109.   until (Ch = CR) or (Ch = ESC);
  110.   ClearInput;
  111.   ChangeCursor(False);
  112.   EditString := Ch <> ESC;
  113.   SetCursor(NoCursor);
  114. end; { EditString }
  115.  
  116. procedure GetInput;
  117. var
  118.   S : IString;
  119. begin
  120.   S := C;
  121.   if (not EditString(S, '', MAXINPUT)) or (S = '') then Exit;
  122.   Act(S);
  123.   Changed := True;
  124. end;
  125.  
  126. function GetWord;
  127. var
  128.   I, Error : Word;
  129.   Good : Boolean;
  130.   Num1, Num2 : String[5];
  131.   Message : String[80];
  132.   S : IString;
  133. begin
  134.   GetWord := False;
  135.   S := '';
  136.   Str(Low, Num1);
  137.   Str(High, Num2);
  138.   Message := MSGBADNUMBER + ' ' + Num1 + ' to ' + Num2 + '.';
  139.   repeat
  140.     if not EditString(S, '1234567890', 4) then Exit;
  141.     Val(S, I, Error);
  142.     Good := (Error = 0) and (I >= Low) and (I <= High);
  143.     if not Good then ErrorMsg(Message);
  144.   until Good;
  145.   Number := I;
  146.   GetWord := True;
  147. end;
  148.  
  149. function GetCell;
  150. var
  151.   Len, NumLen, OldCol, OldRow, Posit, Error : Word;
  152.   Data : IString;
  153.   NumString : IString;
  154.   First, Good : Boolean;
  155. begin
  156.   NumLen := RowWidth(MAXROWS);
  157.   OldCol := Col;
  158.   OldRow := Row;
  159.   First := True;
  160.   Good := False;
  161.   Data := '';
  162.   repeat
  163.     if not First then ErrorMsg(MSGBADCELL);
  164.     First := False;
  165.     Posit := 1;
  166.     if not EditString(Data, '', NumLen + 2) then
  167.     begin
  168.       Col := OldCol;
  169.       Row := OldRow;
  170.       GetCell := False;
  171.       Exit;
  172.     end;
  173.     if (Data <> '') and (Data[1] in Letters) then
  174.     begin
  175.       Col := Succ(Ord(UpCase(Data[1])) - Ord('A'));
  176.       Inc(Posit);
  177.       if (Posit <= Length(Data)) and (Data[Posit] in LETTERS) then
  178.       begin
  179.         Col := Col * 26;
  180.         Inc(Col, Succ(Ord(UpCase(Data[Posit])) - Ord('A')));
  181.         Inc(Posit);
  182.       end;
  183.       if Col <= MAXCOLS then
  184.       begin
  185.         NumString := Copy(Data, Posit, Succ(Length(Data) - Posit));
  186.         Val(NumString, Row, Error);
  187.         if (Row <= MAXROWS) and (Error = 0) then
  188.           Good := True;
  189.       end;
  190.     end;
  191.   until Good;
  192.   GetCell := True;
  193. end;
  194.  
  195. function GetYesNo;
  196. begin
  197.   SetCursor(ULCursor);
  198.   GetYesNo := False;
  199.   WritePrompt(Prompt + ' ');
  200.   repeat
  201.     YesNo := UpCase(GetKey);
  202.     if YesNo = ESC then Exit;
  203.   until YesNo in ['J', 'N'];
  204.   SetCursor(NoCursor);
  205.   GetYesNo := True;
  206. end;
  207.  
  208. function GetCommand;
  209. var
  210.   Counter, Len : Word;
  211.   Ch : Char;
  212. begin
  213.   Len := Length(MsgStr);
  214.   GotoXY(1, ScreenRows + 4);
  215.   ClrEol;
  216.   for Counter := 1 to Len do
  217.   begin
  218.     if MsgStr[Counter] in ['A'..'Z'] then SetColor(COMMANDCOLOR)
  219.      else SetColor(LOWCOMMANDCOLOR);
  220.     Write(MsgStr[Counter]);
  221.   end;
  222.   GotoXY(1, ScreenRows + 5);
  223.   repeat
  224.     Ch := UpCase(GetKey);
  225.   until (Pos(Ch, ComStr) <> 0) or (Ch = ESC);
  226.   ClearInput;
  227.   if Ch = ESC then
  228.     GetCommand := 0
  229.   else
  230.     GetCommand := Pos(Ch, ComStr);
  231. end;
  232.  
  233. begin   { keine Initialisierungen }
  234. end.
  235.