
(********************************************************************)
(*                                                                  *)
(*  console.s7i   Text implementation type for text console/window. *)
(*  Copyright (C) 1992, 1993, 1994, 2005, 2011  Thomas Mertes       *)
(*                                                                  *)
(*  This file is part of the Seed7 Runtime Library.                 *)
(*                                                                  *)
(*  The Seed7 Runtime Library is free software; you can             *)
(*  redistribute it and/or modify it under the terms of the GNU     *)
(*  Lesser General Public License as published by the Free Software *)
(*  Foundation; either version 2.1 of the License, or (at your      *)
(*  option) any later version.                                      *)
(*                                                                  *)
(*  The Seed7 Runtime Library is distributed in the hope that it    *)
(*  will be useful, but WITHOUT ANY WARRANTY; without even the      *)
(*  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *)
(*  PURPOSE.  See the GNU Lesser General Public License for more    *)
(*  details.                                                        *)
(*                                                                  *)
(*  You should have received a copy of the GNU Lesser General       *)
(*  Public License along with this program; if not, write to the    *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


include "null_file.s7i";
include "text.s7i";
include "enable_io.s7i";
include "color.s7i";


(**
 *  [[text|Text]] implementation type for text console/window.
 *  This type allows writing to text terminals, terminal emulations
 *  and console windows.
 *)
const type: console_file is sub null_file struct
  end struct;


type_implements_interface(console_file, text);

const console_file: STD_CONSOLE is console_file.value;

const proc: CON_OPEN is                               action "CON_OPEN";


(**
 *  Creates a ''console_file'' at the upper left corner of the console/window.
 *  The ''console_file'' extends over the whole text console/window.
 *  @return the file opened.
 *)
const func console_file: open (CONSOLE) is func
  result
    var console_file: result is STD_CONSOLE;
  begin
    CON_OPEN;
    result := STD_CONSOLE;
  end func;


(**
 *  Forces that all buffered data of ''aFile'' is sent to its destination.
 *  This causes data to be sent to the text console/window of the OS.
 *)
const proc: flush (ref console_file: aFile)                       is action "CON_FLUSH";


(**
 *  Get the height of 'aConsoleFile'.
 *  @return the height of 'aConsoleFile'.
 *)
const func integer: height (ref console_file: aConsoleFile)        is action "CON_HEIGHT";


(**
 *  Get the width of 'aConsoleFile'.
 *  @return the width of 'aConsoleFile'.
 *)
const func integer: width (ref console_file: aConsoleFile)         is action "CON_WIDTH";


(**
 *  Set the current position of 'aConsoleFile' to 'line' and 'column'.
 *)
const proc: setPos (ref console_file: aConsoleFile,
    ref integer: line, ref integer: column)                      is action "CON_SETPOS";


const proc: cursor (ref console_file param, ref boolean param)    is action "CON_CURSOR";
const proc: clearArea (in console_file: aConsoleFile, in integer: upper,
    in integer: left, in integer: lower, in integer: right)      is action "CON_CLEAR";


(**
 *  Clear an area of ''aConsoleFile'' with space characters.
 *  The area is specified in (line, column) coordinates and is
 *  between the (''upper'', ''left'') and (''lower'', ''right'').
 *)
const proc: clear (in console_file: aConsoleFile, in integer: upper,
    in integer: left, in integer: lower, in integer: right) is func
  begin
    clearArea(aConsoleFile, upper, left, lower, right);
    setPos(aConsoleFile, 1, 1);
  end func;


(**
 *  Clear 'aConsoleFile' with space characters.
 *)
const proc: clear (in console_file: aConsoleFile) is func
  begin
    clear(aConsoleFile, 1, 1, height(aConsoleFile), width(aConsoleFile));
  end func;


const proc: v_scroll (inout console_file param,
    in integer param, in integer param, in integer param,
    in integer param, in integer param)                          is action "CON_V_SCL";
const proc: h_scroll (ref console_file param,
    in integer param, in integer param, in integer param,
    in integer param, in integer param)                          is action "CON_H_SCL";


const proc: color (ref console_file param, ref color param) is noop;
const proc: color (ref console_file param, ref color param, ref color param) is noop;


(**
 *  Write a string to the current position of 'aConsoleFile'.
 *)
const proc: write (ref console_file: aConsoleFile, in string: stri) is action "CON_WRITE";


(*
const proc: writeln (ref console_file: aConsoleFile) is func
  begin
    if LINE(aConsoleFile) = height(aConsoleFile) then
      SCROLL_UP(aConsoleFile, 1, 1, height(aConsoleFile), width(aConsoleFile), 1);
      setPos(aConsoleFile, LINE(aConsoleFile), 1);
    else
      setPos(aConsoleFile, succ(LINE(aConsoleFile)), 1);
    end if;
  end func;


const proc: backSpace (ref console_file: aConsoleFile) is func
  begin
    if COLUMN(aConsoleFile) > 1 then
      setPos(aConsoleFile, LINE(aConsoleFile); pred(COLUMN(aConsoleFile)));
      if COLUMN(aConsoleFile) <= width(aConsoleFile) then
        write(aConsoleFile.OUT_FILE, " ");
        setPos(aConsoleFile, LINE(aConsoleFile); pred(COLUMN(aConsoleFile)));
      end if;
    end if;
  end func;
*)


const proc: cursor_on (inout console_file: aConsoleFile) is func
  begin
    write(aConsoleFile, "_");
  end func;


const proc: cursor_off (inout console_file: aConsoleFile) is func
  begin
    write(aConsoleFile, " ");
  end func;


const proc: box (ref console_file param) is noop;
const proc: clear_box (ref console_file param) is noop;
