{--------------------------------------------------------}
{    Programa de captura do QWS3270
{    Autor: José Antonio Borges
{    Em 30/9/99
{--------------------------------------------------------}

{$M 16536,8192}
program qwscap;

uses dvcrt, dvwin, dvmacro, videovox, dvexec,
     winprocs, wintypes, strings, win31;

var minhaJan, janProg: hwnd;
    monitorAtivo: boolean;
    capturando: boolean;
    arq: text;
    latual: string;

{---------------------------------------------------------------------}

procedure trocaJan (j: hWnd);
var vezes: integer;
begin
    if not isWindow (j) then exit;

    setActiveWindow (j);
    vezes := 1;
    while (vezes < 20) and (getActiveWindow <> j) and (isWindow (j)) do
        begin
            delay (100);
            vezes := vezes + 1;
        end;
end;

{---------------------------------------------------------------------}

procedure cancelaMonitor;
var janMonitor, janAtual: hWnd;
begin
    janMonitor := findWindow (NIL, 'MONITVOX');
    monitorAtivo := janMonitor <> 0;
    janAtual := getActiveWindow;
    if monitorAtivo then
        begin
            showWindow (janMonitor, sw_maximize);
            playInit (300);
            playVirtKey (VK_F4, false, false, true, 1);
            playStart;
            while  playIsActive do;
        end;
    setActiveWindow (janAtual);
end;

{--------------------------------------------------------}

procedure recuperaMonitor;
var i: integer;
begin
    if monitorAtivo then
        begin
            executaProg (sintAmbiente ('DOSVOX', 'MONITOR'), '', '');
            setActiveWindow (minhaJan);   { para evitar problemas }

            setActiveWindow (janProg);
            for i := 1 to 10 do
                begin
                   keypressed;
                   if getActiveWindow = janProg then
                       exit;
                   delay (100);
                end;
        end;
end;

{---------------------------------------------------------------------}

procedure capturaTela;
var
    curx, cury, y, i: integer;
    s: string;
    buf: PChar;
    pt: TPoint;
    coords: TRect;

begin
    trocaJan (janProg);

    getCaretPos (pt);
    getClientRect (janProg, coords);
    curx := 1+(pt.x - coords.left) div 7;
    cury := 1+(pt.y - coords.top) div 15;

    setWindowText (janProg, 'QWS Telnet');
    playInit (300);
    playAltKey ('E', 1);
    playKey ('L', 1);
    playAltKey ('E', 1);
    playKey ('Y', 1);
    playStart;
    while  playIsActive do;

    clrscr;
    getmem (buf, 5000);
    getClipboard (buf, 5000);

    i := 0;
    y := 1;
    while (y < 25) and (buf[i] <> #$0) do
      begin
         s := '';
         while (buf[i] <> #$0d) and (buf[i] <> #$0) do
             begin
                 if buf[i] <> #$0a then
                      s := s + buf[i];
                 i := i + 1;
             end;

         if buf[i] = #$0d then
             i := i + 1;

         gotoxy (1, y);
         writeln (s);
         if y = cury then latual := s;
         y := y + 1;
      end;

    freemem (buf, 5000);

    trocaJan (minhaJan);
    gotoxy (curx, cury);
end;

{---------------------------------------------------------------------}

var s, salva: string;
    resp, c: char;
label fim;
begin
    janProg := getActiveWindow;

    textBackGround (BLUE);
    writeln ('Capturador de linhas para o DOSVOX - ESC termina');
    textBackGround (BLACK);
    checkBreak := false;

    sintInic (0, '');
    sintBip;
    capturando := false;

    minhaJan := getActiveWindow;
    cancelaMonitor;

    capturaTela;
    sintetiza (latual);
    leitorDeTela;

fim:
    sintFim;

    recuperaMonitor;
    doneWinCrt;
end.
