File src/curse_utils.cxx added (mode: 100644) (index 0000000..39eeea6) |
|
1 |
|
/*************************************************************************** |
|
2 |
|
* Pinfo is a ncurses based lynx style info documentation browser |
|
3 |
|
* |
|
4 |
|
* Copyright (C) 1999 Przemek Borys <pborys@dione.ids.pl> |
|
5 |
|
* Copyright (C) 2005 Bas Zoetekouw <bas@debian.org> |
|
6 |
|
* Copyright 2005 Nathanael Nerode <neroden@gcc.gnu.org> |
|
7 |
|
* |
|
8 |
|
* This program is free software; you can redistribute it and/or modify |
|
9 |
|
* it under the terms of version 2 of the GNU General Public License as |
|
10 |
|
* published by the Free Software Foundation. |
|
11 |
|
* |
|
12 |
|
* This program is distributed in the hope that it will be useful, but |
|
13 |
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 |
|
* General Public License for more details. |
|
16 |
|
* |
|
17 |
|
* You should have received a copy of the GNU General Public License |
|
18 |
|
* along with this program; if not, write to the Free Software |
|
19 |
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
|
20 |
|
* USA |
|
21 |
|
***************************************************************************/ |
|
22 |
|
|
|
23 |
|
#include "common_includes.h" |
|
24 |
|
|
|
25 |
|
#include <string> |
|
26 |
|
using std::string; |
|
27 |
|
|
|
28 |
|
#include <clocale> // for setlocale |
|
29 |
|
|
|
30 |
|
#include "colors.h" |
|
31 |
|
#include "datatypes.h" |
|
32 |
|
#include "keyboard.h" |
|
33 |
|
#include "tmpfiles.h" |
|
34 |
|
|
|
35 |
|
/* Readline */ |
|
36 |
|
#include <readline/readline.h> |
|
37 |
|
#include <readline/history.h> |
|
38 |
|
#include <term.h> |
|
39 |
|
|
|
40 |
|
#ifndef HAVE_CURS_SET |
|
41 |
|
void |
|
42 |
|
curs_set(int a) |
|
43 |
|
{ |
|
44 |
|
} |
|
45 |
|
#endif |
|
46 |
|
|
|
47 |
|
int curses_open = 0; |
|
48 |
|
|
|
49 |
|
int shell_cursor = 1; |
|
50 |
|
|
|
51 |
|
void |
|
52 |
|
initlocale() |
|
53 |
|
{ |
|
54 |
|
#ifdef HAVE_SETLOCALE |
|
55 |
|
std::setlocale(LC_ALL, ""); |
|
56 |
|
#endif |
|
57 |
|
bindtextdomain(PACKAGE, LOCALEDIR); |
|
58 |
|
textdomain(PACKAGE); |
|
59 |
|
} |
|
60 |
|
|
|
61 |
|
void |
|
62 |
|
mymvhline(int y, int x, char ch, int len) |
|
63 |
|
{ |
|
64 |
|
int i; |
|
65 |
|
for (i = 0; i < len; i++) |
|
66 |
|
mvaddch(y, x + i, ch); |
|
67 |
|
} |
|
68 |
|
|
|
69 |
|
/* custom function that readline will use to display text */ |
|
70 |
|
void |
|
71 |
|
my_rl_display() |
|
72 |
|
{ |
|
73 |
|
/* go to the bottom line, empty it, and print the prompt and buffer */ |
|
74 |
|
attrset(bottomline); |
|
75 |
|
mymvhline(maxy - 1, 0, ' ', maxx); |
|
76 |
|
move(maxy-1,0); |
|
77 |
|
printw("%s%s", rl_prompt, rl_line_buffer); |
|
78 |
|
refresh(); |
|
79 |
|
} |
|
80 |
|
|
|
81 |
|
string |
|
82 |
|
getstring(const char *prompt) |
|
83 |
|
{ |
|
84 |
|
char *buf; |
|
85 |
|
|
|
86 |
|
curs_set(1); |
|
87 |
|
move(maxy - 1, 0); |
|
88 |
|
refresh(); |
|
89 |
|
|
|
90 |
|
rl_readline_name = PACKAGE; |
|
91 |
|
|
|
92 |
|
/* set display function for readline to my_rl_display and call readline */ |
|
93 |
|
rl_redisplay_function = my_rl_display; |
|
94 |
|
buf = readline(prompt); |
|
95 |
|
if (buf && *buf) |
|
96 |
|
add_history(buf); |
|
97 |
|
|
|
98 |
|
curs_set(0); |
|
99 |
|
|
|
100 |
|
string my_string; |
|
101 |
|
if (buf == NULL) { |
|
102 |
|
my_string = ""; |
|
103 |
|
} else { |
|
104 |
|
my_string = buf; |
|
105 |
|
free(buf); |
|
106 |
|
} |
|
107 |
|
return my_string; |
|
108 |
|
} |
|
109 |
|
|
|
110 |
|
void |
|
111 |
|
init_curses() |
|
112 |
|
{ |
|
113 |
|
initscr(); |
|
114 |
|
noecho(); |
|
115 |
|
cbreak(); |
|
116 |
|
keypad(stdscr, TRUE); |
|
117 |
|
/* meta(stdscr, TRUE); */ |
|
118 |
|
initcolors(); |
|
119 |
|
shell_cursor = curs_set(0); |
|
120 |
|
#ifdef NCURSES_MOUSE_VERSION |
|
121 |
|
mousemask(BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED, NULL); |
|
122 |
|
/* NCURSES_MOUSE_VERSION */ |
|
123 |
|
#endif |
|
124 |
|
curses_open = 1; |
|
125 |
|
} |
|
126 |
|
|
|
127 |
|
int |
|
128 |
|
pinfo_getch() |
|
129 |
|
{ |
|
130 |
|
int key = getch(); |
|
131 |
|
/* following key will be alt's value */ |
|
132 |
|
if (key == META_KEY) |
|
133 |
|
{ |
|
134 |
|
key = getch(); |
|
135 |
|
key |= 0x200; |
|
136 |
|
} |
|
137 |
|
return key; |
|
138 |
|
} |
|
139 |
|
|
|
140 |
|
void |
|
141 |
|
waitforgetch() |
|
142 |
|
{ |
|
143 |
|
fd_set rdfs; |
|
144 |
|
FD_ZERO(&rdfs); |
|
145 |
|
FD_SET(0, &rdfs); |
|
146 |
|
select(1, &rdfs, NULL, NULL, NULL); |
|
147 |
|
} |
|
148 |
|
|
|
149 |
|
int |
|
150 |
|
yesno(const char *prompt, int def) |
|
151 |
|
{ |
|
152 |
|
const char *yes = _("yes"); |
|
153 |
|
const char *no = _("no"); |
|
154 |
|
int key; |
|
155 |
|
|
|
156 |
|
attrset(bottomline); |
|
157 |
|
mymvhline(maxy - 1, 0, ' ', maxx); |
|
158 |
|
move(maxy - 1, 0); |
|
159 |
|
/* if default answer is yes */ |
|
160 |
|
if (def) |
|
161 |
|
printw("%s([%c]/%c)", prompt, *yes, *no); |
|
162 |
|
else |
|
163 |
|
printw("%s([%c]/%c)", prompt, *no, *yes); |
|
164 |
|
nodelay(stdscr, FALSE); |
|
165 |
|
while (1) |
|
166 |
|
{ |
|
167 |
|
key = getch(); |
|
168 |
|
if (key == ERR) |
|
169 |
|
return -1; |
|
170 |
|
if (is_enter_key(key)) |
|
171 |
|
break; |
|
172 |
|
else |
|
173 |
|
{ |
|
174 |
|
if (tolower(key) == tolower(*yes)) |
|
175 |
|
{ |
|
176 |
|
def = 1; |
|
177 |
|
break; |
|
178 |
|
} |
|
179 |
|
else |
|
180 |
|
{ |
|
181 |
|
if (tolower(key) == tolower(*no)) |
|
182 |
|
{ |
|
183 |
|
def = 0; |
|
184 |
|
break; |
|
185 |
|
} |
|
186 |
|
else |
|
187 |
|
beep(); |
|
188 |
|
} |
|
189 |
|
} |
|
190 |
|
} |
|
191 |
|
|
|
192 |
|
nodelay(stdscr, TRUE); |
|
193 |
|
if (def) |
|
194 |
|
addstr(yes); |
|
195 |
|
else |
|
196 |
|
addstr(no); |
|
197 |
|
attrset(normal); |
|
198 |
|
return def; |
|
199 |
|
} |
|
200 |
|
|
|
201 |
|
void |
|
202 |
|
myclrtoeol() |
|
203 |
|
{ |
|
204 |
|
int x, y, i; |
|
205 |
|
getyx(stdscr, y, x); |
|
206 |
|
for (i = x; i < maxx; i++) |
|
207 |
|
mvaddch(y, i, ' '); |
|
208 |
|
} |
|
209 |
|
|
|
210 |
|
void |
|
211 |
|
myendwin() |
|
212 |
|
{ |
|
213 |
|
curs_set(shell_cursor); |
|
214 |
|
endwin(); |
|
215 |
|
} |
|
216 |
|
|
|
217 |
|
void |
|
218 |
|
handlewinch() |
|
219 |
|
{ |
|
220 |
|
myendwin(); |
|
221 |
|
init_curses(); |
|
222 |
|
doupdate(); |
|
223 |
|
getmaxyx(stdscr, maxy, maxx); |
|
224 |
|
ungetch(keys.refresh_1); |
|
225 |
|
} |
|
226 |
|
|
|
227 |
|
void |
|
228 |
|
closeprogram() |
|
229 |
|
{ |
|
230 |
|
if (curses_open) |
|
231 |
|
myendwin(); |
|
232 |
|
if (ClearScreenAtExit) |
|
233 |
|
system("clear"); |
|
234 |
|
else |
|
235 |
|
printf("\n"); |
|
236 |
|
rmtmpfiles(); |
|
237 |
|
} |
File src/curse_utils.h copied from file src/manual.h (similarity 53%) (mode: 100644) (index c1c947b..610b3d7) |
20 |
20 |
* USA |
* USA |
21 |
21 |
***************************************************************************/ |
***************************************************************************/ |
22 |
22 |
|
|
23 |
|
#ifndef __MANUAL_H |
|
24 |
|
#define __MANUAL_H |
|
|
23 |
|
#ifndef __CURSE_UTILS_H |
|
24 |
|
#define __CURSE_UTILS_H |
|
25 |
|
|
25 |
26 |
#include <string> |
#include <string> |
26 |
27 |
|
|
27 |
|
/* passes control to the manual code */ |
|
28 |
|
int handlemanual (const std::string &); |
|
|
28 |
|
#ifndef HAVE_CURS_SET |
|
29 |
|
void curs_set (int a); |
|
30 |
|
#endif |
|
31 |
|
|
|
32 |
|
/* initializes GNU locales */ |
|
33 |
|
void initlocale (); |
|
34 |
|
/* closes the program, and removes temporary files */ |
|
35 |
|
void closeprogram (); |
|
36 |
|
|
|
37 |
|
/* is curses screen open? */ |
|
38 |
|
extern int curses_open; |
|
39 |
|
|
|
40 |
|
/* initializes curses interface */ |
|
41 |
|
void init_curses (); |
|
42 |
|
/* user defined getch, capable of handling ALT keybindings */ |
|
43 |
|
int pinfo_getch (); |
|
44 |
|
/* Block until something's on STDIN */ |
|
45 |
|
void waitforgetch (); |
|
46 |
|
/* an interface to gnu readline */ |
|
47 |
|
std::string getstring (const char *prompt); |
|
48 |
|
/* for some reasons mvhline does not work quite properly... */ |
|
49 |
|
void mymvhline (int y, int x, char ch, int len); |
|
50 |
|
/* this one supports color back/foreground */ |
|
51 |
|
void myclrtoeol (); |
|
52 |
|
/* takes care of the cursor, which is turned off */ |
|
53 |
|
void myendwin (); |
|
54 |
|
|
|
55 |
|
/* handle localized `(y/n)' dialog box. */ |
|
56 |
|
int yesno (const char *prompt, int def); |
|
57 |
|
|
|
58 |
|
/* Handle SIGWINCH */ |
|
59 |
|
void handlewinch (); |
29 |
60 |
|
|
30 |
61 |
#endif |
#endif |