textbox.c 8.3 KB

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