textbox.c 9.2 KB

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