textbox.c 8.9 KB

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