Merge remote-tracking branch 'refs/remotes/origin/master' into YaSp
[scilab.git] / scilab / modules / console / src / c / scilines.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Allan CORNET 
4  * Copyright (C) 2008 - INRIA - Sylvestre LEDRU (Detection of the term size)
5  * 
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at    
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13 /*--------------------------------------------------------------------------*/
14
15 #include "machine.h"
16
17 /* for getenv() */
18 #ifdef HAVE_STDLIB_H
19 #include <stdlib.h>
20 #endif
21
22 #ifndef _MSC_VER
23
24 #  if defined(HAVE_CURSES_H)
25 #    include <curses.h>
26 #  elif defined(HAVE_NCURSES_H)
27 #    include <ncurses.h>
28 #  endif
29
30
31 #ifdef HAVE_TERMCAP_H
32 #include <termcap.h>
33 #endif
34
35 #ifndef HAVE_TERMCAP_H
36 #ifdef HAVE_TERM_H
37 #include <term.h>
38 #endif
39 #endif
40
41 #endif /* !defined(_MSC_VER) */
42
43 #include "scilines.h"
44 #include "core_math.h"
45 #include "stack-def.h"
46 #ifdef _MSC_VER
47 #include "../../../windows_tools/src/c/scilab_windows/console.h"
48 #include "sci_mode.h"
49 #endif
50 /*--------------------------------------------------------------------------*/
51 #define DEFAULT_NUMBERS_LINES 28
52 #define DEFAULT_NUMBERS_COLUMNS 80
53 #define MIN_NUMBERS_LINES 0
54 #define MIN_NUMBERS_COLUMNS 10
55 /*--------------------------------------------------------------------------*/
56 int scilines(int nblines, int nbcolumns)
57 {
58         setLinesSize(nblines);
59         setColumnsSize(nbcolumns);
60         return 0;
61 }
62 /*--------------------------------------------------------------------------*/
63 int scilinesdefault(void)
64 {
65 #ifndef _MSC_VER
66
67         char tc_buf[1024];       /* holds termcap buffer */
68         if(tgetent(tc_buf, getenv("TERM")) == 1) {
69                 setLinesSize(tgetnum("li")); /* retrieve from the term info the number
70                                                                                 of lines */
71                 setColumnsSize(tgetnum("co")); /* And the number of columns */
72         }else{
73                 /* Haven't been able to detect the terminal */
74                 setLinesSize(DEFAULT_NUMBERS_LINES);
75                 setColumnsSize(DEFAULT_NUMBERS_COLUMNS);
76         }
77         
78 #else
79         if ( (getScilabMode() != SCILAB_STD) && (getScilabMode() != SCILAB_API) )
80         {
81                 /* -NW or -NWNI mode */
82                 int X = getXConsoleScreenSize();
83                 int Y = getYConsoleScreenSize();
84
85                 if (X < DEFAULT_NUMBERS_COLUMNS) X = DEFAULT_NUMBERS_COLUMNS;
86                 if (Y < DEFAULT_NUMBERS_LINES) Y = DEFAULT_NUMBERS_LINES;
87                 setColumnsSize(X);
88                 setLinesSize(Y);
89         }
90         else
91         {
92                 setLinesSize(DEFAULT_NUMBERS_LINES);
93                 setColumnsSize(DEFAULT_NUMBERS_COLUMNS);
94         }
95 #endif
96        return 0;
97 }
98 /*--------------------------------------------------------------------------*/
99 int C2F(scilines)(int *nblines, int *nbcolumns)
100 {
101         return scilines(*nblines,*nbcolumns);
102 }
103 /*--------------------------------------------------------------------------*/
104 int C2F(scilinesdefault)(void)
105 {
106         return scilinesdefault();
107 }
108 /*--------------------------------------------------------------------------*/
109 BOOL setColumnsSize(int colums)
110 {
111         C2F(iop).lct[4] = Max(MIN_NUMBERS_COLUMNS,colums);
112         return TRUE;
113 }
114 /*--------------------------------------------------------------------------*/
115 BOOL setLinesSize(int lines_)
116 {
117         C2F(iop).lct[1] = Max(MIN_NUMBERS_LINES,lines_);
118         return TRUE;
119 }
120 /*--------------------------------------------------------------------------*/
121 int getColumnsSize(void)
122 {
123         return C2F(iop).lct[4];
124 }
125 /*--------------------------------------------------------------------------*/
126 int getLinesSize(void)
127 {
128         return C2F(iop).lct[1];
129 }
130 /*--------------------------------------------------------------------------*/