/*
* this is the ncurses implementation of sc-im user interface, or called tui.
* it mainly consists on the following two windows:
* main_win: window that shows the grid
* input_win: status bar window (or called header) and stdin input
*
* these are the functions called outside tui.c:
* ui_start_screen // function called to start ui
* ui_stop_screen // function called to stop ui
* ui_show_header // function that updates status bar in top of screen.
* ui_update // function used to refresh content of screen
* ui_getch // function that asks the user input from stdin (non blocking)
* ui_getch_b // function that asks the user input from stdin (blocking)
* ui_query // function to read text from stdin
* ui_do_welcome // function used when starting sc-im without a file as a parameter
* ui_handle_cursor // function used to handle cursor depending on current mode
* yyerror // error routine for yacc parser
* ui_show_text // function that shows text in a child process.
* used for set, version, showmaps, print_graph,
* showfilters, hiddenrows and hiddencols commands
* ui_bail // function to print errors of lua scripts
* ui_sc_msg // function that is used for sc_info, sc_error and sc_debug macros
* ui_winchg // function that handles SIGWINCH
* ui_print_mult_pend // function that shows multiplier in top left of screen
* ui_show_celldetails // function that shows cell details in header bar
* ui_start_colors // exclusive ui startup routine for colors
* ui_clr_header // functions that clears a line in header bar
* ui_print_mode // function that shows current mode in top right of screen
* ui_get_formated_value // function used for exporting spreadsheet to plain text
*
* these are local functions that might not be needed to reimplement if writing another ui:
* ui_set_ucolor // function called internally for setting a color
* ui_show_content
* ui_show_sc_col_headings
* ui_show_sc_row_headings
* ui_write_j
* ui_add_cell_detail // Add details of an ent to a char * received as a parameter. used for input_win
*
* ANYONE WHO WANTS TO PORT THIS TO ANOTHER UI, WOULD JUST NEED TO REIMPLEMENT THIS FILE
* AND HELP() IN HELP.C
*
*
* if not working with ncurses, you should also have to define LINES and COLS macros in Xui.h as well.
* see ui example inside /files folder
*/
#include <string.h>
#include <ncurses.h>
#include <stdio.h>
#include <time.h>
#include <locale.h>
#include <stdlib.h>
#include <stdarg.h>
#include "main.h"
#include "conf.h"
#include "input.h"
#include "tui.h"
#include "range.h"
#include "sc.h"
#include "cmds.h"
#include "cmds_visual.h"
#include "conf.h"
#include "version.h"
#include "file.h"
#include "format.h"
#include "utils/string.h"
extern struct dictionary * d_colors_param;
extern int cmd_pending;
extern int cmd_multiplier;
extern char insert_edit_submode;
WINDOW * main_win;
WINDOW * input_win;
SCREEN * sstderr;
SCREEN * sstdout;
srange * ranges;
void ui_start_screen() {
//def_shell_mode();
sstderr = newterm(NULL, stderr, NULL);
noecho();
sstdout = newterm(NULL, stdout, stdin);
set_term(sstdout);
main_win = newwin(LINES - RESROW, COLS, RESROW, 0);
input_win = newwin(RESROW, COLS, 0, 0); // just 2 rows (RESROW = 2)
#ifdef USECOLORS
if (has_colors()) {
start_color();
if (get_d_colors_param() == NULL) {
start_default_ucolors();
// in case we decide to change colors
// Create a dictionary and save equivalences between macros and
// values defined in '.sc' files
set_colors_param_dict();
}
wbkgd(main_win, COLOR_PAIR((ucolors[DEFAULT].fg+1) * 9 + ucolors[DEFAULT].bg + 2));
wbkgd(input_win, COLOR_PAIR((ucolors[DEFAULT].fg+1) * 9 + ucolors[DEFAULT].bg + 2));
}
#endif
wtimeout(input_win, TIMEOUT_CURSES);
noecho();
curs_set(0);
#ifndef NETBSD
if ((char *) getenv ("ESCDELAY") == NULL) set_escdelay(ESC_DELAY);
#endif
cbreak();
keypad(input_win, 1);
}
void ui_stop_screen() {
#ifdef USECOLORS
//if (get_d_colors_param() != NULL)
free_colors_param_dict();
#endif
move(0, 0);
clrtobot();
refresh();
set_term(sstdout);
endwin();
set_term(sstderr);
endwin();
return;
}
/*
* this function asks user for input from stdin.
* should be non blocking and should
* return -1 when no key was press
* return 0 when key was press.
* it receives * wint_t as a parameter.
* when a valid key is press, its value its then updated in that wint_t variable.
*/
int ui_getch(wint_t * wd) {
return wget_wch(input_win, wd);
}
/*
* this function asks user for input from stdin.
* should be blocking and should
* return -1 when ESC was pressed
* return 0 otherwise.
* it receives * wint_t as a parameter.
* when a valid key is press, its value its then updated in that wint_t variable.
*/
int ui_getch_b(wint_t * wd) {
wtimeout(input_win, -1);
move(0, rescol + inputline_pos + 1);
wget_wch(input_win, wd);
wtimeout(input_win, TIMEOUT_CURSES);
if ( *wd != OKEY_ESC ) return 0;
return -1;
}
// sc_msg - used for sc_info, sc_error and sc_debug macros
void ui_sc_msg(