home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / textedit.swg / 0016_EDITLINE Unit.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1997-08-30  |  12.5 KB  |  455 lines

  1.  
  2. Unit EditLine;
  3. {$O+,F+}
  4.  
  5. interface
  6.  
  7. Const CrLf:string[2]=#13+#10;
  8.  
  9.  
  10. Type EditorFuncFlagsType = set of (Ins,Caps,EchoDots);
  11.      EditorExitType = (Up,Down,Enter,Tab,Esc);
  12.      EditorExitAllowType = Set of EditorExitType;
  13.  
  14.      WriteType = Procedure (s:string);
  15.      movexType = Procedure (x:integer);
  16.      WhereXType = Function:byte;
  17.      TextColorType = Procedure(c:byte);
  18.      TextBackgroundtype = procedure(c:byte);
  19.      ReadKeyType = function(var extend:char):char;
  20.      GotoXYType = procedure(x,y:byte);
  21.      ClrEolType = Procedure;
  22.  
  23. Type
  24.  pLineEditObj = ^LineEditObj;
  25.  LineEditObj = object
  26.    Constructor Init(sx,sy,MLen,fg,bg,abarcolor_:byte;
  27.                ansi_:boolean;
  28.                instr:string;
  29.                edf:editorfuncflagstype;
  30.                exf:editorexitallowtype;
  31.                Write_:WriteType;
  32.                movex_:movextype;
  33.                WhereX_:WhereXType;
  34.                textcolor_:textcolortype;
  35.                textbackground_:textbackgroundtype;
  36.                readkey_:readkeytype;
  37.                gotoxy_:gotoxytype;
  38.                ClrEol_:ClrEolType;
  39.                pstr_:string;
  40.                psmlen_:byte;
  41.                psc_:byte);
  42.  
  43.    Procedure AnsiPrompt;
  44.    Function Edit: EditorExitType;
  45.    Procedure NonAnsiPrompt;
  46.    Function Answer:string;
  47.    Procedure AntiBar(f:boolean);
  48.    Destructor Done;
  49.    Private
  50.        startx,starty,MaxLen,FGc,BGc,abarcolor:byte;
  51.        ansi: boolean;
  52.        EditFlags:EditorFuncFlagsType;
  53.        ExitFlags:EditorExitAllowType;
  54.        movex:movextype;
  55.        write:writetype;
  56.        wherex:wherextype;
  57.        textcolor:textcolortype;
  58.        textbackground:textbackgroundtype;
  59.        gotoxy:gotoxytype;
  60.        readkey:readkeytype;
  61.        clreol:clreoltype;
  62.        s:string;
  63.        pstr:string;
  64.        psmlen:byte;
  65.        psc:byte;
  66.    end;
  67.  
  68.  
  69. implementation
  70.  
  71. uses etc;
  72.  
  73. Constructor LineEditObj.Init(sx,sy,MLen,fg,bg,abarcolor_:byte;
  74.                ansi_:boolean;
  75.                instr:string;
  76.                edf:editorfuncflagstype;
  77.                exf:editorexitallowtype;
  78.                Write_:WriteType;
  79.                movex_:movexType;
  80.                WhereX_:WhereXType;
  81.                textcolor_:textcolortype;
  82.                textbackground_:textbackgroundtype;
  83.                readkey_:readkeytype;
  84.                gotoxy_:gotoxytype;
  85.                clreol_:clreoltype;
  86.                pstr_:string;
  87.                psmlen_:byte;
  88.                psc_:byte);
  89.  begin
  90.  clreol:=clreol_;
  91.  MaxLen:=mlen;
  92.  abarcolor:=abarcolor_;
  93.  
  94.  psc:=psc_;
  95.  
  96.  psmlen:=psmlen_;
  97.  
  98.  fgc:=fg;
  99.  bgc:=bg;
  100.  ansi:=ansi_;
  101.  EditFlags:=edf;
  102.  ExitFlags:=exf;
  103.  
  104.  movex:=movex_;
  105.  write:=write_;
  106.  wherex:=wherex_;
  107.  
  108.  textcolor:=textcolor_;
  109.  textbackground:=textbackground_;
  110.  
  111.  gotoxy:=gotoxy_;
  112.  
  113.  readkey:=readkey_;
  114.  pstr:=pstr_;
  115.  
  116.  startx:=sx;
  117.  starty:=sy;
  118.  
  119.  s:=instr;
  120.  end;
  121.  
  122. Procedure LineEditObj.NonAnsiPrompt;
  123.  begin
  124.  write(crlf+crlf);
  125.  write(pstr+crlf)
  126.  end;
  127.  
  128. procedure LineEditObj.AnsiPrompt;
  129.  begin
  130.  
  131.  gotoxy(startx,starty);
  132.  textcolor(psc);
  133.  write(rjustify(pstr,psmlen)+' ');
  134.  
  135.  end;
  136.  
  137. procedure LineEditObj.AntiBar(f:boolean);
  138.  var i:word;
  139.  begin
  140.  if not(f) or ((not(abarcolor=fgc)) and (not(bgc=0))) then
  141.    begin
  142.    textcolor(abarcolor);
  143.    textbackground(0);
  144.    if ((startx>0) and (starty>0)) then gotoxy(startx+psmlen+1,starty);
  145.    write(' ');
  146.  
  147.    if echodots in editflags then
  148.      begin
  149.      for i:=1 to length(s) do write('.');
  150.      end
  151.    else
  152.     write(s);
  153.  
  154.    clreol;
  155.    end;
  156.  end;
  157.  
  158. destructor LineEditObj.Done;
  159.  begin
  160.  end;
  161.  
  162. function LineEditObj.answer:string;
  163.  begin
  164.  answer := s;
  165.  end;
  166.  
  167. Function LineEditObj.Edit:EditorExitType;
  168.    var
  169.        extended: char;
  170.        tempkey : char;
  171.        done_   : boolean;
  172.        index   : byte;
  173.        answ    : string;
  174.        baseX   : byte;
  175.        i       : byte;
  176.        insmode : boolean;
  177.   stringtempkey: string[1];
  178.  
  179.    begin
  180.    if ansi then if ((startx>0) and (starty>0))
  181.        then gotoxy(startx+psmlen+1,starty);
  182.  
  183.    InsMode:= (Ins in EditFlags);
  184.  
  185.    done_ := false;
  186.    index := 0;
  187.    answ := '';
  188.    if length(s) <> 0 then
  189.       begin
  190.       answ := s;
  191.       index := length(s);
  192.       end;
  193.  
  194.    if ansi then begin
  195.    TextColor(fgc);
  196.    TextBackground(bgc);
  197.    end;
  198.  
  199.    write(' ');
  200.    if echodots in editflags then
  201.     begin
  202.     for i:=1 to length(s) do write('.')
  203.     end
  204.    else Write(s);
  205.  
  206.    if ANSI and (not(fgc=abarcolor) or not(bgc=0)) then
  207.      begin
  208.      for i:=length(s)+1 to maxlen+1 do Write(' ');
  209.      movex(-maxlen+length(answ)-1);
  210.      end;
  211.  
  212.    { functions ... backspace, right, left, overwrite mode for L, R }
  213.    {               enter, delete                                   }
  214.  
  215.    repeat
  216.       tempkey := readkey(extended);
  217.       case tempkey of
  218.         ^U: if ansi then
  219.                begin
  220.                answ:=s;
  221.                movex(-index);
  222.                if echodots in editflags then
  223.                  begin
  224.                  for i:=1 to length(answ) do write('.')
  225.                  end
  226.                else Write(answ);
  227.                for i:=length(answ)+1 to maxlen do write(' ');
  228.                index:=length(answ);
  229.                movex(-maxlen+length(answ));
  230.                end
  231.             else
  232.                begin
  233.                for i:=1 to length(answ) do write(#8+' '+#8);
  234.                answ:=s;
  235.                if echodots in editflags then
  236.                  begin
  237.                  for i:=1 to length(answ) do write('.')
  238.                  end
  239.                else Write(answ);
  240.                index:=length(answ);
  241.                end;
  242.  
  243.         #27,^Z: if esc in exitflags then
  244.               begin
  245.               done_:=true;
  246.               edit:=esc;
  247.               end;
  248.  
  249.         #09: if ansi and (tab in exitflags) then
  250.               begin
  251.               done_:=true;
  252.               edit:=tab;
  253.               end;
  254.  
  255.  
  256.         #32,
  257.  
  258.              {'A'..'Z', 'a'..'z','0'..'9', ',' , '.':}
  259.  
  260.              ' '..'~':
  261.  
  262.              begin
  263.               if ord(answ[0]) < maxlen then
  264.                begin
  265.                inc(index);
  266.                if index <= maxlen then
  267.                begin
  268.                if (Caps in EditFlags) then
  269.    {for upcase} if (answ[index-1] = #32) or (answ[index-1] = #0) then
  270.    {checking}     begin
  271.                   tempkey := upcase(tempkey);
  272.                   end
  273.                 else tempkey := lowcase (tempkey);
  274.  
  275.                if (Insmode) and ansi then
  276.                   begin
  277.                   if ord(answ[0]) < maxlen then
  278.                     begin
  279.                     stringtempkey := tempkey;
  280.                     insert(stringtempkey, answ, index);
  281.                     if (Caps in EditFlags) then answ := CaseStr(answ);
  282.                     if index <> ord(answ[0]) then
  283.                      begin
  284.                      if echodots in editflags then
  285.                       begin
  286.                       for i:=index to length(answ)-index+3 do write('.');
  287.                       write(' ');
  288.                       end
  289.                      else
  290.                       begin
  291.                       write( copy(answ,index,length(answ)-index+1)+' ' );
  292.                       end;
  293.                      movex(-length(answ)-1+index);
  294.                      end
  295.                     else if echodots in editflags then write('.') else Write(tempkey);
  296.                     end;
  297.                   end
  298.                else
  299.                   begin
  300.                   if index < ord(answ[0])+1 then answ[index] := tempkey
  301.                   else answ := answ + tempkey;
  302.                   if echodots in editflags then write('.') else Write(tempkey)
  303.                   end;
  304.                end;
  305.               end;
  306.              end;
  307.         #13: if Enter in ExitFlags then
  308.              begin
  309.              done_ := true;
  310.              edit := Enter;
  311.              end;
  312.  
  313.         ^V: if (ins in editflags) then insmode := not insmode;
  314.  
  315.         #8:
  316.              begin
  317.  
  318.              if (index > 0)  then
  319.               begin
  320.               delete(answ, Index, 1);
  321.               if ANSI then
  322.                begin
  323.                if (index=length(answ)+1) then
  324.                  begin
  325.                  movex(-1);
  326.                  write(' ');
  327.                  movex(-1);
  328.                  end
  329.                else
  330.                  begin
  331.                  movex(-1);
  332.                  if echodots in editflags then
  333.                   begin
  334.                   for i:=index to length(answ)-index+1 do write('.');
  335.                   write(' ');
  336.                   end
  337.                  else
  338.                   write( copy(answ,index,length(answ)-index+1)+' ' );
  339.                  movex(-length(answ)-1+index-1);
  340.                  end;
  341.                end
  342.               else Write(#8+' '+#8);
  343.               dec(index);
  344.               end;
  345.              end;
  346.         #0:                         { test for extended characters }
  347.              begin
  348.              case extended of        { poll for extended part }
  349.                #60:  if ansi then
  350.                       begin
  351.                       answ:=s;
  352.                       movex(-index);
  353.                       if echodots in editflags then
  354.                        begin
  355.                        for i:=1 to length(answ) do write('.');
  356.                        end
  357.                       else
  358.                        Write(answ);
  359.  
  360.                       for i:=length(answ)+1 to maxlen do write(' ');
  361.                       index:=length(answ);
  362.                       movex(-maxlen+length(answ));
  363.                       end
  364.                      else
  365.                       begin
  366.                       for i:=1 to length(answ) do write(#8+' '+#8);
  367.                       answ:=s;
  368.                       if echodots in editflags then
  369.                        begin
  370.                        for i:=1 to length(answ) do write('.');
  371.                        end
  372.                       else
  373.                        write(answ);
  374.                        index:=length(answ);
  375.                       end;
  376.  
  377.                #80: if ansi and (down in exitflags) then
  378.                     begin
  379.                     done_:=true;
  380.                     edit:=down;
  381.                     end;
  382.  
  383.                #72: if ansi and (up in exitflags) then
  384.                     begin
  385.                     done_:=true;
  386.                     edit:=up;
  387.                     end;
  388.  
  389.                #75:                 { left arrow }
  390.                    begin
  391.                    if ANSI then
  392.                     begin
  393.                     if index >= 1 then
  394.                      begin
  395.                      dec(index);
  396.                      movex(-1);
  397.                      end;
  398.                     end;
  399.                    end;
  400.                #77:                 { right arrow }
  401.                   begin
  402.                   if ANSI then
  403.                    begin
  404.                    if index < ord(answ[0]) then
  405.                      begin
  406.                      inc(index);
  407.                      if echodots in editflags then write('.') else write(answ[index]);
  408.                      end;
  409.                    end;
  410.                   end;
  411.                #71:                 { home }
  412.                   begin
  413.                   if ANSI then
  414.                      begin
  415.                      movex(-index);
  416.                      index:=0;
  417.                      end;
  418.                   end;
  419.  
  420.                #79: IF ANSI then { end }
  421.                   begin
  422.                   movex(length(answ)-index);
  423.                   index := ord(answ[0]);
  424.                   end;
  425.  
  426.                #82:      { ins }
  427.                 if (ins in editflags) then insmode := not insmode;
  428.  
  429.                #83:         { del }
  430.                   begin
  431.                   if ANSI then
  432.                     begin
  433.                     delete(answ,index+1,1);
  434.                     If (Caps in EditFlags) then answ := CaseStr(answ);
  435.                     if echodots in editflags then
  436.                      begin
  437.                      for i:=index+1 to length(answ)-index do write('.');
  438.                      write(' ');
  439.                      end
  440.                     else
  441.                      write( copy(answ,index+1,length(answ)-index)+' ' );
  442.                      movex(-length(answ)-1+index);
  443.                      end;
  444.                   end;
  445.  
  446.                end;                           { end of 'case readkey of' }
  447.              end;                             { end of '#0: begin' }
  448.         end;                                  { end of 'case tempkey of' }
  449.    until done_;
  450.    s := answ;
  451.    end;
  452.  
  453. end.
  454.  
  455.