2
0

textbox.c 9.0 KB

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