Merge 15329:15546 from trunk
[reactos.git] / rosapps / mc / pc / slint_pc.c
1 /* Slang interface to the Midnight Commander for Windows NT and OS/2
2 This emulates some features of ncurses on top of slang
3 S-lang is not fully consistent between its Unix and non-Unix versions.
4
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include "../src/tty.h"
23 #include "../src/mad.h"
24 #include "../src/color.h"
25 #include "../src/util.h"
26 #include "../src/mouse.h" /* Gpm_Event is required in key.h */
27 #include "../src/key.h" /* define_sequence */
28 #include "../src/main.h" /* extern: force_colors */
29 #include "../src/win.h" /* do_exit_ca_mode */
30
31 #ifdef HAVE_SLANG
32
33
34 static void slang_sigterm ()
35 {
36 SLsmg_reset_smg ();
37 }
38
39 static int slinterrupt;
40
41 void enable_interrupt_key(void)
42 {
43 SLang_set_abort_signal(NULL);
44 slinterrupt = 1;
45 }
46 void disable_interrupt_key(void)
47 {
48 slinterrupt = 0;
49 }
50 int got_interrupt ()
51 {
52 int t;
53 int SLKeyboard_Quit=0; /* FIXME!! */
54 t = slinterrupt ? SLKeyboard_Quit : 0;
55 /* SLKeyboard_Quit = 0; */
56 return t;
57 }
58
59 /* Only done the first time */
60 void slang_init (void)
61 {
62 SLtt_get_terminfo ();
63 SLang_init_tty (XCTRL('c'), 1, 0);
64 slang_prog_mode ();
65 load_terminfo_keys ();
66 }
67
68 /* Done each time we come back from done mode */
69 void slang_prog_mode (void)
70 {
71 SLsmg_init_smg ();
72 SLsmg_touch_lines (0, LINES);
73 }
74
75 /* Called each time we want to shutdown slang screen manager */
76 void slang_shell_mode (void)
77 {
78
79 }
80
81 void slang_shutdown ()
82 {
83 slang_shell_mode ();
84 do_exit_ca_mode ();
85 SLang_reset_tty ();
86
87 /* reset the colors to those that were
88 * active when the program was started up
89 (not written)
90 */
91 }
92
93 /* keypad routines */
94 void slang_keypad (int set)
95 {
96 /* enable keypad strings */
97 }
98
99 static int no_slang_delay;
100
101 void set_slang_delay (int v)
102 {
103 no_slang_delay = v;
104 }
105
106 void hline (int ch, int len)
107 {
108 int last_x, last_y;
109
110 last_x = SLsmg_get_column ();
111 last_y = SLsmg_get_row ();
112
113 if (ch == 0)
114 ch = ACS_HLINE;
115
116 if (ch == ACS_HLINE){
117 SLsmg_draw_hline (len);
118 } else {
119 while (len--)
120 addch (ch);
121 }
122 move (last_y, last_x);
123 }
124
125 void vline (int character, int len)
126 {
127 if (!slow_terminal){
128 SLsmg_draw_vline (len);
129 } else {
130 int last_x, last_y, pos = 0;
131
132 last_x = SLsmg_get_column ();
133 last_y = SLsmg_get_row ();
134
135 while (len--){
136 move (last_y + pos++, last_x);
137 addch (' ');
138 }
139 move (last_x, last_y);
140 }
141 }
142
143 int has_colors ()
144 {
145 /* No terminals on NT, make default color */
146 if (!disable_colors)
147 SLtt_Use_Ansi_Colors = 1;
148
149 /* Setup emulated colors */
150 if (SLtt_Use_Ansi_Colors){
151 /* DO NOT TRANSLATE WITH gettext SYNTAX coloring will be broken */
152 init_pair (A_REVERSE, "black", "white");
153 } else {
154 /* SLtt_set_mono (A_BOLD, NULL, SLTT_BOLD_MASK);
155 SLtt_set_mono (A_REVERSE, NULL, SLTT_REV_MASK);
156 SLtt_set_mono (A_BOLD|A_REVERSE, NULL, SLTT_BOLD_MASK | SLTT_REV_MASK);
157 */ }
158 return SLtt_Use_Ansi_Colors;
159 }
160
161 void attrset (int color)
162 {
163 if (!SLtt_Use_Ansi_Colors){
164 SLsmg_set_color (color);
165 return;
166 }
167
168 if (color & A_BOLD){
169 if (color == A_BOLD)
170 SLsmg_set_color (A_BOLD);
171 else
172 SLsmg_set_color ((color & (~A_BOLD)) + 8);
173 return;
174 }
175
176 if (color == A_REVERSE)
177 SLsmg_set_color (A_REVERSE);
178 else
179 SLsmg_set_color (color);
180 }
181
182 void load_terminfo_keys ()
183 {
184 }
185
186 int getch ()
187 {
188 if (no_slang_delay)
189 if (SLang_input_pending (0) == 0)
190 return -1;
191
192 return SLang_getkey ();
193 }
194
195 extern int slow_terminal;
196
197 #else
198
199 /* Non slang builds do not understand got_interrupt */
200 int got_interrupt ()
201 {
202 return 0;
203 }
204 #endif /* HAVE_SLANG */
205
206 void mc_refresh (void)
207 {
208 /* if (!we_are_background) (no background mode yet) */
209 refresh ();
210 }
211
212 void slang_set_raw_mode (void)
213 {
214 return;
215 }
216
217 int max_index = 0;
218
219 void
220 init_pair (int index, char *foreground, char *background)
221 {
222
223 SLtt_set_color (index, "", foreground, background);
224 if (index > max_index)
225 max_index = index;
226 }
227
228 int
229 alloc_color_pair (char *foreground, char *background)
230 {
231 init_pair (++max_index, foreground, background);
232 return max_index;
233 }
234
235 int
236 try_alloc_color_pair (char *fg, char *bg)
237 {
238 static struct colors_avail {
239 struct colors_avail *next;
240 char *fg, *bg;
241 int index;
242 } *p, c =
243 {
244 0, 0, 0, 0
245 };
246
247 c.index = NORMAL_COLOR;
248 p = &c;
249 for (;;) {
250 if (((fg && p->fg) ? !strcmp (fg, p->fg) : fg == p->fg) != 0
251 && ((bg && p->bg) ? !strcmp (bg, p->bg) : bg == p->bg) != 0)
252 return p->index;
253 if (!p->next)
254 break;
255 p = p->next;
256 }
257 p->next = malloc (sizeof (c));
258 p = p->next;
259 p->next = 0;
260 p->fg = fg ? strdup (fg) : 0;
261 p->bg = bg ? strdup (bg) : 0;
262 /* DO NOT TRANSLATE WITH gettext SYNTAX coloring will be broken */
263 if (!fg)
264 fg = "white";
265 if (!bg)
266 bg = "blue";
267 p->index = alloc_color_pair (fg, bg);
268 return p->index;
269 }