(********************************************************************)
(*                                                                  *)
(*  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);


(**
 *  Standard console file of the current process.
 *  When characters are written to ''STD_CONSOLE'' they are displayed
 *  on the console/terminal (if it exists and is able to display them).
 *  Unlike the byte file [[stdio#STD_OUT|STD_OUT]] ''STD_CONSOLE''
 *  accepts also Unicode characters. Unicode characters are written
 *  with the encoding of the operating system. If the standard output
 *  file of the operating system has been redirected UTF-8 encoded
 *  characters are written to the redirected file.
 *)
var 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: consoleFile is STD_CONSOLE;
  begin
    CON_OPEN;
    consoleFile := 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 (in console_file: aFile)                       is action "CON_FLUSH";


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


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


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


(**
 *  Get the cursor column of 'aConsoleFile'.
 *  @return the cursor column of 'aConsoleFile'.
 *)
const func integer: column (in console_file: aConsoleFile)       is action "CON_COLUMN";


(**
 *  Get the cursor line of 'aConsoleFile'.
 *  @return the cursor line of 'aConsoleFile'.
 *)
const func integer: line (in console_file: aConsoleFile)         is action "CON_LINE";


const proc: cursor (in console_file: aConsoleFile, in boolean: on)  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: aConsoleFile,
    in integer: upper, in integer: left, in integer: lower,
    in integer: right, in integer: count)                        is action "CON_V_SCL";
const proc: h_scroll (inout console_file: aConsoleFile,
    in integer: upper, in integer: left, in integer: lower,
    in integer: right, in integer: count)                        is action "CON_H_SCL";


const proc: color (in console_file: aConsoleFile, in color: foreground) is noop;
const proc: color (in console_file: aConsoleFile, in color: foreground, in color: background) is noop;


(**
 *  Write a string to the current position of 'aConsoleFile'.
 *  Unicode characters are written with the encoding of the
 *  operating system. The cursor position is changed, if
 *  one of the characters '\n', '\r' and '\b' is written.
 *  If the standard output file of the operating system has
 *  been redirected UTF-8 encoded characters are written to
 *  the redirected file.
 *)
const proc: write (in console_file: aConsoleFile, in string: stri) is action "CON_WRITE";


const proc: moveLeft (in console_file: aConsoleFile, in string: stri) is func
  begin
    write(aConsoleFile, "\b" mult width(stri));
  end func;


const proc: erase (in console_file: aConsoleFile, in string: stri) is func
  begin
    write(aConsoleFile, " " mult width(stri));
  end func;


const proc: cursorOn (in console_file: aConsoleFile, in char: cursorChar) is noop;
const proc: cursorOff (in console_file: aConsoleFile, in char: cursorChar) is noop;


const proc: box (in console_file: aConsoleFile)       is noop;
const proc: clear_box (in console_file: aConsoleFile) is noop;