textbox.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. delwin(box);
  114. delwin(dialog);
  115. return 0;
  116. case 'g': /* First page */
  117. case KEY_HOME:
  118. if (!begin_reached) {
  119. begin_reached = 1;
  120. page = buf;
  121. refresh_text_box(dialog, box, boxh, boxw,
  122. cur_y, cur_x);
  123. }
  124. break;
  125. case 'G': /* Last page */
  126. case KEY_END:
  127. end_reached = 1;
  128. /* point to last char in buf */
  129. page = buf + strlen(buf);
  130. back_lines(boxh);
  131. refresh_text_box(dialog, box, boxh, boxw,
  132. cur_y, cur_x);
  133. break;
  134. case 'K': /* Previous line */
  135. case 'k':
  136. case KEY_UP:
  137. if (!begin_reached) {
  138. back_lines(page_length + 1);
  139. /* We don't call print_page() here but use
  140. * scrolling to ensure faster screen update.
  141. * However, 'end_reached' and 'page_length'
  142. * should still be updated, and 'page' should
  143. * point to start of next page. This is done
  144. * by calling get_line() in the following
  145. * 'for' loop. */
  146. scrollok(box, TRUE);
  147. wscrl(box, -1); /* Scroll box region down one line */
  148. scrollok(box, FALSE);
  149. page_length = 0;
  150. passed_end = 0;
  151. for (i = 0; i < boxh; i++) {
  152. if (!i) {
  153. /* print first line of page */
  154. print_line(box, 0, boxw);
  155. wnoutrefresh(box);
  156. } else
  157. /* Called to update 'end_reached' and 'page' */
  158. get_line();
  159. if (!passed_end)
  160. page_length++;
  161. if (end_reached && !passed_end)
  162. passed_end = 1;
  163. }
  164. print_position(dialog);
  165. wmove(dialog, cur_y, cur_x); /* Restore cursor position */
  166. wrefresh(dialog);
  167. }
  168. break;
  169. case 'B': /* Previous page */
  170. case 'b':
  171. case KEY_PPAGE:
  172. if (begin_reached)
  173. break;
  174. back_lines(page_length + boxh);
  175. refresh_text_box(dialog, box, boxh, boxw,
  176. cur_y, cur_x);
  177. break;
  178. case 'J': /* Next line */
  179. case 'j':
  180. case KEY_DOWN:
  181. if (!end_reached) {
  182. begin_reached = 0;
  183. scrollok(box, TRUE);
  184. scroll(box); /* Scroll box region up one line */
  185. scrollok(box, FALSE);
  186. print_line(box, boxh - 1, boxw);
  187. wnoutrefresh(box);
  188. print_position(dialog);
  189. wmove(dialog, cur_y, cur_x); /* Restore cursor position */
  190. wrefresh(dialog);
  191. }
  192. break;
  193. case KEY_NPAGE: /* Next page */
  194. case ' ':
  195. if (end_reached)
  196. break;
  197. begin_reached = 0;
  198. refresh_text_box(dialog, box, boxh, boxw,
  199. cur_y, cur_x);
  200. break;
  201. case '0': /* Beginning of line */
  202. case 'H': /* Scroll left */
  203. case 'h':
  204. case KEY_LEFT:
  205. if (hscroll <= 0)
  206. break;
  207. if (key == '0')
  208. hscroll = 0;
  209. else
  210. hscroll--;
  211. /* Reprint current page to scroll horizontally */
  212. back_lines(page_length);
  213. refresh_text_box(dialog, box, boxh, boxw,
  214. cur_y, cur_x);
  215. break;
  216. case 'L': /* Scroll right */
  217. case 'l':
  218. case KEY_RIGHT:
  219. if (hscroll >= MAX_LEN)
  220. break;
  221. hscroll++;
  222. /* Reprint current page to scroll horizontally */
  223. back_lines(page_length);
  224. refresh_text_box(dialog, box, boxh, boxw,
  225. cur_y, cur_x);
  226. break;
  227. case KEY_ESC:
  228. key = on_key_esc(dialog);
  229. break;
  230. case KEY_RESIZE:
  231. back_lines(height);
  232. delwin(box);
  233. delwin(dialog);
  234. on_key_resize();
  235. goto do_resize;
  236. }
  237. }
  238. delwin(box);
  239. delwin(dialog);
  240. return key; /* ESC pressed */
  241. }
  242. /*
  243. * Go back 'n' lines in text. Called by dialog_textbox().
  244. * 'page' will be updated to point to the desired line in 'buf'.
  245. */
  246. static void back_lines(int n)
  247. {
  248. int i;
  249. begin_reached = 0;
  250. /* Go back 'n' lines */
  251. for (i = 0; i < n; i++) {
  252. if (*page == '\0') {
  253. if (end_reached) {
  254. end_reached = 0;
  255. continue;
  256. }
  257. }
  258. if (page == buf) {
  259. begin_reached = 1;
  260. return;
  261. }
  262. page--;
  263. do {
  264. if (page == buf) {
  265. begin_reached = 1;
  266. return;
  267. }
  268. page--;
  269. } while (*page != '\n');
  270. page++;
  271. }
  272. }
  273. /*
  274. * Print a new page of text. Called by dialog_textbox().
  275. */
  276. static void print_page(WINDOW * win, int height, int width)
  277. {
  278. int i, passed_end = 0;
  279. page_length = 0;
  280. for (i = 0; i < height; i++) {
  281. print_line(win, i, width);
  282. if (!passed_end)
  283. page_length++;
  284. if (end_reached && !passed_end)
  285. passed_end = 1;
  286. }
  287. wnoutrefresh(win);
  288. }
  289. /*
  290. * Print a new line of text. Called by dialog_textbox() and print_page().
  291. */
  292. static void print_line(WINDOW * win, int row, int width)
  293. {
  294. int y, x;
  295. char *line;
  296. line = get_line();
  297. line += MIN(strlen(line), hscroll); /* Scroll horizontally */
  298. wmove(win, row, 0); /* move cursor to correct line */
  299. waddch(win, ' ');
  300. waddnstr(win, line, MIN(strlen(line), width - 2));
  301. getyx(win, y, x);
  302. /* Clear 'residue' of previous line */
  303. #if OLD_NCURSES
  304. {
  305. int i;
  306. for (i = 0; i < width - x; i++)
  307. waddch(win, ' ');
  308. }
  309. #else
  310. wclrtoeol(win);
  311. #endif
  312. }
  313. /*
  314. * Return current line of text. Called by dialog_textbox() and print_line().
  315. * 'page' should point to start of current line before calling, and will be
  316. * updated to point to start of next line.
  317. */
  318. static char *get_line(void)
  319. {
  320. int i = 0;
  321. static char line[MAX_LEN + 1];
  322. end_reached = 0;
  323. while (*page != '\n') {
  324. if (*page == '\0') {
  325. if (!end_reached) {
  326. end_reached = 1;
  327. break;
  328. }
  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 pass '\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. }