textbox.c 9.0 KB

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