[comp.lang.pascal] Reading a string in graphics mode

dave@tygra.UUCP (David Conrad) (04/01/91)

Recently the question was asked, "How can a string be read from the user
while in graphics mode, readln doesn't cut it?"  I forget who asked this.
Anyway, in a program I wrote about a week ago, I had to solve just this
problem.  I've cut the code out of my program, and deleted the application
specific stuff.  I generalized it a little by letting the client pass the
prompt, but much more could be done.  The maximum length, for example, 
should be passed to it, and it could save the current fill state.  Also,
it works only with a fixed font/size.  Perhaps the best solution there
would be to require the client to have the text style already set before
calling this routine.  The only place the length is referenced is in the
Until clause of the Repeat statement.  Anyway, here is a simple, first
order approximation of a solution to the problem.  This article, including
the program it contains, is in the Public Domain.

David Conrad
dave%tygra@sharkey.cc.umich.edu

{$A+,B-,D-,E-,F-,I+,L-,N-,O-,R-,S+,V-}
{$M 16384,0,655360}
program grreadstr;

uses graph, crt;

procedure grgetstr (var s : string; prompt : string);
var
  ch : char;
  i, x : integer;

begin
  setfillstyle (SolidFill, 0);
  settextstyle (TriplexFont, HorizDir, 2);
  settextjustify (lefttext, toptext);
  i := 0;
  s := '';
  x := 20;
  outtextxy (x, 8, prompt);
  x := x + textwidth(prompt);
  repeat
    ch := readkey;
    if ch = #0 then if readkey = #0 then;
    if ch > #31 then
      begin
        s := s + ch;
        inc (i);
        outtextxy (x, 8, ch);
        x := x + textwidth(s[i]);
      end;
    if (ch = #8) and (i <> 0) then
      begin
        bar (x - textwidth(s[i]), 0, x, 48);
        x := x - textwidth(s[i]);
        delete (s, length(s), 1);
        dec (i);
      end;
    if ch = #27 then
      begin
        bar (0, 0, getmaxx, 48);
        outtextxy (20, 8, prompt);
        x := 20 + textwidth(prompt);
        i := 0;
        s := '';
      end;
  until (ch = #13) or (i > 32);
end;

var
  grd, grm, grerr : integer;
  name : string;

begin
  grd := Detect;
  initgraph (grd, grm, '');
  grerr := graphresult;
  if grerr <> grOk then
    begin
      writeln ('Error: ',grapherrormsg(grerr),'.');
      halt;
    end;
  grgetstr (name, 'Enter your name: ');
  closegraph;
  writeln ('name = ',name);
end.
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave%tygra@sharkey.cc.umich.edu