textbox.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * textbox.c -- implements the text box
  3. *
  4. * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5. * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include "dialog.h"
  22. static void back_lines(int n);
  23. static void print_page(WINDOW * win, int height, int width);
  24. static void print_line(WINDOW * win, int row, int width);
  25. static char *get_line(void);
  26. static void print_position(WINDOW * win);
  27. static int hscroll;
  28. static int begin_reached, end_reached, page_length;
  29. static const char *buf;
  30. static const char *page;
  31. /*
  32. * refresh window content
  33. */
  34. static void refresh_text_box(WINDOW *dialog, WINDOW *box, int boxh, int boxw,
  35. int cur_y, int cur_x)
  36. {
  37. print_page(box, boxh, boxw);
  38. print_position(dialog);
  39. wmove(dialog, cur_y, cur_x); /* Restore cursor position */
  40. wrefresh(dialog);
  41. }
  42. /*
  43. * Display text from a file in a dialog box.
  44. *
  45. * keys is a null-terminated array
  46. */
  47. int dialog_textbox(const char *title, const char *tbuf, int initial_height,
  48. int initial_width, int *keys, int *_vscroll, int *_hscroll)
  49. {
  50. int i, x, y, cur_x, cur_y, key = 0;
  51. int height, width, boxh, boxw;
  52. WINDOW *dialog, *box;
  53. bool done = false;
  54. begin_reached = 1;
  55. end_reached = 0;
  56. page_length = 0;
  57. hscroll = 0;
  58. buf = tbuf;
  59. page = buf; /* page is pointer to start of page to be displayed */
  60. if (_vscroll && *_vscroll) {
  61. begin_reached = 0;
  62. for (i = 0; i < *_vscroll; i++)
  63. get_line();
  64. }
  65. if (_hscroll)
  66. hscroll = *_hscroll;
  67. do_resize:
  68. getmaxyx(stdscr, height, width);
  69. if (height < 8 || width < 8)
  70. return -ERRDISPLAYTOOSMALL;
  71. if (initial_height != 0)
  72. height = initial_height;
  73. else
  74. if (height > 4)
  75. height -= 4;
  76. else
  77. height = 0;
  78. if (initial_width != 0)
  79. width = initial_width;
  80. else
  81. if (width > 5)
  82. width -= 5;
  83. else
  84. width = 0;
  85. /* center dialog box on screen */
  86. x = (COLS - width) / 2;
  87. y = (LINES - height) / 2;
  88. draw_shadow(stdscr, y, x, height, width);
  89. dialog = newwin(height, width, y, x);
  90. keypad(dialog, TRUE);
  91. /* Create window for box region, used for scrolling text */
  92. boxh = height - 4;
  93. boxw = width - 2;
  94. box = subwin(dialog, boxh, boxw, y + 1, x + 1);
  95. wattrset(box, dlg.dialog.atr);
  96. wbkgdset(box, dlg.dialog.atr & A_COLOR);
  97. keypad(box, TRUE);
  98. /* register the new window, along with its borders */
  99. draw_box(dialog, 0, 0, height, width,
  100. dlg.dialog.atr, dlg.border.atr);
  101. wattrset(dialog, dlg.border.atr);
  102. mvwaddch(dialog, height - 3, 0, ACS_LTEE);
  103. for (i = 0; i < width - 2; i++)
  104. waddch(dialog, ACS_HLINE);
  105. wattrset(dialog, dlg.dialog.atr);
  106. wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
  107. waddch(dialog, ACS_RTEE);
  108. print_title(dialog, title, width);
  109. print_button(dialog, gettext(" Exit "), height - 2, width / 2 - 4, TRUE);
  110. wnoutrefresh(dialog);
  111. getyx(dialog, cur_y, cur_x); /* Save cursor position */
  112. /* Print first page of text */
  113. attr_clear(box, boxh, boxw, dlg.dialog.atr);
  114. refresh_text_box(dialog, box, boxh, boxw, cur_y, cur_x);
  115. while (!done) {
  116. key = wgetch(dialog);
  117. switch (key) {
  118. case 'E': /* Exit */
  119. case 'e':
  120. case 'X':
  121. case 'x':
  122. case 'q':
  123. case '\n':
  124. done = true;
  125. break;
  126. case 'g': /* First page */
  127. case KEY_HOME:
  128. if (!begin_reached) {
  129. begin_reached = 1;
  130. page = buf;
  131. refresh_text_box(dialog, box, boxh, boxw,
  132. cur_y, cur_x);
  133. }
  134. break;
  135. case 'G': /* Last page */
  136. case KEY_END:
  137. end_reached = 1;
  138. /* point to last char in buf */
  139. page = buf + strlen(buf);
  140. back_lines(boxh);
  141. refresh_text_box(dialog, box, boxh, boxw,
  142. cur_y, cur_x);
  143. break;
  144. case 'K': /* Previous line */
  145. case 'k':
  146. case KEY_UP:
  147. if (!begin_reached) {
  148. int passed_end = 0;
  149. back_lines(page_length + 1);
  150. /* We don't call print_page() here but use
  151. * scrolling to ensure faster screen update.
  152. * However, 'end_reached' and 'page_length'
  153. * should still be updated, and 'page' should
  154. * point to start of next page. This is done
  155. * by calling get_line() in the following
  156. * 'for' loop. */
  157. scrollok(box, TRUE);
  158. wscrl(box, -1); /* Scroll box region down one line */
  159. scrollok(box, FALSE);
  160. page_length = 0;
  161. for (i = 0; i < boxh; i++) {
  162. if (!i) {
  163. /* print first line of page */
  164. print_line(box, 0, boxw);
  165. wnoutrefresh(box);
  166. } else
  167. /* Called to update 'end_reached' and 'page' */
  168. get_line();
  169. if (!passed_end)
  170. page_length++;
  171. if (end_reached && !passed_end)
  172. passed_end = 1;
  173. }
  174. print_position(dialog);
  175. wmove(dialog, cur_y, cur_x); /* Restore cursor position */
  176. wrefresh(dialog);
  177. }
  178. break;
  179. case 'B': /* Previous page */
  180. case 'b':
  181. case 'u':
  182. case KEY_PPAGE:
  183. if (begin_reached)
  184. break;
  185. back_lines(page_length + boxh);
  186. refresh_text_box(dialog, box, boxh, boxw,
  187. cur_y, cur_x);
  188. break;
  189. case 'J': /* Next line */
  190. case 'j':
  191. case KEY_DOWN:
  192. if (!end_reached) {
  193. begin_reached = 0;
  194. scrollok(box, TRUE);
  195. scroll(box); /* Scroll box region up one line */
  196. scrollok(box, FALSE);
  197. print_line(box, boxh - 1, boxw);
  198. wnoutrefresh(box);
  199. print_position(dialog);
  200. wmove(dialog, cur_y, cur_x); /* Restore cursor position */
  201. wrefresh(dialog);
  202. }
  203. break;
  204. case KEY_NPAGE: /* Next page */
  205. case ' ':
  206. case 'd':
  207. if (end_reached)
  208. break;
  209. begin_reached = 0;
  210. refresh_text_box(dialog, box, boxh, boxw,
  211. cur_y, cur_x);
  212. break;
  213. case '0': /* Beginning of line */
  214. case 'H': /* Scroll left */
  215. case 'h':
  216. case KEY_LEFT:
  217. if (hscroll <= 0)
  218. break;
  219. if (key == '0')
  220. hscroll = 0;
  221. else
  222. hscroll--;
  223. /* Reprint current page to scroll horizontally */
  224. back_lines(page_length);
  225. refresh_text_box(dialog, box, boxh, boxw,
  226. cur_y, cur_x);
  227. break;
  228. case 'L': /* Scroll right */
  229. case 'l':
  230. case KEY_RIGHT:
  231. if (hscroll >= MAX_LEN)
  232. break;
  233. hscroll++;
  234. /* Reprint current page to scroll horizontally */
  235. back_lines(page_length);
  236. refresh_text_box(dialog, box, boxh, boxw,
  237. cur_y, cur_x);
  238. break;
  239. case KEY_ESC:
  240. if (on_key_esc(dialog) == KEY_ESC)
  241. done = true;
  242. break;
  243. case KEY_RESIZE:
  244. back_lines(height);
  245. delwin(box);
  246. delwin(dialog);
  247. on_key_resize();
  248. goto do_resize;
  249. default:
  250. for (i = 0; keys[i]; i++) {
  251. if (key == keys[i]) {
  252. done = true;
  253. break;
  254. }
  255. }
  256. }
  257. }
  258. delwin(box);
  259. delwin(dialog);
  260. if (_vscroll) {
  261. const char *s;
  262. s = buf;
  263. *_vscroll = 0;
  264. back_lines(page_length);
  265. while (s < page && (s = strchr(s, '\n'))) {
  266. (*_vscroll)++;
  267. s++;
  268. }
  269. }
  270. if (_hscroll)
  271. *_hscroll = hscroll;
  272. return key;
  273. }
  274. /*
  275. * Go back 'n' lines in text. Called by dialog_textbox().
  276. * 'page' will be updated to point to the desired line in 'buf'.
  277. */
  278. static void back_lines(int n)
  279. {
  280. int i;
  281. begin_reached = 0;
  282. /* Go back 'n' lines */
  283. for (i = 0; i < n; i++) {
  284. if (*page == '\0') {
  285. if (end_reached) {
  286. end_reached = 0;
  287. continue;
  288. }
  289. }
  290. if (page == buf) {
  291. begin_reached = 1;
  292. return;
  293. }
  294. page--;
  295. do {
  296. if (page == buf) {
  297. begin_reached = 1;
  298. return;
  299. }
  300. page--;
  301. } while (*page != '\n');
  302. page++;
  303. }
  304. }
  305. /*
  306. * Print a new page of text. Called by dialog_textbox().
  307. */
  308. static void print_page(WINDOW * win, int height, int width)
  309. {
  310. int i, passed_end = 0;
  311. page_length = 0;
  312. for (i = 0; i < height; i++) {
  313. print_line(win, i, width);
  314. if (!passed_end)
  315. page_length++;
  316. if (end_reached && !passed_end)
  317. passed_end = 1;
  318. }
  319. wnoutrefresh(win);
  320. }
  321. /*
  322. * Print a new line of text. Called by dialog_textbox() and print_page().
  323. */
  324. static void print_line(WINDOW * win, int row, int width)
  325. {
  326. char *line;
  327. line = get_line();
  328. line += MIN(strlen(line), hscroll); /* Scroll horizontally */
  329. wmove(win, row, 0); /* move cursor to correct line */
  330. waddch(win, ' ');
  331. waddnstr(win, line, MIN(strlen(line), width - 2));
  332. /* Clear 'residue' of previous line */
  333. #if OLD_NCURSES
  334. {
  335. int x = getcurx(win);
  336. int i;
  337. for (i = 0; i < width - x; i++)
  338. waddch(win, ' ');
  339. }
  340. #else
  341. wclrtoeol(win);
  342. #endif
  343. }
  344. /*
  345. * Return current line of text. Called by dialog_textbox() and print_line().
  346. * 'page' should point to start of current line before calling, and will be
  347. * updated to point to start of next line.
  348. */
  349. static char *get_line(void)
  350. {
  351. int i = 0;
  352. static char line[MAX_LEN + 1];
  353. end_reached = 0;
  354. while (*page != '\n') {
  355. if (*page == '\0') {
  356. end_reached = 1;
  357. break;
  358. } else if (i < MAX_LEN)
  359. line[i++] = *(page++);
  360. else {
  361. /* Truncate lines longer than MAX_LEN characters */
  362. if (i == MAX_LEN)
  363. line[i++] = '\0';
  364. page++;
  365. }
  366. }
  367. if (i <= MAX_LEN)
  368. line[i] = '\0';
  369. if (!end_reached)
  370. page++; /* move past '\n' */
  371. return line;
  372. }
  373. /*
  374. * Print current position
  375. */
  376. static void print_position(WINDOW * win)
  377. {
  378. int percent;
  379. wattrset(win, dlg.position_indicator.atr);
  380. wbkgdset(win, dlg.position_indicator.atr & A_COLOR);
  381. percent = (page - buf) * 100 / strlen(buf);
  382. wmove(win, getmaxy(win) - 3, getmaxx(win) - 9);
  383. wprintw(win, "(%3d%%)", percent);
  384. }